]> granicus.if.org Git - clang/commitdiff
Cleanup linkage computation for static locals.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 17 Jun 2013 20:04:51 +0000 (20:04 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 17 Jun 2013 20:04:51 +0000 (20:04 +0000)
With this patch we assign VisibleNoLinkage to static locals in inline functions.
This lets us simplify CodeGen a bit.

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

lib/AST/Decl.cpp
lib/CodeGen/CGDecl.cpp
test/CodeGenCXX/linkage.cpp

index a36fcf2507b2f15b254aaba20a7f38ca5170c7c3..c799161e9b772340126fda810ebbf5ccaa060415 100644 (file)
@@ -1043,9 +1043,13 @@ static LinkageInfo getLVForLocalDecl(const NamedDecl *D,
 
       return LV;
     }
+
+    if (!Var->isStaticLocal())
+      return LinkageInfo::none();
   }
 
-  if (!isa<TagDecl>(D))
+  ASTContext &Context = D->getASTContext();
+  if (!Context.getLangOpts().CPlusPlus)
     return LinkageInfo::none();
 
   const FunctionDecl *FD = getOutermostFunctionContext(D);
index cb15748646310ba3c84f54bfa97a0d38bc365795..614eaab3f373e631af589ca943116ca2619fb606 100644 (file)
@@ -126,10 +126,8 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
 
     // If the function definition has some sort of weak linkage, its
     // static variables should also be weak so that they get properly
-    // 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) {
+    // uniqued.
+    if (D.isExternallyVisible()) {
       const Decl *D = CurCodeDecl;
       while (true) {
         if (isa<BlockDecl>(D)) {
@@ -143,7 +141,7 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
         }
       }
       // FIXME: Do we really only care about FunctionDecls here?
-      if (D && isa<FunctionDecl>(D)) {
+      if (isa<FunctionDecl>(D)) {
         llvm::GlobalValue::LinkageTypes ParentLinkage =
             CGM.getFunctionLinkage(cast<FunctionDecl>(D));
         if (llvm::GlobalValue::isWeakForLinker(ParentLinkage))
index 7c670293996b3fe569ca089f5db43c154730ed1c..c7feaefeb85d90f16acab7f7b869c96b168759f6 100644 (file)
@@ -209,3 +209,14 @@ namespace test16 {
   }
   void *test() { return foo<int>::bar(); }
 }
+
+namespace test17 {
+  // CHECK-DAG: @_ZZN6test173fooILi42EEEPivE3bar = weak_odr
+  // CHECK-DAG: define weak_odr i32* @_ZN6test173fooILi42EEEPiv(
+  template<int I>
+  int *foo() {
+    static int bar;
+    return &bar;
+  }
+  template int *foo<42>();
+}