blob: 5bed51eaa4020b6f154fde7b3738cdce8463a2f7 [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 Kyrtzidisbff73f72008-08-23 12:05:53 +000018/// EnableBacktrackAtThisPos - From the point that this method is called, and
19/// until DisableBacktrack() or Backtrack() is called, the Preprocessor keeps
20/// track of the lexed tokens so that a subsequent Backtrack() call will make
21/// the Preprocessor re-lex the same tokens.
22///
23/// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
24/// be called multiple times and DisableBacktrack/Backtrack calls will be
25/// combined with the EnableBacktrackAtThisPos calls in reverse order.
26void Preprocessor::EnableBacktrackAtThisPos() {
27 CacheTokens = true;
28 BacktrackPositions.push_back(CachedLexPos);
29 EnterCachingLexMode();
30}
Argyrios Kyrtzidisa9e274c2008-08-22 21:27:50 +000031
Argyrios Kyrtzidisbff73f72008-08-23 12:05:53 +000032/// DisableBacktrack - Disable the last EnableBacktrackAtThisPos() call.
33void Preprocessor::DisableBacktrack() {
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.
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}