File: ClangdLSPServerTests.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (391 lines) | stat: -rw-r--r-- 12,763 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//===-- ClangdLSPServerTests.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "Annotations.h"
#include "ClangdLSPServer.h"
#include "LSPClient.h"
#include "Protocol.h"
#include "TestFS.h"
#include "support/Logger.h"
#include "support/TestTracer.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/JSON.h"
#include "llvm/Testing/Support/Error.h"
#include "llvm/Testing/Support/SupportHelpers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {
using llvm::Succeeded;
using testing::ElementsAre;

MATCHER_P(diagMessage, M, "") {
  if (const auto *O = arg.getAsObject()) {
    if (const auto Msg = O->getString("message"))
      return *Msg == M;
  }
  return false;
}

class LSPTest : public ::testing::Test {
protected:
  LSPTest() : LogSession(L) {
    ClangdServer::Options &Base = Opts;
    Base = ClangdServer::optsForTest();
    // This is needed to we can test index-based operations like call hierarchy.
    Base.BuildDynamicSymbolIndex = true;
    Base.FeatureModules = &FeatureModules;
  }

  LSPClient &start() {
    EXPECT_FALSE(Server.hasValue()) << "Already initialized";
    Server.emplace(Client.transport(), FS, Opts);
    ServerThread.emplace([&] { EXPECT_TRUE(Server->run()); });
    Client.call("initialize", llvm::json::Object{});
    return Client;
  }

  void stop() {
    assert(Server);
    Client.call("shutdown", nullptr);
    Client.notify("exit", nullptr);
    Client.stop();
    ServerThread->join();
    Server.reset();
    ServerThread.reset();
  }

  ~LSPTest() {
    if (Server)
      stop();
  }

  MockFS FS;
  ClangdLSPServer::Options Opts;
  FeatureModuleSet FeatureModules;

private:
  class Logger : public clang::clangd::Logger {
    // Color logs so we can distinguish them from test output.
    void log(Level L, const char *Fmt,
             const llvm::formatv_object_base &Message) override {
      raw_ostream::Colors Color;
      switch (L) {
      case Level::Verbose:
        Color = raw_ostream::BLUE;
        break;
      case Level::Error:
        Color = raw_ostream::RED;
        break;
      default:
        Color = raw_ostream::YELLOW;
        break;
      }
      std::lock_guard<std::mutex> Lock(LogMu);
      (llvm::outs().changeColor(Color) << Message << "\n").resetColor();
    }
    std::mutex LogMu;
  };

  Logger L;
  LoggingSession LogSession;
  llvm::Optional<ClangdLSPServer> Server;
  llvm::Optional<std::thread> ServerThread;
  LSPClient Client;
};

TEST_F(LSPTest, GoToDefinition) {
  Annotations Code(R"cpp(
    int [[fib]](int n) {
      return n >= 2 ? ^fib(n - 1) + fib(n - 2) : 1;
    }
  )cpp");
  auto &Client = start();
  Client.didOpen("foo.cpp", Code.code());
  auto &Def = Client.call("textDocument/definition",
                          llvm::json::Object{
                              {"textDocument", Client.documentID("foo.cpp")},
                              {"position", Code.point()},
                          });
  llvm::json::Value Want = llvm::json::Array{llvm::json::Object{
      {"uri", Client.uri("foo.cpp")}, {"range", Code.range()}}};
  EXPECT_EQ(Def.takeValue(), Want);
}

TEST_F(LSPTest, Diagnostics) {
  auto &Client = start();
  Client.didOpen("foo.cpp", "void main(int, char**);");
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("'main' must return 'int' (fix available)"))));

  Client.didChange("foo.cpp", "int x = \"42\";");
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("Cannot initialize a variable of type 'int' with "
                              "an lvalue of type 'const char[3]'"))));

  Client.didClose("foo.cpp");
  EXPECT_THAT(Client.diagnostics("foo.cpp"), llvm::ValueIs(testing::IsEmpty()));
}

TEST_F(LSPTest, DiagnosticsHeaderSaved) {
  auto &Client = start();
  Client.didOpen("foo.cpp", R"cpp(
    #include "foo.h"
    int x = VAR;
  )cpp");
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("'foo.h' file not found"),
                  diagMessage("Use of undeclared identifier 'VAR'"))));
  // Now create the header.
  FS.Files["foo.h"] = "#define VAR original";
  Client.notify(
      "textDocument/didSave",
      llvm::json::Object{{"textDocument", Client.documentID("foo.h")}});
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("Use of undeclared identifier 'original'"))));
  // Now modify the header from within the "editor".
  FS.Files["foo.h"] = "#define VAR changed";
  Client.notify(
      "textDocument/didSave",
      llvm::json::Object{{"textDocument", Client.documentID("foo.h")}});
  // Foo.cpp should be rebuilt with new diagnostics.
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("Use of undeclared identifier 'changed'"))));
}

TEST_F(LSPTest, RecordsLatencies) {
  trace::TestTracer Tracer;
  auto &Client = start();
  llvm::StringLiteral MethodName = "method_name";
  EXPECT_THAT(Tracer.takeMetric("lsp_latency", MethodName), testing::SizeIs(0));
  llvm::consumeError(Client.call(MethodName, {}).take().takeError());
  stop();
  EXPECT_THAT(Tracer.takeMetric("lsp_latency", MethodName), testing::SizeIs(1));
}

TEST_F(LSPTest, IncomingCalls) {
  Annotations Code(R"cpp(
    void calle^e(int);
    void caller1() {
      [[callee]](42);
    }
  )cpp");
  auto &Client = start();
  Client.didOpen("foo.cpp", Code.code());
  auto Items = Client
                   .call("textDocument/prepareCallHierarchy",
                         llvm::json::Object{
                             {"textDocument", Client.documentID("foo.cpp")},
                             {"position", Code.point()}})
                   .takeValue();
  auto FirstItem = (*Items.getAsArray())[0];
  auto Calls = Client
                   .call("callHierarchy/incomingCalls",
                         llvm::json::Object{{"item", FirstItem}})
                   .takeValue();
  auto FirstCall = *(*Calls.getAsArray())[0].getAsObject();
  EXPECT_EQ(FirstCall["fromRanges"], llvm::json::Value{Code.range()});
  auto From = *FirstCall["from"].getAsObject();
  EXPECT_EQ(From["name"], "caller1");
}

TEST_F(LSPTest, CDBConfigIntegration) {
  auto CfgProvider =
      config::Provider::fromAncestorRelativeYAMLFiles(".clangd", FS);
  Opts.ConfigProvider = CfgProvider.get();

  // Map bar.cpp to a different compilation database which defines FOO->BAR.
  FS.Files[".clangd"] = R"yaml(
If:
  PathMatch: bar.cpp
CompileFlags:
  CompilationDatabase: bar
)yaml";
  FS.Files["bar/compile_flags.txt"] = "-DFOO=BAR";

  auto &Client = start();
  // foo.cpp gets parsed as normal.
  Client.didOpen("foo.cpp", "int x = FOO;");
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("Use of undeclared identifier 'FOO'"))));
  // bar.cpp shows the configured compile command.
  Client.didOpen("bar.cpp", "int x = FOO;");
  EXPECT_THAT(Client.diagnostics("bar.cpp"),
              llvm::ValueIs(testing::ElementsAre(
                  diagMessage("Use of undeclared identifier 'BAR'"))));
}

TEST_F(LSPTest, ModulesTest) {
  class MathModule final : public FeatureModule {
    OutgoingNotification<int> Changed;
    void initializeLSP(LSPBinder &Bind, const llvm::json::Object &ClientCaps,
                       llvm::json::Object &ServerCaps) override {
      Bind.notification("add", this, &MathModule::add);
      Bind.method("get", this, &MathModule::get);
      Changed = Bind.outgoingNotification("changed");
    }

    int Value = 0;

    void add(const int &X) {
      Value += X;
      Changed(Value);
    }
    void get(const std::nullptr_t &, Callback<int> Reply) {
      scheduler().runQuick(
          "get", "",
          [Reply(std::move(Reply)), Value(Value)]() mutable { Reply(Value); });
    }
  };
  FeatureModules.add(std::make_unique<MathModule>());

  auto &Client = start();
  Client.notify("add", 2);
  Client.notify("add", 8);
  EXPECT_EQ(10, Client.call("get", nullptr).takeValue());
  EXPECT_THAT(Client.takeNotifications("changed"),
              ElementsAre(llvm::json::Value(2), llvm::json::Value(10)));
}

// Creates a Callback that writes its received value into an Optional<Expected>.
template <typename T>
llvm::unique_function<void(llvm::Expected<T>)>
capture(llvm::Optional<llvm::Expected<T>> &Out) {
  Out.reset();
  return [&Out](llvm::Expected<T> V) { Out.emplace(std::move(V)); };
}

TEST_F(LSPTest, FeatureModulesThreadingTest) {
  // A feature module that does its work on a background thread, and so
  // exercises the block/shutdown protocol.
  class AsyncCounter final : public FeatureModule {
    bool ShouldStop = false;
    int State = 0;
    std::deque<Callback<int>> Queue; // null = increment, non-null = read.
    std::condition_variable CV;
    std::mutex Mu;
    std::thread Thread;

    void run() {
      std::unique_lock<std::mutex> Lock(Mu);
      while (true) {
        CV.wait(Lock, [&] { return ShouldStop || !Queue.empty(); });
        if (ShouldStop) {
          Queue.clear();
          CV.notify_all();
          return;
        }
        Callback<int> &Task = Queue.front();
        if (Task)
          Task(State);
        else
          ++State;
        Queue.pop_front();
        CV.notify_all();
      }
    }

    bool blockUntilIdle(Deadline D) override {
      std::unique_lock<std::mutex> Lock(Mu);
      return clangd::wait(Lock, CV, D, [this] { return Queue.empty(); });
    }

    void stop() override {
      {
        std::lock_guard<std::mutex> Lock(Mu);
        ShouldStop = true;
      }
      CV.notify_all();
    }

  public:
    AsyncCounter() : Thread([this] { run(); }) {}
    ~AsyncCounter() {
      // Verify shutdown sequence was performed.
      // Real modules would not do this, to be robust to no ClangdServer.
      {
        // We still need the lock here, as Queue might be empty when
        // ClangdServer calls blockUntilIdle, but run() might not have returned
        // yet.
        std::lock_guard<std::mutex> Lock(Mu);
        EXPECT_TRUE(ShouldStop) << "ClangdServer should request shutdown";
        EXPECT_EQ(Queue.size(), 0u) << "ClangdServer should block until idle";
      }
      Thread.join();
    }

    void initializeLSP(LSPBinder &Bind, const llvm::json::Object &ClientCaps,
                       llvm::json::Object &ServerCaps) override {
      Bind.notification("increment", this, &AsyncCounter::increment);
    }

    // Get the current value, bypassing the queue.
    // Used to verify that sync->blockUntilIdle avoids races in tests.
    int getSync() {
      std::lock_guard<std::mutex> Lock(Mu);
      return State;
    }

    // Increment the current value asynchronously.
    void increment(const std::nullptr_t &) {
      {
        std::lock_guard<std::mutex> Lock(Mu);
        Queue.push_back(nullptr);
      }
      CV.notify_all();
    }
  };

  FeatureModules.add(std::make_unique<AsyncCounter>());
  auto &Client = start();

  Client.notify("increment", nullptr);
  Client.notify("increment", nullptr);
  Client.notify("increment", nullptr);
  EXPECT_THAT_EXPECTED(Client.call("sync", nullptr).take(), Succeeded());
  EXPECT_EQ(3, FeatureModules.get<AsyncCounter>()->getSync());
  // Throw some work on the queue to make sure shutdown blocks on it.
  Client.notify("increment", nullptr);
  Client.notify("increment", nullptr);
  Client.notify("increment", nullptr);
  // And immediately shut down. FeatureModule destructor verifies we blocked.
}

TEST_F(LSPTest, DiagModuleTest) {
  static constexpr llvm::StringLiteral DiagMsg = "DiagMsg";
  class DiagModule final : public FeatureModule {
    struct DiagHooks : public ASTListener {
      void sawDiagnostic(const clang::Diagnostic &, clangd::Diag &D) override {
        D.Message = DiagMsg.str();
      }
    };

  public:
    std::unique_ptr<ASTListener> astListeners() override {
      return std::make_unique<DiagHooks>();
    }
  };
  FeatureModules.add(std::make_unique<DiagModule>());

  auto &Client = start();
  Client.didOpen("foo.cpp", "test;");
  EXPECT_THAT(Client.diagnostics("foo.cpp"),
              llvm::ValueIs(testing::ElementsAre(diagMessage(DiagMsg))));
}
} // namespace
} // namespace clangd
} // namespace clang