[clang-tools-extra] r336810 - [clangd] Uprank delcarations when "using q::name" is present in the main file
Author: omtcyfz Date: Wed Jul 11 07:49:49 2018 New Revision: 336810 URL: http://llvm.org/viewvc/llvm-project?rev=336810=rev Log: [clangd] Uprank delcarations when "using q::name" is present in the main file Having `using qualified::name;` for some symbol is an important signal for clangd code completion as the user is more likely to use such symbol. This patch helps to uprank the relevant symbols by saving UsingShadowDecl in the new field of CodeCompletionResult and checking whether the corresponding UsingShadowDecl is located in the main file later in ClangD code completion routine. While the relative importance of such signal is a subject to change in the future, this patch simply bumps DeclProximity score to the value of 1.0 which should be enough for now. The patch was tested using `$ ninja check-clang check-clang-tools` No unexpected failures were noticed after running the relevant testsets. Reviewers: sammccall, ioeric Subscribers: MaskRay, jkorous, cfe-commits Differential Revision: https://reviews.llvm.org/D49012 Modified: clang-tools-extra/trunk/clangd/Quality.cpp clang-tools-extra/trunk/unittests/clangd/QualityTests.cpp Modified: clang-tools-extra/trunk/clangd/Quality.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Quality.cpp?rev=336810=336809=336810=diff == --- clang-tools-extra/trunk/clangd/Quality.cpp (original) +++ clang-tools-extra/trunk/clangd/Quality.cpp Wed Jul 11 07:49:49 2018 @@ -41,6 +41,17 @@ static bool hasDeclInMainFile(const Decl return false; } +static bool hasUsingDeclInMainFile(const CodeCompletionResult ) { + const auto = R.Declaration->getASTContext(); + const auto = Context.getSourceManager(); + if (R.ShadowDecl) { +const auto Loc = SourceMgr.getExpansionLoc(R.ShadowDecl->getLocation()); +if (SourceMgr.isWrittenInMainFile(Loc)) + return true; + } + return false; +} + static SymbolQualitySignals::SymbolCategory categorize(const NamedDecl ) { class Switch : public ConstDeclVisitor { @@ -231,8 +242,10 @@ void SymbolRelevanceSignals::merge(const // We boost things that have decls in the main file. We give a fixed score // for all other declarations in sema as they are already included in the // translation unit. -float DeclProximity = -hasDeclInMainFile(*SemaCCResult.Declaration) ? 1.0 : 0.6; +float DeclProximity = (hasDeclInMainFile(*SemaCCResult.Declaration) || + hasUsingDeclInMainFile(SemaCCResult)) + ? 1.0 + : 0.6; SemaProximityScore = std::max(DeclProximity, SemaProximityScore); } Modified: clang-tools-extra/trunk/unittests/clangd/QualityTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/QualityTests.cpp?rev=336810=336809=336810=diff == --- clang-tools-extra/trunk/unittests/clangd/QualityTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/QualityTests.cpp Wed Jul 11 07:49:49 2018 @@ -77,18 +77,31 @@ TEST(QualityTests, SymbolQualitySignalEx TEST(QualityTests, SymbolRelevanceSignalExtraction) { TestTU Test; Test.HeaderCode = R"cpp( -int header(); -int header_main(); -)cpp"; + int header(); + int header_main(); + + namespace hdr { class Bar {}; } // namespace hdr + + #define DEFINE_FLAG(X) \ + namespace flags { \ + int FLAGS_##X; \ + } \ + + DEFINE_FLAG(FOO) + )cpp"; Test.Code = R"cpp( -int ::header_main() {} -int main(); + using hdr::Bar; + + using flags::FLAGS_FOO; + + int ::header_main() {} + int main(); -[[deprecated]] -int deprecated() { return 0; } + [[deprecated]] + int deprecated() { return 0; } -namespace { struct X { void y() { int z; } }; } -struct S{} + namespace { struct X { void y() { int z; } }; } + struct S{} )cpp"; auto AST = Test.build(); @@ -111,6 +124,32 @@ TEST(QualityTests, SymbolRelevanceSignal EXPECT_FLOAT_EQ(Relevance.SemaProximityScore, 1.0f) << "Current file and header"; + auto constructShadowDeclCompletionResult = [&](const std::string DeclName) { +auto *Shadow = +*dyn_cast( + (AST, + [&](const NamedDecl ) { +if (const UsingDecl *Using = +dyn_cast()) + if (Using->shadow_size() && + Using->getQualifiedNameAsString() == DeclName) +return true; +return false; + })) + ->shadow_begin(); +CodeCompletionResult Result(Shadow->getTargetDecl(), 42); +Result.ShadowDecl = Shadow; +return Result; + }; + + Relevance = {}; +
r336810 - [clangd] Uprank delcarations when "using q::name" is present in the main file
Author: omtcyfz Date: Wed Jul 11 07:49:49 2018 New Revision: 336810 URL: http://llvm.org/viewvc/llvm-project?rev=336810=rev Log: [clangd] Uprank delcarations when "using q::name" is present in the main file Having `using qualified::name;` for some symbol is an important signal for clangd code completion as the user is more likely to use such symbol. This patch helps to uprank the relevant symbols by saving UsingShadowDecl in the new field of CodeCompletionResult and checking whether the corresponding UsingShadowDecl is located in the main file later in ClangD code completion routine. While the relative importance of such signal is a subject to change in the future, this patch simply bumps DeclProximity score to the value of 1.0 which should be enough for now. The patch was tested using `$ ninja check-clang check-clang-tools` No unexpected failures were noticed after running the relevant testsets. Reviewers: sammccall, ioeric Subscribers: MaskRay, jkorous, cfe-commits Differential Revision: https://reviews.llvm.org/D49012 Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h cfe/trunk/lib/Sema/SemaCodeComplete.cpp Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h?rev=336810=336809=336810=diff == --- cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h (original) +++ cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h Wed Jul 11 07:49:49 2018 @@ -45,8 +45,9 @@ class LangOptions; class NamedDecl; class NestedNameSpecifier; class Preprocessor; -class Sema; class RawComment; +class Sema; +class UsingShadowDecl; /// Default priority values for code-completion results based /// on their kind. @@ -836,6 +837,12 @@ public: /// informative rather than required. NestedNameSpecifier *Qualifier = nullptr; + /// If this Decl was unshadowed by using declaration, this can store a + /// pointer to the UsingShadowDecl which was used in the unshadowing process. + /// This information can be used to uprank CodeCompletionResults / which have + /// corresponding `using decl::qualified::name;` nearby. + const UsingShadowDecl *ShadowDecl = nullptr; + /// Build a result that refers to a declaration. CodeCompletionResult(const NamedDecl *Declaration, unsigned Priority, NestedNameSpecifier *Qualifier = nullptr, @@ -847,7 +854,7 @@ public: QualifierIsInformative(QualifierIsInformative), StartsNestedNameSpecifier(false), AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(Qualifier) { -//FIXME: Add assert to check FixIts range requirements. +// FIXME: Add assert to check FixIts range requirements. computeCursorKindAndAvailability(Accessible); } Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=336810=336809=336810=diff == --- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original) +++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed Jul 11 07:49:49 2018 @@ -859,12 +859,12 @@ void ResultBuilder::MaybeAddResult(Resul } // Look through using declarations. - if (const UsingShadowDecl *Using = - dyn_cast(R.Declaration)) { -MaybeAddResult(Result(Using->getTargetDecl(), - getBasePriority(Using->getTargetDecl()), - R.Qualifier), - CurContext); + if (const UsingShadowDecl *Using = dyn_cast(R.Declaration)) { +CodeCompletionResult Result(Using->getTargetDecl(), +getBasePriority(Using->getTargetDecl()), +R.Qualifier); +Result.ShadowDecl = Using; +MaybeAddResult(Result, CurContext); return; } @@ -977,10 +977,11 @@ void ResultBuilder::AddResult(Result R, // Look through using declarations. if (const UsingShadowDecl *Using = dyn_cast(R.Declaration)) { -AddResult(Result(Using->getTargetDecl(), - getBasePriority(Using->getTargetDecl()), - R.Qualifier), - CurContext, Hiding); +CodeCompletionResult Result(Using->getTargetDecl(), +getBasePriority(Using->getTargetDecl()), +R.Qualifier); +Result.ShadowDecl = Using; +AddResult(Result, CurContext, Hiding); return; } @@ -1004,10 +1005,10 @@ void ResultBuilder::AddResult(Result R, if (AsNestedNameSpecifier) { R.StartsNestedNameSpecifier = true; R.Priority = CCP_NestedNameSpecifier; - } - else if (Filter == ::IsMember && !R.Qualifier && InBaseClass && - isa(R.Declaration->getDeclContext() - ->getRedeclContext())) + } else if (Filter == ::IsMember