redefinition of identifiers in function parameter list should be a warning not an error
| Bugzilla Link | 5464 |
| Resolution | FIXED |
| Resolved on | Feb 28, 2012 12:13 |
| Version | trunk |
| OS | MacOS X |
| Reporter | LLVM Bugzilla Contributor |
| CC | @jmolloy |
Extended Description
Example code: %%% struct a { };
int foo(struct a { } *); %%%
Some non-trivial code in the wild seems to engage in this absurdity. GCC treats it as a warning, as well. (While, like Clang, treating redefinitions in other contexts as errors.)
Patch: %%% Index: include/clang/Basic/DiagnosticSemaKinds.td
--- include/clang/Basic/DiagnosticSemaKinds.td (revision 86705)
+++ include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -81,6 +81,8 @@
InGroup
- "redefinition of %0 ignored; it would not be visible outside of this function">;
def warn_implicit_function_decl : Warning< "implicit declaration of function %0">, Index: lib/Sema/SemaDecl.cpp
--- lib/Sema/SemaDecl.cpp (revision 86705)
+++ lib/Sema/SemaDecl.cpp (working copy)
@@ -4417,7 +4417,13 @@
!isa
-
Diag(NameLoc, diag::err_redefinition) << Name;
-
// A redeclaration in function prototype scope in C isn't -
// visible elsewhere, so merely issue a warning. -
if (S->isFunctionPrototypeScope() && !getLangOptions().CPlusPlus) { -
Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; -
} else { -
Diag(NameLoc, diag::err_redefinition) << Name; -
} Diag(Def->getLocation(), diag::note_previous_definition); // If this is a redefinition, recover by making this // struct be anonymous, which will make any later
%%%
Fixed in r151638.
This bug applies more widely than just structs. Enums are affected, too. For example:
enum eAABB {AA, BB}; void f(enum {AA, BB} n) {}
Any fix should also note that redefined identifiers in function prototype scope must hide the ones in the outer scope (see section 6.2.1 of C99). For example, the following is conforming code:
enum eAABB {AA, BB}; void f(enum {BB, AA} n) {}
Indeed, the previous patch is bogus, as it doesn't handle this sort of case; sorry for the bug churn: %%% struct a { struct b { int j; } b; };
int foo(struct a { struct b { int j; } b; struct b { char glorx; } glorx; } *); %%%
This patch seems sufficient, but may be too eager.
Index: include/clang/Basic/DiagnosticSemaKinds.td
--- include/clang/Basic/DiagnosticSemaKinds.td (revision 86705)
+++ include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -81,6 +81,8 @@
InGroup
- "redefinition of %0 ignored; it would not be visible outside of this function">;
def warn_implicit_function_decl : Warning< "implicit declaration of function %0">, Index: lib/Sema/SemaDecl.cpp
--- lib/Sema/SemaDecl.cpp (revision 86705)
+++ lib/Sema/SemaDecl.cpp (working copy)
@@ -4417,7 +4417,18 @@
!isa
-
Diag(NameLoc, diag::err_redefinition) << Name;
-
// A redeclaration in function prototype scope in C isn't -
// visible elsewhere, so merely issue a warning. -
Scope *S2 = S; -
while (S2->isClassScope()) { -
S2 = S->getParent(); -
} -
if (S2->isFunctionPrototypeScope() && !getLangOptions().CPlusPlus) { -
Diag(NameLoc, diag::warn_redefinition_in_param_list) << Name; -
} else { -
Diag(NameLoc, diag::err_redefinition) << Name; -
} Diag(Def->getLocation(), diag::note_previous_definition); // If this is a redefinition, recover by making this // struct be anonymous, which will make any later
Err. I now realize that that patch is woefully inadequate because it doesn't handle things like:
%%% struct a { struct b { int j; } t; };
int main(struct a { struct b { int j; } t; } *); %%%