Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 1 | //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements pieces of the Preprocessor interface that manage the |
| 11 | // caching of lexed tokens. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | using namespace clang; |
| 17 | |
Argyrios Kyrtzidis | 02e7e74 | 2008-09-05 08:53:53 +0000 | [diff] [blame] | 18 | /// EnableBacktrackAtThisPos - From the point that this method is called, and |
| 19 | /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor |
| 20 | /// keeps track of the lexed tokens so that a subsequent Backtrack() call will |
| 21 | /// make the Preprocessor re-lex the same tokens. |
| 22 | /// |
| 23 | /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can |
| 24 | /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will |
| 25 | /// be combined with the EnableBacktrackAtThisPos calls in reverse order. |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 26 | void Preprocessor::EnableBacktrackAtThisPos() { |
| 27 | CacheTokens = true; |
| 28 | BacktrackPositions.push_back(CachedLexPos); |
| 29 | EnterCachingLexMode(); |
| 30 | } |
Argyrios Kyrtzidis | a9e274c | 2008-08-22 21:27:50 +0000 | [diff] [blame] | 31 | |
Argyrios Kyrtzidis | ed5c386 | 2008-08-24 12:29:43 +0000 | [diff] [blame] | 32 | /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call. |
| 33 | void Preprocessor::CommitBacktrackedTokens() { |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 34 | assert(!BacktrackPositions.empty() |
| 35 | && "EnableBacktrackAtThisPos was not called!"); |
| 36 | BacktrackPositions.pop_back(); |
| 37 | CacheTokens = !BacktrackPositions.empty(); |
| 38 | } |
| 39 | |
| 40 | /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since |
| 41 | /// EnableBacktrackAtThisPos() was previously called. |
| 42 | void Preprocessor::Backtrack() { |
| 43 | assert(!BacktrackPositions.empty() |
| 44 | && "EnableBacktrackAtThisPos was not called!"); |
| 45 | CachedLexPos = BacktrackPositions.back(); |
| 46 | BacktrackPositions.pop_back(); |
| 47 | CacheTokens = !BacktrackPositions.empty(); |
| 48 | } |
Argyrios Kyrtzidis | a9e274c | 2008-08-22 21:27:50 +0000 | [diff] [blame] | 49 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 50 | void Preprocessor::CachingLex(Token &Result) { |
| 51 | if (CachedLexPos < CachedTokens.size()) { |
| 52 | Result = CachedTokens[CachedLexPos++]; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | ExitCachingLexMode(); |
| 57 | Lex(Result); |
| 58 | |
| 59 | if (!CacheTokens) { |
| 60 | // All cached tokens were consumed. |
| 61 | CachedTokens.clear(); |
| 62 | CachedLexPos = 0; |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // We should cache the lexed token. |
| 67 | |
| 68 | EnterCachingLexMode(); |
| 69 | if (Result.isNot(tok::eof)) { |
| 70 | CachedTokens.push_back(Result); |
| 71 | ++CachedLexPos; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void Preprocessor::EnterCachingLexMode() { |
| 76 | if (InCachingLexMode()) |
| 77 | return; |
| 78 | |
| 79 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 80 | CurTokenLexer)); |
| 81 | CurLexer = 0; |
| 82 | CurTokenLexer = 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | const Token &Preprocessor::PeekAhead(unsigned N) { |
| 87 | assert(CachedLexPos + N > CachedTokens.size() && "Confused caching."); |
| 88 | ExitCachingLexMode(); |
| 89 | for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) { |
| 90 | CachedTokens.push_back(Token()); |
| 91 | Lex(CachedTokens.back()); |
| 92 | } |
| 93 | EnterCachingLexMode(); |
| 94 | return CachedTokens.back(); |
| 95 | } |