/
ClangdServer.cpp
273 lines (231 loc) · 8.86 KB
/
ClangdServer.cpp
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
//===--- ClangdServer.cpp - Main clangd server code --------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===-------------------------------------------------------------------===//
#include "ClangdServer.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <future>
using namespace clang;
using namespace clang::clangd;
namespace {
std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename,
ArrayRef<tooling::Range> Ranges) {
// Call clang-format.
// FIXME: Don't ignore style.
format::FormatStyle Style = format::getLLVMStyle();
auto Result = format::reformat(Style, Code, Ranges, Filename);
return std::vector<tooling::Replacement>(Result.begin(), Result.end());
}
std::string getStandardResourceDir() {
static int Dummy; // Just an address in this process.
return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
}
} // namespace
size_t clangd::positionToOffset(StringRef Code, Position P) {
size_t Offset = 0;
for (int I = 0; I != P.line; ++I) {
// FIXME: \r\n
// FIXME: UTF-8
size_t F = Code.find('\n', Offset);
if (F == StringRef::npos)
return 0; // FIXME: Is this reasonable?
Offset = F + 1;
}
return (Offset == 0 ? 0 : (Offset - 1)) + P.character;
}
/// Turn an offset in Code into a [line, column] pair.
Position clangd::offsetToPosition(StringRef Code, size_t Offset) {
StringRef JustBefore = Code.substr(0, Offset);
// FIXME: \r\n
// FIXME: UTF-8
int Lines = JustBefore.count('\n');
int Cols = JustBefore.size() - JustBefore.rfind('\n') - 1;
return {Lines, Cols};
}
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
return make_tagged(vfs::getRealFileSystem(), VFSTag());
}
ClangdScheduler::ClangdScheduler(bool RunSynchronously)
: RunSynchronously(RunSynchronously) {
if (RunSynchronously) {
// Don't start the worker thread if we're running synchronously
return;
}
// Initialize Worker in ctor body, rather than init list to avoid potentially
// using not-yet-initialized members
Worker = std::thread([this]() {
while (true) {
std::function<void()> Request;
// Pick request from the queue
{
std::unique_lock<std::mutex> Lock(Mutex);
// Wait for more requests.
RequestCV.wait(Lock, [this] { return !RequestQueue.empty() || Done; });
if (Done)
return;
assert(!RequestQueue.empty() && "RequestQueue was empty");
// We process requests starting from the front of the queue. Users of
// ClangdScheduler have a way to prioritise their requests by putting
// them to the either side of the queue (using either addToEnd or
// addToFront).
Request = std::move(RequestQueue.front());
RequestQueue.pop_front();
} // unlock Mutex
Request();
}
});
}
ClangdScheduler::~ClangdScheduler() {
if (RunSynchronously)
return; // no worker thread is running in that case
{
std::lock_guard<std::mutex> Lock(Mutex);
// Wake up the worker thread
Done = true;
} // unlock Mutex
RequestCV.notify_one();
Worker.join();
}
void ClangdScheduler::addToFront(std::function<void()> Request) {
if (RunSynchronously) {
Request();
return;
}
{
std::lock_guard<std::mutex> Lock(Mutex);
RequestQueue.push_front(Request);
}
RequestCV.notify_one();
}
void ClangdScheduler::addToEnd(std::function<void()> Request) {
if (RunSynchronously) {
Request();
return;
}
{
std::lock_guard<std::mutex> Lock(Mutex);
RequestQueue.push_back(Request);
}
RequestCV.notify_one();
}
ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
DiagnosticsConsumer &DiagConsumer,
FileSystemProvider &FSProvider,
bool RunSynchronously,
llvm::Optional<StringRef> ResourceDir)
: CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider),
ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
PCHs(std::make_shared<PCHContainerOperations>()),
WorkScheduler(RunSynchronously) {}
void ClangdServer::addDocument(PathRef File, StringRef Contents) {
DocVersion Version = DraftMgr.updateDraft(File, Contents);
Path FileStr = File;
WorkScheduler.addToFront([this, FileStr, Version]() {
auto FileContents = DraftMgr.getDraft(FileStr);
if (FileContents.Version != Version)
return; // This request is outdated, do nothing
assert(FileContents.Draft &&
"No contents inside a file that was scheduled for reparse");
auto TaggedFS = FSProvider.getTaggedFileSystem(FileStr);
Units.runOnUnit(
FileStr, *FileContents.Draft, ResourceDir, CDB, PCHs, TaggedFS.Value,
[&](ClangdUnit const &Unit) {
DiagConsumer.onDiagnosticsReady(
FileStr, make_tagged(Unit.getLocalDiagnostics(), TaggedFS.Tag));
});
});
}
void ClangdServer::removeDocument(PathRef File) {
auto Version = DraftMgr.removeDraft(File);
Path FileStr = File;
WorkScheduler.addToFront([this, FileStr, Version]() {
if (Version != DraftMgr.getVersion(FileStr))
return; // This request is outdated, do nothing
Units.removeUnitIfPresent(FileStr);
});
}
void ClangdServer::forceReparse(PathRef File) {
// The addDocument schedules the reparse even if the contents of the file
// never changed, so we just call it here.
addDocument(File, getDocument(File));
}
Tagged<std::vector<CompletionItem>>
ClangdServer::codeComplete(PathRef File, Position Pos,
llvm::Optional<StringRef> OverridenContents) {
std::string DraftStorage;
if (!OverridenContents) {
auto FileContents = DraftMgr.getDraft(File);
assert(FileContents.Draft &&
"codeComplete is called for non-added document");
DraftStorage = std::move(*FileContents.Draft);
OverridenContents = DraftStorage;
}
std::vector<CompletionItem> Result;
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
// It would be nice to use runOnUnitWithoutReparse here, but we can't
// guarantee the correctness of code completion cache here if we don't do the
// reparse.
Units.runOnUnit(File, *OverridenContents, ResourceDir, CDB, PCHs,
TaggedFS.Value, [&](ClangdUnit &Unit) {
Result = Unit.codeComplete(*OverridenContents, Pos,
TaggedFS.Value);
});
return make_tagged(std::move(Result), TaggedFS.Tag);
}
std::vector<tooling::Replacement> ClangdServer::formatRange(PathRef File,
Range Rng) {
std::string Code = getDocument(File);
size_t Begin = positionToOffset(Code, Rng.start);
size_t Len = positionToOffset(Code, Rng.end) - Begin;
return formatCode(Code, File, {tooling::Range(Begin, Len)});
}
std::vector<tooling::Replacement> ClangdServer::formatFile(PathRef File) {
// Format everything.
std::string Code = getDocument(File);
return formatCode(Code, File, {tooling::Range(0, Code.size())});
}
std::vector<tooling::Replacement> ClangdServer::formatOnType(PathRef File,
Position Pos) {
// Look for the previous opening brace from the character position and
// format starting from there.
std::string Code = getDocument(File);
size_t CursorPos = positionToOffset(Code, Pos);
size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
if (PreviousLBracePos == StringRef::npos)
PreviousLBracePos = CursorPos;
size_t Len = 1 + CursorPos - PreviousLBracePos;
return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
}
std::string ClangdServer::getDocument(PathRef File) {
auto draft = DraftMgr.getDraft(File);
assert(draft.Draft && "File is not tracked, cannot get contents");
return *draft.Draft;
}
std::string ClangdServer::dumpAST(PathRef File) {
std::promise<std::string> DumpPromise;
auto DumpFuture = DumpPromise.get_future();
auto Version = DraftMgr.getVersion(File);
WorkScheduler.addToEnd([this, &DumpPromise, File, Version]() {
assert(DraftMgr.getVersion(File) == Version && "Version has changed");
Units.runOnExistingUnit(File, [&DumpPromise](ClangdUnit &Unit) {
std::string Result;
llvm::raw_string_ostream ResultOS(Result);
Unit.dumpAST(ResultOS);
ResultOS.flush();
DumpPromise.set_value(std::move(Result));
});
});
return DumpFuture.get();
}