From: Chris Lattner Date: Mon, 3 Nov 2008 09:28:22 +0000 (+0000) Subject: Fix PR3001: if we have an error parsing an initializer, make sure to remove X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e2f56196e916e4d2f9eeec5c8978490cdedb3f64;p=clang Fix PR3001: if we have an error parsing an initializer, make sure to remove the designator corresponding to it, otherwise Sema and later parsing will get confused. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58603 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Parse/Designator.h b/include/clang/Parse/Designator.h index 52476383ae..cf3f8777d5 100644 --- a/include/clang/Parse/Designator.h +++ b/include/clang/Parse/Designator.h @@ -220,6 +220,17 @@ public: return &Designations[i]; return 0; } + + /// EraseDesignation - If there is a designator for the specified initializer + /// index, remove it. + void EraseDesignation(unsigned Idx) { + Designation *D =const_cast(getDesignationForInitializer(Idx)); + if (D == 0) return; // No designator. + + D->FreeExprs(Actions); + unsigned SlotNo = D-&Designations[0]; + Designations.erase(Designations.begin()+SlotNo); + } }; diff --git a/lib/Parse/ParseInit.cpp b/lib/Parse/ParseInit.cpp index 82a33c21da..9b488566a6 100644 --- a/lib/Parse/ParseInit.cpp +++ b/lib/Parse/ParseInit.cpp @@ -263,10 +263,18 @@ Parser::ExprResult Parser::ParseBraceInitializer() { ExprResult SubElt; if (!MayBeDesignationStart(Tok.getKind(), PP)) SubElt = ParseInitializer(); - else + else { SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations, InitExprs.size()); - + + // If we had an erroneous initializer, and we had a potentially valid + // designator, make sure to remove the designator from + // InitExprDesignations, otherwise we'll end up with a designator with no + // making initializer. + if (SubElt.isInvalid) + InitExprDesignations.EraseDesignation(InitExprs.size()); + } + // If we couldn't parse the subelement, bail out. if (!SubElt.isInvalid) { InitExprs.push_back(SubElt.Val); diff --git a/test/Sema/init.c b/test/Sema/init.c index 9f91c42f44..a078a2a96b 100644 --- a/test/Sema/init.c +++ b/test/Sema/init.c @@ -67,3 +67,10 @@ int sym_fw1a_scr[] = { ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0, 8 * ((int)(&((struct sym_reg *)0)->nc_gpreg)) }; + +// PR3001 +struct s1 s2 = { + .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} + .b = bogus // expected-error {{use of undeclared identifier 'bogus'}} +} +