17std::pair<Preprocessor::CachedTokensTy::size_type, bool>
18Preprocessor::LastBacktrackPos() {
20 auto BacktrackPos = BacktrackPositions.back();
22 static_cast<CachedTokensTy::difference_type
>(BacktrackPos) < 0;
23 return {Unannotated ? ~BacktrackPos : BacktrackPos, Unannotated};
35 assert(LexLevel == 0 &&
"cannot use lookahead while lexing");
36 BacktrackPositions.push_back(Unannotated ? ~CachedLexPos : CachedLexPos);
38 UnannotatedBacktrackTokens.emplace_back(CachedTokens, CachedTokens.size());
39 EnterCachingLexMode();
42Preprocessor::CachedTokensTy Preprocessor::PopUnannotatedBacktrackTokens() {
44 auto [UnannotatedTokens, NumCachedToks] =
45 std::move(UnannotatedBacktrackTokens.back());
46 UnannotatedBacktrackTokens.pop_back();
50 UnannotatedBacktrackTokens.back().first.append(
51 UnannotatedTokens.begin() + NumCachedToks, UnannotatedTokens.end());
52 return std::move(UnannotatedTokens);
58 auto [BacktrackPos, Unannotated] = LastBacktrackPos();
59 BacktrackPositions.pop_back();
61 PopUnannotatedBacktrackTokens();
68 auto [BacktrackPos, Unannotated] = LastBacktrackPos();
69 BacktrackPositions.pop_back();
70 CachedLexPos = BacktrackPos;
72 CachedTokens = PopUnannotatedBacktrackTokens();
77 if (!InCachingLexMode())
81 assert(LexLevel == 1 &&
82 "should not use token caching within the preprocessor");
95 EnterCachingLexModeUnchecked();
99 UnannotatedBacktrackTokens.back().first.push_back(
Result);
103 if (CachedLexPos < CachedTokens.size()) {
104 EnterCachingLexModeUnchecked();
107 CachedTokens.clear();
112void Preprocessor::EnterCachingLexMode() {
117 assert(LexLevel == 0 &&
118 "entered caching lex mode while lexing something else");
120 if (InCachingLexMode())
123 EnterCachingLexModeUnchecked();
126void Preprocessor::EnterCachingLexModeUnchecked() {
127 assert(!InCachingLexMode() &&
"already in caching lex mode");
128 PushIncludeMacroStack();
129 CurLexerCallback = CLK_CachingLexer;
133const Token &Preprocessor::PeekAhead(
unsigned N) {
134 assert(CachedLexPos + N > CachedTokens.size() &&
"Confused caching.");
135 ExitCachingLexMode();
136 for (
size_t C = CachedLexPos + N - CachedTokens.size();
C > 0; --
C) {
137 CachedTokens.push_back(Token());
138 Lex(CachedTokens.back());
140 UnannotatedBacktrackTokens.back().first.push_back(CachedTokens.back());
142 EnterCachingLexMode();
143 return CachedTokens.back();
146void Preprocessor::AnnotatePreviousCachedTokens(
const Token &
Tok) {
148 assert(CachedLexPos != 0 &&
"Expected to have some cached tokens");
150 &&
"The annotation should be until the most recent cached token");
154 for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
155 CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
158 "The backtrack pos points inside the annotated tokens!");
160 if (i < CachedLexPos)
161 CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
174 const Token LastCachedTok = CachedTokens[CachedLexPos - 1];
188 assert(CachedLexPos != 0 &&
"Expected to have some cached tokens");
189 CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),
191 CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());
192 CachedLexPos += NewToks.size() - 1;
Result
Implement __builtin_bit_cast and related operations.
Defines the clang::Preprocessor interface.
bool IsPreviousCachedToken(const Token &Tok) const
Whether Tok is the most recent token (CachedLexPos - 1) in CachedTokens.
void CommitBacktrackedTokens()
Disable the last EnableBacktrackAtThisPos call.
void Lex(Token &Result)
Lex the next token for this preprocessor.
void Backtrack()
Make Preprocessor re-lex the tokens that were lexed since EnableBacktrackAtThisPos() was previously c...
SourceManager & getSourceManager() const
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
bool isUnannotatedBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of unannotated tokens is on.
void recomputeCurLexerKind()
Recompute the current lexer kind based on the CurLexer/ CurTokenLexer pointers.
void ReplacePreviousCachedToken(ArrayRef< Token > NewToks)
Replace token in CachedLexPos - 1 in CachedTokens by the tokens in NewToks.
void EnableBacktrackAtThisPos(bool Unannotated=false)
From the point that this method is called, and until CommitBacktrackedTokens() or Backtrack() is call...
SourceLocation getLastCachedTokenLocation() const
Get the location of the last cached token, suitable for setting the end location of an annotation tok...
Token - This structure provides full information about a lexed token.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
SourceLocation getAnnotationEndLoc() const
tok::TokenKind getKind() const
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
SmallVector< Token, 4 > CachedTokens
A set of tokens that has been cached for later parsing.