[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames added a comment. Thanks! Repository: rL LLVM CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
This revision was automatically updated to reflect the committed changes. Closed by commit rL366163: [Sema] Suppress additional warnings for Cs zero initializer (authored by Lekensteyn, committed by ). Herald added a project: LLVM. Changed prior to commit: https://reviews.llvm.org/D61838?vs=209994=210009#toc Repository: rL LLVM CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 Files: cfe/trunk/lib/AST/Expr.cpp cfe/trunk/test/Sema/zero-initializer.c Index: cfe/trunk/lib/AST/Expr.cpp === --- cfe/trunk/lib/AST/Expr.cpp +++ cfe/trunk/lib/AST/Expr.cpp @@ -2303,11 +2303,11 @@ bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions ) 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(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); return Lit && Lit->getValue() == 0; } Index: cfe/trunk/test/Sema/zero-initializer.c === --- cfe/trunk/test/Sema/zero-initializer.c +++ cfe/trunk/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: cfe/trunk/lib/AST/Expr.cpp === --- cfe/trunk/lib/AST/Expr.cpp +++ cfe/trunk/lib/AST/Expr.cpp @@ -2303,11 +2303,11 @@ bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions ) 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(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); return Lit && Lit->getValue() == 0; } Index: cfe/trunk/test/Sema/zero-initializer.c === --- cfe/trunk/test/Sema/zero-initializer.c +++ cfe/trunk/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; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn accepted this revision. Lekensteyn added a comment. Thanks, I'll push once the build and test pass. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames updated this revision to Diff 209994. al3xtjames marked an inline comment as done. al3xtjames added a comment. Added null check for `getInit(0)` Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ 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 @@ -2303,11 +2303,11 @@ bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions ) 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(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); 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 @@ -2303,11 +2303,11 @@ bool InitListExpr::isIdiomaticZeroInitializer(const LangOptions ) 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(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); return Lit && Lit->getValue() == 0; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn added a comment. @al3xtjames I was about to commit this but noticed that some others check whether `getInit(0)` is NULL or not before proceeding. Should that be done here as well? If not, why? See for example "Harden InitListExpr::isStringLiteralInit() against getInit() returning null." in https://github.com/llvm/llvm-project/commit/256bd96d1c2464b0f0ecb482ca52075a3158f6a1#diff-dac09655ff6a54658c320a28a6ea297c and InitListExpr::isTransparent in https://github.com/llvm/llvm-project/commit/122f88d481971b68d05ba12395c3b73ab4b31ab3#diff-dac09655ff6a54658c320a28a6ea297c Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn added a comment. @rsmith The latest patch version has addressed your feedback, can you confirm that this is ready to be merged? Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn added a comment. @rsmith Are you happy with the changes, is it ready to be merged? Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn accepted this revision. Lekensteyn added a comment. Verified again! Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames updated this revision to Diff 200159. al3xtjames added a comment. Switched to using Expr::IgnoreImplicit() instead of manually checking for an ImplicitCastExpression. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ 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 @@ -2175,7 +2175,7 @@ return false; } - const IntegerLiteral *Lit = dyn_cast(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); 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 @@ -2175,7 +2175,7 @@ return false; } - const IntegerLiteral *Lit = dyn_cast(getInit(0)); + const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); return Lit && Lit->getValue() == 0; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
rsmith added inline comments. Comment at: clang/lib/AST/Expr.cpp:2092-2096 const IntegerLiteral *Lit = dyn_cast(getInit(0)); + if (!Lit) { +if (const ImplicitCastExpr *ICE = dyn_cast(getInit(0))) + Lit = dyn_cast(ICE->getSubExpr()); + } Use `Expr::IgnoreImplicit` rather than checking for an `ImplicitCastExpr` here: ``` const IntegerLiteral *Lit = dyn_cast(getInit(0)->IgnoreImplicit()); ``` Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames added a comment. Thanks for reviewing! I don't have commit access, so I can't commit this patch myself. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn accepted this revision. Lekensteyn added a comment. This revision is now accepted and ready to land. Thanks, I have also verified this patch against the above test case. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames added a comment. Such nested structures work fine (no warnings generated). Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
Lekensteyn added a comment. This looks reasonable to fix the problem at hand, but would it handle nested structures too? struct s1 { short f1; // "int f1" is fine. }; struct s2 { struct s1 f2; int x; }; struct s3 { struct s2 f3; int x; }; struct s2 x1 = {0}; struct s3 x2 = {0}; produces this AST (`clang-check -ast-dump x.c --`): |-VarDecl 0x55e7bbc60390 col:11 x1 'struct s2':'struct s2' cinit | `-InitListExpr 0x55e7bbc60498 'struct s2':'struct s2' | |-InitListExpr 0x55e7bbc604e8 'struct s1':'struct s1' | | `-ImplicitCastExpr 0x55e7bbc60530 'short' | | `-IntegerLiteral 0x55e7bbc60430 'int' 0 | `-ImplicitValueInitExpr 0x55e7bbc60548 <> 'int' |-VarDecl 0x55e7bbc605b0 col:11 x2 'struct s3':'struct s3' cinit | `-InitListExpr 0x55e7bbc60678 'struct s3':'struct s3' | |-InitListExpr 0x55e7bbc606c8 'struct s2':'struct s2' | | |-InitListExpr 0x55e7bbc60718 'struct s1':'struct s1' | | | `-ImplicitCastExpr 0x55e7bbc60760 'short' | | | `-IntegerLiteral 0x55e7bbc60610 'int' 0 | | `-ImplicitValueInitExpr 0x55e7bbc60778 <> 'int' | `-ImplicitValueInitExpr 0x55e7bbc60788 <> 'int' Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ https://reviews.llvm.org/D61838 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
al3xtjames updated this revision to Diff 199192. al3xtjames edited the summary of this revision. al3xtjames added a comment. Formatting changes. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D61838/new/ 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,10 @@ } const IntegerLiteral *Lit = dyn_cast(getInit(0)); + if (!Lit) { +if (const ImplicitCastExpr *ICE = dyn_cast(getInit(0))) + Lit = dyn_cast(ICE->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,10 @@ } const IntegerLiteral *Lit = dyn_cast(getInit(0)); + if (!Lit) { +if (const ImplicitCastExpr *ICE = dyn_cast(getInit(0))) + Lit = dyn_cast(ICE->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
[PATCH] D61838: [Sema] Suppress additional warnings for C's zero initializer
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(getInit(0)); + if (!Lit) { +const ImplicitCastExpr *Cast = dyn_cast(getInit(0)); +if (!Cast) + return false; +Lit = dyn_cast(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(getInit(0)); + if (!Lit) { +const ImplicitCastExpr *Cast = dyn_cast(getInit(0)); +if (!Cast) + return false; +Lit = dyn_cast(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