[cfe-commits] r53375 - in /cfe/trunk: include/clang/Lex/Preprocessor.h lib/Lex/PPLexerChange.cpp

Argiris Kirtzidis akyrtzi at gmail.com
Wed Jul 9 15:46:46 PDT 2008


Author: akirtzidis
Date: Wed Jul  9 17:46:46 2008
New Revision: 53375

URL: http://llvm.org/viewvc/llvm-project?rev=53375&view=rev
Log:
Add Preprocessor::LookNext method, which implements an efficient way to 'take a peek' at the next token without consuming it.

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

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul  9 17:46:46 2008
@@ -138,6 +138,11 @@
   enum { TokenLexerCacheSize = 8 };
   unsigned NumCachedTokenLexers;
   TokenLexer *TokenLexerCache[TokenLexerCacheSize];
+
+  /// PeekedToken - Cache the token that was retrieved through LookNext().
+  /// This is a valid token (its Location is valid) when LookNext() is
+  /// called and gets invalid again when it is "consumed" by Lex().
+  Token PeekedToken;
 public:
   Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target,
                SourceManager &SM, HeaderSearch &Headers);
@@ -258,8 +263,13 @@
   void Lex(Token &Result) {
     if (CurLexer)
       CurLexer->Lex(Result);
-    else
+    else if (CurTokenLexer)
       CurTokenLexer->Lex(Result);
+    else {
+      // We have a peeked token that hasn't been consumed yet.
+      Result = PeekedToken;
+      ConsumedPeekedToken();
+    }
   }
   
   /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
@@ -294,6 +304,27 @@
   /// code paths if possible!
   ///
   Token LookAhead(unsigned N);
+
+  /// LookNext - Returns the next token that would be returned by Lex() without
+  /// consuming it.
+  const Token &LookNext() {
+    if (PeekedToken.getLocation().isInvalid()) {
+      // We don't have a peeked token that hasn't been consumed yet.
+      // Peek it now.
+      PeekToken();
+    }
+    return PeekedToken;
+  }
+
+private:
+  /// PeekToken - Lexes one token into PeekedToken and pushes CurLexer,
+  /// CurLexerToken into the IncludeMacroStack before setting them to null.
+  void PeekToken();
+
+  /// ConsumedPeekedToken - Called when Lex() is about to return the PeekedToken
+  /// and have it "consumed".
+  void ConsumedPeekedToken();
+public:
   
   /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
   /// the specified Token's location, translating the token's start

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

==============================================================================
--- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
+++ cfe/trunk/lib/Lex/PPLexerChange.cpp Wed Jul  9 17:46:46 2008
@@ -85,6 +85,10 @@
   // size we new to a multiple of 16 tokens.  If the previous buffer has space
   // left, we can just grow it.  This means we only have to do the new 1/16th as
   // often.
+
+  // Optimized LookAhead(0) case.
+  if (N == 0)
+    return LookNext();
   
   Token *LookaheadTokens = new Token[N+1];
 
@@ -122,6 +126,28 @@
   return Tok;
 }
 
+/// PeekToken - Lexes one token into PeekedToken and pushes CurLexer,
+/// CurLexerToken into the IncludeMacroStack before setting them to null.
+void Preprocessor::PeekToken() {
+  Lex(PeekedToken);
+  // Cache the current Lexer, TokenLexer and set them both to null.
+  // When Lex() is called, PeekedToken will be "consumed".
+  IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
+                                               CurTokenLexer));
+  CurLexer = 0;
+  CurTokenLexer = 0;
+}
+
+/// ConsumedPeekedToken - Called when Lex() is about to return the PeekedToken
+/// and have it "consumed".
+void Preprocessor::ConsumedPeekedToken() {
+  assert(PeekedToken.getLocation().isValid() && "Confused Peeking?");
+  // Restore CurLexer, TokenLexer.
+  RemoveTopOfLexerStack();
+  // Make PeekedToken invalid.
+  PeekedToken.startToken();
+}
+
 
 //===----------------------------------------------------------------------===//
 // Methods for Entering and Callbacks for leaving various contexts





More information about the cfe-commits mailing list