Add a new error message and fixit hint for += after a declaration.  This is
similar to how Clang handles a == after a declaration.

plusequaldeclare1.cc:3:9: error: invalid '+=' at end of declaration; did
      you mean '='?
  int x += 6;
        ^~
        =
1 error generated.
Index: test/FixIt/fixit.cpp
===================================================================
--- test/FixIt/fixit.cpp	(revision 148165)
+++ test/FixIt/fixit.cpp	(working copy)
@@ -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;
+    }
 }
 }
 
Index: include/clang/Basic/DiagnosticParseKinds.td
===================================================================
--- include/clang/Basic/DiagnosticParseKinds.td	(revision 148165)
+++ include/clang/Basic/DiagnosticParseKinds.td	(working copy)
@@ -155,6 +155,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">;
Index: include/clang/Parse/Parser.h
===================================================================
--- include/clang/Parse/Parser.h	(revision 148165)
+++ include/clang/Parse/Parser.h	(working copy)
@@ -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
Index: lib/Parse/ParseDecl.cpp
===================================================================
--- lib/Parse/ParseDecl.cpp	(revision 148165)
+++ lib/Parse/ParseDecl.cpp	(working copy)
@@ -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())
Index: lib/Parse/ParseExprCXX.cpp
===================================================================
--- lib/Parse/ParseExprCXX.cpp	(revision 148165)
+++ lib/Parse/ParseExprCXX.cpp	(working copy)
@@ -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()) 
Index: lib/Parse/Parser.cpp
===================================================================
--- lib/Parse/Parser.cpp	(revision 148165)
+++ lib/Parse/Parser.cpp	(working copy)
@@ -1402,18 +1402,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() {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to