<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jun 25, 2015 at 8:37 AM, Aaron Ballman <span dir="ltr"><<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: aaronballman<br>
Date: Thu Jun 25 10:37:16 2015<br>
New Revision: 240653<br>
<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D240653-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=BSqEv9KvKMW_Ob8SyngJ70KdZISM_ASROnREeq0cCxk&m=Ytnk4JD1AjeGEN2FFUT_2rwFeqyAeU8GM-EPpPgURKg&s=sSE085Hp7VRx8-UIzH8C_iBX9Z7-mOfBnT5ENtzcpyA&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=240653&view=rev</a><br>
Log:<br>
Fix #pragma redefine_extname when there is a local variable of the same name. The local should not be renamed, only the externally-available declaration should be.<br>
<br>
Patch by Andrey Bokhanko!<br>
<br>
Modified:<br>
    cfe/trunk/lib/Sema/SemaDecl.cpp<br>
    cfe/trunk/test/CodeGen/redefine_extname.c<br>
    cfe/trunk/test/CodeGenCXX/redefine_extname.cpp<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_cfe_trunk_lib_Sema_SemaDecl.cpp-3Frev-3D240653-26r1-3D240652-26r2-3D240653-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=BSqEv9KvKMW_Ob8SyngJ70KdZISM_ASROnREeq0cCxk&m=Ytnk4JD1AjeGEN2FFUT_2rwFeqyAeU8GM-EPpPgURKg&s=VsMgGLSxxEEh715MvzXTCr4gaSVTN9t1Vq9sZ2-rzdo&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=240653&r1=240652&r2=240653&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Jun 25 10:37:16 2015<br>
@@ -5525,6 +5525,19 @@ bool Sema::adjustContextForLocalExternDe<br>
   return true;<br>
 }<br>
<br>
+/// \brief Returns true if given declaration is TU-scoped and externally<br>
+/// visible.<br>
+static bool isDeclTUScopedExternallyVisible(const Decl *D) {<br>
+  if (auto *FD = dyn_cast<FunctionDecl>(D))<br>
+    return (FD->getDeclContext()->isTranslationUnit() || FD->isExternC()) &&<br>
+           FD->hasExternalFormalLinkage();<br></blockquote><div><br></div><div>Hmm, should non-extern-C functions get this behavior at all in C++? My understanding is that redefine_extname is supposed to have the semantics of "search and replace on the generated assembly", so it should only apply to names that don't get mangled.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+  else if (auto *VD = dyn_cast<VarDecl>(D))<br>
+    return (VD->getDeclContext()->isTranslationUnit() || VD->isExternC()) &&<br>
+           VD->hasExternalFormalLinkage();<br>
+<br>
+  llvm_unreachable("Unknown type of decl!");<br>
+}<br>
+<br>
 NamedDecl *<br>
 Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,<br>
                               TypeSourceInfo *TInfo, LookupResult &Previous,<br>
@@ -5949,7 +5962,8 @@ Sema::ActOnVariableDeclarator(Scope *S,<br>
<br>
     NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0),<br>
                                                 Context, Label, 0));<br>
-  } else if (!ExtnameUndeclaredIdentifiers.empty()) {<br>
+  } else if (!ExtnameUndeclaredIdentifiers.empty() &&<br>
+             isDeclTUScopedExternallyVisible(NewVD)) {<br>
     llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =<br>
       ExtnameUndeclaredIdentifiers.find(NewVD->getIdentifier());<br>
     if (I != ExtnameUndeclaredIdentifiers.end()) {<br>
@@ -7471,7 +7485,8 @@ Sema::ActOnFunctionDeclarator(Scope *S,<br>
     StringLiteral *SE = cast<StringLiteral>(E);<br>
     NewFD->addAttr(::new (Context) AsmLabelAttr(SE->getStrTokenLoc(0), Context,<br>
                                                 SE->getString(), 0));<br>
-  } else if (!ExtnameUndeclaredIdentifiers.empty()) {<br>
+  } else if (!ExtnameUndeclaredIdentifiers.empty() &&<br>
+             isDeclTUScopedExternallyVisible(NewFD)) {<br>
     llvm::DenseMap<IdentifierInfo*,AsmLabelAttr*>::iterator I =<br>
       ExtnameUndeclaredIdentifiers.find(NewFD->getIdentifier());<br>
     if (I != ExtnameUndeclaredIdentifiers.end()) {<br>
<br>
Modified: cfe/trunk/test/CodeGen/redefine_extname.c<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_cfe_trunk_test_CodeGen_redefine-5Fextname.c-3Frev-3D240653-26r1-3D240652-26r2-3D240653-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=BSqEv9KvKMW_Ob8SyngJ70KdZISM_ASROnREeq0cCxk&m=Ytnk4JD1AjeGEN2FFUT_2rwFeqyAeU8GM-EPpPgURKg&s=lLrG3QPPeqodMtIUbVd6349i2d4Edo9P-RBXXLG0_CM&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/redefine_extname.c?rev=240653&r1=240652&r2=240653&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGen/redefine_extname.c (original)<br>
+++ cfe/trunk/test/CodeGen/redefine_extname.c Thu Jun 25 10:37:16 2015<br>
@@ -13,3 +13,14 @@ int fish() { return fake() + __PRAGMA_RE<br>
 // CHECK:   call i32 @real()<br>
 // Check that this also works with variables names<br>
 // CHECK:   load i32, i32* @alias<br>
+<br>
+// This is a case when redefenition is deferred *and* we have a local of the<br>
+// same name. PR23923.<br>
+#pragma redefine_extname foo bar<br>
+int f() {<br>
+  int foo = 0;<br>
+  return foo;<br>
+}<br>
+extern int foo() { return 1; }<br>
+// CHECK: define i32 @bar()<br>
+<br>
<br>
Modified: cfe/trunk/test/CodeGenCXX/redefine_extname.cpp<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_cfe_trunk_test_CodeGenCXX_redefine-5Fextname.cpp-3Frev-3D240653-26r1-3D240652-26r2-3D240653-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=BSqEv9KvKMW_Ob8SyngJ70KdZISM_ASROnREeq0cCxk&m=Ytnk4JD1AjeGEN2FFUT_2rwFeqyAeU8GM-EPpPgURKg&s=JKppJf6ri0iObx7Gw4AG-PiJlyQdPmWnJOB7-f7F_rw&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/redefine_extname.cpp?rev=240653&r1=240652&r2=240653&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGenCXX/redefine_extname.cpp (original)<br>
+++ cfe/trunk/test/CodeGenCXX/redefine_extname.cpp Thu Jun 25 10:37:16 2015<br>
@@ -8,7 +8,7 @@ extern "C" {<br>
   int statvfs64(struct statvfs64 *);<br>
 }<br>
<br>
-void foo() {<br>
+void some_func() {<br>
   struct statvfs64 st;<br>
   statvfs64(&st);<br>
 // Check that even if there is a structure with redefined name before the<br>
@@ -16,3 +16,15 @@ void foo() {<br>
 // CHECK:  call i32 @statvfs(%struct.statvfs64* %st)<br>
 }<br>
<br>
+// This is a case when redefenition is deferred *and* we have a local of the<br>
+// same name. PR23923.<br>
+#pragma redefine_extname foo bar<br>
+int f() {<br>
+  int foo = 0;<br>
+  return foo;<br>
+}<br>
+extern "C" {<br>
+  int foo() { return 1; }<br>
+// CHECK: define i32 @bar()<br>
+}<br>
+<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>