Index: include/clang/Lex/Preprocessor.h =================================================================== --- include/clang/Lex/Preprocessor.h +++ include/clang/Lex/Preprocessor.h @@ -1185,6 +1185,17 @@ return CachedTokens[CachedLexPos-1].getLastLoc(); } + /// \brief Whether \p Tok is the most recent token (`CachedLexPos - 1`) in + /// CachedTokens. + bool IsPreviousCachedToken(const Token &Tok) const; + + /// \brief Replace token in `CachedLexPos - 1` in CachedTokens by the tokens + /// in \p NewToks. + /// + /// Useful when a token needs to be split in smaller ones and CachedTokens + /// most recent token must to be updated to reflect that. + void ReplacePreviousCachedToken(SmallVectorImpl &NewToks); + /// \brief Replace the last token with an annotation token. /// /// Like AnnotateCachedTokens(), this routine replaces an Index: lib/Lex/PPCaching.cpp =================================================================== --- lib/Lex/PPCaching.cpp +++ lib/Lex/PPCaching.cpp @@ -116,3 +116,27 @@ } } } + +bool Preprocessor::IsPreviousCachedToken(const Token &Tok) const { + assert(CachedLexPos != 0 && "Expected to have some cached tokens"); + const Token LastCachedTok = CachedTokens[CachedLexPos - 1]; + + if (LastCachedTok.getKind() != Tok.getKind()) + return false; + + int RelOffset = 0; + if ((!getSourceManager().isInSameSLocAddrSpace( + Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) || + RelOffset) + return false; + + return true; +} + +void Preprocessor::ReplacePreviousCachedToken(SmallVectorImpl &NewToks) { + assert(CachedLexPos != 0 && "Expected to have some cached tokens"); + CachedTokens.insert(CachedTokens.begin() + CachedLexPos - 1, NewToks.begin(), + NewToks.end()); + CachedTokens.erase(CachedTokens.begin() + CachedLexPos - 1 + NewToks.size()); + CachedLexPos += NewToks.size() - 1; +} Index: lib/Parse/ParseTemplate.cpp =================================================================== --- lib/Parse/ParseTemplate.cpp +++ lib/Parse/ParseTemplate.cpp @@ -827,6 +827,7 @@ } // Strip the initial '>' from the token. + Token PrevTok = Tok; if (RemainingToken == tok::equal && Next.is(tok::equal) && areTokensAdjacent(Tok, Next)) { // Join two adjacent '=' tokens into one, for cases like: @@ -843,6 +844,17 @@ PP.getSourceManager(), getLangOpts())); + // The advance from '>>' to '>' in a ObjectiveC template argument list needs + // to be properly reflected in the token cache to allow correct interaction + // between annotation and backtracking. + if (ObjCGenericList && PrevTok.getKind() == tok::greatergreater && + RemainingToken == tok::greater && PP.IsPreviousCachedToken(PrevTok)) { + PrevTok.setKind(RemainingToken); + PrevTok.setLength(1); + SmallVector NewToks = {PrevTok, Tok}; + PP.ReplacePreviousCachedToken(NewToks); + } + if (!ConsumeLastToken) { // Since we're not supposed to consume the '>' token, we need to push // this token and revert the current token back to the '>'. Index: test/Parser/objcxx11-protocol-in-template.mm =================================================================== --- test/Parser/objcxx11-protocol-in-template.mm +++ test/Parser/objcxx11-protocol-in-template.mm @@ -8,3 +8,11 @@ vector> v; vector>> v2; + +@protocol PA; +@protocol PB; + +@class NSArray; +typedef int some_t; + +id FA(NSArray> *h, some_t group);