xref: /minix3/external/bsd/llvm/dist/clang/lib/Lex/PPCaching.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc // This file implements pieces of the Preprocessor interface that manage the
11*f4a2713aSLionel Sambuc // caching of lexed tokens.
12*f4a2713aSLionel Sambuc //
13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
16*f4a2713aSLionel Sambuc using namespace clang;
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc /// EnableBacktrackAtThisPos - From the point that this method is called, and
19*f4a2713aSLionel Sambuc /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
20*f4a2713aSLionel Sambuc /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
21*f4a2713aSLionel Sambuc /// make the Preprocessor re-lex the same tokens.
22*f4a2713aSLionel Sambuc ///
23*f4a2713aSLionel Sambuc /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
24*f4a2713aSLionel Sambuc /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
25*f4a2713aSLionel Sambuc /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
26*f4a2713aSLionel Sambuc void Preprocessor::EnableBacktrackAtThisPos() {
27*f4a2713aSLionel Sambuc   BacktrackPositions.push_back(CachedLexPos);
28*f4a2713aSLionel Sambuc   EnterCachingLexMode();
29*f4a2713aSLionel Sambuc }
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
32*f4a2713aSLionel Sambuc void Preprocessor::CommitBacktrackedTokens() {
33*f4a2713aSLionel Sambuc   assert(!BacktrackPositions.empty()
34*f4a2713aSLionel Sambuc          && "EnableBacktrackAtThisPos was not called!");
35*f4a2713aSLionel Sambuc   BacktrackPositions.pop_back();
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
39*f4a2713aSLionel Sambuc /// EnableBacktrackAtThisPos() was previously called.
40*f4a2713aSLionel Sambuc void Preprocessor::Backtrack() {
41*f4a2713aSLionel Sambuc   assert(!BacktrackPositions.empty()
42*f4a2713aSLionel Sambuc          && "EnableBacktrackAtThisPos was not called!");
43*f4a2713aSLionel Sambuc   CachedLexPos = BacktrackPositions.back();
44*f4a2713aSLionel Sambuc   BacktrackPositions.pop_back();
45*f4a2713aSLionel Sambuc   recomputeCurLexerKind();
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc void Preprocessor::CachingLex(Token &Result) {
49*f4a2713aSLionel Sambuc   if (!InCachingLexMode())
50*f4a2713aSLionel Sambuc     return;
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   if (CachedLexPos < CachedTokens.size()) {
53*f4a2713aSLionel Sambuc     Result = CachedTokens[CachedLexPos++];
54*f4a2713aSLionel Sambuc     return;
55*f4a2713aSLionel Sambuc   }
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   ExitCachingLexMode();
58*f4a2713aSLionel Sambuc   Lex(Result);
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   if (isBacktrackEnabled()) {
61*f4a2713aSLionel Sambuc     // Cache the lexed token.
62*f4a2713aSLionel Sambuc     EnterCachingLexMode();
63*f4a2713aSLionel Sambuc     CachedTokens.push_back(Result);
64*f4a2713aSLionel Sambuc     ++CachedLexPos;
65*f4a2713aSLionel Sambuc     return;
66*f4a2713aSLionel Sambuc   }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc   if (CachedLexPos < CachedTokens.size()) {
69*f4a2713aSLionel Sambuc     EnterCachingLexMode();
70*f4a2713aSLionel Sambuc   } else {
71*f4a2713aSLionel Sambuc     // All cached tokens were consumed.
72*f4a2713aSLionel Sambuc     CachedTokens.clear();
73*f4a2713aSLionel Sambuc     CachedLexPos = 0;
74*f4a2713aSLionel Sambuc   }
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc void Preprocessor::EnterCachingLexMode() {
78*f4a2713aSLionel Sambuc   if (InCachingLexMode())
79*f4a2713aSLionel Sambuc     return;
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc   PushIncludeMacroStack();
82*f4a2713aSLionel Sambuc   CurLexerKind = CLK_CachingLexer;
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc const Token &Preprocessor::PeekAhead(unsigned N) {
87*f4a2713aSLionel Sambuc   assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
88*f4a2713aSLionel Sambuc   ExitCachingLexMode();
89*f4a2713aSLionel Sambuc   for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
90*f4a2713aSLionel Sambuc     CachedTokens.push_back(Token());
91*f4a2713aSLionel Sambuc     Lex(CachedTokens.back());
92*f4a2713aSLionel Sambuc   }
93*f4a2713aSLionel Sambuc   EnterCachingLexMode();
94*f4a2713aSLionel Sambuc   return CachedTokens.back();
95*f4a2713aSLionel Sambuc }
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
98*f4a2713aSLionel Sambuc   assert(Tok.isAnnotation() && "Expected annotation token");
99*f4a2713aSLionel Sambuc   assert(CachedLexPos != 0 && "Expected to have some cached tokens");
100*f4a2713aSLionel Sambuc   assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
101*f4a2713aSLionel Sambuc          && "The annotation should be until the most recent cached token");
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   // Start from the end of the cached tokens list and look for the token
104*f4a2713aSLionel Sambuc   // that is the beginning of the annotation token.
105*f4a2713aSLionel Sambuc   for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
106*f4a2713aSLionel Sambuc     CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
107*f4a2713aSLionel Sambuc     if (AnnotBegin->getLocation() == Tok.getLocation()) {
108*f4a2713aSLionel Sambuc       assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
109*f4a2713aSLionel Sambuc              "The backtrack pos points inside the annotated tokens!");
110*f4a2713aSLionel Sambuc       // Replace the cached tokens with the single annotation token.
111*f4a2713aSLionel Sambuc       if (i < CachedLexPos)
112*f4a2713aSLionel Sambuc         CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
113*f4a2713aSLionel Sambuc       *AnnotBegin = Tok;
114*f4a2713aSLionel Sambuc       CachedLexPos = i;
115*f4a2713aSLionel Sambuc       return;
116*f4a2713aSLionel Sambuc     }
117*f4a2713aSLionel Sambuc   }
118*f4a2713aSLionel Sambuc }
119