Index: include/clang/Lex/Preprocessor.h =================================================================== --- include/clang/Lex/Preprocessor.h +++ include/clang/Lex/Preprocessor.h @@ -630,6 +630,13 @@ /// invoked (at which point the last position is popped). std::vector BacktrackPositions; + struct CachedTokensRange { + CachedTokensTy::size_type Begin, End; + }; + /// \brief A range of cached tokens that should be erased after lexing + /// when backtracking requires the erasure of such cached tokens. + Optional ErasedBacktrackRange; + struct MacroInfoChain { MacroInfo MI; MacroInfoChain *Next; Index: lib/Lex/PPCaching.cpp =================================================================== --- lib/Lex/PPCaching.cpp +++ lib/Lex/PPCaching.cpp @@ -32,7 +32,20 @@ void Preprocessor::CommitBacktrackedTokens() { assert(!BacktrackPositions.empty() && "EnableBacktrackAtThisPos was not called!"); + auto PrevCachedLexPos = BacktrackPositions.back(); BacktrackPositions.pop_back(); + // When committing in a macro argument pre-expansion when tokens are still + // being cached, we want to ensure that the tokens which were just committed + // won't remain in the cache and that the caching stops. + // Otherwise the caching will interfere with the way macro expansion works, + // because we will continue to cache tokens after consuming the backtracked + // tokens, which shouldn't happen when we're dealing with macro argument + // pre-expansion. + if (InMacroArgPreExpansion && !BacktrackPositions.empty()) { + CachedTokens.erase(CachedTokens.begin() + PrevCachedLexPos, + CachedTokens.begin() + CachedLexPos); + ExitCachingLexMode(); + } } // Make Preprocessor re-lex the tokens that were lexed since @@ -40,9 +53,20 @@ void Preprocessor::Backtrack() { assert(!BacktrackPositions.empty() && "EnableBacktrackAtThisPos was not called!"); + auto CurrentCachedLexPos = CachedLexPos; CachedLexPos = BacktrackPositions.back(); BacktrackPositions.pop_back(); recomputeCurLexerKind(); + // When backtracking in a macro argument pre-expansion when tokens are still + // being cached, we want to ensure that the tokens over which we just + // backtracked won't remain in the cache after they're consumed and and that + // the caching will stop after consuming them. + // Otherwise they will interfere with the way macro expansion works, because + // we will continue to cache tokens after consuming the backtracked tokens, + // which shouldn't happen when we're dealing with macro argument + // pre-expansion. + if (InMacroArgPreExpansion && !BacktrackPositions.empty()) + ErasedBacktrackRange = CachedTokensRange{CachedLexPos, CurrentCachedLexPos}; } void Preprocessor::CachingLex(Token &Result) { @@ -51,6 +75,15 @@ if (CachedLexPos < CachedTokens.size()) { Result = CachedTokens[CachedLexPos++]; + // Erase the cached token range after the whole range is consumed if + // necessary. + if (ErasedBacktrackRange && ErasedBacktrackRange->End == CachedLexPos) { + CachedTokens.erase(CachedTokens.begin() + ErasedBacktrackRange->Begin, + CachedTokens.begin() + CachedLexPos); + CachedLexPos = ErasedBacktrackRange->Begin; + ErasedBacktrackRange = None; + ExitCachingLexMode(); + } return; } Index: test/CodeCompletion/pragma-macro-token-caching.c =================================================================== --- /dev/null +++ test/CodeCompletion/pragma-macro-token-caching.c @@ -0,0 +1,18 @@ + +#define Outer(action) action + +void completeParam(int param) { + ; + Outer(__extension__({ _Pragma("clang diagnostic push") })); + param; +} + +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:7:1 %s | FileCheck %s +// CHECK: param : [#int#]param + +void completeParamPragmaError(int param) { + Outer(__extension__({ _Pragma(2) })); // expected-error {{_Pragma takes a parenthesized string literal}} + param; +} + +// RUN: %clang_cc1 -fsyntax-only -verify -code-completion-at=%s:16:1 %s | FileCheck %s