[clang-tools-extra] e397a0a - [clangd] Add instrumentation mode in clangd for metrics collection.
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 5 03:39:32 PST 2020
Author: Utkarsh Saxena
Date: 2020-03-05T12:39:15+01:00
New Revision: e397a0a5c3c0fa1912fbac23b550fa7d239196ba
URL: https://github.com/llvm/llvm-project/commit/e397a0a5c3c0fa1912fbac23b550fa7d239196ba
DIFF: https://github.com/llvm/llvm-project/commit/e397a0a5c3c0fa1912fbac23b550fa7d239196ba.diff
LOG: [clangd] Add instrumentation mode in clangd for metrics collection.
Summary:
This patch adds an instrumentation mode for clangd (enabled by
corresponding option in cc_opts).
If this mode is enabled then user can specify callbacks to run on the
final code completion result.
Moreover the CodeCompletion::Score will contain the detailed Quality and
Relevance signals used to compute the score when this mode is enabled.
These are required because we do not any place in which the final
candidates (scored and sorted) are available along with the above
signals. The signals are temporary structures in `addCandidate`.
The callback is needed as it gives access to many data structures that
are internal to CodeCompleteFlow and are available once Sema has run. Eg:
ScopeDistnace and FileDistance.
If this mode is disabled (as in default) then Score would just contain 2
shared pointers (null). Thus cost(memory/time) increase for the default
mode would be fairly cheap and insignificant.
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75603
Added:
Modified:
clang-tools-extra/clangd/CodeComplete.cpp
clang-tools-extra/clangd/CodeComplete.h
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 3fbf98970cce..b2e97729ee6d 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -1660,6 +1660,10 @@ class CodeCompleteFlow {
? Scores.Total / Relevance.NameMatch
: Scores.Quality;
+ if (Opts.RecordCCResult)
+ Opts.RecordCCResult(toCodeCompletion(Bundle), Quality, Relevance,
+ Scores.Total);
+
dlog("CodeComplete: {0} ({1}) = {2}\n{3}{4}\n", First.Name,
llvm::to_string(Origin), Scores.Total, llvm::to_string(Quality),
llvm::to_string(Relevance));
diff --git a/clang-tools-extra/clangd/CodeComplete.h b/clang-tools-extra/clangd/CodeComplete.h
index 3b3dca3ee8c5..df06c156049f 100644
--- a/clang-tools-extra/clangd/CodeComplete.h
+++ b/clang-tools-extra/clangd/CodeComplete.h
@@ -19,6 +19,7 @@
#include "Logger.h"
#include "Path.h"
#include "Protocol.h"
+#include "Quality.h"
#include "index/Index.h"
#include "index/Symbol.h"
#include "index/SymbolOrigin.h"
@@ -29,12 +30,14 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
+#include <functional>
#include <future>
namespace clang {
class NamedDecl;
namespace clangd {
struct PreambleData;
+struct CodeCompletion;
struct CodeCompleteOptions {
/// Returns options that can be passed to clang's completion engine.
@@ -129,6 +132,16 @@ struct CodeCompleteOptions {
/// Always use text-based completion.
NeverParse,
} RunParser = ParseIfReady;
+
+ /// Callback invoked on all CompletionCandidate after they are scored and
+ /// before they are ranked (by -Score). Thus the results are yielded in
+ /// arbitrary order.
+ ///
+ /// This callbacks allows capturing various internal structures used by clangd
+ /// during code completion. Eg: Symbol quality and relevance signals.
+ std::function<void(const CodeCompletion &, const SymbolQualitySignals &,
+ const SymbolRelevanceSignals &, float Score)>
+ RecordCCResult;
};
// Semi-structured representation of a code-complete suggestion for our C++ API.
diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index e61dca9cf4fb..0d6e37f00118 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -29,7 +29,9 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <condition_variable>
+#include <functional>
#include <mutex>
+#include <vector>
namespace clang {
namespace clangd {
@@ -1041,6 +1043,21 @@ template <template <class> class TT> int foo() {
EXPECT_THAT(Completions, Contains(Named("TT")));
}
+TEST(CompletionTest, RecordCCResultCallback) {
+ std::vector<CodeCompletion> RecordedCompletions;
+ CodeCompleteOptions Opts;
+ Opts.RecordCCResult = [&RecordedCompletions](const CodeCompletion &CC,
+ const SymbolQualitySignals &,
+ const SymbolRelevanceSignals &,
+ float Score) {
+ RecordedCompletions.push_back(CC);
+ };
+
+ completions("int xy1, xy2; int a = xy^", /*IndexSymbols=*/{}, Opts);
+ EXPECT_THAT(RecordedCompletions,
+ UnorderedElementsAre(Named("xy1"), Named("xy2")));
+}
+
SignatureHelp signatures(llvm::StringRef Text, Position Point,
std::vector<Symbol> IndexSymbols = {}) {
std::unique_ptr<SymbolIndex> Index;
More information about the cfe-commits
mailing list