]> granicus.if.org Git - clang/commitdiff
Fix PR10531. Attach an initializer to anonymous unions, since the default constructor...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 18 Sep 2011 00:06:34 +0000 (00:06 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 18 Sep 2011 00:06:34 +0000 (00:06 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139991 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/CodeGenCXX/mangle.cpp
test/CodeGenCXX/member-init-anon-union.cpp [new file with mode: 0644]
test/SemaCXX/cxx0x-deleted-default-ctor.cpp

index dfa8f15b213f5db175ed5402ba41bb48ba1f2ea5..497aa6c9ade6494ccd398cd9fbda4da11073bb1c 100644 (file)
@@ -2739,6 +2739,12 @@ Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
                            Record->getLocation(), /*IdentifierInfo=*/0,
                            Context.getTypeDeclType(Record),
                            TInfo, SC, SCAsWritten);
+
+    // Default-initialize the implicit variable. This initialization will be
+    // trivial in almost all cases, except if a union member has an in-class
+    // initializer:
+    //   union { int n = 0; };
+    ActOnUninitializedDecl(Anon, /*TypeMayContainAuto=*/false);
   }
   Anon->setImplicit();
 
index 453b7b713ac6d8745601f09d6b688083d8550993..60edc42bc97243d585455278e4339c1ed2e93115 100644 (file)
@@ -533,17 +533,6 @@ namespace test15 {
   template void f<7>(S<7 + e>);
 }
 
-// rdar://problem/8125400.  Don't crash.
-namespace test16 {
-  static union {};
-  static union { union {}; };
-  static union { struct {}; };
-  static union { union { union {}; }; };
-  static union { union { struct {}; }; };
-  static union { struct { union {}; }; };
-  static union { struct { struct {}; }; };
-}
-
 // rdar://problem/8302148
 namespace test17 {
   template <int N> struct A {};
diff --git a/test/CodeGenCXX/member-init-anon-union.cpp b/test/CodeGenCXX/member-init-anon-union.cpp
new file mode 100644 (file)
index 0000000..d45dfa0
--- /dev/null
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 %s -std=c++0x -emit-llvm -o - | FileCheck %s
+
+// PR10531.
+
+static union {
+  int a = 42;
+  char *b;
+};
+
+int f() { return a; }
+
+// CHECK: define internal void @__cxx_global_var_init
+// CHECK-NOT: }
+// CHECK: call {{.*}}@"[[CONSTRUCT_GLOBAL:.*]]C1Ev"
+
+
+int g() {
+  union {
+    int a;
+    int b = 81;
+  };
+  // CHECK: define {{.*}}_Z1gv
+  // CHECK-NOT: }
+  // CHECK: call {{.*}}@"[[CONSTRUCT_LOCAL:.*]]C1Ev"
+  return b;
+}
+
+
+// CHECK: define {{.*}}@"[[CONSTRUCT_LOCAL]]C2Ev"
+// CHECK-NOT: }
+// CHECK: store i32 81
+
+// CHECK: define {{.*}}@"[[CONSTRUCT_GLOBAL]]C2Ev"
+// CHECK-NOT: }
+// CHECK: store i32 42
index dcb6ba2790be221626ac00d055ab88de2b78dcbd..d2fd6cb62fa7df66bc9de262c0afc792305cf50a 100644 (file)
@@ -118,3 +118,15 @@ struct late_delete {
   late_delete();
 };
 late_delete::late_delete() = default; // expected-error {{would delete it}}
+
+// See also rdar://problem/8125400.
+namespace empty {
+  static union {}; // expected-error {{deleted constructor}} expected-note {{here}}
+  static union { union {}; };
+  static union { struct {}; };
+  static union { union { union {}; }; };
+  static union { union { struct {}; }; };
+  static union { struct { union {}; }; }; // expected-error {{deleted constructor}} expected-note {{here}}
+  static union { struct { struct {}; }; };
+}
+