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

Argiris Kirtzidis akyrtzi at gmail.com
Fri Aug 22 14:27:57 PDT 2008


Author: akirtzidis
Date: Fri Aug 22 16:27:50 2008
New Revision: 55204

URL: http://llvm.org/viewvc/llvm-project?rev=55204&view=rev
Log:
Allow nested backtracks.

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

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

==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Fri Aug 22 16:27:50 2008
@@ -155,10 +155,11 @@
   /// a normal Lex() should be invoked.
   CachedTokensTy::size_type CachedLexPos;
 
-  /// CachedBacktrackPos - Gets set by the EnableBacktrackAtThisPos() method,
-  /// to indicate the position where CachedLexPos should be set when the
-  /// BackTrack() method is invoked.
-  CachedTokensTy::size_type CachedBacktrackPos;
+  /// BacktrackPositions - Stack of backtrack positions, allowing nested
+  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
+  /// indicate where CachedLexPos should be set when the BackTrack() method is
+  /// invoked (at which point the last position is popped).
+  std::vector<CachedTokensTy::size_type> BacktrackPositions;
 
 public:
   Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target,
@@ -281,34 +282,22 @@
   /// track of the lexed tokens so that a subsequent Backtrack() call will make
   /// the Preprocessor re-lex the same tokens.
   ///
-  /// EnableBacktrackAtThisPos should not be called again until DisableBacktrack
-  /// or Backtrack is called.
+  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
+  /// be called multiple times and DisableBacktrack/Backtrack calls will be
+  /// combined with the EnableBacktrackAtThisPos calls in reverse order.
   ///
   /// NOTE: *DO NOT* forget to call either DisableBacktrack() or Backtrack() at
   /// some point after EnableBacktrackAtThisPos. If you don't, caching of tokens
   /// will continue indefinitely.
   ///
-  void EnableBacktrackAtThisPos() {
-    assert(!CacheTokens && "Backtrack is already enabled!");
-    CacheTokens = true;
-    CachedBacktrackPos = CachedLexPos;
-    EnterCachingLexMode();
-  }
+  void EnableBacktrackAtThisPos();
 
-  /// DisableBacktrack - Stop the caching of tokens that was enabled by
-  /// EnableBacktrackAtThisPos().
-  void DisableBacktrack() {
-    assert(CacheTokens && "Backtrack is not enabled!");
-    CacheTokens = false;
-  }
+  /// DisableBacktrack - Disable the last EnableBacktrackAtThisPos() call.
+  void DisableBacktrack();
 
   /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
   /// EnableBacktrackAtThisPos() was previously called. 
-  void Backtrack() {
-    assert(CacheTokens && "Backtrack is not enabled!");
-    CacheTokens = false;
-    CachedLexPos = CachedBacktrackPos;
-  }
+  void Backtrack();
 
   /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
   /// caching of tokens is on.

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

==============================================================================
--- cfe/trunk/lib/Lex/PPCaching.cpp (original)
+++ cfe/trunk/lib/Lex/PPCaching.cpp Fri Aug 22 16:27:50 2008
@@ -15,6 +15,38 @@
 #include "clang/Lex/Preprocessor.h"
 using namespace clang;
 
+/// EnableBacktrackAtThisPos - From the point that this method is called, and
+/// until DisableBacktrack() or Backtrack() is called, the Preprocessor keeps
+/// track of the lexed tokens so that a subsequent Backtrack() call will make
+/// the Preprocessor re-lex the same tokens.
+///
+/// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
+/// be called multiple times and DisableBacktrack/Backtrack calls will be
+/// combined with the EnableBacktrackAtThisPos calls in reverse order.
+void Preprocessor::EnableBacktrackAtThisPos() {
+  CacheTokens = true;
+  BacktrackPositions.push_back(CachedLexPos);
+  EnterCachingLexMode();
+}
+
+/// DisableBacktrack - Disable the last EnableBacktrackAtThisPos() call.
+void Preprocessor::DisableBacktrack() {
+  assert(!BacktrackPositions.empty()
+         && "EnableBacktrackAtThisPos was not called!");
+  BacktrackPositions.pop_back();
+  CacheTokens = !BacktrackPositions.empty();
+}
+
+/// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
+/// EnableBacktrackAtThisPos() was previously called. 
+void Preprocessor::Backtrack() {
+  assert(!BacktrackPositions.empty()
+         && "EnableBacktrackAtThisPos was not called!");
+  CachedLexPos = BacktrackPositions.back();
+  BacktrackPositions.pop_back();
+  CacheTokens = !BacktrackPositions.empty();
+}
+
 void Preprocessor::CachingLex(Token &Result) {
   if (CachedLexPos < CachedTokens.size()) {
     Result = CachedTokens[CachedLexPos++];





More information about the cfe-commits mailing list