[cfe-commits] r38994 - in /cfe/cfe/trunk: Parse/ParseDecl.cpp Parse/ParseExpr.cpp Parse/ParseStmt.cpp include/clang/Parse/Parser.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:26:37 PDT 2007


Author: sabre
Date: Wed Jul 11 11:26:37 2007
New Revision: 38994

URL: http://llvm.org/viewvc/llvm-project?rev=38994&view=rev
Log:
Make ConsumeToken return the loc of the consumed token to simplify clients

Modified:
    cfe/cfe/trunk/Parse/ParseDecl.cpp
    cfe/cfe/trunk/Parse/ParseExpr.cpp
    cfe/cfe/trunk/Parse/ParseStmt.cpp
    cfe/cfe/trunk/include/clang/Parse/Parser.h

Modified: cfe/cfe/trunk/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseDecl.cpp?rev=38994&r1=38993&r2=38994&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:26:37 2007
@@ -430,9 +430,8 @@
 void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
   assert((Tok.getKind() == tok::kw_struct ||
           Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
-  SourceLocation Start = Tok.getLocation();
   bool isUnion = Tok.getKind() == tok::kw_union;
-  ConsumeToken();
+  SourceLocation Start = ConsumeToken();
 
   // If attributes exist after tag, parse them.
   if (Tok.getKind() == tok::kw___attribute)
@@ -554,8 +553,7 @@
 ///
 void Parser::ParseEnumSpecifier(DeclSpec &DS) {
   assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
-  SourceLocation Start = Tok.getLocation();
-  ConsumeToken();
+  SourceLocation Start = ConsumeToken();
   
   if (Tok.getKind() == tok::kw___attribute)
     ParseAttributes();
@@ -589,8 +587,7 @@
       
       if (Tok.getKind() != tok::comma)
         break;
-      SourceLocation CommaLoc = Tok.getLocation();
-      ConsumeToken();
+      SourceLocation CommaLoc = ConsumeToken();
       
       if (Tok.getKind() != tok::identifier && !getLang().C99)
         Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
@@ -780,8 +777,7 @@
     return ParseDirectDeclarator(D);
   
   // Otherwise, '*' -> pointer.
-  SourceLocation Loc = Tok.getLocation();
-  ConsumeToken();  // Eat the *.
+  SourceLocation Loc = ConsumeToken();  // Eat the *.
   DeclSpec DS;
   ParseTypeQualifierListOpt(DS);
   
@@ -1038,10 +1034,8 @@
   
   // If valid, this location is the position where we read the 'static' keyword.
   SourceLocation StaticLoc;
-  if (Tok.getKind() == tok::kw_static) {
-    StaticLoc = Tok.getLocation();
-    ConsumeToken();
-  }
+  if (Tok.getKind() == tok::kw_static)
+    StaticLoc = ConsumeToken();
   
   // If there is a type-qualifier-list, read it now.
   DeclSpec DS;
@@ -1049,10 +1043,8 @@
   
   // If we haven't already read 'static', check to see if there is one after the
   // type-qualifier-list.
-  if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
-    StaticLoc = Tok.getLocation();
-    ConsumeToken();
-  }
+  if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
+    StaticLoc = ConsumeToken();
   
   // Handle "direct-declarator [ type-qual-list[opt] * ]".
   bool isStar = false;

Modified: cfe/cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseExpr.cpp?rev=38994&r1=38993&r2=38994&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:26:37 2007
@@ -329,8 +329,7 @@
       }
       
       // Eat the colon.
-      ColonLoc = Tok.getLocation();
-      ConsumeToken();
+      ColonLoc = ConsumeToken();
     }
     
     // Parse another leaf here for the RHS of the operator.
@@ -617,8 +616,8 @@
           
           if (Tok.getKind() != tok::comma)
             break;
-          CommaLocs.push_back(Tok.getLocation());
-          ConsumeToken();  // Next argument.
+          // Move to the next argument, remember where the comma was.
+          CommaLocs.push_back(ConsumeToken());
         }
       }
         
@@ -635,9 +634,8 @@
     }
     case tok::arrow:       // postfix-expression: p-e '->' identifier
     case tok::period: {    // postfix-expression: p-e '.' identifier
-      SourceLocation OpLoc = Tok.getLocation();
       tok::TokenKind OpKind = Tok.getKind();
-      ConsumeToken();  // Eat the "." or "->" token.
+      SourceLocation OpLoc = ConsumeToken();  // Eat the "." or "->" token.
       
       if (Tok.getKind() != tok::identifier) {
         Diag(Tok, diag::err_expected_ident);
@@ -720,11 +718,10 @@
 ///
 Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
   ExprResult Res(false);
-  SourceLocation StartLoc = Tok.getLocation();
   const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
 
   tok::TokenKind T = Tok.getKind();
-  ConsumeToken();   // Eat the builtin identifier.
+  SourceLocation StartLoc = ConsumeToken();   // Eat the builtin identifier.
 
   // All of these start with an open paren.
   if (Tok.getKind() != tok::l_paren) {

Modified: cfe/cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseStmt.cpp?rev=38994&r1=38993&r2=38994&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseStmt.cpp Wed Jul 11 11:26:37 2007
@@ -187,8 +187,7 @@
   
   // identifier ':' statement
   if (Tok.getKind() == tok::colon) {
-    SourceLocation ColonLoc = Tok.getLocation();
-    ConsumeToken();
+    SourceLocation ColonLoc = ConsumeToken();
 
     // Read label attributes, if present.
     if (Tok.getKind() == tok::kw___attribute)
@@ -256,8 +255,7 @@
 ///
 Parser::StmtResult Parser::ParseCaseStatement() {
   assert(Tok.getKind() == tok::kw_case && "Not a case stmt!");
-  SourceLocation CaseLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'case'.
+  SourceLocation CaseLoc = ConsumeToken();  // eat the 'case'.
 
   ExprResult LHS = ParseConstantExpression();
   if (LHS.isInvalid) {
@@ -270,7 +268,7 @@
   ExprTy *RHSVal = 0;
   if (Tok.getKind() == tok::ellipsis) {
     Diag(Tok, diag::ext_gnu_case_range);
-    ConsumeToken();
+    DotDotDotLoc = ConsumeToken();
     
     ExprResult RHS = ParseConstantExpression();
     if (RHS.isInvalid) {
@@ -286,8 +284,7 @@
     return true;
   }
   
-  SourceLocation ColonLoc = Tok.getLocation();
-  ConsumeToken();
+  SourceLocation ColonLoc = ConsumeToken();
   
   // Diagnose the common error "switch (X) { case 4: }", which is not valid.
   if (Tok.getKind() == tok::r_brace) {
@@ -311,8 +308,7 @@
 ///
 Parser::StmtResult Parser::ParseDefaultStatement() {
   assert(Tok.getKind() == tok::kw_default && "Not a default stmt!");
-  SourceLocation DefaultLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'default'.
+  SourceLocation DefaultLoc = ConsumeToken();  // eat the 'default'.
 
   if (Tok.getKind() != tok::colon) {
     Diag(Tok, diag::err_expected_colon_after, "'default'");
@@ -320,8 +316,7 @@
     return true;
   }
   
-  SourceLocation ColonLoc = Tok.getLocation();
-  ConsumeToken();
+  SourceLocation ColonLoc = ConsumeToken();
   
   // Diagnose the common error "switch (X) {... default: }", which is not valid.
   if (Tok.getKind() == tok::r_brace) {
@@ -396,8 +391,7 @@
 ///
 Parser::StmtResult Parser::ParseIfStatement() {
   assert(Tok.getKind() == tok::kw_if && "Not an if stmt!");
-  SourceLocation IfLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'if'.
+  SourceLocation IfLoc = ConsumeToken();  // eat the 'if'.
 
   if (Tok.getKind() != tok::l_paren) {
     Diag(Tok, diag::err_expected_lparen_after, "if");
@@ -419,8 +413,7 @@
   SourceLocation ElseLoc;
   StmtResult ElseStmt(false);
   if (Tok.getKind() == tok::kw_else) {
-    ElseLoc = Tok.getLocation();
-    ConsumeToken();
+    ElseLoc = ConsumeToken();
     ElseStmt = ParseStatement();
   }
   
@@ -436,8 +429,7 @@
 ///         'switch' '(' expression ')' statement
 Parser::StmtResult Parser::ParseSwitchStatement() {
   assert(Tok.getKind() == tok::kw_switch && "Not a switch stmt!");
-  SourceLocation SwitchLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'switch'.
+  SourceLocation SwitchLoc = ConsumeToken();  // eat the 'switch'.
 
   if (Tok.getKind() != tok::l_paren) {
     Diag(Tok, diag::err_expected_lparen_after, "switch");
@@ -487,8 +479,7 @@
 /// Note: this lets the caller parse the end ';'.
 Parser::StmtResult Parser::ParseDoStatement() {
   assert(Tok.getKind() == tok::kw_do && "Not a do stmt!");
-  SourceLocation DoLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'do'.
+  SourceLocation DoLoc = ConsumeToken();  // eat the 'do'.
   
   // Read the body statement.
   StmtResult Body = ParseStatement();
@@ -499,8 +490,7 @@
     SkipUntil(tok::semi);
     return true;
   }
-  SourceLocation WhileLoc = Tok.getLocation();
-  ConsumeToken();
+  SourceLocation WhileLoc = ConsumeToken();
   
   if (Tok.getKind() != tok::l_paren) {
     Diag(Tok, diag::err_expected_lparen_after, "do/while");
@@ -521,8 +511,7 @@
 ///         'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
 Parser::StmtResult Parser::ParseForStatement() {
   assert(Tok.getKind() == tok::kw_for && "Not a for stmt!");
-  SourceLocation ForLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'for'.
+  SourceLocation ForLoc = ConsumeToken();  // eat the 'for'.
   
   if (Tok.getKind() != tok::l_paren) {
     Diag(Tok, diag::err_expected_lparen_after, "for");
@@ -597,8 +586,7 @@
 ///
 Parser::StmtResult Parser::ParseGotoStatement() {
   assert(Tok.getKind() == tok::kw_goto && "Not a goto stmt!");
-  SourceLocation GotoLoc;
-  ConsumeToken();  // eat the 'goto'.
+  SourceLocation GotoLoc = ConsumeToken();  // eat the 'goto'.
   
   StmtResult Res;
   if (Tok.getKind() == tok::identifier) {
@@ -607,8 +595,7 @@
   } else if (Tok.getKind() == tok::star && !getLang().NoExtensions) {
     // GNU indirect goto extension.
     Diag(Tok, diag::ext_gnu_indirect_goto);
-    SourceLocation StarLoc = Tok.getLocation();
-    ConsumeToken();
+    SourceLocation StarLoc = ConsumeToken();
     ExprResult R = ParseExpression();
     if (R.isInvalid) {  // Skip to the semicolon, but don't consume it.
       SkipUntil(tok::semi, false, true);
@@ -624,8 +611,7 @@
 ///         'return' expression[opt] ';'
 Parser::StmtResult Parser::ParseReturnStatement() {
   assert(Tok.getKind() == tok::kw_return && "Not a return stmt!");
-  SourceLocation ReturnLoc = Tok.getLocation();
-  ConsumeToken();  // eat the 'return'.
+  SourceLocation ReturnLoc = ConsumeToken();  // eat the 'return'.
   
   ExprResult R(0);
   if (Tok.getKind() != tok::semi) {

Modified: cfe/cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Parse/Parser.h?rev=38994&r1=38993&r2=38994&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:26:37 2007
@@ -97,12 +97,15 @@
   
   /// ConsumeToken - Consume the current 'peek token' and lex the next one.
   /// This does not work will all kinds of tokens: strings and specific other
-  /// tokens must be consumed with custom methods below.
-  void ConsumeToken() {
+  /// tokens must be consumed with custom methods below.  This returns the
+  /// location of the consumed token.
+  SourceLocation ConsumeToken() {
     assert(!isTokenStringLiteral() && !isTokenParen() && !isTokenBracket() &&
            !isTokenBrace() &&
            "Should consume special tokens with Consume*Token");
+    SourceLocation L = Tok.getLocation();
     PP.Lex(Tok);
+    return L;
   }
   
   /// ConsumeAnyToken - Dispatch to the right Consume* method based on the





More information about the cfe-commits mailing list