Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -5525,15 +5525,16 @@ return true; } -/// \brief Returns true if given declaration is TU-scoped and externally -/// visible. -static bool isDeclTUScopedExternallyVisible(const Decl *D) { +/// \brief Returns true if given declaration has external C language linkage. +static bool isDeclExternC(const Decl *D) { if (auto *FD = dyn_cast(D)) - return (FD->getDeclContext()->isTranslationUnit() || FD->isExternC()) && - FD->hasExternalFormalLinkage(); + { + return FD->isExternC(); + } else if (auto *VD = dyn_cast(D)) - return (VD->getDeclContext()->isTranslationUnit() || VD->isExternC()) && - VD->hasExternalFormalLinkage(); + { + return VD->isExternC(); + } llvm_unreachable("Unknown type of decl!"); } @@ -5963,7 +5964,7 @@ NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, Label, 0)); } else if (!ExtnameUndeclaredIdentifiers.empty() && - isDeclTUScopedExternallyVisible(NewVD)) { + isDeclExternC(NewVD)) { llvm::DenseMap::iterator I = ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier()); if (I != ExtnameUndeclaredIdentifiers.end()) { @@ -7491,7 +7492,7 @@ NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context, SE->getString(), 0)); } else if (!ExtnameUndeclaredIdentifiers.empty() && - isDeclTUScopedExternallyVisible(NewFD)) { + isDeclExternC(NewFD)) { llvm::DenseMap::iterator I = ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier()); if (I != ExtnameUndeclaredIdentifiers.end()) { @@ -14284,7 +14285,7 @@ // already exists, add a label attribute to it. if (PrevDecl && (isa(PrevDecl) || isa(PrevDecl)) && - PrevDecl->hasExternalFormalLinkage()) + isDeclExternC(PrevDecl)) PrevDecl->addAttr(Attr); // Otherwise, add a label atttibute to ExtnameUndeclaredIdentifiers. else Index: test/CodeGen/redefine_extname.c =================================================================== --- test/CodeGen/redefine_extname.c +++ test/CodeGen/redefine_extname.c @@ -24,3 +24,9 @@ extern int foo() { return 1; } // CHECK: define i32 @bar() +// Check that pragma redefine_extname applies to external declarations only. +#pragma redefine_extname foo_static bar_static +static int foo_static() { return 1; } +int baz() { return foo_static(); } +// CHECK-NOT: call i32 @bar_static() + Index: test/CodeGenCXX/redefine_extname.cpp =================================================================== --- test/CodeGenCXX/redefine_extname.cpp +++ test/CodeGenCXX/redefine_extname.cpp @@ -28,3 +28,9 @@ // CHECK: define i32 @bar() } +// Check that pragma redefine_extname applies to C code only, and shouldn't be +// applied to C++. +#pragma redefine_extname foo_cpp bar_cpp +extern int foo_cpp() { return 1; } +// CHECK-NOT: define i32 @bar_cpp() +