extern redeclared as static = error (C spec: undefined)

static redeclared as extern = warning (C spec: ignore extern)
See: #2111
This commit is contained in:
bbbradsmith
2023-05-11 19:50:58 -04:00
parent 79018fd424
commit 3b7be09a7f

View File

@@ -1340,15 +1340,14 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
Name); Name);
Entry = 0; Entry = 0;
} else if ((Flags & SC_ESUTYPEMASK) != SC_TYPEDEF) { } else if ((Flags & SC_ESUTYPEMASK) != SC_TYPEDEF) {
/* If a static declaration follows a non-static declaration, then /* If a static declaration follows a non-static declaration, then the result is undefined.
** diagnose the conflict. It will warn and compile an extern ** Most compilers choose to either give an error at compile time,
** declaration if both declarations are global, otherwise give an ** or remove the extern property for a link time error if used.
** error.
*/ */
if (SymTab == SymTab0 && if (SymTab == SymTab0 &&
(Flags & SC_EXTERN) == 0 && (Flags & SC_EXTERN) == 0 &&
(Entry->Flags & SC_EXTERN) != 0) { (Entry->Flags & SC_EXTERN) != 0) {
Warning ("Static declaration of '%s' follows non-static declaration", Name); Error ("Static declaration of '%s' follows non-static declaration", Name);
} else if ((Flags & SC_EXTERN) != 0 && } else if ((Flags & SC_EXTERN) != 0 &&
(Entry->Owner == SymTab0 || (Entry->Flags & SC_DEF) != 0) && (Entry->Owner == SymTab0 || (Entry->Flags & SC_DEF) != 0) &&
(Entry->Flags & SC_EXTERN) == 0) { (Entry->Flags & SC_EXTERN) == 0) {
@@ -1360,8 +1359,12 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
*/ */
if (Entry->Owner == SymTab0) { if (Entry->Owner == SymTab0) {
if ((Flags & SC_STORAGE) == 0) { if ((Flags & SC_STORAGE) == 0) {
/* Linkage must be unchanged */ /* Linkage must be unchanged.
** The C standard specifies that a later extern declaration will be ignored,
** and will use the previous linkage instead. Giving a warning for this case.
*/
Flags &= ~SC_EXTERN; Flags &= ~SC_EXTERN;
Warning ("Extern declaration of '%s' follows static declaration, extern ignored", Name);
} else { } else {
Error ("Non-static declaration of '%s' follows static declaration", Name); Error ("Non-static declaration of '%s' follows static declaration", Name);
} }