diff options
| author | Peter Wu <peter@lekensteyn.nl> | 2019-07-16 01:13:36 +0000 |
|---|---|---|
| committer | Peter Wu <peter@lekensteyn.nl> | 2019-07-16 01:13:36 +0000 |
| commit | fa52e00c85ce3feeec14be34265781f721b966c0 (patch) | |
| tree | 39f47928bcf49eabec1c274adf43008edcbfbdf4 /clang | |
| parent | 543ba4e9e0c421bedaea2d8a0f1965092cec300e (diff) | |
| download | bcm5719-llvm-fa52e00c85ce3feeec14be34265781f721b966c0.tar.gz bcm5719-llvm-fa52e00c85ce3feeec14be34265781f721b966c0.zip | |
[Sema] Suppress additional warnings for C's zero initializer
Summary:
D28148 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.
Patch By: al3xtjames
Reviewed By: Lekensteyn
Differential Revision: https://reviews.llvm.org/D61838
llvm-svn: 366163
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/AST/Expr.cpp | 4 | ||||
| -rw-r--r-- | clang/test/Sema/zero-initializer.c | 7 |
2 files changed, 9 insertions, 2 deletions
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index f8017bb7ade..10ab2bf72b7 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -2303,11 +2303,11 @@ bool InitListExpr::isTransparent() const { bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions &LangOpts) const { assert(isSyntacticForm() && "only test syntactic form as zero initializer"); - if (LangOpts.CPlusPlus || getNumInits() != 1) { + if (LangOpts.CPlusPlus || getNumInits() != 1 || !getInit(0)) { return false; } - const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(getInit(0)); + const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(getInit(0)->IgnoreImplicit()); return Lit && Lit->getValue() == 0; } diff --git a/clang/test/Sema/zero-initializer.c b/clang/test/Sema/zero-initializer.c index 0ab410d4c6d..e54021a582c 100644 --- a/clang/test/Sema/zero-initializer.c +++ b/clang/test/Sema/zero-initializer.c @@ -7,6 +7,8 @@ struct A { int a; }; 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 @@ int main(void) 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 @@ int main(void) 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; } |

