[PATCH] D28148: [Sema] Suppress warnings for C's zero initializer

S. Gilles via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 29 22:23:17 PST 2016


sgilles updated this revision to Diff 82720.
sgilles added a comment.

Address rsmith's comments, in particular: factor out testing zero initializers to a method of `InitListExpr`; use `ParentIList` instead of `StructuredSubobjectInitList`.

The warning is (still) not relaxed for C++ code.  I have no opinion on this beyond wanting to avoid regressions, but for lack of consensus I'll default to changing as little as possible.


https://reviews.llvm.org/D28148

Files:
  include/clang/AST/Expr.h
  lib/AST/Expr.cpp
  lib/Sema/SemaInit.cpp
  test/Sema/zero-initializer.c


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;
+}
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<RecordType>()->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: 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<IntegerLiteral>(getInit(0));
+  if (!lit) {
+    return false;
+  }
+
+  return lit->getValue() == 0;
+}
+
 SourceLocation InitListExpr::getLocStart() const {
   if (InitListExpr *SyntacticForm = getSyntacticForm())
     return SyntacticForm->getLocStart();
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; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28148.82720.patch
Type: text/x-patch
Size: 3728 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161230/011226c3/attachment.bin>


More information about the cfe-commits mailing list