summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-10-08 02:39:23 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-10-08 02:39:23 +0000
commitb5c7c5139281ea8435afd7385527b1801ef21c41 (patch)
tree3dddbcc7aea6d909f43424d8ad328aed3afe7753
parentd4e9c3b43a0c446378e1c5175e800a16a3e7c1f5 (diff)
downloadbcm5719-llvm-b5c7c5139281ea8435afd7385527b1801ef21c41.tar.gz
bcm5719-llvm-b5c7c5139281ea8435afd7385527b1801ef21c41.zip
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. llvm-svn: 116035
-rw-r--r--clang/include/clang/Basic/DiagnosticParseKinds.td2
-rw-r--r--clang/include/clang/Parse/Parser.h5
-rw-r--r--clang/lib/Parse/ParseDecl.cpp3
-rw-r--r--clang/lib/Parse/ParseExprCXX.cpp5
-rw-r--r--clang/lib/Parse/Parser.cpp14
-rw-r--r--clang/test/FixIt/fixit.cpp12
6 files changed, 38 insertions, 3 deletions
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 5751ae90f29..430ed9f011e 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -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">;
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 1cdeef727e0..f1548460e34 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -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
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 89b41828b06..e6a4b91e2d1 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -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();
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index e8b921b8f7f..77cb4492f67 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -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())
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 33c7b7dcb4c..8084088afca 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -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) {
diff --git a/clang/test/FixIt/fixit.cpp b/clang/test/FixIt/fixit.cpp
index 95d8a88af1f..1993425c955 100644
--- a/clang/test/FixIt/fixit.cpp
+++ b/clang/test/FixIt/fixit.cpp
@@ -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;
+ }
+}
+}
+
OpenPOWER on IntegriCloud