1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-27 00:29:31 +00:00

Merge pull request #197 from greg-king5/static

Handle almost-duplicate C declarations that have different linkages.
This commit is contained in:
Oliver Schmidt 2015-08-15 06:53:40 +02:00
commit a85ac88fca
6 changed files with 103 additions and 4 deletions

View File

@ -126,19 +126,19 @@ void DumpSymEntry (FILE* F, const SymEntry* E)
/* Print the assembler name if we have one */
if (E->AsmName) {
fprintf (F, " AsmName: %s\n", E->AsmName);
}
}
/* Print the flags */
SymFlags = E->Flags;
fprintf (F, " Flags: ");
fprintf (F, " Flags:");
for (I = 0; I < sizeof (Flags) / sizeof (Flags[0]) && SymFlags != 0; ++I) {
if ((SymFlags & Flags[I].Val) == Flags[I].Val) {
SymFlags &= ~Flags[I].Val;
fprintf (F, "%s ", Flags[I].Name);
fprintf (F, " %s", Flags[I].Name);
}
}
if (SymFlags != 0) {
fprintf (F, "%04X", SymFlags);
fprintf (F, " 0x%05X", SymFlags);
}
fprintf (F, "\n");

View File

@ -813,6 +813,25 @@ SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
}
}
/* If a static declaration follows a non-static declaration, then
** warn about the conflict. (It will compile a public declaration.)
*/
if ((Flags & SC_EXTERN) == 0 && (Entry->Flags & SC_EXTERN) != 0) {
Warning ("static declaration follows non-static declaration of `%s'.", Name);
}
/* An extern declaration must not change the current linkage. */
if (IsFunc || (Flags & (SC_EXTERN | SC_DEF)) == SC_EXTERN) {
Flags &= ~SC_EXTERN;
}
/* If a public declaration follows a static declaration, then
** warn about the conflict. (It will compile a public declaration.)
*/
if ((Flags & SC_EXTERN) != 0 && (Entry->Flags & SC_EXTERN) == 0) {
Warning ("public declaration follows static declaration of `%s'.", Name);
}
/* Add the new flags */
Entry->Flags |= Flags;

20
test/err/static-2.c Normal file
View File

@ -0,0 +1,20 @@
/*
!!DESCRIPTION!! global non-static and static conflicts
!!ORIGIN!! cc65 regression tests
!!LICENCE!! Public Domain
!!AUTHOR!! Greg King
*/
/*
see: https://github.com/cc65/cc65/issues/191
*/
#pragma warn(error, on)
int n;
static int n; /* should give an error */
int main(void)
{
return n;
}

20
test/err/static-3.c Normal file
View File

@ -0,0 +1,20 @@
/*
!!DESCRIPTION!! global non-static and static conflicts
!!ORIGIN!! cc65 regression tests
!!LICENCE!! Public Domain
!!AUTHOR!! Greg King
*/
/*
see: https://github.com/cc65/cc65/issues/191
*/
#pragma warn(error, on)
extern int n;
static int n; /* should give an error */
int main(void)
{
return n;
}

20
test/err/static-4.c Normal file
View File

@ -0,0 +1,20 @@
/*
!!DESCRIPTION!! global non-static and static conflicts
!!ORIGIN!! cc65 regression tests
!!LICENCE!! Public Domain
!!AUTHOR!! Greg King
*/
/*
see: https://github.com/cc65/cc65/issues/191
*/
#pragma warn(error, on)
static int n;
int n; /* should give an error */
int main(void)
{
return n;
}

20
test/val/static-1.c Normal file
View File

@ -0,0 +1,20 @@
/*
!!DESCRIPTION!! global non-static and static conflicts
!!ORIGIN!! cc65 regression tests
!!LICENCE!! Public Domain
!!AUTHOR!! Greg King
*/
/*
see: https://github.com/cc65/cc65/issues/191
*/
#pragma warn(error, on)
static int n = 0;
extern int n; /* should not give an error */
int main(void)
{
return n;
}