annotate clang/lib/Lex/PPCaching.cpp @ 176:de4ac79aef9d

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