When we encounter a '==' in a context expecting a '=', assume the user made a typo:
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
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp
index 89b4182..e6a4b91 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -594,7 +594,8 @@
}
// 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();
diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp
index e8b921b..77cb449 100644
--- a/lib/Parse/ParseExprCXX.cpp
+++ b/lib/Parse/ParseExprCXX.cpp
@@ -821,9 +821,10 @@
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())
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index 33c7b7d..8084088 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -1156,6 +1156,20 @@
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) {