blob: 89e935a6f1a6d724546d51567c2af18a79fa12f7 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- ClangdServer.cpp - Main clangd server code --------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===-------------------------------------------------------------------===//
9
10#include "ClangdServer.h"
11#include "clang/Frontend/ASTUnit.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/CompilerInvocation.h"
14#include "clang/Tooling/CompilationDatabase.h"
15#include "llvm/Support/FileSystem.h"
16
17using namespace clang::clangd;
18
19WorkerRequest::WorkerRequest(WorkerRequestKind Kind, Path File,
20 DocVersion Version)
21 : Kind(Kind), File(File), Version(Version) {}
22
23ClangdScheduler::ClangdScheduler(ClangdServer &Server, bool RunSynchronously)
24 : RunSynchronously(RunSynchronously) {
25 if (RunSynchronously) {
26 // Don't start the worker thread if we're running synchronously
27 return;
28 }
29
30 // Initialize Worker in ctor body, rather than init list to avoid potentially
31 // using not-yet-initialized members
32 Worker = std::thread([&Server, this]() {
33 while (true) {
34 WorkerRequest Request;
35
36 // Pick request from the queue
37 {
38 std::unique_lock<std::mutex> Lock(Mutex);
39 // Wait for more requests.
40 RequestCV.wait(Lock, [this] { return !RequestQueue.empty() || Done; });
41 if (Done)
42 return;
43
44 assert(!RequestQueue.empty() && "RequestQueue was empty");
45
46 Request = std::move(RequestQueue.back());
47 RequestQueue.pop_back();
48
49 // Skip outdated requests
50 if (Request.Version != Server.DraftMgr.getVersion(Request.File)) {
51 // FIXME(ibiryukov): Logging
52 // Output.log("Version for " + Twine(Request.File) +
53 // " in request is outdated, skipping request\n");
54 continue;
55 }
56 } // unlock Mutex
57
58 Server.handleRequest(std::move(Request));
59 }
60 });
61}
62
63ClangdScheduler::~ClangdScheduler() {
64 if (RunSynchronously)
65 return; // no worker thread is running in that case
66
67 {
68 std::lock_guard<std::mutex> Lock(Mutex);
69 // Wake up the worker thread
70 Done = true;
71 RequestCV.notify_one();
72 } // unlock Mutex
73 Worker.join();
74}
75
76void ClangdScheduler::enqueue(ClangdServer &Server, WorkerRequest Request) {
77 if (RunSynchronously) {
78 Server.handleRequest(Request);
79 return;
80 }
81
82 std::lock_guard<std::mutex> Lock(Mutex);
83 RequestQueue.push_back(Request);
84 RequestCV.notify_one();
85}
86
87ClangdServer::ClangdServer(std::unique_ptr<GlobalCompilationDatabase> CDB,
88 std::unique_ptr<DiagnosticsConsumer> DiagConsumer,
89 bool RunSynchronously)
90 : CDB(std::move(CDB)), DiagConsumer(std::move(DiagConsumer)),
91 PCHs(std::make_shared<PCHContainerOperations>()),
92 WorkScheduler(*this, RunSynchronously) {}
93
94void ClangdServer::addDocument(PathRef File, StringRef Contents) {
95 DocVersion NewVersion = DraftMgr.updateDraft(File, Contents);
96 WorkScheduler.enqueue(
97 *this, WorkerRequest(WorkerRequestKind::ParseAndPublishDiagnostics, File,
98 NewVersion));
99}
100
101void ClangdServer::removeDocument(PathRef File) {
102 auto NewVersion = DraftMgr.removeDraft(File);
103 WorkScheduler.enqueue(
104 *this, WorkerRequest(WorkerRequestKind::RemoveDocData, File, NewVersion));
105}
106
107std::vector<CompletionItem> ClangdServer::codeComplete(PathRef File,
108 Position Pos) {
109 auto FileContents = DraftMgr.getDraft(File);
110 assert(FileContents.Draft && "codeComplete is called for non-added document");
111
112 std::vector<CompletionItem> Result;
113 Units.runOnUnitWithoutReparse(
114 File, *FileContents.Draft, *CDB, PCHs, [&](ClangdUnit &Unit) {
115 Result = Unit.codeComplete(*FileContents.Draft, Pos);
116 });
117 return Result;
118}
119
120std::string ClangdServer::getDocument(PathRef File) {
121 auto draft = DraftMgr.getDraft(File);
122 assert(draft.Draft && "File is not tracked, cannot get contents");
123 return *draft.Draft;
124}
125
126void ClangdServer::handleRequest(WorkerRequest Request) {
127 switch (Request.Kind) {
128 case WorkerRequestKind::ParseAndPublishDiagnostics: {
129 auto FileContents = DraftMgr.getDraft(Request.File);
130 if (FileContents.Version != Request.Version)
131 return; // This request is outdated, do nothing
132
133 assert(FileContents.Draft &&
134 "No contents inside a file that was scheduled for reparse");
135 Units.runOnUnit(Request.File, *FileContents.Draft, *CDB, PCHs,
136 [&](ClangdUnit const &Unit) {
137 DiagConsumer->onDiagnosticsReady(
138 Request.File, Unit.getLocalDiagnostics());
139 });
140 break;
141 }
142 case WorkerRequestKind::RemoveDocData:
143 if (Request.Version != DraftMgr.getVersion(Request.File))
144 return; // This request is outdated, do nothing
145
146 Units.removeUnitIfPresent(Request.File);
147 break;
148 }
149}