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
This commit is contained in:
Argyrios Kyrtzidis 2010-10-08 02:39:23 +00:00
parent d4e9c3b43a
commit b5c7c51392
6 changed files with 38 additions and 3 deletions

View 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">;

View 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

View 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();

View 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())

View 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) {

View 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;
}
}
}