[clang-tools-extra] r347562 - [clangd] Add type boosting in code completion
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 26 07:38:01 PST 2018
Author: ibiryukov
Date: Mon Nov 26 07:38:01 2018
New Revision: 347562
URL: http://llvm.org/viewvc/llvm-project?rev=347562&view=rev
Log:
[clangd] Add type boosting in code completion
Reviewers: sammccall, ioeric
Reviewed By: sammccall
Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D52276
Modified:
clang-tools-extra/trunk/clangd/CodeComplete.cpp
clang-tools-extra/trunk/clangd/Quality.cpp
clang-tools-extra/trunk/clangd/Quality.h
clang-tools-extra/trunk/clangd/index/Index.h
Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=347562&r1=347561&r2=347562&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Mon Nov 26 07:38:01 2018
@@ -24,6 +24,7 @@
#include "CodeCompletionStrings.h"
#include "Compiler.h"
#include "Diagnostics.h"
+#include "ExpectedTypes.h"
#include "FileDistance.h"
#include "FuzzyMatch.h"
#include "Headers.h"
@@ -1225,6 +1226,7 @@ class CodeCompleteFlow {
std::vector<std::string> QueryScopes; // Initialized once Sema runs.
// Initialized once QueryScopes is initialized, if there are scopes.
Optional<ScopeDistance> ScopeProximity;
+ llvm::Optional<OpaqueType> PreferredType; // Initialized once Sema runs.
// Whether to query symbols from any scope. Initialized once Sema runs.
bool AllScopes = false;
// Include-insertion and proximity scoring rely on the include structure.
@@ -1302,9 +1304,12 @@ public:
Inserter.reset(); // Make sure this doesn't out-live Clang.
SPAN_ATTACH(Tracer, "sema_completion_kind",
getCompletionKindString(Recorder->CCContext.getKind()));
- log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2})",
+ log("Code complete: sema context {0}, query scopes [{1}] (AnyScope={2}), "
+ "expected type {3}",
getCompletionKindString(Recorder->CCContext.getKind()),
- join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes);
+ join(QueryScopes.begin(), QueryScopes.end(), ","), AllScopes,
+ PreferredType ? Recorder->CCContext.getPreferredType().getAsString()
+ : "<none>");
});
Recorder = RecorderOwner.get();
@@ -1354,6 +1359,9 @@ private:
getQueryScopes(Recorder->CCContext, *Recorder->CCSema, Opts);
if (!QueryScopes.empty())
ScopeProximity.emplace(QueryScopes);
+ PreferredType =
+ OpaqueType::fromType(Recorder->CCSema->getASTContext(),
+ Recorder->CCContext.getPreferredType());
// Sema provides the needed context to query the index.
// FIXME: in addition to querying for extra/overlapping symbols, we should
// explicitly request symbols corresponding to Sema results.
@@ -1492,6 +1500,8 @@ private:
Relevance.FileProximityMatch = FileProximity.getPointer();
if (ScopeProximity)
Relevance.ScopeProximityMatch = ScopeProximity.getPointer();
+ if (PreferredType)
+ Relevance.HadContextType = true;
auto &First = Bundle.front();
if (auto FuzzyScore = fuzzyScore(First))
@@ -1506,10 +1516,24 @@ private:
Relevance.merge(*Candidate.IndexResult);
Origin |= Candidate.IndexResult->Origin;
FromIndex = true;
+ if (!Candidate.IndexResult->Type.empty())
+ Relevance.HadSymbolType |= true;
+ if (PreferredType &&
+ PreferredType->raw() == Candidate.IndexResult->Type) {
+ Relevance.TypeMatchesPreferred = true;
+ }
}
if (Candidate.SemaResult) {
Quality.merge(*Candidate.SemaResult);
Relevance.merge(*Candidate.SemaResult);
+ if (PreferredType) {
+ if (auto CompletionType = OpaqueType::fromCompletionResult(
+ Recorder->CCSema->getASTContext(), *Candidate.SemaResult)) {
+ Relevance.HadSymbolType |= true;
+ if (PreferredType == CompletionType)
+ Relevance.TypeMatchesPreferred = true;
+ }
+ }
Origin |= SymbolOrigin::AST;
}
}
Modified: clang-tools-extra/trunk/clangd/Quality.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Quality.cpp?rev=347562&r1=347561&r2=347562&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Quality.cpp (original)
+++ clang-tools-extra/trunk/clangd/Quality.cpp Mon Nov 26 07:38:01 2018
@@ -369,6 +369,9 @@ float SymbolRelevanceSignals::evaluate()
}
}
+ if (TypeMatchesPreferred)
+ Score *= 5.0;
+
// Penalize non-instance members when they are accessed via a class instance.
if (!IsInstanceMember &&
(Context == CodeCompletionContext::CCC_DotMemberAccess ||
@@ -412,6 +415,10 @@ raw_ostream &operator<<(raw_ostream &OS,
OS << formatv("\tIndex scope boost: {0}\n",
scopeBoost(*S.ScopeProximityMatch, S.SymbolScope));
+ OS << formatv(
+ "\tType matched preferred: {0} (Context type: {1}, Symbol type: {2}\n",
+ S.TypeMatchesPreferred, S.HadContextType, S.HadSymbolType);
+
return OS;
}
Modified: clang-tools-extra/trunk/clangd/Quality.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Quality.h?rev=347562&r1=347561&r2=347562&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Quality.h (original)
+++ clang-tools-extra/trunk/clangd/Quality.h Mon Nov 26 07:38:01 2018
@@ -28,6 +28,7 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H
+#include "ExpectedTypes.h"
#include "FileDistance.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "llvm/ADT/ArrayRef.h"
@@ -122,6 +123,13 @@ struct SymbolRelevanceSignals {
// Whether symbol is an instance member of a class.
bool IsInstanceMember = false;
+ // Whether clang provided a preferred type in the completion context.
+ bool HadContextType = false;
+ // Whether a source completion item or a symbol had a type information.
+ bool HadSymbolType = false;
+ // Whether the item matches the type expected in the completion context.
+ bool TypeMatchesPreferred = false;
+
void merge(const CodeCompletionResult &SemaResult);
void merge(const Symbol &IndexResult);
Modified: clang-tools-extra/trunk/clangd/index/Index.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/Index.h?rev=347562&r1=347561&r2=347562&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/index/Index.h (original)
+++ clang-tools-extra/trunk/clangd/index/Index.h Mon Nov 26 07:38:01 2018
@@ -494,6 +494,8 @@ struct FuzzyFindRequest {
/// Paths should be absolute.
std::vector<std::string> ProximityPaths;
+ // FIXME(ibiryukov): add expected type to the request.
+
bool operator==(const FuzzyFindRequest &Req) const {
return std::tie(Query, Scopes, Limit, RestrictForCodeCompletion,
ProximityPaths) ==
More information about the cfe-commits
mailing list