[cfe-commits] r148499 - in /cfe/trunk: 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
Richard Trieu
rtrieu at google.com
Thu Jan 19 14:01:51 PST 2012
Author: rtrieu
Date: Thu Jan 19 16:01:51 2012
New Revision: 148499
URL: http://llvm.org/viewvc/llvm-project?rev=148499&view=rev
Log:
Extend the error of invalid token after declarations to include fixits for
!=, %=, ^=, &=, *=, -=, |=, /=, <<=, <=, >=, and >>= to =.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
cfe/trunk/include/clang/Parse/Parser.h
cfe/trunk/lib/Parse/ParseDecl.cpp
cfe/trunk/lib/Parse/ParseExprCXX.cpp
cfe/trunk/lib/Parse/Parser.cpp
cfe/trunk/test/FixIt/fixit.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Thu Jan 19 16:01:51 2012
@@ -154,10 +154,8 @@
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_invalid_plusequal_after_declarator : Error<
- "invalid '+=' at end of declaration; did you mean '='?">;
+def err_invalid_token_after_declarator_suggest_equal : Error<
+ "invalid '%0' 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">;
Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Thu Jan 19 16:01:51 2012
@@ -288,12 +288,9 @@
Tok.getKind() == tok::utf32_string_literal;
}
- /// \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);
+ /// \brief Returns true if the current token is '=' or is a type of '='.
+ /// For typos, give a fixit to '='
+ bool isTokenEqualOrEqualTypo();
/// ConsumeToken - Consume the current 'peek token' and lex the next one.
/// This does not work with all kinds of tokens: strings and specific other
Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Thu Jan 19 16:01:51 2012
@@ -1270,11 +1270,7 @@
// Parse declarator '=' initializer.
// 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)) {
+ if (isTokenEqualOrEqualTypo()) {
ConsumeToken();
if (Tok.is(tok::kw_delete)) {
if (D.isFunctionDeclarator())
Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Thu Jan 19 16:01:51 2012
@@ -1260,11 +1260,7 @@
// '=' assignment-expression
// 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)) {
+ if (isTokenEqualOrEqualTypo()) {
ConsumeToken();
ExprResult AssignExpr(ParseAssignmentExpression());
if (!AssignExpr.isInvalid())
Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Thu Jan 19 16:01:51 2012
@@ -1401,21 +1401,31 @@
return false;
}
-bool Parser::CreateTokenReplacement(tok::TokenKind ExpectedToken,
- tok::TokenKind FoundToken,
- unsigned DiagID) {
- if (Tok.isNot(FoundToken))
+bool Parser::isTokenEqualOrEqualTypo() {
+ tok::TokenKind Kind = Tok.getKind();
+ switch (Kind) {
+ default:
return false;
-
- // 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;
+ case tok::ampequal: // &=
+ case tok::starequal: // *=
+ case tok::plusequal: // +=
+ case tok::minusequal: // -=
+ case tok::exclaimequal: // !=
+ case tok::slashequal: // /=
+ case tok::percentequal: // %=
+ case tok::lessequal: // <=
+ case tok::lesslessequal: // <<=
+ case tok::greaterequal: // >=
+ case tok::greatergreaterequal: // >>=
+ case tok::caretequal: // ^=
+ case tok::pipeequal: // |=
+ case tok::equalequal: // ==
+ Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
+ << getTokenSimpleSpelling(Kind)
+ << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
+ case tok::equal:
+ return true;
+ }
}
SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
Modified: cfe/trunk/test/FixIt/fixit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit.cpp?rev=148499&r1=148498&r2=148499&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/fixit.cpp (original)
+++ cfe/trunk/test/FixIt/fixit.cpp Thu Jan 19 16:01:51 2012
@@ -68,20 +68,68 @@
};
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 '='?}}
+int x = 0;
+int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
+int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
+int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
+int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
+int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
+int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
+int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
+int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
+int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
+int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
+int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
+int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
+int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
+int x14 == 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 '='?}}
+ int x = 0;
(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;
- }
+ int x1 &= 0; // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
+ (void)x1;
+ int x2 *= 0; // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
+ (void)x2;
+ int x3 += 0; // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
+ (void)x3;
+ int x4 -= 0; // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
+ (void)x4;
+ int x5 != 0; // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
+ (void)x5;
+ int x6 /= 0; // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
+ (void)x6;
+ int x7 %= 0; // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
+ (void)x7;
+ int x8 <= 0; // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
+ (void)x8;
+ int x9 <<= 0; // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
+ (void)x9;
+ int x10 >= 0; // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
+ (void)x10;
+ int x11 >>= 0; // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
+ (void)x11;
+ int x12 ^= 0; // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
+ (void)x12;
+ int x13 |= 0; // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
+ (void)x13;
+ int x14 == 0; // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
+ (void)x14;
+ if (int x = 0) { (void)x; }
+ if (int x1 &= 0) { (void)x1; } // expected-error {{invalid '&=' at end of declaration; did you mean '='?}}
+ if (int x2 *= 0) { (void)x2; } // expected-error {{invalid '*=' at end of declaration; did you mean '='?}}
+ if (int x3 += 0) { (void)x3; } // expected-error {{invalid '+=' at end of declaration; did you mean '='?}}
+ if (int x4 -= 0) { (void)x4; } // expected-error {{invalid '-=' at end of declaration; did you mean '='?}}
+ if (int x5 != 0) { (void)x5; } // expected-error {{invalid '!=' at end of declaration; did you mean '='?}}
+ if (int x6 /= 0) { (void)x6; } // expected-error {{invalid '/=' at end of declaration; did you mean '='?}}
+ if (int x7 %= 0) { (void)x7; } // expected-error {{invalid '%=' at end of declaration; did you mean '='?}}
+ if (int x8 <= 0) { (void)x8; } // expected-error {{invalid '<=' at end of declaration; did you mean '='?}}
+ if (int x9 <<= 0) { (void)x9; } // expected-error {{invalid '<<=' at end of declaration; did you mean '='?}}
+ if (int x10 >= 0) { (void)x10; } // expected-error {{invalid '>=' at end of declaration; did you mean '='?}}
+ if (int x11 >>= 0) { (void)x11; } // expected-error {{invalid '>>=' at end of declaration; did you mean '='?}}
+ if (int x12 ^= 0) { (void)x12; } // expected-error {{invalid '^=' at end of declaration; did you mean '='?}}
+ if (int x13 |= 0) { (void)x13; } // expected-error {{invalid '|=' at end of declaration; did you mean '='?}}
+ if (int x14 == 0) { (void)x14; } // expected-error {{invalid '==' at end of declaration; did you mean '='?}}
}
}
More information about the cfe-commits
mailing list