Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -14197,16 +14197,22 @@ SourceLocation PragmaLoc, SourceLocation NameLoc, SourceLocation AliasNameLoc) { - Decl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, - LookupOrdinaryName); - AsmLabelAttr *Attr = ::new (Context) AsmLabelAttr(AliasNameLoc, Context, - AliasName->getName(), 0); - - if (PrevDecl) - PrevDecl->addAttr(Attr); - else + NamedDecl *PrevDecl = LookupSingleName(TUScope, Name, NameLoc, + LookupOrdinaryName); + // If a declaration that: + // 1) declares a function or a variable + // 2) has external linkage + // already exists, add a label attribute to it. + if (PrevDecl && + (isa(PrevDecl) || isa(PrevDecl)) && + PrevDecl->hasExternalFormalLinkage()) + PrevDecl->addAttr(AsmLabelAttr::CreateImplicit( + Context, AliasName->getName(), AliasNameLoc)); + // Otherwise, add a label atttibute to ExtnameUndeclaredIdentifiers. + else (void)ExtnameUndeclaredIdentifiers.insert( - std::pair(Name, Attr)); + std::make_pair(Name, AsmLabelAttr::CreateImplicit( + Context, AliasName->getName(), AliasNameLoc))); } void Sema::ActOnPragmaWeakID(IdentifierInfo* Name, Index: test/CodeGen/redefine_extname.c =================================================================== --- test/CodeGen/redefine_extname.c +++ test/CodeGen/redefine_extname.c @@ -3,6 +3,12 @@ #pragma redefine_extname fake real #pragma redefine_extname name alias +struct statvfs64 { + int f; +}; +#pragma redefine_extname statvfs64 statvfs +int statvfs64(struct statvfs64 *); + extern int fake(void); int name; @@ -13,3 +19,12 @@ // CHECK: call i32 @real() // Check that this also works with variables names // CHECK: load i32, i32* @alias + +void foo() { + struct statvfs64 st; + statvfs64(&st); +// Check that even if there is a structure with redefined name before the +// pragma, subsequent function name redefined properly. PR5172, Comment 11. +// CHECK: call i32 @statvfs(%struct.statvfs64* %st) +} +