From 521ff2b430c9f97314c6f286235358aa52b30043 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 6 Apr 2008 05:26:30 +0000 Subject: Use token lookahead to simplify some code that is rarely executed. Since it is rare, the cost is not significant and we enjoy the simplification. llvm-svn: 49263 --- clang/lib/Parse/ParseDecl.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'clang/lib/Parse/ParseDecl.cpp') diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 9b8ecf8bd39..1659af30e6a 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -1440,24 +1440,20 @@ void Parser::ParseBracketDeclarator(Declarator &D) { // Handle "direct-declarator [ type-qual-list[opt] * ]". bool isStar = false; ExprResult NumElements(false); - if (Tok.is(tok::star)) { + + // Handle the case where we have '[*]' as the array size. However, a leading + // star could be the start of an expression, for example 'X[*p + 4]'. Verify + // the the token after the star is a ']'. Since stars in arrays are + // infrequent, use of lookahead is not costly here. + if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { // Remember the '*' token, in case we have to un-get it. Token StarTok = Tok; ConsumeToken(); - // Check that the ']' token is present to avoid incorrectly parsing - // expressions starting with '*' as [*]. - if (Tok.is(tok::r_square)) { - if (StaticLoc.isValid()) - Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); - StaticLoc = SourceLocation(); // Drop the static. - isStar = true; - } else { - // Otherwise, the * must have been some expression (such as '*ptr') that - // started an assignment-expr. We already consumed the token, but now we - // need to reparse it. This handles cases like 'X[*p + 4]' - NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); - } + if (StaticLoc.isValid()) + Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); + StaticLoc = SourceLocation(); // Drop the static. + isStar = true; } else if (Tok.isNot(tok::r_square)) { // Parse the assignment-expression now. NumElements = ParseAssignmentExpression(); -- cgit v1.2.3