diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp b/contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp index 45bdce32062a..f5e8cdc25d38 100644 --- a/contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp +++ b/contrib/llvm/tools/clang/lib/Lex/PPCaching.cpp @@ -35,6 +35,29 @@ void Preprocessor::CommitBacktrackedTokens() { BacktrackPositions.pop_back(); } +Preprocessor::CachedTokensRange Preprocessor::LastCachedTokenRange() { + assert(isBacktrackEnabled()); + auto PrevCachedLexPos = BacktrackPositions.back(); + return CachedTokensRange{PrevCachedLexPos, CachedLexPos}; +} + +void Preprocessor::EraseCachedTokens(CachedTokensRange TokenRange) { + assert(TokenRange.Begin <= TokenRange.End); + if (CachedLexPos == TokenRange.Begin && TokenRange.Begin != TokenRange.End) { + // We have backtracked to the start of the token range as we want to consume + // them again. Erase the tokens only after consuming then. + assert(!CachedTokenRangeToErase); + CachedTokenRangeToErase = TokenRange; + return; + } + // The cached tokens were committed, so they should be erased now. + assert(TokenRange.End == CachedLexPos); + CachedTokens.erase(CachedTokens.begin() + TokenRange.Begin, + CachedTokens.begin() + TokenRange.End); + CachedLexPos = TokenRange.Begin; + ExitCachingLexMode(); +} + // Make Preprocessor re-lex the tokens that were lexed since // EnableBacktrackAtThisPos() was previously called. void Preprocessor::Backtrack() { @@ -51,6 +74,13 @@ void Preprocessor::CachingLex(Token &Result) { if (CachedLexPos < CachedTokens.size()) { Result = CachedTokens[CachedLexPos++]; + // Erase the some of the cached tokens after they are consumed when + // asked to do so. + if (CachedTokenRangeToErase && + CachedTokenRangeToErase->End == CachedLexPos) { + EraseCachedTokens(*CachedTokenRangeToErase); + CachedTokenRangeToErase = None; + } return; } |