diff options
| author | Chris Lattner <sabre@nondot.org> | 2010-05-24 22:31:37 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2010-05-24 22:31:37 +0000 |
| commit | fb58515bc3a1c521dc7ac71bf7304e8097501ec6 (patch) | |
| tree | 1c0d1dd067e10f7264c4eca2ecbb31b490d52b9d /clang | |
| parent | 76b3de92f2a325304f0f652751ecf9f4adf715bf (diff) | |
| download | bcm5719-llvm-fb58515bc3a1c521dc7ac71bf7304e8097501ec6.tar.gz bcm5719-llvm-fb58515bc3a1c521dc7ac71bf7304e8097501ec6.zip | |
improve the fixit for the missing : error when parsing ?:. When
there are already two spaces before the token where the : was expected,
put the : in between the spaces. This means we get it right in both
of these cases:
t.c:2:17: error: expected ':'
return a ? b c;
^
:
t.c:3:16: error: expected ':'
return a ? b c;
^
:
In the later case, the diagnostic says to insert ": ", in the former
case it says to insert ":" between the spaces. This fixes rdar://8007231
llvm-svn: 104569
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/Parse/ParseExpr.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index b9e632a1846..ed27f3bccb0 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -315,8 +315,29 @@ Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, prec::Level MinPrec) { // Eat the colon. ColonLoc = ConsumeToken(); } else { + // Otherwise, we're missing a ':'. Assume that this was a typo that the + // user forgot. If we're not in a macro instantion, we can suggest a + // fixit hint. If there were two spaces before the current token, + // suggest inserting the colon in between them, otherwise insert ": ". + SourceLocation FILoc = Tok.getLocation(); + const char *FIText = ": "; + if (FILoc.isFileID()) { + const SourceManager &SM = PP.getSourceManager(); + bool IsInvalid = false; + const char *SourcePtr = + SM.getCharacterData(FILoc.getFileLocWithOffset(-1), &IsInvalid); + if (!IsInvalid && *SourcePtr == ' ') { + SourcePtr = + SM.getCharacterData(FILoc.getFileLocWithOffset(-2), &IsInvalid); + if (!IsInvalid && *SourcePtr == ' ') { + FILoc = FILoc.getFileLocWithOffset(-1); + FIText = ":"; + } + } + } + Diag(Tok, diag::err_expected_colon) - << FixItHint::CreateInsertion(Tok.getLocation(), ": "); + << FixItHint::CreateInsertion(FILoc, FIText); Diag(OpToken, diag::note_matching) << "?"; ColonLoc = Tok.getLocation(); } |

