From: Fariborz Jahanian Date: Wed, 2 Sep 2009 17:10:17 +0000 (+0000) Subject: Allow null initialization of scalara data members X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=636a0ffbd2bda998236ddc9bb14b9222f7b49359;p=clang Allow null initialization of scalara data members in constructors's initializer list. pr4854 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80802 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index f22a3e88b7..f80a0b88e5 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -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 index 0000000000..183b31a801 --- /dev/null +++ b/test/CodeGenCXX/trivial-constructor-init.cpp @@ -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() { +}