]> granicus.if.org Git - clang/commitdiff
When we encounter a '==' in a context expecting a '=', assume the user made a typo:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Oct 2010 02:39:23 +0000 (02:39 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 8 Oct 2010 02:39:23 +0000 (02:39 +0000)
t.c:1:7: error: invalid '==' at end of declaration; did you mean '='?
int x == 0;
      ^~
      =

Implements rdar://8488464.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@116035 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 5751ae90f294560c7f564f289754b11a490255d5..430ed9f011ec8b05b42c053365b0bb6d85e4620c 100644 (file)
@@ -112,6 +112,8 @@ def err_expected_fn_body : Error<
 def err_expected_method_body : Error<"expected method body">;
 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_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 1cdeef727e01cbb139ee7de04583eecdf74b2319..f1548460e340465019cb17c1bbb411a3d7a57e81 100644 (file)
@@ -236,6 +236,11 @@ private:
            Tok.getKind() == tok::wide_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);
+
   /// ConsumeToken - Consume the current 'peek token' and lex the next one.
   /// This does not work with all kinds of tokens: strings and specific other
   /// tokens must be consumed with custom methods below.  This returns the
index 89b41828b062301ea2e3397ad00ebd814ccf5b3b..e6a4b91e2d1ce426f706417d099524a5cc3fb000 100644 (file)
@@ -594,7 +594,8 @@ Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
   }
 
   // Parse declarator '=' initializer.
-  if (Tok.is(tok::equal)) {
+  if (isTokenEqualOrMistypedEqualEqual(
+                               diag::err_invalid_equalequal_after_declarator)) {
     ConsumeToken();
     if (Tok.is(tok::kw_delete)) {
       SourceLocation DelLoc = ConsumeToken();
index e8b921b8f7f272f1baf3da55eac2035453bd3c72..77cb4492f6750278649981a9ca97bc1826b7b4ce 100644 (file)
@@ -821,9 +821,10 @@ bool Parser::ParseCXXCondition(ExprResult &ExprOut,
                                                                 DeclaratorInfo);
   DeclOut = Dcl.get();
   ExprOut = ExprError();
-  
+
   // '=' assignment-expression
-  if (Tok.is(tok::equal)) {
+  if (isTokenEqualOrMistypedEqualEqual(
+                               diag::err_invalid_equalequal_after_declarator)) {
     SourceLocation EqualLoc = ConsumeToken();
     ExprResult AssignExpr(ParseAssignmentExpression());
     if (!AssignExpr.isInvalid()) 
index 33c7b7dcb4c50e9b0d423f9961fc5273a39d0c7e..8084088afcad5d7affa7e5dff9a07315419d9698 100644 (file)
@@ -1156,6 +1156,20 @@ 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;
+  }
+
+  return Tok.is(tok::equal);
+}
+
 void Parser::CodeCompletionRecovery() {
   for (Scope *S = getCurScope(); S; S = S->getParent()) {
     if (S->getFlags() & Scope::FnScope) {
index 95d8a88af1f8d6b637cc66342942a554dcc3aeb0..1993425c955d147203eb6f0a3ed7983a8b15537b 100644 (file)
@@ -71,3 +71,15 @@ class C {
   int C::foo();
 };
 
+namespace rdar8488464 {
+int x == 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;
+    if (int x == 0) { // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
+      (void)x;
+    }
+}
+}
+