Skip to content

Commit 67de4af

Browse files
committed
Merge branch 'feature/merge-upstream-20201102' into develop
2 parents 298bea8 + 474f12d commit 67de4af

File tree

1,103 files changed

+36312
-23422
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,103 files changed

+36312
-23422
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class ErrorReporter {
110110
DiagOpts->ShowColors = Context.getOptions().UseColor.getValueOr(
111111
llvm::sys::Process::StandardOutHasColors());
112112
DiagPrinter->BeginSourceFile(LangOpts);
113+
if (DiagOpts->ShowColors && !llvm::sys::Process::StandardOutIsDisplayed()) {
114+
llvm::sys::Process::UseANSIEscapeCodes(true);
115+
}
113116
}
114117

115118
SourceManager &getSourceManager() { return SourceMgr; }

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -993,12 +993,24 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
993993
if (!Code)
994994
return Reply(llvm::make_error<LSPError>(
995995
"onCodeAction called for non-added file", ErrorCode::InvalidParams));
996+
997+
// Checks whether a particular CodeActionKind is included in the response.
998+
auto KindAllowed = [Only(Params.context.only)](llvm::StringRef Kind) {
999+
if (Only.empty())
1000+
return true;
1001+
return llvm::any_of(Only, [&](llvm::StringRef Base) {
1002+
return Kind.consume_front(Base) && (Kind.empty() || Kind.startswith("."));
1003+
});
1004+
};
1005+
9961006
// We provide a code action for Fixes on the specified diagnostics.
9971007
std::vector<CodeAction> FixIts;
998-
for (const Diagnostic &D : Params.context.diagnostics) {
999-
for (auto &F : getFixes(File.file(), D)) {
1000-
FixIts.push_back(toCodeAction(F, Params.textDocument.uri));
1001-
FixIts.back().diagnostics = {D};
1008+
if (KindAllowed(CodeAction::QUICKFIX_KIND)) {
1009+
for (const Diagnostic &D : Params.context.diagnostics) {
1010+
for (auto &F : getFixes(File.file(), D)) {
1011+
FixIts.push_back(toCodeAction(F, Params.textDocument.uri));
1012+
FixIts.back().diagnostics = {D};
1013+
}
10021014
}
10031015
}
10041016

@@ -1038,14 +1050,10 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10381050
}
10391051
return Reply(llvm::json::Array(Commands));
10401052
};
1041-
10421053
Server->enumerateTweaks(
10431054
File.file(), Params.range,
1044-
[&](const Tweak &T) {
1045-
if (!Opts.TweakFilter(T))
1046-
return false;
1047-
// FIXME: also consider CodeActionContext.only
1048-
return true;
1055+
[this, KindAllowed(std::move(KindAllowed))](const Tweak &T) {
1056+
return Opts.TweakFilter(T) && KindAllowed(T.kind());
10491057
},
10501058
std::move(ConsumeActions));
10511059
}

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,19 +1644,10 @@ class CodeCompleteFlow {
16441644
return Scores;
16451645

16461646
case RM::DecisionForest:
1647-
Scores.Quality = 0;
1648-
Scores.Relevance = 0;
1649-
// Exponentiating DecisionForest prediction makes the score of each tree a
1650-
// multiplciative boost (like NameMatch). This allows us to weigh the
1651-
// prediciton score and NameMatch appropriately.
1652-
Scores.ExcludingName = pow(Opts.DecisionForestBase,
1653-
evaluateDecisionForest(Quality, Relevance));
1654-
// NeedsFixIts is not part of the DecisionForest as generating training
1655-
// data that needs fixits is not-feasible.
1656-
if (Relevance.NeedsFixIts)
1657-
Scores.ExcludingName *= 0.5;
1658-
// NameMatch should be a multiplier on total score to support rescoring.
1659-
Scores.Total = Relevance.NameMatch * Scores.ExcludingName;
1647+
DecisionForestScores DFScores = Opts.DecisionForestScorer(
1648+
Quality, Relevance, Opts.DecisionForestBase);
1649+
Scores.ExcludingName = DFScores.ExcludingName;
1650+
Scores.Total = DFScores.Total;
16601651
return Scores;
16611652
}
16621653
llvm_unreachable("Unhandled CodeCompletion ranking model.");

clang-tools-extra/clangd/CodeComplete.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,21 @@ struct CodeCompleteOptions {
154154
DecisionForest,
155155
} RankingModel = Heuristics;
156156

157+
/// Callback used to score a CompletionCandidate if DecisionForest ranking
158+
/// model is enabled.
159+
/// This allows us to inject experimental models and compare them with
160+
/// baseline model using A/B testing.
161+
std::function<DecisionForestScores(
162+
const SymbolQualitySignals &, const SymbolRelevanceSignals &, float Base)>
163+
DecisionForestScorer = &evaluateDecisionForest;
157164
/// Weight for combining NameMatch and Prediction of DecisionForest.
158165
/// CompletionScore is NameMatch * pow(Base, Prediction).
159166
/// The optimal value of Base largely depends on the semantics of the model
160167
/// and prediction score (e.g. algorithm used during training, number of
161168
/// trees, etc.). Usually if the range of Prediciton is [-20, 20] then a Base
162169
/// in [1.2, 1.7] works fine.
163-
/// Semantics: E.g. the completion score reduces by 50% if the Prediciton
164-
/// score is reduced by 2.6 points for Base = 1.3.
170+
/// Semantics: E.g. For Base = 1.3, if the Prediciton score reduces by 2.6
171+
/// points then completion score reduces by 50% or 1.3^(-2.6).
165172
float DecisionForestBase = 1.3f;
166173
};
167174

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,10 @@ llvm::json::Value toJSON(const PublishDiagnosticsParams &PDP) {
599599
bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R,
600600
llvm::json::Path P) {
601601
llvm::json::ObjectMapper O(Params, P);
602-
return O && O.map("diagnostics", R.diagnostics);
602+
if (!O || !O.map("diagnostics", R.diagnostics))
603+
return false;
604+
O.map("only", R.only);
605+
return true;
603606
}
604607

605608
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {

clang-tools-extra/clangd/Protocol.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,19 @@ struct PublishDiagnosticsParams {
863863
llvm::json::Value toJSON(const PublishDiagnosticsParams &);
864864

865865
struct CodeActionContext {
866-
/// An array of diagnostics.
866+
/// An array of diagnostics known on the client side overlapping the range
867+
/// provided to the `textDocument/codeAction` request. They are provided so
868+
/// that the server knows which errors are currently presented to the user for
869+
/// the given range. There is no guarantee that these accurately reflect the
870+
/// error state of the resource. The primary parameter to compute code actions
871+
/// is the provided range.
867872
std::vector<Diagnostic> diagnostics;
873+
874+
/// Requested kind of actions to return.
875+
///
876+
/// Actions not of this kind are filtered out by the client before being
877+
/// shown. So servers can omit computing them.
878+
std::vector<std::string> only;
868879
};
869880
bool fromJSON(const llvm::json::Value &, CodeActionContext &, llvm::json::Path);
870881

clang-tools-extra/clangd/Quality.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,9 @@ float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
487487
return SymbolQuality * SymbolRelevance;
488488
}
489489

490-
float evaluateDecisionForest(const SymbolQualitySignals &Quality,
491-
const SymbolRelevanceSignals &Relevance) {
490+
DecisionForestScores
491+
evaluateDecisionForest(const SymbolQualitySignals &Quality,
492+
const SymbolRelevanceSignals &Relevance, float Base) {
492493
Example E;
493494
E.setIsDeprecated(Quality.Deprecated);
494495
E.setIsReservedName(Quality.ReservedName);
@@ -512,7 +513,19 @@ float evaluateDecisionForest(const SymbolQualitySignals &Quality,
512513
E.setHadSymbolType(Relevance.HadSymbolType);
513514
E.setTypeMatchesPreferred(Relevance.TypeMatchesPreferred);
514515
E.setFilterLength(Relevance.FilterLength);
515-
return Evaluate(E);
516+
517+
DecisionForestScores Scores;
518+
// Exponentiating DecisionForest prediction makes the score of each tree a
519+
// multiplciative boost (like NameMatch). This allows us to weigh the
520+
// prediciton score and NameMatch appropriately.
521+
Scores.ExcludingName = pow(Base, Evaluate(E));
522+
// NeedsFixIts is not part of the DecisionForest as generating training
523+
// data that needs fixits is not-feasible.
524+
if (Relevance.NeedsFixIts)
525+
Scores.ExcludingName *= 0.5;
526+
// NameMatch should be a multiplier on total score to support rescoring.
527+
Scores.Total = Relevance.NameMatch * Scores.ExcludingName;
528+
return Scores;
516529
}
517530

518531
// Produces an integer that sorts in the same order as F.

clang-tools-extra/clangd/Quality.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,18 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &,
165165
/// Combine symbol quality and relevance into a single score.
166166
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance);
167167

168-
float evaluateDecisionForest(const SymbolQualitySignals &Quality,
169-
const SymbolRelevanceSignals &Relevance);
168+
/// Same semantics as CodeComplete::Score. Quality score and Relevance score
169+
/// have been removed since DecisionForest cannot assign individual scores to
170+
/// Quality and Relevance signals.
171+
struct DecisionForestScores {
172+
float Total = 0.f;
173+
float ExcludingName = 0.f;
174+
};
175+
176+
DecisionForestScores
177+
evaluateDecisionForest(const SymbolQualitySignals &Quality,
178+
const SymbolRelevanceSignals &Relevance, float Base);
179+
170180
/// TopN<T> is a lossy container that preserves only the "best" N elements.
171181
template <typename T, typename Compare = std::greater<T>> class TopN {
172182
public:

clang-tools-extra/clangd/index/remote/CMakeLists.txt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
if (CLANGD_ENABLE_REMOTE)
2-
generate_protos(RemoteIndexServiceProto "Service.proto" GRPC)
32
generate_protos(RemoteIndexProto "Index.proto")
4-
# Ensure dependency headers are generated before dependent protos are built.
5-
# FIXME: this should be encapsulated in generate_protos.
6-
# FIXME: CMake docs say OBJECT_DEPENDS isn't needed, but I can't get the
7-
# recommended add_dependencies() approach to work.
8-
set_source_files_properties(
9-
${CMAKE_CURRENT_BINARY_DIR}/Service.pb.cc
10-
PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Index.pb.h)
3+
generate_protos(RemoteIndexServiceProto "Service.proto"
4+
DEPENDS "Index.proto"
5+
GRPC)
116
include_directories(${CMAKE_CURRENT_BINARY_DIR})
127
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../)
138

clang-tools-extra/clangd/test/code-action-request.test

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,47 @@
5151
# CHECK-NEXT: }
5252
# CHECK-NEXT: ]
5353
---
54+
{
55+
"jsonrpc": "2.0",
56+
"id": 2,
57+
"method": "textDocument/codeAction",
58+
"params": {
59+
"textDocument": { "uri": "test:///main.cpp" },
60+
"range": {
61+
"start": {"line": 0, "character": 0},
62+
"end": {"line": 0, "character": 4}
63+
},
64+
"context": {
65+
"diagnostics": [],
66+
"only": ["quickfix"]
67+
}
68+
}
69+
}
70+
# CHECK: "id": 2,
71+
# CHECK-NEXT: "jsonrpc": "2.0",
72+
# CHECK-NEXT: "result": []
73+
---
74+
{
75+
"jsonrpc": "2.0",
76+
"id": 3,
77+
"method": "textDocument/codeAction",
78+
"params": {
79+
"textDocument": { "uri": "test:///main.cpp" },
80+
"range": {
81+
"start": {"line": 0, "character": 0},
82+
"end": {"line": 0, "character": 4}
83+
},
84+
"context": {
85+
"diagnostics": [],
86+
"only": ["refactor"]
87+
}
88+
}
89+
}
90+
# CHECK: "id": 3,
91+
# CHECK-NEXT: "jsonrpc": "2.0",
92+
# CHECK-NEXT: "result": [
93+
# CHECK-NEXT: {
94+
---
5495
{"jsonrpc":"2.0","id":4,"method":"workspace/executeCommand","params":{"command":"clangd.applyTweak","arguments":[{"file":"test:///main.cpp","selection":{"end":{"character":4,"line":0},"start":{"character":0,"line":0}},"tweakID":"ExpandAutoType"}]}}
5596
# CHECK: "newText": "int",
5697
# CHECK-NEXT: "range": {
@@ -64,7 +105,7 @@
64105
# CHECK-NEXT: }
65106
# CHECK-NEXT: }
66107
---
67-
{"jsonrpc":"2.0","id":4,"method":"shutdown"}
108+
{"jsonrpc":"2.0","id":5,"method":"shutdown"}
68109
---
69110
{"jsonrpc":"2.0","method":"exit"}
70111
---

0 commit comments

Comments
 (0)