diff options
author | Kaelyn Uhrain <rikka@google.com> | 2012-01-25 20:49:08 +0000 |
---|---|---|
committer | Kaelyn Uhrain <rikka@google.com> | 2012-01-25 20:49:08 +0000 |
commit | cd78e612d6fa8e214e6a6bb0e739a0c3e419df91 (patch) | |
tree | 80fe736fdb339888bc309422e146c3753a84396d /lib/Parse/ParseExpr.cpp | |
parent | ada4fa7978dc91a5e615af2ec29e12e3c6973c7f (diff) |
Avoid correcting unknown identifiers to types where types aren't allowed.
Pass a typo correction callback object from ParseCastExpr to
Sema::ActOnIdExpression to be a bit more selective about what kinds of
corrections will be allowed for unknown identifiers.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148973 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Parse/ParseExpr.cpp')
-rw-r--r-- | lib/Parse/ParseExpr.cpp | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index 606c108d7d..22c5841e45 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -23,6 +23,7 @@ #include "clang/Sema/DeclSpec.h" #include "clang/Sema/Scope.h" #include "clang/Sema/ParsedTemplate.h" +#include "clang/Sema/TypoCorrection.h" #include "clang/Basic/PrettyStackTrace.h" #include "RAIIObjectsForParser.h" #include "llvm/ADT/SmallVector.h" @@ -174,8 +175,8 @@ static prec::Level getBinOpPrecedence(tok::TokenKind Kind, /// expression: [C99 6.5.17] /// assignment-expression ...[opt] /// expression ',' assignment-expression ...[opt] -ExprResult Parser::ParseExpression() { - ExprResult LHS(ParseAssignmentExpression()); +ExprResult Parser::ParseExpression(TypeCastState isTypeCast) { + ExprResult LHS(ParseAssignmentExpression(isTypeCast)); return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); } @@ -211,7 +212,7 @@ Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) { } /// ParseAssignmentExpression - Parse an expr that doesn't include commas. -ExprResult Parser::ParseAssignmentExpression() { +ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) { if (Tok.is(tok::code_completion)) { Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); cutOffParsing(); @@ -221,7 +222,9 @@ ExprResult Parser::ParseAssignmentExpression() { if (Tok.is(tok::kw_throw)) return ParseThrowExpression(); - ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false); + ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false, + /*isAddressOfOperand=*/false, + isTypeCast); return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment); } @@ -417,7 +420,7 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) { /// ExprResult Parser::ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand, - bool isTypeCast) { + TypeCastState isTypeCast) { bool NotCastExpr; ExprResult Res = ParseCastExpression(isUnaryExpression, isAddressOfOperand, @@ -428,6 +431,29 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression, return move(Res); } +namespace { +class CastExpressionIdValidator : public CorrectionCandidateCallback { + public: + CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes) + : AllowNonTypes(AllowNonTypes) { + WantTypeSpecifiers = AllowTypes; + } + + virtual bool ValidateCandidate(const TypoCorrection &candidate) { + NamedDecl *ND = candidate.getCorrectionDecl(); + if (!ND) + return candidate.isKeyword(); + + if (isa<TypeDecl>(ND)) + return WantTypeSpecifiers; + return AllowNonTypes; + } + + private: + bool AllowNonTypes; +}; +} + /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is /// true, parse a unary-expression. isAddressOfOperand exists because an /// id-expression that is the operand of address-of gets special treatment @@ -592,7 +618,7 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression, ExprResult Parser::ParseCastExpression(bool isUnaryExpression, bool isAddressOfOperand, bool &NotCastExpr, - bool isTypeCast) { + TypeCastState isTypeCast) { ExprResult Res; tok::TokenKind SavedKind = Tok.getKind(); NotCastExpr = false; @@ -623,7 +649,7 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression, ColonProtectionRAIIObject X(*this, false); Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, - isTypeCast, CastTy, RParenLoc); + isTypeCast == IsTypeCast, CastTy, RParenLoc); } switch (ParenExprType) { @@ -769,9 +795,12 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression, // not. UnqualifiedId Name; CXXScopeSpec ScopeSpec; + CastExpressionIdValidator Validator(isTypeCast != NotTypeCast, + isTypeCast != IsTypeCast); Name.setIdentifier(&II, ILoc); Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, Name, - Tok.is(tok::l_paren), isAddressOfOperand); + Tok.is(tok::l_paren), isAddressOfOperand, + &Validator); break; } case tok::char_constant: // constant: character-constant @@ -1954,7 +1983,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, // TODO: For cast expression with CastTy. Result = ParseCastExpression(/*isUnaryExpression=*/false, /*isAddressOfOperand=*/false, - /*isTypeCast=*/true); + /*isTypeCast=*/IsTypeCast); if (!Result.isInvalid()) { Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc, DeclaratorInfo, CastTy, @@ -1981,7 +2010,7 @@ Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, } else { InMessageExpressionRAIIObject InMessage(*this, false); - Result = ParseExpression(); + Result = ParseExpression(MaybeTypeCast); ExprType = SimpleExpr; // Don't build a paren expression unless we actually match a ')'. |