blob: 794e9c4e76558fee3042f29f2a356f05b66d5d4d [file] [log] [blame]
Argiris Kirtzidisf55b1102008-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
18void Preprocessor::CachingLex(Token &Result) {
19 if (CachedLexPos < CachedTokens.size()) {
20 Result = CachedTokens[CachedLexPos++];
21 return;
22 }
23
24 ExitCachingLexMode();
25 Lex(Result);
26
27 if (!CacheTokens) {
28 // All cached tokens were consumed.
29 CachedTokens.clear();
30 CachedLexPos = 0;
31 return;
32 }
33
34 // We should cache the lexed token.
35
36 EnterCachingLexMode();
37 if (Result.isNot(tok::eof)) {
38 CachedTokens.push_back(Result);
39 ++CachedLexPos;
40 }
41}
42
43void Preprocessor::EnterCachingLexMode() {
44 if (InCachingLexMode())
45 return;
46
47 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
48 CurTokenLexer));
49 CurLexer = 0;
50 CurTokenLexer = 0;
51}
52
53
54const Token &Preprocessor::PeekAhead(unsigned N) {
55 assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
56 ExitCachingLexMode();
57 for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
58 CachedTokens.push_back(Token());
59 Lex(CachedTokens.back());
60 }
61 EnterCachingLexMode();
62 return CachedTokens.back();
63}