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!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201965 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alp Toker 2014-02-23 03:45:03 +00:00
parent f1026e6ca9
commit b5595cd0e4
2 changed files with 18 additions and 10 deletions

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;
}

6
test/Parser/diag-crash.c Normal file
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}}