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() { |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 27 | BacktrackPositions.push_back(CachedLexPos); |
| 28 | EnterCachingLexMode(); |
| 29 | } |
Argyrios Kyrtzidis | a9e274c | 2008-08-22 21:27:50 +0000 | [diff] [blame] | 30 | |
Argyrios Kyrtzidis | ed5c386 | 2008-08-24 12:29:43 +0000 | [diff] [blame] | 31 | /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call. |
| 32 | void Preprocessor::CommitBacktrackedTokens() { |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 33 | assert(!BacktrackPositions.empty() |
| 34 | && "EnableBacktrackAtThisPos was not called!"); |
| 35 | BacktrackPositions.pop_back(); |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | /// EnableBacktrackAtThisPos() was previously called. |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 40 | void Preprocessor::Backtrack() { |
| 41 | assert(!BacktrackPositions.empty() |
| 42 | && "EnableBacktrackAtThisPos was not called!"); |
| 43 | CachedLexPos = BacktrackPositions.back(); |
| 44 | BacktrackPositions.pop_back(); |
Argyrios Kyrtzidis | bff73f7 | 2008-08-23 12:05:53 +0000 | [diff] [blame] | 45 | } |
Argyrios Kyrtzidis | a9e274c | 2008-08-22 21:27:50 +0000 | [diff] [blame] | 46 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 47 | void Preprocessor::CachingLex(Token &Result) { |
Argyrios Kyrtzidis | 03569ea | 2010-07-12 18:49:30 +0000 | [diff] [blame^] | 48 | if (!InCachingLexMode()) |
| 49 | return; |
| 50 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 51 | if (CachedLexPos < CachedTokens.size()) { |
| 52 | Result = CachedTokens[CachedLexPos++]; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | ExitCachingLexMode(); |
Argyrios Kyrtzidis | 03569ea | 2010-07-12 18:49:30 +0000 | [diff] [blame^] | 57 | // True if we consumed everything already. |
| 58 | bool PastEOF = CurPPLexer == 0 && CurTokenLexer == 0; |
| 59 | if (!PastEOF) |
| 60 | Lex(Result); |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 61 | |
Argyrios Kyrtzidis | fad03b2 | 2008-11-19 14:23:14 +0000 | [diff] [blame] | 62 | if (!isBacktrackEnabled()) { |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 63 | // All cached tokens were consumed. |
| 64 | CachedTokens.clear(); |
| 65 | CachedLexPos = 0; |
| 66 | return; |
| 67 | } |
| 68 | |
Argyrios Kyrtzidis | 03569ea | 2010-07-12 18:49:30 +0000 | [diff] [blame^] | 69 | // Cache the lexed token if it's not a repeated tok::eof. |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 70 | EnterCachingLexMode(); |
Argyrios Kyrtzidis | 03569ea | 2010-07-12 18:49:30 +0000 | [diff] [blame^] | 71 | if (!PastEOF) { |
| 72 | CachedTokens.push_back(Result); |
| 73 | ++CachedLexPos; |
| 74 | } |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void Preprocessor::EnterCachingLexMode() { |
| 78 | if (InCachingLexMode()) |
| 79 | return; |
| 80 | |
Ted Kremenek | 174da89 | 2008-11-12 22:21:57 +0000 | [diff] [blame] | 81 | PushIncludeMacroStack(); |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | const Token &Preprocessor::PeekAhead(unsigned N) { |
| 86 | assert(CachedLexPos + N > CachedTokens.size() && "Confused caching."); |
| 87 | ExitCachingLexMode(); |
| 88 | for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) { |
| 89 | CachedTokens.push_back(Token()); |
| 90 | Lex(CachedTokens.back()); |
| 91 | } |
| 92 | EnterCachingLexMode(); |
| 93 | return CachedTokens.back(); |
| 94 | } |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 95 | |
| 96 | void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) { |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 97 | assert(Tok.isAnnotation() && "Expected annotation token"); |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 98 | assert(CachedLexPos != 0 && "Expected to have some cached tokens"); |
Sebastian Redl | 39d6711 | 2010-02-08 19:35:18 +0000 | [diff] [blame] | 99 | assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc() |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 100 | && "The annotation should be until the most recent cached token"); |
| 101 | |
| 102 | // Start from the end of the cached tokens list and look for the token |
| 103 | // that is the beginning of the annotation token. |
| 104 | for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) { |
| 105 | CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1; |
| 106 | if (AnnotBegin->getLocation() == Tok.getLocation()) { |
| 107 | assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) && |
| 108 | "The backtrack pos points inside the annotated tokens!"); |
| 109 | // Replace the cached tokens with the single annotation token. |
Nuno Lopes | 1b68f71 | 2009-07-26 16:36:45 +0000 | [diff] [blame] | 110 | if (i < CachedLexPos) |
| 111 | CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos); |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 112 | *AnnotBegin = Tok; |
| 113 | CachedLexPos = i; |
| 114 | return; |
| 115 | } |
| 116 | } |
Argyrios Kyrtzidis | 3604e38 | 2008-11-08 16:17:04 +0000 | [diff] [blame] | 117 | } |