r183951 - Fix the linkage of static locals inside a CapturedStmt. (Found in the

Eli Friedman eli.friedman at gmail.com
Thu Jun 13 14:50:45 PDT 2013


Author: efriedma
Date: Thu Jun 13 16:50:44 2013
New Revision: 183951

URL: http://llvm.org/viewvc/llvm-project?rev=183951&view=rev
Log:
Fix the linkage of static locals inside a CapturedStmt.  (Found in the
process of trying to fix the related issue for block literals.)


Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/test/CodeGenCXX/captured-statements.cpp

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=183951&r1=183950&r2=183951&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu Jun 13 16:50:44 2013
@@ -129,9 +129,27 @@ void CodeGenFunction::EmitVarDecl(const
     // uniqued.  We can't do this in C, though, because there's no
     // standard way to agree on which variables are the same (i.e.
     // there's no mangling).
-    if (getLangOpts().CPlusPlus)
-      if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
-        Linkage = CurFn->getLinkage();
+    if (getLangOpts().CPlusPlus) {
+      const Decl *D = CurCodeDecl;
+      while (true) {
+        if (isa<BlockDecl>(D)) {
+          // FIXME: Handle this case properly!  (Should be similar to the
+          // way we handle lambdas in computeLVForDecl in Decl.cpp.)
+          break;
+        } else if (isa<CapturedDecl>(D)) {
+          D = cast<Decl>(cast<CapturedDecl>(D)->getParent());
+        } else {
+          break;
+        }
+      }
+      // FIXME: Do we really only care about FunctionDecls here?
+      if (D && isa<FunctionDecl>(D)) {
+        llvm::GlobalValue::LinkageTypes ParentLinkage =
+            CGM.getFunctionLinkage(cast<FunctionDecl>(D));
+        if (llvm::GlobalValue::isWeakForLinker(ParentLinkage))
+          Linkage = ParentLinkage;
+      }
+    }
 
     return EmitStaticVarDecl(D, Linkage);
   }

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=183951&r1=183950&r2=183951&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Jun 13 16:50:44 2013
@@ -510,7 +510,11 @@ void CodeGenModule::EmitCtorList(const C
 
 llvm::GlobalValue::LinkageTypes
 CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
-  const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl());
+  return getFunctionLinkage(cast<FunctionDecl>(GD.getDecl()));
+}
+
+llvm::GlobalValue::LinkageTypes
+CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
   GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
 
   if (Linkage == GVA_Internal)

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=183951&r1=183950&r2=183951&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Jun 13 16:50:44 2013
@@ -925,6 +925,7 @@ public:
   void AddDependentLib(StringRef Lib);
 
   llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD);
+  llvm::GlobalVariable::LinkageTypes getFunctionLinkage(const FunctionDecl *D);
 
   void setFunctionLinkage(GlobalDecl GD, llvm::GlobalValue *V) {
     V->setLinkage(getFunctionLinkage(GD));

Modified: cfe/trunk/test/CodeGenCXX/captured-statements.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/captured-statements.cpp?rev=183951&r1=183950&r2=183951&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/captured-statements.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/captured-statements.cpp Thu Jun 13 16:50:44 2013
@@ -5,6 +5,7 @@
 // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-4
 // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-5
 // RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-6
+// RUN: FileCheck %s -input-file=%t -check-prefix=CHECK-7
 
 struct Foo {
   int x;
@@ -172,3 +173,17 @@ void test_capture_lambda() {
   // CHECK-6-NEXT: ret void
   template_capture_lambda<int>();
 }
+
+inline int test_captured_linkage() {
+  // CHECK-7: @_ZN21test_captured_linkage1iE = linkonce_odr global i32 0
+  int j;
+  #pragma clang __debug captured
+  {
+    static int i = 0;
+    j = ++i;
+  }
+  return j;
+}
+void call_test_captured_linkage() {
+  test_captured_linkage();
+}





More information about the cfe-commits mailing list