Index: include/clang/AST/Expr.h =================================================================== --- include/clang/AST/Expr.h +++ include/clang/AST/Expr.h @@ -3899,6 +3899,10 @@ /// initializer)? bool isTransparent() const; + /// Is this the zero initializer {0} in a language which considers it + /// idiomatic? + bool isIdiomaticZeroInitializer(const LangOptions &LangOpts) const; + SourceLocation getLBraceLoc() const { return LBraceLoc; } void setLBraceLoc(SourceLocation Loc) { LBraceLoc = Loc; } SourceLocation getRBraceLoc() const { return RBraceLoc; } Index: lib/AST/Expr.cpp =================================================================== --- lib/AST/Expr.cpp +++ lib/AST/Expr.cpp @@ -1883,6 +1883,21 @@ getInit(0)->getType().getCanonicalType(); } +bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions &LangOpts) const { + assert(!getSyntacticForm() && "only test syntactic form as zero initializer"); + + if (LangOpts.CPlusPlus || getNumInits() != 1) { + return false; + } + + const IntegerLiteral *lit = dyn_cast(getInit(0)); + if (!lit) { + return false; + } + + return lit->getValue() == 0; +} + SourceLocation InitListExpr::getLocStart() const { if (InitListExpr *SyntacticForm = getSyntacticForm()) return SyntacticForm->getLocStart(); Index: lib/Sema/SemaInit.cpp =================================================================== --- lib/Sema/SemaInit.cpp +++ lib/Sema/SemaInit.cpp @@ -881,7 +881,8 @@ } // Complain about missing braces. - if (T->isArrayType() || T->isRecordType()) { + if ((T->isArrayType() || T->isRecordType()) && + !ParentIList->isIdiomaticZeroInitializer(SemaRef.getLangOpts())) { SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), diag::warn_missing_braces) << StructuredSubobjectInitList->getSourceRange() @@ -1827,7 +1828,9 @@ // worthwhile to skip over the rest of the initializer, though. RecordDecl *RD = DeclType->getAs()->getDecl(); RecordDecl::field_iterator FieldEnd = RD->field_end(); - bool CheckForMissingFields = true; + bool CheckForMissingFields = + !IList->isIdiomaticZeroInitializer(SemaRef.getLangOpts()); + while (Index < IList->getNumInits()) { Expr *Init = IList->getInit(Index); Index: test/Sema/zero-initializer.c =================================================================== --- /dev/null +++ test/Sema/zero-initializer.c @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -std=c99 -Wmissing-field-initializers -Wmissing-braces + +struct foo { int x; int y; }; +struct bar { struct foo a; struct foo b; }; +struct A { int a; } +struct B { struct A a; } +struct C { struct B b; } + +int main(void) +{ + struct foo f = { 0 }; // expected-no-diagnostics + struct foo g = { 9 }; // expected-warning {{missing field 'y' initializer}} + struct foo h = { 9, 9 }; // expected-no-diagnostics + struct bar i = { 0 }; // expected-no-diagnostics + struct bar j = { 0, 0 }; // expected-warning {{suggest braces around initialization of suboject}} expected-warning {{missing field 'b' initializer}} + struct bar k = { { 9, 9 }, { 9, 9 } }; // expected-no-diagnostics + struct bar l = { { 9, 9 }, { 0 } }; // expected-no-diagnostics + struct bar m = { { 0 }, { 0 } }; // expected-no-diagnostics + struct bar n = { { 0 }, { 9, 9 } }; // expected-no-diagnostics + struct bar o = { { 9, 9 }, { 0 } }; // expected-no-diagnostics + struct bar p = { { 9 }, { 9, 9 } }; // expected-warning {{missing field 'y' initializer}} + struct C q = { 0 }; // expected-no-diagnostics + struct C r = { 9 }; // expected-warning {{suggest braces around initialization of suboject}} + + return 0; +}