diff options
author | Kaelyn Uhrain <rikka@google.com> | 2012-01-25 18:37:44 +0000 |
---|---|---|
committer | Kaelyn Uhrain <rikka@google.com> | 2012-01-25 18:37:44 +0000 |
commit | 60a09dcc4656b82c7f96f8719a403a83247f6742 (patch) | |
tree | 758cc3516985272b59ce994de854a98b6a7d0d5b /lib/Sema/SemaOverload.cpp | |
parent | d1bb4ae6cbc0f8bea4b329e040f58b18c03388e7 (diff) |
Add custom callback object for typo correction in BuildRecoveryCallExpr.
The new callback, in addition to limiting which keywords to include in
the pool of typo correction candidates, also filters out non-keyword
candidates that don't refer to (template) functions that accept the
number of arguments that are present for the call being recovered.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148962 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaOverload.cpp')
-rw-r--r-- | lib/Sema/SemaOverload.cpp | 52 |
1 files changed, 49 insertions, 3 deletions
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index fac3c26d0e..3333b4eea9 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -9169,6 +9169,54 @@ DiagnoseTwoPhaseOperatorLookup(Sema &SemaRef, OverloadedOperatorKind Op, /*ExplicitTemplateArgs=*/0, Args, NumArgs); } +namespace { +// Callback to limit the allowed keywords and to only accept typo corrections +// that are keywords or whose decls refer to functions (or template functions) +// that accept the given number of arguments. +class RecoveryCallCCC : public CorrectionCandidateCallback { + public: + RecoveryCallCCC(Sema &SemaRef, unsigned NumArgs, bool HasExplicitTemplateArgs) + : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs) { + WantTypeSpecifiers = SemaRef.getLangOptions().CPlusPlus; + WantRemainingKeywords = false; + } + + virtual bool ValidateCandidate(const TypoCorrection &candidate) { + if (!candidate.getCorrectionDecl()) + return candidate.isKeyword(); + + for (TypoCorrection::const_decl_iterator DI = candidate.begin(), + DIEnd = candidate.end(); DI != DIEnd; ++DI) { + FunctionDecl *FD = 0; + NamedDecl *ND = (*DI)->getUnderlyingDecl(); + if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) + FD = FTD->getTemplatedDecl(); + if (!HasExplicitTemplateArgs && !FD) { + if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { + // If the Decl is neither a function nor a template function, + // determine if it is a pointer or reference to a function. If so, + // check against the number of arguments expected for the pointee. + QualType ValType = cast<ValueDecl>(ND)->getType(); + if (ValType->isAnyPointerType() || ValType->isReferenceType()) + ValType = ValType->getPointeeType(); + if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) + if (FPT->getNumArgs() == NumArgs) + return true; + } + } + if (FD && FD->getNumParams() >= NumArgs && + FD->getMinRequiredArguments() <= NumArgs) + return true; + } + return false; + } + + private: + unsigned NumArgs; + bool HasExplicitTemplateArgs; +}; +} + /// Attempts to recover from a call where no functions were found. /// /// Returns true if new candidates were found. @@ -9192,9 +9240,7 @@ BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn, LookupResult R(SemaRef, ULE->getName(), ULE->getNameLoc(), Sema::LookupOrdinaryName); - CorrectionCandidateCallback Validator; - Validator.WantTypeSpecifiers = SemaRef.getLangOptions().CPlusPlus; - Validator.WantRemainingKeywords = false; + RecoveryCallCCC Validator(SemaRef, NumArgs, ExplicitTemplateArgs != 0); if (!DiagnoseTwoPhaseLookup(SemaRef, Fn->getExprLoc(), SS, R, ExplicitTemplateArgs, Args, NumArgs) && (!EmptyLookup || |