[cfe-commits] r59733 - in /cfe/trunk: include/clang/Lex/PTHLexer.h lib/Lex/PTHLexer.cpp

Ted Kremenek kremenek at apple.com
Thu Nov 20 08:32:22 PST 2008


Author: kremenek
Date: Thu Nov 20 10:32:22 2008
New Revision: 59733

URL: http://llvm.org/viewvc/llvm-project?rev=59733&view=rev
Log:
PTHLexer:
- Rename 'CurToken' and 'LastToken' to 'CurTokenIdx' and 'LastTokenIdx'
  respectively.
- Add helper methods GetToken(), AdvanceToken(), AtLastToken() to abstract away
  details of the token stream. This also allows us to easily replace their
  implementation later.

Modified:
    cfe/trunk/include/clang/Lex/PTHLexer.h
    cfe/trunk/lib/Lex/PTHLexer.cpp

Modified: cfe/trunk/include/clang/Lex/PTHLexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PTHLexer.h?rev=59733&r1=59732&r2=59733&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/PTHLexer.h (original)
+++ cfe/trunk/include/clang/Lex/PTHLexer.h Thu Nov 20 10:32:22 2008
@@ -26,10 +26,10 @@
   
   /// LastTokenIdx - The index of the last token in Tokens.  This token
   ///  will be an eof token.
-  unsigned LastToken;
+  unsigned LastTokenIdx;
   
-  /// CurToken - This is the index of the next token that Lex will return.
-  unsigned CurToken;
+  /// CurTokenIdx - This is the index of the next token that Lex will return.
+  unsigned CurTokenIdx;
         
   PTHLexer(const PTHLexer&);  // DO NOT IMPLEMENT
   void operator=(const PTHLexer&); // DO NOT IMPLEMENT
@@ -53,11 +53,25 @@
   /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
   /// tok::l_paren token, 0 if it is something else and 2 if there are no more
   /// tokens controlled by this lexer.
-  unsigned isNextPPTokenLParen();
-  
+  unsigned isNextPPTokenLParen() {
+    return AtLastToken() ? 2 : GetToken().is(tok::l_paren);
+  }
+
   /// IndirectLex - An indirect call to 'Lex' that can be invoked via
   ///  the PreprocessorLexer interface.
   void IndirectLex(Token &Result) { Lex(Result); }
+  
+private:
+  
+  /// AtLastToken - Returns true if the PTHLexer is at the last token.
+  bool AtLastToken() const { return CurTokenIdx == LastTokenIdx; }
+  
+  /// GetToken - Returns the next token.  This method does not advance the
+  ///  PTHLexer to the next token.
+  Token GetToken() { return Tokens[CurTokenIdx]; }
+  
+  /// AdvanceToken - Advances the PTHLexer to the next token.
+  void AdvanceToken() { ++CurTokenIdx; }
 };
 
 }  // end namespace clang

Modified: cfe/trunk/lib/Lex/PTHLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PTHLexer.cpp?rev=59733&r1=59732&r2=59733&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PTHLexer.cpp (original)
+++ cfe/trunk/lib/Lex/PTHLexer.cpp Thu Nov 20 10:32:22 2008
@@ -20,19 +20,19 @@
                    const Token *TokArray, unsigned NumTokens)
   : PreprocessorLexer(&pp, fileloc),
     Tokens(TokArray),
-    LastToken(NumTokens - 1),
-    CurToken(0) {
+    LastTokenIdx(NumTokens - 1),
+    CurTokenIdx(0) {
 
   assert(NumTokens >= 1);
-  assert(Tokens[LastToken].is(tok::eof));
+  assert(Tokens[LastTokenIdx].is(tok::eof));
 }
 
 void PTHLexer::Lex(Token& Tok) {
 LexNextToken:
-  if (CurToken == LastToken) {
+  if (AtLastToken()) {
     if (ParsingPreprocessorDirective) {
       ParsingPreprocessorDirective = false;
-      Tok = Tokens[LastToken];
+      Tok = GetToken();
       Tok.setKind(tok::eom);
       MIOpt.ReadToken();
       return;
@@ -45,7 +45,7 @@
     return;
   }
 
-  Tok = Tokens[CurToken];
+  Tok = GetToken();
   
   // Don't advance to the next token yet.  Check if we are at the
   // start of a new line and we're processing a directive.  If so, we
@@ -58,7 +58,7 @@
   }
   
   // Advance to the next token.
-  ++CurToken;
+  AdvanceToken();
     
   if (Tok.is(tok::hash)) {    
     if (Tok.isAtStartOfLine() && !LexingRawMode) {
@@ -80,7 +80,7 @@
 }
 
 void PTHLexer::setEOF(Token& Tok) {
-  Tok = Tokens[LastToken];
+  Tok = Tokens[LastTokenIdx];
 }
 
 void PTHLexer::DiscardToEndOfLine() {
@@ -88,19 +88,11 @@
          "Must be in a preprocessing directive!");
 
   // Already at end-of-file?
-  if (CurToken == LastToken)
+  if (AtLastToken())
     return;
 
   // Find the first token that is not the start of the *current* line.
-  for ( ++CurToken; CurToken != LastToken ; ++CurToken )
-    if (Tokens[CurToken].isAtStartOfLine())
+  for (AdvanceToken(); !AtLastToken(); AdvanceToken())
+    if (GetToken().isAtStartOfLine())
       return;
 }
-
-unsigned PTHLexer::isNextPPTokenLParen() {  
-  if (CurToken == LastToken)
-    return 2;
-  
-  return Tokens[CurToken].is(tok::l_paren);
-}
-





More information about the cfe-commits mailing list