1*7330f729Sjoerg //===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg //
9*7330f729Sjoerg // This file implements pieces of the Preprocessor interface that manage the
10*7330f729Sjoerg // caching of lexed tokens.
11*7330f729Sjoerg //
12*7330f729Sjoerg //===----------------------------------------------------------------------===//
13*7330f729Sjoerg
14*7330f729Sjoerg #include "clang/Lex/Preprocessor.h"
15*7330f729Sjoerg using namespace clang;
16*7330f729Sjoerg
17*7330f729Sjoerg // EnableBacktrackAtThisPos - From the point that this method is called, and
18*7330f729Sjoerg // until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
19*7330f729Sjoerg // keeps track of the lexed tokens so that a subsequent Backtrack() call will
20*7330f729Sjoerg // make the Preprocessor re-lex the same tokens.
21*7330f729Sjoerg //
22*7330f729Sjoerg // Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
23*7330f729Sjoerg // be called multiple times and CommitBacktrackedTokens/Backtrack calls will
24*7330f729Sjoerg // be combined with the EnableBacktrackAtThisPos calls in reverse order.
EnableBacktrackAtThisPos()25*7330f729Sjoerg void Preprocessor::EnableBacktrackAtThisPos() {
26*7330f729Sjoerg assert(LexLevel == 0 && "cannot use lookahead while lexing");
27*7330f729Sjoerg BacktrackPositions.push_back(CachedLexPos);
28*7330f729Sjoerg EnterCachingLexMode();
29*7330f729Sjoerg }
30*7330f729Sjoerg
31*7330f729Sjoerg // Disable the last EnableBacktrackAtThisPos call.
CommitBacktrackedTokens()32*7330f729Sjoerg void Preprocessor::CommitBacktrackedTokens() {
33*7330f729Sjoerg assert(!BacktrackPositions.empty()
34*7330f729Sjoerg && "EnableBacktrackAtThisPos was not called!");
35*7330f729Sjoerg BacktrackPositions.pop_back();
36*7330f729Sjoerg }
37*7330f729Sjoerg
38*7330f729Sjoerg // Make Preprocessor re-lex the tokens that were lexed since
39*7330f729Sjoerg // EnableBacktrackAtThisPos() was previously called.
Backtrack()40*7330f729Sjoerg void Preprocessor::Backtrack() {
41*7330f729Sjoerg assert(!BacktrackPositions.empty()
42*7330f729Sjoerg && "EnableBacktrackAtThisPos was not called!");
43*7330f729Sjoerg CachedLexPos = BacktrackPositions.back();
44*7330f729Sjoerg BacktrackPositions.pop_back();
45*7330f729Sjoerg recomputeCurLexerKind();
46*7330f729Sjoerg }
47*7330f729Sjoerg
CachingLex(Token & Result)48*7330f729Sjoerg void Preprocessor::CachingLex(Token &Result) {
49*7330f729Sjoerg if (!InCachingLexMode())
50*7330f729Sjoerg return;
51*7330f729Sjoerg
52*7330f729Sjoerg // The assert in EnterCachingLexMode should prevent this from happening.
53*7330f729Sjoerg assert(LexLevel == 1 &&
54*7330f729Sjoerg "should not use token caching within the preprocessor");
55*7330f729Sjoerg
56*7330f729Sjoerg if (CachedLexPos < CachedTokens.size()) {
57*7330f729Sjoerg Result = CachedTokens[CachedLexPos++];
58*7330f729Sjoerg Result.setFlag(Token::IsReinjected);
59*7330f729Sjoerg return;
60*7330f729Sjoerg }
61*7330f729Sjoerg
62*7330f729Sjoerg ExitCachingLexMode();
63*7330f729Sjoerg Lex(Result);
64*7330f729Sjoerg
65*7330f729Sjoerg if (isBacktrackEnabled()) {
66*7330f729Sjoerg // Cache the lexed token.
67*7330f729Sjoerg EnterCachingLexModeUnchecked();
68*7330f729Sjoerg CachedTokens.push_back(Result);
69*7330f729Sjoerg ++CachedLexPos;
70*7330f729Sjoerg return;
71*7330f729Sjoerg }
72*7330f729Sjoerg
73*7330f729Sjoerg if (CachedLexPos < CachedTokens.size()) {
74*7330f729Sjoerg EnterCachingLexModeUnchecked();
75*7330f729Sjoerg } else {
76*7330f729Sjoerg // All cached tokens were consumed.
77*7330f729Sjoerg CachedTokens.clear();
78*7330f729Sjoerg CachedLexPos = 0;
79*7330f729Sjoerg }
80*7330f729Sjoerg }
81*7330f729Sjoerg
EnterCachingLexMode()82*7330f729Sjoerg void Preprocessor::EnterCachingLexMode() {
83*7330f729Sjoerg // The caching layer sits on top of all the other lexers, so it's incorrect
84*7330f729Sjoerg // to cache tokens while inside a nested lex action. The cached tokens would
85*7330f729Sjoerg // be retained after returning to the enclosing lex action and, at best,
86*7330f729Sjoerg // would appear at the wrong position in the token stream.
87*7330f729Sjoerg assert(LexLevel == 0 &&
88*7330f729Sjoerg "entered caching lex mode while lexing something else");
89*7330f729Sjoerg
90*7330f729Sjoerg if (InCachingLexMode()) {
91*7330f729Sjoerg assert(CurLexerKind == CLK_CachingLexer && "Unexpected lexer kind");
92*7330f729Sjoerg return;
93*7330f729Sjoerg }
94*7330f729Sjoerg
95*7330f729Sjoerg EnterCachingLexModeUnchecked();
96*7330f729Sjoerg }
97*7330f729Sjoerg
EnterCachingLexModeUnchecked()98*7330f729Sjoerg void Preprocessor::EnterCachingLexModeUnchecked() {
99*7330f729Sjoerg assert(CurLexerKind != CLK_CachingLexer && "already in caching lex mode");
100*7330f729Sjoerg PushIncludeMacroStack();
101*7330f729Sjoerg CurLexerKind = CLK_CachingLexer;
102*7330f729Sjoerg }
103*7330f729Sjoerg
104*7330f729Sjoerg
PeekAhead(unsigned N)105*7330f729Sjoerg const Token &Preprocessor::PeekAhead(unsigned N) {
106*7330f729Sjoerg assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
107*7330f729Sjoerg ExitCachingLexMode();
108*7330f729Sjoerg for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
109*7330f729Sjoerg CachedTokens.push_back(Token());
110*7330f729Sjoerg Lex(CachedTokens.back());
111*7330f729Sjoerg }
112*7330f729Sjoerg EnterCachingLexMode();
113*7330f729Sjoerg return CachedTokens.back();
114*7330f729Sjoerg }
115*7330f729Sjoerg
AnnotatePreviousCachedTokens(const Token & Tok)116*7330f729Sjoerg void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
117*7330f729Sjoerg assert(Tok.isAnnotation() && "Expected annotation token");
118*7330f729Sjoerg assert(CachedLexPos != 0 && "Expected to have some cached tokens");
119*7330f729Sjoerg assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
120*7330f729Sjoerg && "The annotation should be until the most recent cached token");
121*7330f729Sjoerg
122*7330f729Sjoerg // Start from the end of the cached tokens list and look for the token
123*7330f729Sjoerg // that is the beginning of the annotation token.
124*7330f729Sjoerg for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
125*7330f729Sjoerg CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
126*7330f729Sjoerg if (AnnotBegin->getLocation() == Tok.getLocation()) {
127*7330f729Sjoerg assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
128*7330f729Sjoerg "The backtrack pos points inside the annotated tokens!");
129*7330f729Sjoerg // Replace the cached tokens with the single annotation token.
130*7330f729Sjoerg if (i < CachedLexPos)
131*7330f729Sjoerg CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
132*7330f729Sjoerg *AnnotBegin = Tok;
133*7330f729Sjoerg CachedLexPos = i;
134*7330f729Sjoerg return;
135*7330f729Sjoerg }
136*7330f729Sjoerg }
137*7330f729Sjoerg }
138*7330f729Sjoerg
IsPreviousCachedToken(const Token & Tok) const139*7330f729Sjoerg bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
140*7330f729Sjoerg // There's currently no cached token...
141*7330f729Sjoerg if (!CachedLexPos)
142*7330f729Sjoerg return false;
143*7330f729Sjoerg
144*7330f729Sjoerg const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
145*7330f729Sjoerg if (LastCachedTok.getKind() != Tok.getKind())
146*7330f729Sjoerg return false;
147*7330f729Sjoerg
148*7330f729Sjoerg int RelOffset = 0;
149*7330f729Sjoerg if ((!getSourceManager().isInSameSLocAddrSpace(
150*7330f729Sjoerg Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
151*7330f729Sjoerg RelOffset)
152*7330f729Sjoerg return false;
153*7330f729Sjoerg
154*7330f729Sjoerg return true;
155*7330f729Sjoerg }
156*7330f729Sjoerg
ReplacePreviousCachedToken(ArrayRef<Token> NewToks)157*7330f729Sjoerg void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
158*7330f729Sjoerg assert(CachedLexPos != 0 && "Expected to have some cached tokens");
159*7330f729Sjoerg CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
160*7330f729Sjoerg NewToks.end());
161*7330f729Sjoerg CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
162*7330f729Sjoerg CachedLexPos += NewToks.size() - 1;
163*7330f729Sjoerg }
164