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 | |
Ted Kremenek | 174da89 | 2008-11-12 22:21:57 +0000 | [diff] [blame^] | 79 | PushIncludeMacroStack(); |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | const Token &Preprocessor::PeekAhead(unsigned N) { |
| 84 | assert(CachedLexPos + N > CachedTokens.size() && "Confused caching."); |
| 85 | ExitCachingLexMode(); |
| 86 | for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) { |
| 87 | CachedTokens.push_back(Token()); |
| 88 | Lex(CachedTokens.back()); |
| 89 | } |
| 90 | EnterCachingLexMode(); |
| 91 | return CachedTokens.back(); |
| 92 | } |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 93 | |
| 94 | void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) { |
| 95 | assert(Tok.isAnnotationToken() && "Expected annotation token"); |
| 96 | assert(CachedLexPos != 0 && "Expected to have some cached tokens"); |
| 97 | assert(CachedTokens[CachedLexPos-1].getLocation() == Tok.getAnnotationEndLoc() |
| 98 | && "The annotation should be until the most recent cached token"); |
| 99 | |
| 100 | // Start from the end of the cached tokens list and look for the token |
| 101 | // that is the beginning of the annotation token. |
| 102 | for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) { |
| 103 | CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1; |
| 104 | if (AnnotBegin->getLocation() == Tok.getLocation()) { |
| 105 | assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) && |
| 106 | "The backtrack pos points inside the annotated tokens!"); |
| 107 | // Replace the cached tokens with the single annotation token. |
| 108 | CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos); |
| 109 | *AnnotBegin = Tok; |
| 110 | CachedLexPos = i; |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | assert(0&&"Didn't find the first token represented by the annotation token!"); |
| 116 | } |