diff options
author | Chris Lattner <sabre@nondot.org> | 2008-04-06 05:26:30 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-04-06 05:26:30 +0000 |
commit | 5dcc6ce4a68b3bd4b89c5697c9728e1533e71e03 (patch) | |
tree | c87ffb4d6edc980ebf03c54c4e22d9e63af9966b /lib/Parse/ParseDecl.cpp | |
parent | 5619688510185081cbb4621d703daf7ee24cf39a (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@49263 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseDecl.cpp')
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 9b8ecf8bd3..1659af30e6 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/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(); |