]> granicus.if.org Git - clang/commitdiff
Do not emit duplicate global initializers for template static data members inside...
authorDavid Blaikie <dblaikie@gmail.com>
Wed, 4 Sep 2013 21:07:37 +0000 (21:07 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 4 Sep 2013 21:07:37 +0000 (21:07 +0000)
A quirk of AST representation leads to class template static data member
definitions being visited twice during Clang IRGen resulting in
duplicate (benign) initializers.

Discovered while investigating a possibly-related debug info bug tickled
by the duplicate emission of these members & their associated debug
info.

With thanks to Richard Smith for help investigating, understanding, and
helping with the fix.

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

lib/CodeGen/CodeGenModule.cpp
test/CodeGenCXX/static-member-variable-explicit-specialization.cpp

index 3b28d9d07fbaec104b383dea604b1cd05a892f09..6c1267184317abfd0c19eb2ddbf460af898d04b9 100644 (file)
@@ -2761,8 +2761,13 @@ void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
 /// EmitNamespace - Emit all declarations in a namespace.
 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
   for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end();
-       I != E; ++I)
+       I != E; ++I) {
+    if (const VarDecl *VD = dyn_cast<VarDecl>(*I))
+      if (VD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
+          VD->getTemplateSpecializationKind() != TSK_Undeclared)
+        continue;
     EmitTopLevelDecl(*I);
+  }
 }
 
 // EmitLinkageSpec - Emit all declarations in a linkage spec.
index a069e8bd7242b9c7b97fd8351a6c0d01caf04ee0..f4b72469c782d66d75008822881b34ea7637771b 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
 // CHECK: ; ModuleID
 
 extern "C" int foo();
@@ -12,11 +12,13 @@ template<> int A<char>::a;
 // CHECK: @_ZN1AIbE1aE = global i32 10
 template<> int A<bool>::a = 10;
 
-// CHECK: @llvm.global_ctors = appending global [5 x { i32, void ()* }]
+// CHECK: @llvm.global_ctors = appending global [7 x { i32, void ()* }]
 // CHECK: [{ i32, void ()* } { i32 65535, void ()* @[[unordered1:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered2:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered3:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered4:[^ ]*]] },
+// CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered5:[^ ]*]] },
+// CHECK:  { i32, void ()* } { i32 65535, void ()* @[[unordered6:[^ ]*]] },
 // CHECK:  { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]
 
 template int A<short>::a;  // Unordered
@@ -37,6 +39,19 @@ template short x<short>;  // Unordered
 template<> int x<int> = foo();
 int e = x<char>; // Unordered
 
+namespace ns {
+template <typename T> struct a {
+  static int i;
+};
+template<typename T> int a<T>::i = foo();
+template struct a<int>;
+
+struct b {
+  template <typename T> static T i;
+};
+template<typename T> T b::i<T> = foo();
+template int b::i<int>;
+}
 // CHECK: define internal void @[[unordered1]]
 // CHECK: call i32 @foo()
 // CHECK: store {{.*}} @_ZN1AIsE1aE
@@ -49,11 +64,21 @@ int e = x<char>; // Unordered
 
 // CHECK: define internal void @[[unordered3]]
 // CHECK: call i32 @foo()
-// CHECK: store {{.*}} @_ZN1AIvE1aE
+// CHECK: store {{.*}} @_ZN2ns1aIiE1iE
 // CHECK: ret
 
 // CHECK: define internal void @[[unordered4]]
 // CHECK: call i32 @foo()
+// CHECK: store {{.*}} @_ZN2ns1b1iIiEE
+// CHECK: ret
+
+// CHECK: define internal void @[[unordered5]]
+// CHECK: call i32 @foo()
+// CHECK: store {{.*}} @_ZN1AIvE1aE
+// CHECK: ret
+
+// CHECK: define internal void @[[unordered6]]
+// CHECK: call i32 @foo()
 // CHECK: store {{.*}} @_Z1xIcE
 // CHECK: ret