]> granicus.if.org Git - clang/commitdiff
Allow null initialization of scalara data members
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 2 Sep 2009 17:10:17 +0000 (17:10 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 2 Sep 2009 17:10:17 +0000 (17:10 +0000)
in constructors's initializer list. pr4854

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

lib/Sema/SemaDeclCXX.cpp
test/CodeGenCXX/trivial-constructor-init.cpp [new file with mode: 0644]

index f22a3e88b72d28734a6d054b77d7c12e8bce09c2..f80a0b88e5902ec2fdc577679f25cd4a54b7fd33 100644 (file)
@@ -786,11 +786,17 @@ Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args,
       C = PerformInitializationByConstructor(
             FieldType, (Expr **)Args, NumArgs, IdLoc, 
             SourceRange(IdLoc, RParenLoc), Member->getDeclName(), IK_Direct);
-  } else if (NumArgs != 1) {
+  } else if (NumArgs != 1 && NumArgs != 0) {
     return Diag(IdLoc, diag::err_mem_initializer_mismatch) 
                 << Member->getDeclName() << SourceRange(IdLoc, RParenLoc);
   } else if (!HasDependentArg) {
-    Expr *NewExp = (Expr*)Args[0];
+    Expr *NewExp;
+    if (NumArgs == 0) {
+      NewExp = new (Context) CXXZeroInitValueExpr(FieldType, IdLoc, RParenLoc);
+      NumArgs = 1;
+    }
+    else
+      NewExp = (Expr*)Args[0];
     if (PerformCopyInitialization(NewExp, FieldType, "passing"))
       return true;
     Args[0] = NewExp;
diff --git a/test/CodeGenCXX/trivial-constructor-init.cpp b/test/CodeGenCXX/trivial-constructor-init.cpp
new file mode 100644 (file)
index 0000000..183b31a
--- /dev/null
@@ -0,0 +1,21 @@
+// RUN: clang-cc  -S %s -o %t-64.s &&
+// RUN: clang-cc  -S %s -o %t-32.s &&
+// RUN: true
+
+extern "C" int printf(...);
+
+struct S {
+  S() { printf("S::S\n"); }
+};
+
+struct A {
+  double x;
+  A() : x(), y(), s() { printf("x = %f y = %x \n", x, y); }
+  int *y;
+  S s;
+};
+
+A a;
+
+int main() {
+}