[PATCH] D28148: [Sema] Suppress warnings for C's zero initializer
S. Gilles via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 28 17:26:05 PST 2016
sgilles updated the summary for this revision.
sgilles updated this revision to Diff 82638.
sgilles added a comment.
Instead of adding a language option to distinguish C, negatively check
against C++.
https://reviews.llvm.org/D28148
Files:
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,29 @@
+// RUN: %clang_cc1 -std=c99 -Wmissing-field-initializers -Wmissing-braces
+
+struct foo {
+ int x;
+ int y;
+};
+
+struct bar {
+ struct foo a;
+ struct foo 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}}
+
+ return f.x + g.x + h.x + i.a.x + j.a.x + k.a.x + l.a.x + m.a.x + n.a.x +
+ o.a.x + p.a.x;
+}
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -880,8 +880,19 @@
StructuredSubobjectInitList->setRBraceLoc(EndLoc);
}
+ bool MissingBracesOkay = false;
+
+ if (!SemaRef.getLangOpts().CPlusPlus &&
+ StructuredSubobjectInitList->getNumInits() == 1) {
+ if (const IntegerLiteral *lit = dyn_cast<IntegerLiteral>(StructuredSubobjectInitList->getInit(0))) {
+ if (lit->getValue() == 0) {
+ MissingBracesOkay = true;
+ }
+ }
+ }
+
// Complain about missing braces.
- if (T->isArrayType() || T->isRecordType()) {
+ if (!MissingBracesOkay && (T->isArrayType() || T->isRecordType())) {
SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
diag::warn_missing_braces)
<< StructuredSubobjectInitList->getSourceRange()
@@ -1828,6 +1839,17 @@
RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
RecordDecl::field_iterator FieldEnd = RD->field_end();
bool CheckForMissingFields = true;
+
+ // 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;
+ }
+ }
+ }
+
while (Index < IList->getNumInits()) {
Expr *Init = IList->getInit(Index);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28148.82638.patch
Type: text/x-patch
Size: 2782 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161229/e1f54ba2/attachment.bin>
More information about the cfe-commits
mailing list