clang-tools 24.0.0git
ClangdLSPServerTests.cpp
Go to the documentation of this file.
1//===-- ClangdLSPServerTests.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Annotations.h"
11#include "ClangdLSPServer.h"
12#include "ClangdServer.h"
13#include "ConfigProvider.h"
14#include "Diagnostics.h"
15#include "Feature.h"
16#include "FeatureModule.h"
17#include "LSPBinder.h"
18#include "LSPClient.h"
19#include "TestFS.h"
20#include "support/Function.h"
21#include "support/Logger.h"
22#include "support/TestTracer.h"
23#include "support/Threading.h"
24#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/LLVM.h"
26#include "llvm/ADT/FunctionExtras.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/Support/Error.h"
29#include "llvm/Support/FormatVariadic.h"
30#include "llvm/Support/JSON.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Testing/Support/Error.h"
33#include "llvm/Testing/Support/SupportHelpers.h"
34#include "gmock/gmock.h"
35#include "gtest/gtest.h"
36#include <cassert>
37#include <condition_variable>
38#include <cstddef>
39#include <deque>
40#include <memory>
41#include <mutex>
42#include <optional>
43#include <thread>
44#include <utility>
45
46namespace clang {
47namespace clangd {
48namespace {
49using testing::ElementsAre;
50
51MATCHER_P(diagMessage, M, "") {
52 if (const auto *O = arg.getAsObject()) {
53 if (const auto Msg = O->getString("message"))
54 return *Msg == M;
55 }
56 return false;
57}
58
59class LSPTest : public ::testing::Test {
60protected:
61 LSPTest() : LogSession(L) {
62 ClangdServer::Options &Base = Opts;
64 // This is needed to we can test index-based operations like call hierarchy.
65 Base.BuildDynamicSymbolIndex = true;
66 Base.FeatureModules = &FeatureModules;
67 }
68
69 LSPClient &start() {
70 EXPECT_FALSE(Server) << "Already initialized";
71 Server.emplace(Client.transport(), FS, std::move(Opts));
72 ServerThread.emplace([&] { EXPECT_TRUE(Server->run()); });
73 Client.call("initialize", llvm::json::Object{});
74 return Client;
75 }
76
77 void stop() {
78 assert(Server);
79 Client.call("shutdown", nullptr);
80 Client.notify("exit", nullptr);
81 Client.stop();
82 ServerThread->join();
83 Server.reset();
84 ServerThread.reset();
85 }
86
87 ~LSPTest() {
88 if (Server)
89 stop();
90 }
91
92 MockFS FS;
93 ClangdLSPServer::Options Opts;
94 FeatureModuleSet FeatureModules;
95
96private:
97 class Logger : public clang::clangd::Logger {
98 // Color logs so we can distinguish them from test output.
99 void log(Level L, const char *Fmt,
100 const llvm::formatv_object_base &Message) override {
101 raw_ostream::Colors Color;
102 switch (L) {
103 case Level::Verbose:
104 Color = raw_ostream::BLUE;
105 break;
106 case Level::Error:
107 Color = raw_ostream::RED;
108 break;
109 default:
110 Color = raw_ostream::YELLOW;
111 break;
112 }
113 std::lock_guard<std::mutex> Lock(LogMu);
114 (llvm::outs().changeColor(Color) << Message << "\n").resetColor();
115 }
116 std::mutex LogMu;
117 };
118
119 Logger L;
120 LoggingSession LogSession;
121 std::optional<ClangdLSPServer> Server;
122 std::optional<std::thread> ServerThread;
123 LSPClient Client;
124};
125
126TEST_F(LSPTest, GoToDefinition) {
127 Annotations Code(R"cpp(
128 int [[fib]](int n) {
129 return n >= 2 ? ^fib(n - 1) + fib(n - 2) : 1;
130 }
131 )cpp");
132 auto &Client = start();
133 Client.didOpen("foo.cpp", Code.code());
134 auto &Def = Client.call("textDocument/definition",
135 llvm::json::Object{
136 {"textDocument", Client.documentID("foo.cpp")},
137 {"position", Code.point()},
138 });
139 llvm::json::Value Want = llvm::json::Array{llvm::json::Object{
140 {"uri", Client.uri("foo.cpp")}, {"range", Code.range()}}};
141 EXPECT_EQ(Def.takeValue(), Want);
142}
143
144TEST_F(LSPTest, Diagnostics) {
145 auto &Client = start();
146 Client.didOpen("foo.cpp", "void main(int, char**);");
147 EXPECT_THAT(Client.diagnostics("foo.cpp"),
148 llvm::ValueIs(testing::ElementsAre(
149 diagMessage("'main' must return 'int' (fix available)"))));
150
151 Client.didChange("foo.cpp", "int x = \"42\";");
152 EXPECT_THAT(Client.diagnostics("foo.cpp"),
153 llvm::ValueIs(testing::ElementsAre(
154 diagMessage("Cannot initialize a variable of type 'int' with "
155 "an lvalue of type 'const char[3]'"))));
156
157 Client.didClose("foo.cpp");
158 EXPECT_THAT(Client.diagnostics("foo.cpp"), llvm::ValueIs(testing::IsEmpty()));
159}
160
161TEST_F(LSPTest, DiagnosticsHeaderSaved) {
162 auto &Client = start();
163 Client.didOpen("foo.cpp", R"cpp(
164 #include "foo.h"
165 int x = VAR;
166 )cpp");
167 EXPECT_THAT(Client.diagnostics("foo.cpp"),
168 llvm::ValueIs(testing::ElementsAre(
169 diagMessage("'foo.h' file not found"),
170 diagMessage("Use of undeclared identifier 'VAR'"))));
171 // Now create the header.
172 FS.Files["foo.h"] = "#define VAR original";
173 Client.notify(
174 "textDocument/didSave",
175 llvm::json::Object{{"textDocument", Client.documentID("foo.h")}});
176 EXPECT_THAT(Client.diagnostics("foo.cpp"),
177 llvm::ValueIs(testing::ElementsAre(
178 diagMessage("Use of undeclared identifier 'original'"))));
179 // Now modify the header from within the "editor".
180 FS.Files["foo.h"] = "#define VAR changed";
181 Client.notify(
182 "textDocument/didSave",
183 llvm::json::Object{{"textDocument", Client.documentID("foo.h")}});
184 // Foo.cpp should be rebuilt with new diagnostics.
185 EXPECT_THAT(Client.diagnostics("foo.cpp"),
186 llvm::ValueIs(testing::ElementsAre(
187 diagMessage("Use of undeclared identifier 'changed'"))));
188}
189
190TEST_F(LSPTest, RecordsLatencies) {
191 trace::TestTracer Tracer;
192 auto &Client = start();
193 llvm::StringLiteral MethodName = "method_name";
194 EXPECT_THAT(Tracer.takeMetric("lsp_latency", MethodName), testing::SizeIs(0));
195 llvm::consumeError(Client.call(MethodName, {}).take().takeError());
196 stop();
197 EXPECT_THAT(Tracer.takeMetric("lsp_latency", MethodName), testing::SizeIs(1));
198}
199
200// clang-tidy's renames are converted to clangd's internal rename functionality,
201// see clangd#1589 and clangd#741
202TEST_F(LSPTest, ClangTidyRename) {
203 // This test requires clang-tidy checks to be linked in.
204 if (!CLANGD_TIDY_CHECKS)
205 return;
206 Annotations Header(R"cpp(
207 void [[foo]]();
208 )cpp");
209 Annotations Source(R"cpp(
210 void [[foo]]() {}
211 )cpp");
212 constexpr auto ClangTidyProvider = [](tidy::ClangTidyOptions &ClangTidyOpts,
213 llvm::StringRef) {
214 ClangTidyOpts.Checks = {"-*,readability-identifier-naming"};
215 ClangTidyOpts.CheckOptions["readability-identifier-naming.FunctionCase"] =
216 "CamelCase";
217 };
218 FeatureModules.add(
219 std::make_unique<ClangTidyFeatureModule>(ClangTidyProvider));
220 auto &Client = start();
221 Client.didOpen("foo.hpp", Header.code());
222 Client.didOpen("foo.cpp", Source.code());
223
224 auto Diags = Client.diagnostics("foo.cpp");
225 ASSERT_TRUE(Diags && !Diags->empty());
226 auto RenameDiag = Diags->front();
227
228 auto RenameCommand =
229 (*Client
230 .call("textDocument/codeAction",
231 llvm::json::Object{
232 {"textDocument", Client.documentID("foo.cpp")},
233 {"context",
234 llvm::json::Object{
235 {"diagnostics", llvm::json::Array{RenameDiag}}}},
236 {"range", Source.range()}})
237 .takeValue()
238 .getAsArray())[0];
239
240 ASSERT_EQ((*RenameCommand.getAsObject())["title"],
241 "Apply fix: change 'foo' to 'Foo'");
242
243 Client.expectServerCall("workspace/applyEdit");
244 Client.call("workspace/executeCommand", RenameCommand);
245 Client.sync();
246
247 auto Params = Client.takeCallParams("workspace/applyEdit");
248 auto Uri = [&](llvm::StringRef Path) {
249 return Client.uri(Path).getAsString().value().str();
250 };
251 llvm::json::Object ExpectedEdit = llvm::json::Object{
252 {"edit", llvm::json::Object{
253 {"changes",
254 llvm::json::Object{
255 {Uri("foo.hpp"), llvm::json::Array{llvm::json::Object{
256 {"range", Header.range()},
257 {"newText", "Foo"},
258 }}},
259
260 {Uri("foo.cpp"), llvm::json::Array{llvm::json::Object{
261 {"range", Source.range()},
262 {"newText", "Foo"},
263 }}}
264
265 }}}}};
266 EXPECT_EQ(Params, std::vector{llvm::json::Value(std::move(ExpectedEdit))});
267}
268
269TEST_F(LSPTest, ClangTidyCrash_Issue109367) {
270 // This test requires clang-tidy checks to be linked in.
271 if (!CLANGD_TIDY_CHECKS)
272 return;
273 constexpr auto ClangTidyProvider = [](tidy::ClangTidyOptions &ClangTidyOpts,
274 llvm::StringRef) {
275 ClangTidyOpts.Checks = {"-*,boost-use-ranges"};
276 };
277 FeatureModules.add(
278 std::make_unique<ClangTidyFeatureModule>(ClangTidyProvider));
279 // Check that registering the boost-use-ranges checker's matchers
280 // on two different threads does not cause a crash.
281 auto &Client = start();
282 Client.didOpen("a.cpp", "");
283 Client.didOpen("b.cpp", "");
284 Client.sync();
285}
286
287TEST_F(LSPTest, IncomingCalls) {
288 Annotations Code(R"cpp(
289 void calle^e(int);
290 void caller1() {
291 [[callee]](42);
292 }
293 )cpp");
294 auto &Client = start();
295 Client.didOpen("foo.cpp", Code.code());
296 auto Items = Client
297 .call("textDocument/prepareCallHierarchy",
298 llvm::json::Object{
299 {"textDocument", Client.documentID("foo.cpp")},
300 {"position", Code.point()}})
301 .takeValue();
302 auto FirstItem = (*Items.getAsArray())[0];
303 auto Calls = Client
304 .call("callHierarchy/incomingCalls",
305 llvm::json::Object{{"item", FirstItem}})
306 .takeValue();
307 auto FirstCall = *(*Calls.getAsArray())[0].getAsObject();
308 EXPECT_EQ(FirstCall["fromRanges"], llvm::json::Value{Code.range()});
309 auto From = *FirstCall["from"].getAsObject();
310 EXPECT_EQ(From["name"], "caller1");
311}
312
313TEST_F(LSPTest, CDBConfigIntegration) {
314 Opts.ConfigProvider =
316
317 // Map bar.cpp to a different compilation database which defines FOO->BAR.
318 FS.Files[".clangd"] = R"yaml(
319If:
320 PathMatch: bar.cpp
321CompileFlags:
322 CompilationDatabase: bar
323)yaml";
324 FS.Files["bar/compile_flags.txt"] = "-DFOO=BAR";
325
326 auto &Client = start();
327 // foo.cpp gets parsed as normal.
328 Client.didOpen("foo.cpp", "int x = FOO;");
329 EXPECT_THAT(Client.diagnostics("foo.cpp"),
330 llvm::ValueIs(testing::ElementsAre(
331 diagMessage("Use of undeclared identifier 'FOO'"))));
332 // bar.cpp shows the configured compile command.
333 Client.didOpen("bar.cpp", "int x = FOO;");
334 EXPECT_THAT(Client.diagnostics("bar.cpp"),
335 llvm::ValueIs(testing::ElementsAre(
336 diagMessage("Use of undeclared identifier 'BAR'"))));
337}
338
339TEST_F(LSPTest, ModulesTest) {
340 class MathModule final : public FeatureModule {
341 OutgoingNotification<int> Changed;
342 void initializeLSP(LSPBinder &Bind, const llvm::json::Object &ClientCaps,
343 llvm::json::Object &ServerCaps) override {
344 Bind.notification("add", this, &MathModule::add);
345 Bind.method("get", this, &MathModule::get);
346 Changed = Bind.outgoingNotification("changed");
347 }
348
349 int Value = 0;
350
351 void add(const int &X) {
352 Value += X;
353 Changed(Value);
354 }
355 void get(const std::nullptr_t &, Callback<int> Reply) {
356 scheduler().runQuick(
357 "get", "",
358 [Reply(std::move(Reply)), Value(Value)]() mutable { Reply(Value); });
359 }
360 };
361 FeatureModules.add(std::make_unique<MathModule>());
362
363 auto &Client = start();
364 Client.notify("add", 2);
365 Client.notify("add", 8);
366 EXPECT_EQ(10, Client.call("get", nullptr).takeValue());
367 EXPECT_THAT(Client.takeNotifications("changed"),
368 ElementsAre(llvm::json::Value(2), llvm::json::Value(10)));
369}
370
371TEST_F(LSPTest, FeatureModulesThreadingTest) {
372 // A feature module that does its work on a background thread, and so
373 // exercises the block/shutdown protocol.
374 class AsyncCounter final : public FeatureModule {
375 bool ShouldStop = false;
376 int State = 0;
377 std::deque<Callback<int>> Queue; // null = increment, non-null = read.
378 std::condition_variable CV;
379 std::mutex Mu;
380 std::thread Thread;
381
382 void run() {
383 std::unique_lock<std::mutex> Lock(Mu);
384 while (true) {
385 CV.wait(Lock, [&] { return ShouldStop || !Queue.empty(); });
386 if (ShouldStop) {
387 Queue.clear();
388 CV.notify_all();
389 return;
390 }
391 Callback<int> &Task = Queue.front();
392 if (Task)
393 Task(State);
394 else
395 ++State;
396 Queue.pop_front();
397 CV.notify_all();
398 }
399 }
400
401 bool blockUntilIdle(Deadline D) override {
402 std::unique_lock<std::mutex> Lock(Mu);
403 return clangd::wait(Lock, CV, D, [this] { return Queue.empty(); });
404 }
405
406 void stop() override {
407 {
408 std::lock_guard<std::mutex> Lock(Mu);
409 ShouldStop = true;
410 }
411 CV.notify_all();
412 }
413
414 public:
415 AsyncCounter() : Thread([this] { run(); }) {}
416 ~AsyncCounter() {
417 // Verify shutdown sequence was performed.
418 // Real modules would not do this, to be robust to no ClangdServer.
419 {
420 // We still need the lock here, as Queue might be empty when
421 // ClangdServer calls blockUntilIdle, but run() might not have returned
422 // yet.
423 std::lock_guard<std::mutex> Lock(Mu);
424 EXPECT_TRUE(ShouldStop) << "ClangdServer should request shutdown";
425 EXPECT_EQ(Queue.size(), 0u) << "ClangdServer should block until idle";
426 }
427 Thread.join();
428 }
429
430 void initializeLSP(LSPBinder &Bind, const llvm::json::Object &ClientCaps,
431 llvm::json::Object &ServerCaps) override {
432 Bind.notification("increment", this, &AsyncCounter::increment);
433 }
434
435 // Get the current value, bypassing the queue.
436 // Used to verify that sync->blockUntilIdle avoids races in tests.
437 int getSync() {
438 std::lock_guard<std::mutex> Lock(Mu);
439 return State;
440 }
441
442 // Increment the current value asynchronously.
443 void increment(const std::nullptr_t &) {
444 {
445 std::lock_guard<std::mutex> Lock(Mu);
446 Queue.push_back(nullptr);
447 }
448 CV.notify_all();
449 }
450 };
451
452 FeatureModules.add(std::make_unique<AsyncCounter>());
453 auto &Client = start();
454
455 Client.notify("increment", nullptr);
456 Client.notify("increment", nullptr);
457 Client.notify("increment", nullptr);
458 Client.sync();
459 EXPECT_EQ(3, FeatureModules.get<AsyncCounter>()->getSync());
460 // Throw some work on the queue to make sure shutdown blocks on it.
461 Client.notify("increment", nullptr);
462 Client.notify("increment", nullptr);
463 Client.notify("increment", nullptr);
464 // And immediately shut down. FeatureModule destructor verifies we blocked.
465}
466
467TEST_F(LSPTest, DiagModuleTest) {
468 static constexpr llvm::StringLiteral DiagMsg = "DiagMsg";
469 class DiagModule final : public FeatureModule {
470 struct DiagHooks : public ASTListener {
471 void sawDiagnostic(const clang::Diagnostic &, clangd::Diag &D) override {
472 D.Message = DiagMsg.str();
473 }
474 };
475
476 public:
477 std::unique_ptr<ASTListener> astListeners() override {
478 return std::make_unique<DiagHooks>();
479 }
480 };
481 FeatureModules.add(std::make_unique<DiagModule>());
482
483 auto &Client = start();
484 Client.didOpen("foo.cpp", "test;");
485 EXPECT_THAT(Client.diagnostics("foo.cpp"),
486 llvm::ValueIs(testing::ElementsAre(diagMessage(DiagMsg))));
487}
488
489// Regression test for https://github.com/llvm/llvm-project/issues/196072.
490TEST_F(LSPTest, CompletionOutOfRangePosition) {
491 auto &Client = start();
492 Client.didOpen("foo.cpp", "int x;");
493 auto &Reply = Client.call(
494 "textDocument/completion",
495 llvm::json::Object{
496 {"textDocument", Client.documentID("foo.cpp")},
497 {"position", llvm::json::Object{{"line", 97}, {"character", 0}}},
498 {"context",
499 llvm::json::Object{
500 {"triggerKind", 2},
501 {"triggerCharacter", ">"},
502 }},
503 });
504 auto Result = Reply.take();
505 ASSERT_TRUE(!!Result) << "Expected a response, not a server crash";
506}
507
508// https://github.com/llvm/llvm-project/issues/196225
509TEST_F(LSPTest, ShutdownDuringRename) {
510 Annotations Code("void ^foo();");
511 auto &Client = start();
512 Client.didOpen("foo.cpp", Code.code());
513 auto &Reply = Client.call("textDocument/rename",
514 llvm::json::Object{
515 {"textDocument", Client.documentID("foo.cpp")},
516 {"position", Code.point()},
517 {"newName", "bar"},
518 });
519 stop();
520 auto Result = Reply.take();
521 ASSERT_TRUE(!!Result) << "Expected a response, not a server crash";
522}
523
524} // namespace
525} // namespace clangd
526} // namespace clang
static clang::FrontendPluginRegistry::Add< clang::tidy::ClangTidyPluginAction > X("clang-tidy", "clang-tidy")
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
Definition Annotations.h:23
A FeatureModule contributes a vertical feature to clangd.
static std::unique_ptr< Provider > fromAncestorRelativeYAMLFiles(llvm::StringRef RelPath, const ThreadsafeFS &, bool Trusted=false)
A RAII Tracer that can be used by tests.
Definition TestTracer.h:28
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
@ Changed
The file got changed.
Definition Protocol.h:833
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
MATCHER_P(named, N, "")
void log(const char *Fmt, Ts &&... Vals)
Definition Logger.h:67
std::string Path
A typedef to represent a file path.
Definition Path.h:26
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//