blob: 363a185d3d2e2f87444cb6a71d7f8fec37383144 [file] [log] [blame]
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +00001//===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements pieces of the Preprocessor interface that manage the
10// caching of lexed tokens.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/Preprocessor.h"
15using namespace clang;
16
James Dennett4a4f72d2013-11-27 01:27:40 +000017// EnableBacktrackAtThisPos - From the point that this method is called, and
18// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
19// keeps track of the lexed tokens so that a subsequent Backtrack() call will
20// make the Preprocessor re-lex the same tokens.
21//
22// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
23// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
24// be combined with the EnableBacktrackAtThisPos calls in reverse order.
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000025void Preprocessor::EnableBacktrackAtThisPos() {
Richard Smith75f96812019-04-11 21:18:22 +000026 assert(LexLevel == 0 && "cannot use lookahead while lexing");
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000027 BacktrackPositions.push_back(CachedLexPos);
28 EnterCachingLexMode();
29}
Argyrios Kyrtzidisa65490c2008-08-22 21:27:50 +000030
James Dennett4a4f72d2013-11-27 01:27:40 +000031// Disable the last EnableBacktrackAtThisPos call.
Argyrios Kyrtzidis75b34532008-08-24 12:29:43 +000032void Preprocessor::CommitBacktrackedTokens() {
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000033 assert(!BacktrackPositions.empty()
34 && "EnableBacktrackAtThisPos was not called!");
35 BacktrackPositions.pop_back();
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000036}
37
James Dennett4a4f72d2013-11-27 01:27:40 +000038// Make Preprocessor re-lex the tokens that were lexed since
39// EnableBacktrackAtThisPos() was previously called.
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000040void Preprocessor::Backtrack() {
41 assert(!BacktrackPositions.empty()
42 && "EnableBacktrackAtThisPos was not called!");
43 CachedLexPos = BacktrackPositions.back();
44 BacktrackPositions.pop_back();
Douglas Gregor8d76cca2012-01-04 06:20:15 +000045 recomputeCurLexerKind();
Argyrios Kyrtzidisbd024c72008-08-23 12:05:53 +000046}
Argyrios Kyrtzidisa65490c2008-08-22 21:27:50 +000047
Richard Smith8af8b862019-04-11 21:18:23 +000048void Preprocessor::CachingLex(Token &Result, bool &IsNewToken) {
Argyrios Kyrtzidisc2924de2010-07-12 18:49:30 +000049 if (!InCachingLexMode())
50 return;
51
Richard Smith75f96812019-04-11 21:18:22 +000052 // The assert in EnterCachingLexMode should prevent this from happening.
53 assert(LexLevel == 1 &&
54 "should not use token caching within the preprocessor");
55
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000056 if (CachedLexPos < CachedTokens.size()) {
57 Result = CachedTokens[CachedLexPos++];
Richard Smith8af8b862019-04-11 21:18:23 +000058 IsNewToken = false;
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000059 return;
60 }
61
62 ExitCachingLexMode();
Argyrios Kyrtzidisd1d239f2010-07-12 21:41:41 +000063 Lex(Result);
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000064
Argyrios Kyrtzidis4a1049c2012-04-04 02:57:01 +000065 if (isBacktrackEnabled()) {
66 // Cache the lexed token.
Richard Smith75f96812019-04-11 21:18:22 +000067 EnterCachingLexModeUnchecked();
Argyrios Kyrtzidis4a1049c2012-04-04 02:57:01 +000068 CachedTokens.push_back(Result);
69 ++CachedLexPos;
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000070 return;
71 }
72
Argyrios Kyrtzidis4a1049c2012-04-04 02:57:01 +000073 if (CachedLexPos < CachedTokens.size()) {
Richard Smith75f96812019-04-11 21:18:22 +000074 EnterCachingLexModeUnchecked();
Argyrios Kyrtzidis4a1049c2012-04-04 02:57:01 +000075 } else {
76 // All cached tokens were consumed.
77 CachedTokens.clear();
78 CachedLexPos = 0;
79 }
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000080}
81
82void Preprocessor::EnterCachingLexMode() {
Richard Smith75f96812019-04-11 21:18:22 +000083 // The caching layer sits on top of all the other lexers, so it's incorrect
84 // to cache tokens while inside a nested lex action. The cached tokens would
85 // be retained after returning to the enclosing lex action and, at best,
86 // would appear at the wrong position in the token stream.
87 assert(LexLevel == 0 &&
88 "entered caching lex mode while lexing something else");
89
Volodymyr Sapsai9d540f12018-01-19 23:41:47 +000090 if (InCachingLexMode()) {
91 assert(CurLexerKind == CLK_CachingLexer && "Unexpected lexer kind");
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000092 return;
Volodymyr Sapsai9d540f12018-01-19 23:41:47 +000093 }
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +000094
Richard Smith75f96812019-04-11 21:18:22 +000095 EnterCachingLexModeUnchecked();
96}
97
98void Preprocessor::EnterCachingLexModeUnchecked() {
99 assert(CurLexerKind != CLK_CachingLexer && "already in caching lex mode");
Ted Kremenek50b4f482008-11-12 22:21:57 +0000100 PushIncludeMacroStack();
Douglas Gregor8d76cca2012-01-04 06:20:15 +0000101 CurLexerKind = CLK_CachingLexer;
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +0000102}
103
104
105const Token &Preprocessor::PeekAhead(unsigned N) {
106 assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
107 ExitCachingLexMode();
Erik Verbruggene4fd6522016-10-26 13:06:13 +0000108 for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
Argyrios Kyrtzidisb3dd1e02008-08-10 13:15:22 +0000109 CachedTokens.push_back(Token());
110 Lex(CachedTokens.back());
111 }
112 EnterCachingLexMode();
113 return CachedTokens.back();
114}
Argyrios Kyrtzidisc7e67a04c2008-11-08 16:17:04 +0000115
116void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
Chris Lattner5a7971e2009-01-26 19:29:26 +0000117 assert(Tok.isAnnotation() && "Expected annotation token");
Argyrios Kyrtzidisc7e67a04c2008-11-08 16:17:04 +0000118 assert(CachedLexPos != 0 && "Expected to have some cached tokens");
Sebastian Redlb0e3e1b2010-02-08 19:35:18 +0000119 assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
Argyrios Kyrtzidisc7e67a04c2008-11-08 16:17:04 +0000120 && "The annotation should be until the most recent cached token");
121
122 // Start from the end of the cached tokens list and look for the token
123 // that is the beginning of the annotation token.
124 for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
125 CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
126 if (AnnotBegin->getLocation() == Tok.getLocation()) {
Reid Klecknerae818502016-10-20 20:53:20 +0000127 assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&
Argyrios Kyrtzidisc7e67a04c2008-11-08 16:17:04 +0000128 "The backtrack pos points inside the annotated tokens!");
129 // Replace the cached tokens with the single annotation token.
Nuno Lopesbd2cd922009-07-26 16:36:45 +0000130 if (i < CachedLexPos)
131 CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
Argyrios Kyrtzidisc7e67a04c2008-11-08 16:17:04 +0000132 *AnnotBegin = Tok;
133 CachedLexPos = i;
134 return;
135 }
Chandler Carruthc4399b12013-11-27 01:40:12 +0000136 }
137}
Bruno Cardoso Lopes428a5aa2016-01-31 00:47:51 +0000138
139bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {
140 // There's currently no cached token...
141 if (!CachedLexPos)
142 return false;
143
144 const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
145 if (LastCachedTok.getKind() != Tok.getKind())
146 return false;
147
148 int RelOffset = 0;
149 if ((!getSourceManager().isInSameSLocAddrSpace(
150 Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||
151 RelOffset)
152 return false;
153
154 return true;
155}
156
157void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {
158 assert(CachedLexPos != 0 && "Expected to have some cached tokens");
159 CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
160 NewToks.end());
161 CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
162 CachedLexPos += NewToks.size() - 1;
163}