Path: blob/main/contrib/llvm-project/clang/lib/Lex/PPCaching.cpp
35233 views
//===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements pieces of the Preprocessor interface that manage the9// caching of lexed tokens.10//11//===----------------------------------------------------------------------===//1213#include "clang/Lex/Preprocessor.h"14using namespace clang;1516// EnableBacktrackAtThisPos - From the point that this method is called, and17// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor18// keeps track of the lexed tokens so that a subsequent Backtrack() call will19// make the Preprocessor re-lex the same tokens.20//21// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can22// be called multiple times and CommitBacktrackedTokens/Backtrack calls will23// be combined with the EnableBacktrackAtThisPos calls in reverse order.24void Preprocessor::EnableBacktrackAtThisPos() {25assert(LexLevel == 0 && "cannot use lookahead while lexing");26BacktrackPositions.push_back(CachedLexPos);27EnterCachingLexMode();28}2930// Disable the last EnableBacktrackAtThisPos call.31void Preprocessor::CommitBacktrackedTokens() {32assert(!BacktrackPositions.empty()33&& "EnableBacktrackAtThisPos was not called!");34BacktrackPositions.pop_back();35}3637// Make Preprocessor re-lex the tokens that were lexed since38// EnableBacktrackAtThisPos() was previously called.39void Preprocessor::Backtrack() {40assert(!BacktrackPositions.empty()41&& "EnableBacktrackAtThisPos was not called!");42CachedLexPos = BacktrackPositions.back();43BacktrackPositions.pop_back();44recomputeCurLexerKind();45}4647void Preprocessor::CachingLex(Token &Result) {48if (!InCachingLexMode())49return;5051// The assert in EnterCachingLexMode should prevent this from happening.52assert(LexLevel == 1 &&53"should not use token caching within the preprocessor");5455if (CachedLexPos < CachedTokens.size()) {56Result = CachedTokens[CachedLexPos++];57Result.setFlag(Token::IsReinjected);58return;59}6061ExitCachingLexMode();62Lex(Result);6364if (isBacktrackEnabled()) {65// Cache the lexed token.66EnterCachingLexModeUnchecked();67CachedTokens.push_back(Result);68++CachedLexPos;69return;70}7172if (CachedLexPos < CachedTokens.size()) {73EnterCachingLexModeUnchecked();74} else {75// All cached tokens were consumed.76CachedTokens.clear();77CachedLexPos = 0;78}79}8081void Preprocessor::EnterCachingLexMode() {82// The caching layer sits on top of all the other lexers, so it's incorrect83// to cache tokens while inside a nested lex action. The cached tokens would84// be retained after returning to the enclosing lex action and, at best,85// would appear at the wrong position in the token stream.86assert(LexLevel == 0 &&87"entered caching lex mode while lexing something else");8889if (InCachingLexMode()) {90assert(CurLexerCallback == CLK_CachingLexer && "Unexpected lexer kind");91return;92}9394EnterCachingLexModeUnchecked();95}9697void Preprocessor::EnterCachingLexModeUnchecked() {98assert(CurLexerCallback != CLK_CachingLexer && "already in caching lex mode");99PushIncludeMacroStack();100CurLexerCallback = CLK_CachingLexer;101}102103104const Token &Preprocessor::PeekAhead(unsigned N) {105assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");106ExitCachingLexMode();107for (size_t C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {108CachedTokens.push_back(Token());109Lex(CachedTokens.back());110}111EnterCachingLexMode();112return CachedTokens.back();113}114115void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {116assert(Tok.isAnnotation() && "Expected annotation token");117assert(CachedLexPos != 0 && "Expected to have some cached tokens");118assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()119&& "The annotation should be until the most recent cached token");120121// Start from the end of the cached tokens list and look for the token122// that is the beginning of the annotation token.123for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {124CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;125if (AnnotBegin->getLocation() == Tok.getLocation()) {126assert((BacktrackPositions.empty() || BacktrackPositions.back() <= i) &&127"The backtrack pos points inside the annotated tokens!");128// Replace the cached tokens with the single annotation token.129if (i < CachedLexPos)130CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);131*AnnotBegin = Tok;132CachedLexPos = i;133return;134}135}136}137138bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const {139// There's currently no cached token...140if (!CachedLexPos)141return false;142143const Token LastCachedTok = CachedTokens[CachedLexPos - 1];144if (LastCachedTok.getKind() != Tok.getKind())145return false;146147SourceLocation::IntTy RelOffset = 0;148if ((!getSourceManager().isInSameSLocAddrSpace(149Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) ||150RelOffset)151return false;152153return true;154}155156void Preprocessor::ReplacePreviousCachedToken(ArrayRef<Token> NewToks) {157assert(CachedLexPos != 0 && "Expected to have some cached tokens");158CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(),159NewToks.end());160CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size());161CachedLexPos += NewToks.size() - 1;162}163164165