diff options
author | Richard Trieu <rtrieu@google.com> | 2012-01-18 22:54:52 +0000 |
---|---|---|
committer | Richard Trieu <rtrieu@google.com> | 2012-01-18 22:54:52 +0000 |
commit | c64d3230d2f37014b9419e1555c9bca22c45634d (patch) | |
tree | 46e53e5fd0c83049cf3a7327dd2a0285d7916e56 /clang | |
parent | 94298a906a2fcab80dee68f63e0657c9008dd27a (diff) | |
download | bcm5719-llvm-c64d3230d2f37014b9419e1555c9bca22c45634d.tar.gz bcm5719-llvm-c64d3230d2f37014b9419e1555c9bca22c45634d.zip |
Change the error when a '+=' follows a declaration to suggest a fixit to '=' instead of just suggesting a ';'.
Old error:
plusequaldeclare1.cc:3:8: error: expected ';' at end of declaration
int x += 6;
^
;
New error:
plusequaldeclare1.cc:3:9: error: invalid '+=' at end of declaration; did you
mean '='?
int x += 6;
^~
=
llvm-svn: 148433
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Basic/DiagnosticParseKinds.td | 2 | ||||
-rw-r--r-- | clang/include/clang/Parse/Parser.h | 10 | ||||
-rw-r--r-- | clang/lib/Parse/ParseDecl.cpp | 8 | ||||
-rw-r--r-- | clang/lib/Parse/ParseExprCXX.cpp | 8 | ||||
-rw-r--r-- | clang/lib/Parse/Parser.cpp | 25 | ||||
-rw-r--r-- | clang/test/FixIt/fixit.cpp | 6 |
6 files changed, 40 insertions, 19 deletions
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td index 7675a566ebd..fa57dd2a913 100644 --- a/clang/include/clang/Basic/DiagnosticParseKinds.td +++ b/clang/include/clang/Basic/DiagnosticParseKinds.td @@ -156,6 +156,8 @@ def err_invalid_token_after_toplevel_declarator : Error< "expected ';' after top level declarator">; def err_invalid_equalequal_after_declarator : Error< "invalid '==' at end of declaration; did you mean '='?">; +def err_invalid_plusequal_after_declarator : Error< + "invalid '+=' at end of declaration; did you mean '='?">; def err_expected_statement : Error<"expected statement">; def err_expected_lparen_after : Error<"expected '(' after '%0'">; def err_expected_lparen_after_id : Error<"expected '(' after %0">; diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 31b50221664..c36f34af8c1 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -288,10 +288,12 @@ private: Tok.getKind() == tok::utf32_string_literal; } - /// \brief Returns true if the current token is a '=' or '==' and - /// false otherwise. If it's '==', we assume that it's a typo and we emit - /// DiagID and a fixit hint to turn '==' -> '='. - bool isTokenEqualOrMistypedEqualEqual(unsigned DiagID); + /// \brief Returns true if the current token is FoundToken. This token + /// will be assumed a typo. A diagnostic will be emitted with DiagID with a + /// a fixit to replace the current token with ExpectedToken. + bool CreateTokenReplacement(tok::TokenKind ExpectedToken, + tok::TokenKind FoundToken, + unsigned DiagID); /// ConsumeToken - Consume the current 'peek token' and lex the next one. /// This does not work with all kinds of tokens: strings and specific other diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 1c7c45e2bdc..0de8c53d906 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -1269,8 +1269,12 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; // Parse declarator '=' initializer. - if (isTokenEqualOrMistypedEqualEqual( - diag::err_invalid_equalequal_after_declarator)) { + // If a '==' or '+=' is found, suggest a fixit to '='. + if (Tok.is(tok::equal) || + CreateTokenReplacement(tok::equal, tok::equalequal, + diag::err_invalid_equalequal_after_declarator) || + CreateTokenReplacement(tok::equal, tok::plusequal, + diag::err_invalid_plusequal_after_declarator)) { ConsumeToken(); if (Tok.is(tok::kw_delete)) { if (D.isFunctionDeclarator()) diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp index f1cedfebfde..831c446fbb5 100644 --- a/clang/lib/Parse/ParseExprCXX.cpp +++ b/clang/lib/Parse/ParseExprCXX.cpp @@ -1259,8 +1259,12 @@ bool Parser::ParseCXXCondition(ExprResult &ExprOut, ExprOut = ExprError(); // '=' assignment-expression - if (isTokenEqualOrMistypedEqualEqual( - diag::err_invalid_equalequal_after_declarator)) { + // If a '==' or '+=' is found, suggest a fixit to '='. + if (Tok.is(tok::equal) || + CreateTokenReplacement(tok::equal, tok::equalequal, + diag::err_invalid_equalequal_after_declarator) || + CreateTokenReplacement(tok::equal, tok::plusequal, + diag::err_invalid_plusequal_after_declarator)) { ConsumeToken(); ExprResult AssignExpr(ParseAssignmentExpression()); if (!AssignExpr.isInvalid()) diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 501e50c1e79..6342b105680 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -1401,18 +1401,21 @@ bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) { return false; } -bool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) { - if (Tok.is(tok::equalequal)) { - // We have '==' in a context that we would expect a '='. - // The user probably made a typo, intending to type '='. Emit diagnostic, - // fixit hint to turn '==' -> '=' and continue as if the user typed '='. - Diag(Tok, DiagID) - << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), - getTokenSimpleSpelling(tok::equal)); - return true; - } +bool Parser::CreateTokenReplacement(tok::TokenKind ExpectedToken, + tok::TokenKind FoundToken, + unsigned DiagID) { + if (Tok.isNot(FoundToken)) + return false; - return Tok.is(tok::equal); + // We have FoundToken in a context that we would expect an ExpectedToken. + // The user probably made a typo, intending to type ExpectedToken. + // Emit diagnostic, fixit hint to turn ReplaceToken -> ExpectedToken + // and continue as if the user typed ExpectedToken. + Tok.setKind(ExpectedToken); + Diag(Tok, DiagID) + << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), + getTokenSimpleSpelling(ExpectedToken)); + return true; } SourceLocation Parser::handleUnexpectedCodeCompletionToken() { diff --git a/clang/test/FixIt/fixit.cpp b/clang/test/FixIt/fixit.cpp index f7bf35b09e3..63726b9b6ef 100644 --- a/clang/test/FixIt/fixit.cpp +++ b/clang/test/FixIt/fixit.cpp @@ -69,13 +69,19 @@ class C { namespace rdar8488464 { int x == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}} +int y += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}} void f() { int x == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}} (void)x; + int y += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}} + (void)y; if (int x == 0) { // expected-error {{invalid '==' at end of declaration; did you mean '='?}} (void)x; } + if (int y += 0) { // expected-error {{invalid '+=' at end of declaration; did you mean '='?}} + (void)y; + } } } |