Fix crash when both ExpectAndConsume and ConsumeAnyToken emit diagnostics

The DiagnosticBuilder's lifetime in parser typo recovery was overlapping with
the subsequent consume which can itself emit PP diagnostics.

Patch by Olivier Goffart!

llvm-svn: 201965
This commit is contained in:
Alp Toker 2014-02-23 03:45:03 +00:00
parent 3aa840e685
commit f2b6e79393
2 changed files with 18 additions and 10 deletions
clang
lib/Parse
test/Parser

View File

@ -118,18 +118,20 @@ bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
// Detect common single-character typos and resume.
if (IsCommonTypo(ExpectedTok, Tok)) {
SourceLocation Loc = Tok.getLocation();
DiagnosticBuilder DB = Diag(Loc, DiagID);
DB << FixItHint::CreateReplacement(SourceRange(Loc),
getPunctuatorSpelling(ExpectedTok));
if (DiagID == diag::err_expected)
DB << ExpectedTok;
else if (DiagID == diag::err_expected_after)
DB << Msg << ExpectedTok;
else
DB << Msg;
ConsumeAnyToken();
{
DiagnosticBuilder DB = Diag(Loc, DiagID);
DB << FixItHint::CreateReplacement(
SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
if (DiagID == diag::err_expected)
DB << ExpectedTok;
else if (DiagID == diag::err_expected_after)
DB << Msg << ExpectedTok;
else
DB << Msg;
}
// Pretend there wasn't a problem.
ConsumeAnyToken();
return false;
}

View File

@ -0,0 +1,6 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// Avoid preprocessor diag crash caused by a parser diag left in flight.
int foo: // expected-error {{expected ';' after top level declarator}}
#endif // expected-error {{#endif without #if}}