]> granicus.if.org Git - clang/commitdiff
Change the error when a '+=' follows a declaration to suggest a fixit to '=' instead...
authorRichard Trieu <rtrieu@google.com>
Wed, 18 Jan 2012 22:54:52 +0000 (22:54 +0000)
committerRichard Trieu <rtrieu@google.com>
Wed, 18 Jan 2012 22:54:52 +0000 (22:54 +0000)
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;
        ^~
        =

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148433 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7675a566ebd8ecc08040210432ea8890361fc734..fa57dd2a913568a93972f437aa8b9929903d9390 100644 (file)
@@ -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">;
index 31b50221664819eeab6a773cadf33725f008730d..c36f34af8c11e58133b7bf8e46fbc14058a75329 100644 (file)
@@ -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
index 1c7c45e2bdcfc0e1137eb02f13fde99939b0f017..0de8c53d9067d84a8ededd789ef860e2bea90867 100644 (file)
@@ -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())
index f1cedfebfde12b200253a08800e141bb03933b86..831c446fbb51fa1cf742071c068a6b0621653823 100644 (file)
@@ -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()) 
index 501e50c1e79d8d58429e4a025aecb5e14329f3b2..6342b1056809afb19e71b76c2ce63d18969c3e86 100644 (file)
@@ -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() {
index f7bf35b09e336263fee915775128c2717c96e26e..63726b9b6ef493c0f9411d1031b3c2d67434402c 100644 (file)
@@ -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;
+    }
 }
 }