Hi Richard, Would it not also be worthwhile adding a case for -=? (and possibly *= / /=, but they're less common)
Cheers, James -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Richard Trieu Sent: 18 January 2012 22:55 To: [email protected] Subject: [cfe-commits] r148433 - in /cfe/trunk: include/clang/Basic/DiagnosticParseKinds.td include/clang/Parse/Parser.h lib/Parse/ParseDecl.cpp lib/Parse/ParseExprCXX.cpp lib/Parse/Parser.cpp test/FixIt/fixit.cpp Author: rtrieu Date: Wed Jan 18 16:54:52 2012 New Revision: 148433 URL: http://llvm.org/viewvc/llvm-project?rev=148433&view=rev Log: 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; ^~ = Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td cfe/trunk/include/clang/Parse/Parser.h cfe/trunk/lib/Parse/ParseDecl.cpp cfe/trunk/lib/Parse/ParseExprCXX.cpp cfe/trunk/lib/Parse/Parser.cpp cfe/trunk/test/FixIt/fixit.cpp Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic ParseKinds.td?rev=148433&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Wed Jan 18 16:54:52 2012 @@ -156,6 +156,8 @@ "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">; Modified: cfe/trunk/include/clang/Parse/Parser.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?r ev=148433&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/include/clang/Parse/Parser.h (original) +++ cfe/trunk/include/clang/Parse/Parser.h Wed Jan 18 16:54:52 2012 @@ -288,10 +288,12 @@ 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 Modified: cfe/trunk/lib/Parse/ParseDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=14 8433&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/lib/Parse/ParseDecl.cpp (original) +++ cfe/trunk/lib/Parse/ParseDecl.cpp Wed Jan 18 16:54:52 2012 @@ -1269,8 +1269,12 @@ 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()) Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev =148433&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original) +++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Wed Jan 18 16:54:52 2012 @@ -1259,8 +1259,12 @@ 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()) Modified: cfe/trunk/lib/Parse/Parser.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=14843 3&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/lib/Parse/Parser.cpp (original) +++ cfe/trunk/lib/Parse/Parser.cpp Wed Jan 18 16:54:52 2012 @@ -1401,18 +1401,21 @@ 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() { Modified: cfe/trunk/test/FixIt/fixit.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit.cpp?rev=14843 3&r1=148432&r2=148433&view=diff ============================================================================ == --- cfe/trunk/test/FixIt/fixit.cpp (original) +++ cfe/trunk/test/FixIt/fixit.cpp Wed Jan 18 16:54:52 2012 @@ -69,13 +69,19 @@ 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; + } } } _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
