diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1045,8 +1045,16 @@ SourceLocation StartLoc, SourceLocation EndLoc) { // make sure we have a token we can turn into an annotation token - if (PP.isBacktrackEnabled()) + if (PP.isBacktrackEnabled()) { PP.RevertCachedTokens(1); + if (DS.getTypeSpecType() == TST_error) { + // We encountered an error in parsing 'decltype(...)' so lets annotate all + // the tokens in the backtracking cache - that we likely had to skip over + // to get to a token that allows us to resume parsing, such as a + // semi-colon. + EndLoc = PP.getLastCachedTokenLocation(); + } + } else PP.EnterToken(Tok, /*IsReinject*/true); diff --git a/clang/test/Parser/PR42049.cpp b/clang/test/Parser/PR42049.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Parser/PR42049.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// https://bugs.llvm.org/show_bug.cgi?id=42049 + +void f() { + g(); // expected-error 2 {{expected}} expected-note {{to match}} + g2 void f2() { + g(); // expected-error 2 {{expected}} expected-note {{to match}} + g2