]> granicus.if.org Git - clang/commitdiff
Ensure that static local variables in function templates inherit the
authorJohn McCall <rjmccall@apple.com>
Tue, 2 Nov 2010 21:04:24 +0000 (21:04 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 2 Nov 2010 21:04:24 +0000 (21:04 +0000)
visibility of their function.

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

lib/CodeGen/CGDecl.cpp
lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/ItaniumCXXABI.cpp
test/CodeGenCXX/visibility.cpp

index e381e979470f19827926292a4e7770a883c29ed1..5ac8508375fb87cfb804c2969641edfe3c57d410 100644 (file)
@@ -172,6 +172,8 @@ CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
                              CGM.EmitNullConstant(D.getType()), Name, 0,
                              D.isThreadSpecified(), Ty.getAddressSpace());
   GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
+  if (Linkage != llvm::GlobalValue::InternalLinkage)
+    GV->setVisibility(CurFn->getVisibility());
   return GV;
 }
 
@@ -209,8 +211,10 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
     GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
                                   OldGV->isConstant(),
                                   OldGV->getLinkage(), Init, "",
-                                  0, D.isThreadSpecified(),
+                                  /*InsertBefore*/ OldGV,
+                                  D.isThreadSpecified(),
                                   D.getType().getAddressSpace());
+    GV->setVisibility(OldGV->getVisibility());
     
     // Steal the name of the old global
     GV->takeName(OldGV);
index d975a22643c71a5ce887f46e229a05af5f9e690c..a2b80bea6c0754e134e0db99fdb012de20dae9b0 100644 (file)
@@ -1344,9 +1344,16 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) {
     Entry = NewFn;
   }
 
+  // We need to set linkage and visibility on the function before
+  // generating code for it because various parts of IR generation
+  // want to propagate this information down (e.g. to local static
+  // declarations).
   llvm::Function *Fn = cast<llvm::Function>(Entry);
   setFunctionLinkage(D, Fn);
 
+  // FIXME: this is redundant with part of SetFunctionDefinitionAttributes
+  setGlobalVisibility(Fn, D, /*ForDef*/ true);
+
   CodeGenFunction(*this).GenerateCode(D, Fn);
 
   SetFunctionDefinitionAttributes(D, Fn);
index b6c40b64db201a36b37b74fbc273867bef502918..94c1f60a8b28fa7bef3ed6f0da2222cebb0da33c 100644 (file)
@@ -1092,6 +1092,9 @@ void ItaniumCXXABI::EmitStaticLocalInit(CodeGenFunction &CGF,
   // Create the guard variable.
   llvm::SmallString<256> GuardVName;
   getMangleContext().mangleItaniumGuardVariable(&D, GuardVName);
+
+  // FIXME: we should just absorb linkage and visibility from the
+  // variable, but that's not always set up properly just yet.
   llvm::GlobalValue::LinkageTypes Linkage = GV->getLinkage();
   if (D.isStaticDataMember() &&
       D.getInstantiatedFromStaticDataMember())
@@ -1102,6 +1105,7 @@ void ItaniumCXXABI::EmitStaticLocalInit(CodeGenFunction &CGF,
                              false, Linkage,
                              llvm::ConstantInt::get(GuardTy, 0),
                              GuardVName.str());
+  GuardVariable->setVisibility(GV->getVisibility());
 
   // Test whether the variable has completed initialization.
   llvm::Value *IsInitialized;
index c9febfbdfe41fd5367a030c04bb514d840dbffcb..8d23da0a372c09dacbbba002c7e129a4cc3423f5 100644 (file)
 // CHECK-HIDDEN: @_ZN6Test143varE = external global
 // CHECK: @_ZN6Test154TempINS_1AEE5Inner6bufferE = external global [0 x i8]
 // CHECK-HIDDEN: @_ZN6Test154TempINS_1AEE5Inner6bufferE = external global [0 x i8]
+// CHECK: @_ZZN6Test193fooIiEEvvE1a = linkonce_odr global
+// CHECK: @_ZGVZN6Test193fooIiEEvvE1a = linkonce_odr global i64
+// CHECK-HIDDEN: @_ZZN6Test193fooIiEEvvE1a = linkonce_odr hidden global
+// CHECK-HIDDEN: @_ZGVZN6Test193fooIiEEvvE1a = linkonce_odr hidden global i64
 // CHECK-HIDDEN: @_ZTTN6Test161AIcEE = external constant
 // CHECK-HIDDEN: @_ZTVN6Test161AIcEE = external constant
 // CHECK: @_ZTVN5Test63fooE = weak_odr hidden constant 
@@ -339,3 +343,16 @@ namespace Test18 {
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE1B3barEv()
   // CHECK-HIDDEN: declare hidden void @_ZN6Test181AINS_1HEE1B3bazEv()
 }
+
+namespace Test19 {
+  struct A { A(); ~A(); };
+
+  // Tested at top of file.
+  template <class T> void foo() {
+    static A a;
+  }
+
+  void test() {
+    foo<int>();
+  }
+}