]> granicus.if.org Git - clang/commitdiff
Properly construct `inline` members without initializers
authorGeorge Burgess IV <george.burgess.iv@gmail.com>
Tue, 20 Mar 2018 03:27:44 +0000 (03:27 +0000)
committerGeorge Burgess IV <george.burgess.iv@gmail.com>
Tue, 20 Mar 2018 03:27:44 +0000 (03:27 +0000)
Digging through commit logs, it appears the checks in this block predate
`inline` class variables. With them, we fail to emit dynamic
initializers for members that don't have an explicit initializer, and we
won't go out of our way to instantiate the class denoted by
`Var->getType()`.

Fixes PR35599.

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

lib/Sema/SemaTemplateInstantiateDecl.cpp
test/CodeGenCXX/cxx1z-inline-variables.cpp

index 6f04dedeed536d8191ed22190f151e888488bd86..3cc37d086ab2c014ff42ddc1d97945e3f4832e69 100644 (file)
@@ -4202,7 +4202,9 @@ void Sema::InstantiateVariableInitializer(
       Var->setInvalidDecl();
     }
   } else {
-    if (Var->isStaticDataMember()) {
+    // `inline` variables are a definition and declaration all in one; we won't
+    // pick up an initializer from anywhere else.
+    if (Var->isStaticDataMember() && !Var->isInline()) {
       if (!Var->isOutOfLine())
         return;
 
index 50eab3b70611f80f45119b8eecbb9342b0df9d85..938ebbbeb3abe0596c1f4cc580f030df99598bed 100644 (file)
@@ -111,3 +111,29 @@ int e = d<int>;
 // CHECK-NOT: __cxa_guard_acquire(i64* @_ZGV1b)
 // CHECK: call i32 @_Z1fv
 // CHECK-NOT: __cxa_guard_release(i64* @_ZGV1b)
+
+namespace PR35599 {
+struct Marker1 {};
+struct Marker2 {};
+
+template <typename>
+struct Foo {
+  struct Bar { Bar(); };
+  inline static Bar bar;
+};
+
+void run() {
+  // All we want here are ODR uses. Anything that requires that the type is
+  // complete is uninteresting.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-value"
+  Foo<Marker1>::bar;
+#pragma clang diagnostic pop
+  static_cast<void>(Foo<Marker2>::bar);
+}
+
+// CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat
+// CHECK: call void @_ZN7PR355993FooINS_7Marker1EE3BarC1Ev
+// CHECK-LABEL: define {{.*}}global_var_init{{.*}}comdat
+// CHECK: call void @_ZN7PR355993FooINS_7Marker2EE3BarC1Ev
+}