al3xtjames created this revision.
al3xtjames added a reviewer: cfe-commits.
Herald added a project: clang.

r314499 relaxed some checks for assigning { 0 } to a structure for all
C standards, but it failed to handle structures with non-integer
subobjects. Relax -Wmissing-braces checks for such structures, and add
some additional tests.

This fixes PR39931.


Repository:
  rC Clang

https://reviews.llvm.org/D61838

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


Index: clang/test/Sema/zero-initializer.c
===================================================================
--- clang/test/Sema/zero-initializer.c
+++ clang/test/Sema/zero-initializer.c
@@ -7,6 +7,8 @@
 struct B { struct A a; };
 struct C { struct B b; };
 struct D { struct C c; int n; };
+struct E { short e; };
+struct F { struct E e; int n; };
 
 int main(void)
 {
@@ -23,6 +25,9 @@
   struct C p = { 0 }; // no-warning
   struct C q = { 9 }; // warning suppressed for struct with single element
   struct D r = { 9 }; // expected-warning {{suggest braces around 
initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+  struct F s = { 0 }; // no-warning
+  struct F t = { 9 }; // expected-warning {{suggest braces around 
initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+
   f = (struct foo ) { 0 }; // no-warning
   g = (struct foo ) { 9 }; // expected-warning {{missing field 'y' 
initializer}}
   h = (struct foo ) { 9, 9 }; // no-warning
@@ -36,6 +41,8 @@
   p = (struct C) { 0 }; // no-warning
   q = (struct C) { 9 }; // warning suppressed for struct with single element
   r = (struct D) { 9 }; // expected-warning {{suggest braces around 
initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+  s = (struct F) { 0 }; // no-warning
+  t = (struct F) { 9 }; // expected-warning {{suggest braces around 
initialization of subobject}} expected-warning {{missing field 'n' initializer}}
 
   return 0;
 }
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -2090,6 +2090,13 @@
   }
 
   const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(getInit(0));
+  if (!Lit) {
+    const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(getInit(0));
+    if (!Cast)
+      return false;
+    Lit = dyn_cast<IntegerLiteral>(Cast->getSubExpr());
+  }
+
   return Lit && Lit->getValue() == 0;
 }
 


Index: clang/test/Sema/zero-initializer.c
===================================================================
--- clang/test/Sema/zero-initializer.c
+++ clang/test/Sema/zero-initializer.c
@@ -7,6 +7,8 @@
 struct B { struct A a; };
 struct C { struct B b; };
 struct D { struct C c; int n; };
+struct E { short e; };
+struct F { struct E e; int n; };
 
 int main(void)
 {
@@ -23,6 +25,9 @@
   struct C p = { 0 }; // no-warning
   struct C q = { 9 }; // warning suppressed for struct with single element
   struct D r = { 9 }; // expected-warning {{suggest braces around initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+  struct F s = { 0 }; // no-warning
+  struct F t = { 9 }; // expected-warning {{suggest braces around initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+
   f = (struct foo ) { 0 }; // no-warning
   g = (struct foo ) { 9 }; // expected-warning {{missing field 'y' initializer}}
   h = (struct foo ) { 9, 9 }; // no-warning
@@ -36,6 +41,8 @@
   p = (struct C) { 0 }; // no-warning
   q = (struct C) { 9 }; // warning suppressed for struct with single element
   r = (struct D) { 9 }; // expected-warning {{suggest braces around initialization of subobject}} expected-warning {{missing field 'n' initializer}}
+  s = (struct F) { 0 }; // no-warning
+  t = (struct F) { 9 }; // expected-warning {{suggest braces around initialization of subobject}} expected-warning {{missing field 'n' initializer}}
 
   return 0;
 }
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -2090,6 +2090,13 @@
   }
 
   const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(getInit(0));
+  if (!Lit) {
+    const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(getInit(0));
+    if (!Cast)
+      return false;
+    Lit = dyn_cast<IntegerLiteral>(Cast->getSubExpr());
+  }
+
   return Lit && Lit->getValue() == 0;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to