llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

redefinition of identifiers in function parameter list should be a warning not an error

Open llvmbot opened this issue 17 years ago • 5 comments

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, DefaultIgnore; def warn_decl_in_param_list : Warning< "declaration of %0 will not be visible outside of this function">; +def warn_redefinition_in_param_list : Warning<

  • "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(Def) || cast(Def)->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {

  •            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
    

%%%

llvmbot avatar Nov 11 '09 11:11 llvmbot

Fixed in r151638.

jmolloy avatar Feb 28 '12 20:02 jmolloy

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) {}

llvmbot avatar Sep 14 '10 18:09 llvmbot

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; } *); %%%

llvmbot avatar Nov 12 '09 00:11 llvmbot

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, DefaultIgnore; def warn_decl_in_param_list : Warning< "declaration of %0 will not be visible outside of this function">; +def warn_redefinition_in_param_list : Warning<

  • "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(Def) || cast(Def)->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) {

  •            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
    

llvmbot avatar Nov 11 '09 12:11 llvmbot

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; } *); %%%

llvmbot avatar Nov 11 '09 11:11 llvmbot