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

Richard Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 29 12:48:49 PST 2016


rsmith added inline comments.


================
Comment at: lib/Sema/SemaInit.cpp:884
+    bool MissingBracesOkay = false;
+
+    if (!SemaRef.getLangOpts().CPlusPlus &&
----------------
Remove empty line.


================
Comment at: lib/Sema/SemaInit.cpp:885-892
+    if (!SemaRef.getLangOpts().CPlusPlus &&
+        StructuredSubobjectInitList->getNumInits() == 1) {
+      if (const IntegerLiteral *lit = dyn_cast<IntegerLiteral>(StructuredSubobjectInitList->getInit(0))) {
+        if (lit->getValue() == 0) {
+          MissingBracesOkay = true;
+        }
+      }
----------------
I think it would make more sense to check `ParentIList` here instead of `StructuredSubobjectInitList` -- we want to check whether the list the user wrote in the source code was `{0}`, not the list after semantic checking. This would matter for a case like:

```
struct A { int n; };
struct B { struct A a; };
struct C { struct B b; };
C c = {0};
```

`ParentIList` will be `{0}` at every level here, but it looks like `StructuredSubobjectInitList` will be `{{0}}` when checking the initialization of `c.b`, so you won't suppress the warning.

It would also matter for a case like

```
struct A { short p; };
struct B { A a; };
B b = {0};
```

where the list after semantic checking will have an implicit conversion wrapped around the initializer.


================
Comment at: lib/Sema/SemaInit.cpp:1843-1851
+  // Check if this is C's zero initializer { 0 }
+  if (!SemaRef.getLangOpts().CPlusPlus &&
+      IList->getNumInits() == 1) {
+    if (const IntegerLiteral *lit = dyn_cast<IntegerLiteral>(IList->getInit(0))) {
+      if (lit->getValue() == 0) {
+        CheckForMissingFields = false;
+      }
----------------
Please factor this check out into something like `InitListExpr::isIdiomaticZeroInitializer()`. It would make sense for that function to also assert `isSyntactic()`.


https://reviews.llvm.org/D28148





More information about the cfe-commits mailing list