]> granicus.if.org Git - clang/commitdiff
Fix #pragma redefine_extname when there is a local variable of the same name. The...
authorAaron Ballman <aaron@aaronballman.com>
Thu, 25 Jun 2015 15:37:16 +0000 (15:37 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Thu, 25 Jun 2015 15:37:16 +0000 (15:37 +0000)
Patch by Andrey Bokhanko!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@240653 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/CodeGen/redefine_extname.c
test/CodeGenCXX/redefine_extname.cpp

index dc826f05b062b2b21a27bdc149ca05fc0412bf8e..19bc7f99c8c93c3e3fc47ab7edd43776f927682f 100644 (file)
@@ -5525,6 +5525,19 @@ bool Sema::adjustContextForLocalExternDecl(DeclContext *&DC) {
   return true;
 }
 
+/// \brief Returns true if given declaration is TU-scoped and externally
+/// visible.
+static bool isDeclTUScopedExternallyVisible(const Decl *D) {
+  if (auto *FD = dyn_cast<FunctionDecl>(D))
+    return (FD->getDeclContext()->isTranslationUnit() || FD->isExternC()) &&
+           FD->hasExternalFormalLinkage();
+  else if (auto *VD = dyn_cast<VarDecl>(D))
+    return (VD->getDeclContext()->isTranslationUnit() || VD->isExternC()) &&
+           VD->hasExternalFormalLinkage();
+
+  llvm_unreachable("Unknown type of decl!");
+}
+
 NamedDecl *
 Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
                               TypeSourceInfo *TInfo, LookupResult &Previous,
@@ -5949,7 +5962,8 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
 
     NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0),
                                                 Context, Label, 0));
-  } else if (!ExtnameUndeclaredIdentifiers.empty()) {
+  } else if (!ExtnameUndeclaredIdentifiers.empty() &&
+             isDeclTUScopedExternallyVisible(NewVD)) {
     llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =
       ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier());
     if (I != ExtnameUndeclaredIdentifiers.end()) {
@@ -7471,7 +7485,8 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
     StringLiteral *SE = cast<StringLiteral>(E);
     NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context,
                                                 SE->getString(), 0));
-  } else if (!ExtnameUndeclaredIdentifiers.empty()) {
+  } else if (!ExtnameUndeclaredIdentifiers.empty() &&
+             isDeclTUScopedExternallyVisible(NewFD)) {
     llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =
       ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier());
     if (I != ExtnameUndeclaredIdentifiers.end()) {
index a91e5b836ae6b4d91735ae7551e4338106e3e580..ad4106dd4535d3183861b9ae0b4e12666789b04a 100644 (file)
@@ -13,3 +13,14 @@ int fish() { return fake() + __PRAGMA_REDEFINE_EXTNAME + name; }
 // CHECK:   call i32 @real()
 // Check that this also works with variables names
 // CHECK:   load i32, i32* @alias
+
+// This is a case when redefenition is deferred *and* we have a local of the
+// same name. PR23923.
+#pragma redefine_extname foo bar
+int f() {
+  int foo = 0;
+  return foo;
+}
+extern int foo() { return 1; }
+// CHECK: define i32 @bar()
+
index 2b6b703a1b8fd96d2df10632a5a1adc33aba1755..f76fe6252fef5151da472b64fa04ac9f27210f58 100644 (file)
@@ -8,7 +8,7 @@ extern "C" {
   int statvfs64(struct statvfs64 *);
 }
 
-void foo() {
+void some_func() {
   struct statvfs64 st;
   statvfs64(&st);
 // Check that even if there is a structure with redefined name before the
@@ -16,3 +16,15 @@ void foo() {
 // CHECK:  call i32 @statvfs(%struct.statvfs64* %st)
 }
 
+// This is a case when redefenition is deferred *and* we have a local of the
+// same name. PR23923.
+#pragma redefine_extname foo bar
+int f() {
+  int foo = 0;
+  return foo;
+}
+extern "C" {
+  int foo() { return 1; }
+// CHECK: define i32 @bar()
+}
+