blob: 1af79b4ba37585aa3a911ca2ecfc09a3ed0af56f [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
79 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
80 CurTokenLexer));
81 CurLexer = 0;
82 CurTokenLexer = 0;
83}
84
85
86const 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}
Argyrios Kyrtzidis3604e382008-11-08 16:17:04 +000096
97void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
98 assert(Tok.isAnnotationToken() && "Expected annotation token");
99 assert(CachedLexPos != 0 && "Expected to have some cached tokens");
100 assert(CachedTokens[CachedLexPos-1].getLocation() == Tok.getAnnotationEndLoc()
101 && "The annotation should be until the most recent cached token");
102
103 // Start from the end of the cached tokens list and look for the token
104 // that is the beginning of the annotation token.
105 for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
106 CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
107 if (AnnotBegin->getLocation() == Tok.getLocation()) {
108 assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
109 "The backtrack pos points inside the annotated tokens!");
110 // Replace the cached tokens with the single annotation token.
111 CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
112 *AnnotBegin = Tok;
113 CachedLexPos = i;
114 return;
115 }
116 }
117
118 assert(0&&"Didn't find the first token represented by the annotation token!");
119}