blob: 1f39088d7f0b6988dff16d9e907c5a31a95cb1b3 [file] [log] [blame]
Argyrios Kyrtzidis03db1b32008-08-10 13:15:22 +00001//===--- 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"
16using namespace clang;
17
Argyrios Kyrtzidis02e7e742008-09-05 08:53:53 +000018/// 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 Kyrtzidisbff73f72008-08-23 12:05:53 +000026void Preprocessor::EnableBacktrackAtThisPos() {
27 CacheTokens = true;
28 BacktrackPositions.push_back(CachedLexPos);
29 EnterCachingLexMode();
30}
Argyrios Kyrtzidisa9e274c2008-08-22 21:27:50 +000031
Argyrios Kyrtzidised5c3862008-08-24 12:29:43 +000032/// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
33void Preprocessor::CommitBacktrackedTokens() {
Argyrios Kyrtzidisbff73f72008-08-23 12:05:53 +000034 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.
42void 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 Kyrtzidisa9e274c2008-08-22 21:27:50 +000049
Argyrios Kyrtzidis03db1b32008-08-10 13:15:22 +000050void 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
75void Preprocessor::EnterCachingLexMode() {
76 if (InCachingLexMode())
77 return;
78
Ted Kremenek174da892008-11-12 22:21:57 +000079 PushIncludeMacroStack();
Argyrios Kyrtzidis03db1b32008-08-10 13:15:22 +000080}
81
82
83const 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 Kyrtzidis3604e382008-11-08 16:17:04 +000093
94void 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}