[cfe-commits] r95512 - in /cfe/trunk: lib/CodeGen/CGDecl.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenCXX/static-init.cpp

Anders Carlsson andersca at mac.com
Sat Feb 6 18:03:08 PST 2010


Author: andersca
Date: Sat Feb  6 20:03:08 2010
New Revision: 95512

URL: http://llvm.org/viewvc/llvm-project?rev=95512&view=rev
Log:
Use the right linkage for static variables inside C++ inline functions.

Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGenCXX/static-init.cpp

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=95512&r1=95511&r2=95512&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Sat Feb  6 20:03:08 2010
@@ -76,8 +76,21 @@
   case VarDecl::Auto:
   case VarDecl::Register:
     return EmitLocalBlockVarDecl(D);
-  case VarDecl::Static:
-    return EmitStaticBlockVarDecl(D);
+  case VarDecl::Static: {
+    llvm::GlobalValue::LinkageTypes Linkage = 
+      llvm::GlobalValue::InternalLinkage;
+
+    // If this is a static declaration inside an inline function, it must have
+    // weak linkage so that the linker will merge multiple definitions of it.
+    if (getContext().getLangOptions().CPlusPlus) {
+      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
+        if (FD->isInlined())
+          Linkage = llvm::GlobalValue::WeakAnyLinkage;
+      }
+    }
+    
+    return EmitStaticBlockVarDecl(D, Linkage);
+  }
   case VarDecl::Extern:
   case VarDecl::PrivateExtern:
     // Don't emit it now, allow it to be emitted lazily on its first use.
@@ -177,12 +190,12 @@
   return GV;
 }
 
-void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
+void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D,
+                                      llvm::GlobalValue::LinkageTypes Linkage) {
   llvm::Value *&DMEntry = LocalDeclMap[&D];
   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
 
-  llvm::GlobalVariable *GV =
-    CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage);
+  llvm::GlobalVariable *GV = CreateStaticBlockVarDecl(D, ".", Linkage);
 
   // Store into LocalDeclMap before generating initializer to handle
   // circular references.
@@ -355,7 +368,7 @@
         // If this variable is marked 'const', emit the value as a global.
         if (CGM.getCodeGenOpts().MergeAllConstants &&
             Ty.isConstant(getContext())) {
-          EmitStaticBlockVarDecl(D);
+          EmitStaticBlockVarDecl(D, llvm::GlobalValue::InternalLinkage);
           return;
         }
         

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=95512&r1=95511&r2=95512&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Sat Feb  6 20:03:08 2010
@@ -845,7 +845,8 @@
   /// This function can be called with a null (unreachable) insert point.
   void EmitLocalBlockVarDecl(const VarDecl &D);
 
-  void EmitStaticBlockVarDecl(const VarDecl &D);
+  void EmitStaticBlockVarDecl(const VarDecl &D,
+                              llvm::GlobalValue::LinkageTypes Linkage);
 
   /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
   void EmitParmDecl(const VarDecl &D, llvm::Value *Arg);

Modified: cfe/trunk/test/CodeGenCXX/static-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/static-init.cpp?rev=95512&r1=95511&r2=95512&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/static-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/static-init.cpp Sat Feb  6 20:03:08 2010
@@ -1,6 +1,10 @@
 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
 
 // CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
+
+// CHECK: @_ZZ2h2vE1i = weak global i32 0
+// CHECK: @_ZGVZ2h2vE1i = weak global i64 0
+
 struct A {
   A();
   ~A();
@@ -22,3 +26,11 @@
 void h() {
   static const int i = a();
 }
+
+inline void h2() {
+  static int i = a();
+}
+
+void h3() {
+  h2();
+}





More information about the cfe-commits mailing list