2009-06-02 17:58:47 +00:00
|
|
|
//===--- PPCaching.cpp - Handle caching lexed tokens ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements pieces of the Preprocessor interface that manage the
|
|
|
|
// caching of lexed tokens.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
/// EnableBacktrackAtThisPos - From the point that this method is called, and
|
|
|
|
/// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
|
|
|
|
/// keeps track of the lexed tokens so that a subsequent Backtrack() call will
|
|
|
|
/// make the Preprocessor re-lex the same tokens.
|
|
|
|
///
|
|
|
|
/// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
|
|
|
|
/// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
|
|
|
|
/// be combined with the EnableBacktrackAtThisPos calls in reverse order.
|
|
|
|
void Preprocessor::EnableBacktrackAtThisPos() {
|
|
|
|
BacktrackPositions.push_back(CachedLexPos);
|
|
|
|
EnterCachingLexMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
|
|
|
|
void Preprocessor::CommitBacktrackedTokens() {
|
|
|
|
assert(!BacktrackPositions.empty()
|
|
|
|
&& "EnableBacktrackAtThisPos was not called!");
|
|
|
|
BacktrackPositions.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
|
2009-10-14 18:03:49 +00:00
|
|
|
/// EnableBacktrackAtThisPos() was previously called.
|
2009-06-02 17:58:47 +00:00
|
|
|
void Preprocessor::Backtrack() {
|
|
|
|
assert(!BacktrackPositions.empty()
|
|
|
|
&& "EnableBacktrackAtThisPos was not called!");
|
|
|
|
CachedLexPos = BacktrackPositions.back();
|
|
|
|
BacktrackPositions.pop_back();
|
2012-04-14 14:01:31 +00:00
|
|
|
recomputeCurLexerKind();
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::CachingLex(Token &Result) {
|
2010-07-13 17:21:42 +00:00
|
|
|
if (!InCachingLexMode())
|
|
|
|
return;
|
|
|
|
|
2009-06-02 17:58:47 +00:00
|
|
|
if (CachedLexPos < CachedTokens.size()) {
|
|
|
|
Result = CachedTokens[CachedLexPos++];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitCachingLexMode();
|
|
|
|
Lex(Result);
|
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
if (isBacktrackEnabled()) {
|
|
|
|
// Cache the lexed token.
|
|
|
|
EnterCachingLexMode();
|
|
|
|
CachedTokens.push_back(Result);
|
|
|
|
++CachedLexPos;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CachedLexPos < CachedTokens.size()) {
|
|
|
|
EnterCachingLexMode();
|
|
|
|
} else {
|
2009-06-02 17:58:47 +00:00
|
|
|
// All cached tokens were consumed.
|
|
|
|
CachedTokens.clear();
|
|
|
|
CachedLexPos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::EnterCachingLexMode() {
|
|
|
|
if (InCachingLexMode())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PushIncludeMacroStack();
|
2012-04-14 14:01:31 +00:00
|
|
|
CurLexerKind = CLK_CachingLexer;
|
2009-06-02 17:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Token &Preprocessor::PeekAhead(unsigned N) {
|
|
|
|
assert(CachedLexPos + N > CachedTokens.size() && "Confused caching.");
|
|
|
|
ExitCachingLexMode();
|
|
|
|
for (unsigned C = CachedLexPos + N - CachedTokens.size(); C > 0; --C) {
|
|
|
|
CachedTokens.push_back(Token());
|
|
|
|
Lex(CachedTokens.back());
|
|
|
|
}
|
|
|
|
EnterCachingLexMode();
|
|
|
|
return CachedTokens.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::AnnotatePreviousCachedTokens(const Token &Tok) {
|
|
|
|
assert(Tok.isAnnotation() && "Expected annotation token");
|
|
|
|
assert(CachedLexPos != 0 && "Expected to have some cached tokens");
|
2010-02-16 09:31:36 +00:00
|
|
|
assert(CachedTokens[CachedLexPos-1].getLastLoc() == Tok.getAnnotationEndLoc()
|
2009-06-02 17:58:47 +00:00
|
|
|
&& "The annotation should be until the most recent cached token");
|
|
|
|
|
|
|
|
// Start from the end of the cached tokens list and look for the token
|
|
|
|
// that is the beginning of the annotation token.
|
|
|
|
for (CachedTokensTy::size_type i = CachedLexPos; i != 0; --i) {
|
|
|
|
CachedTokensTy::iterator AnnotBegin = CachedTokens.begin() + i-1;
|
|
|
|
if (AnnotBegin->getLocation() == Tok.getLocation()) {
|
|
|
|
assert((BacktrackPositions.empty() || BacktrackPositions.back() < i) &&
|
|
|
|
"The backtrack pos points inside the annotated tokens!");
|
|
|
|
// Replace the cached tokens with the single annotation token.
|
2009-10-14 18:03:49 +00:00
|
|
|
if (i < CachedLexPos)
|
|
|
|
CachedTokens.erase(AnnotBegin + 1, CachedTokens.begin() + CachedLexPos);
|
2009-06-02 17:58:47 +00:00
|
|
|
*AnnotBegin = Tok;
|
|
|
|
CachedLexPos = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|