From: Kaelyn Uhrain Date: Mon, 12 Aug 2013 19:54:38 +0000 (+0000) Subject: Add hooks for typo correction to ExternalSemaSource, courtesy of Luke Zarko. X-Git-Tag: llvmorg-3.4.0-rc1~6618 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f0aabdadc6eb69c1dc3d9108caa67da7f4e052b1;p=platform%2Fupstream%2Fllvm.git Add hooks for typo correction to ExternalSemaSource, courtesy of Luke Zarko. llvm-svn: 188196 --- diff --git a/clang/include/clang/Sema/ExternalSemaSource.h b/clang/include/clang/Sema/ExternalSemaSource.h index a626a090f63e..fd32d7f007a7 100644 --- a/clang/include/clang/Sema/ExternalSemaSource.h +++ b/clang/include/clang/Sema/ExternalSemaSource.h @@ -14,6 +14,7 @@ #define LLVM_CLANG_SEMA_EXTERNAL_SEMA_SOURCE_H #include "clang/AST/ExternalASTSource.h" +#include "clang/Sema/TypoCorrection.h" #include "clang/Sema/Weak.h" #include "llvm/ADT/MapVector.h" #include @@ -187,6 +188,22 @@ public: virtual void ReadLateParsedTemplates( llvm::DenseMap &LPTMap) {} + /// \copydoc Sema::CorrectTypo + /// \note LookupKind must correspond to a valid Sema::LookupNameKind + /// + /// ExternalSemaSource::CorrectTypo is always given the first chance to + /// correct a typo (really, to offer suggestions to repair a failed lookup). + /// It will even be called when SpellChecking is turned off or after a + /// fatal error has already been detected. + virtual TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, + int LookupKind, Scope *S, CXXScopeSpec *SS, + CorrectionCandidateCallback &CCC, + DeclContext *MemberContext, + bool EnteringContext, + const ObjCObjectPointerType *OPT) { + return TypoCorrection(); + } + // isa/cast/dyn_cast support static bool classof(const ExternalASTSource *Source) { return Source->SemaSource; diff --git a/clang/include/clang/Sema/MultiplexExternalSemaSource.h b/clang/include/clang/Sema/MultiplexExternalSemaSource.h index 033400743b94..bb7ba239cac1 100644 --- a/clang/include/clang/Sema/MultiplexExternalSemaSource.h +++ b/clang/include/clang/Sema/MultiplexExternalSemaSource.h @@ -331,6 +331,15 @@ public: virtual void ReadLateParsedTemplates( llvm::DenseMap &LPTMap); + /// \copydoc ExternalSemaSource::CorrectTypo + /// \note Returns the first nonempty correction. + virtual TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, + int LookupKind, Scope *S, CXXScopeSpec *SS, + CorrectionCandidateCallback &CCC, + DeclContext *MemberContext, + bool EnteringContext, + const ObjCObjectPointerType *OPT); + // isa/cast/dyn_cast support static bool classof(const MultiplexExternalSemaSource*) { return true; } //static bool classof(const ExternalSemaSource*) { return true; } diff --git a/clang/lib/Sema/MultiplexExternalSemaSource.cpp b/clang/lib/Sema/MultiplexExternalSemaSource.cpp index de790e9a2d1b..43bafd3482bc 100644 --- a/clang/lib/Sema/MultiplexExternalSemaSource.cpp +++ b/clang/lib/Sema/MultiplexExternalSemaSource.cpp @@ -273,3 +273,19 @@ void MultiplexExternalSemaSource::ReadLateParsedTemplates( for (size_t i = 0; i < Sources.size(); ++i) Sources[i]->ReadLateParsedTemplates(LPTMap); } + +TypoCorrection MultiplexExternalSemaSource::CorrectTypo( + const DeclarationNameInfo &Typo, + int LookupKind, Scope *S, CXXScopeSpec *SS, + CorrectionCandidateCallback &CCC, + DeclContext *MemberContext, + bool EnteringContext, + const ObjCObjectPointerType *OPT) { + for (size_t I = 0, E = Sources.size(); I < E; ++I) { + if (TypoCorrection C = Sources[I]->CorrectTypo(Typo, LookupKind, S, SS, CCC, + MemberContext, + EnteringContext, OPT)) + return C; + } + return TypoCorrection(); +} diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index e95ffce0549f..d0bb9f6abe6c 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -3872,6 +3872,14 @@ TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName, DeclContext *MemberContext, bool EnteringContext, const ObjCObjectPointerType *OPT) { + // Always let the ExternalSource have the first chance at correction, even + // if we would otherwise have given up. + if (ExternalSource) { + if (TypoCorrection Correction = ExternalSource->CorrectTypo( + TypoName, LookupKind, S, SS, CCC, MemberContext, EnteringContext, OPT)) + return Correction; + } + if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking) return TypoCorrection(); diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt index fed775ef0245..479b36f84c5a 100644 --- a/clang/unittests/CMakeLists.txt +++ b/clang/unittests/CMakeLists.txt @@ -19,4 +19,5 @@ if(CLANG_ENABLE_REWRITER) add_subdirectory(AST) add_subdirectory(Tooling) add_subdirectory(Format) + add_subdirectory(Sema) endif() diff --git a/clang/unittests/Makefile b/clang/unittests/Makefile index e01a6ac463bb..542863e591d9 100644 --- a/clang/unittests/Makefile +++ b/clang/unittests/Makefile @@ -23,7 +23,7 @@ PARALLEL_DIRS += Format endif ifeq ($(ENABLE_CLANG_REWRITER),1) -PARALLEL_DIRS += ASTMatchers AST Tooling +PARALLEL_DIRS += ASTMatchers AST Tooling Sema endif ifeq ($(ENABLE_CLANG_STATIC_ANALYZER),1)