From: Richard Smith Date: Wed, 3 Feb 2016 02:58:20 +0000 (+0000) Subject: Fix miscompile and rejects-valids when disambiguating after an ambiguous X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0baa998ea1e867d5d7f73b3a24e23c6f283d7ef7;p=clang Fix miscompile and rejects-valids when disambiguating after an ambiguous C-style-cast to function/array type or parenthesized function-style cast/array indexing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259622 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index ad23804d1d..13b77223ac 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -1010,15 +1010,24 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression, // unary-expression: // ++ cast-expression // -- cast-expression - SourceLocation SavedLoc = ConsumeToken(); + Token SavedTok = Tok; + ConsumeToken(); // One special case is implicitly handled here: if the preceding tokens are // an ambiguous cast expression, such as "(T())++", then we recurse to // determine whether the '++' is prefix or postfix. Res = ParseCastExpression(!getLangOpts().CPlusPlus, /*isAddressOfOperand*/false, NotCastExpr, - NotTypeCast); + isTypeCast); + if (NotCastExpr) { + // If we return with NotCastExpr = true, we must not consume any tokens, + // so put the token back where we found it. + assert(Res.isInvalid()); + UnconsumeToken(SavedTok); + return ExprError(); + } if (!Res.isInvalid()) - Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); + Res = Actions.ActOnUnaryOp(getCurScope(), SavedTok.getLocation(), + SavedKind, Res.get()); return Res; } case tok::amp: { // unary-expression: '&' cast-expression diff --git a/test/Parser/cxx-ambig-paren-expr.cpp b/test/Parser/cxx-ambig-paren-expr.cpp index 3988205672..1535b00972 100644 --- a/test/Parser/cxx-ambig-paren-expr.cpp +++ b/test/Parser/cxx-ambig-paren-expr.cpp @@ -21,8 +21,13 @@ void f() { struct S{int operator()();}; (S())(); - // FIXME: Special case: "++" is postfix here, not prefix - // (S())++; + // Special case: "++" is postfix here, not prefix + (S())++; // expected-error {{cannot increment value of type 'S'}} + + struct X { int &operator++(int); X operator[](int); int &operator++(); }; + int &postfix_incr = (X()[3])++; + (X())++ ++; // ok, not a C-style cast + (X())++ ++X(); // expected-error {{C-style cast from 'int' to 'X ()'}} } // Make sure we do tentative parsing correctly in conditions.