/
ClangdServer.cpp
575 lines (492 loc) · 20.8 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//===--- 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 "CodeComplete.h"
#include "Headers.h"
#include "SourceCode.h"
#include "XRefs.h"
#include "index/Merge.h"
#include "clang/Format/Format.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <future>
using namespace clang;
using namespace clang::clangd;
namespace {
void ignoreError(llvm::Error Err) {
handleAllErrors(std::move(Err), [](const llvm::ErrorInfoBase &) {});
}
std::string getStandardResourceDir() {
static int Dummy; // Just an address in this process.
return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
}
class RefactoringResultCollector final
: public tooling::RefactoringResultConsumer {
public:
void handleError(llvm::Error Err) override {
assert(!Result.hasValue());
// FIXME: figure out a way to return better message for DiagnosticError.
// clangd uses llvm::toString to convert the Err to string, however, for
// DiagnosticError, only "clang diagnostic" will be generated.
Result = std::move(Err);
}
// Using the handle(SymbolOccurrences) from parent class.
using tooling::RefactoringResultConsumer::handle;
void handle(tooling::AtomicChanges SourceReplacements) override {
assert(!Result.hasValue());
Result = std::move(SourceReplacements);
}
Optional<Expected<tooling::AtomicChanges>> Result;
};
} // namespace
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
return make_tagged(vfs::getRealFileSystem(), VFSTag());
}
ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
DiagnosticsConsumer &DiagConsumer,
FileSystemProvider &FSProvider,
unsigned AsyncThreadsCount,
bool StorePreamblesInMemory,
bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx,
llvm::Optional<StringRef> ResourceDir)
: CompileArgs(CDB,
ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
DiagConsumer(DiagConsumer), FSProvider(FSProvider),
FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
PCHs(std::make_shared<PCHContainerOperations>()),
// Pass a callback into `WorkScheduler` to extract symbols from a newly
// parsed file and rebuild the file index synchronously each time an AST
// is parsed.
// FIXME(ioeric): this can be slow and we may be able to index on less
// critical paths.
WorkScheduler(AsyncThreadsCount, StorePreamblesInMemory,
FileIdx
? [this](PathRef Path,
ParsedAST *AST) { FileIdx->update(Path, AST); }
: ASTParsedCallback()) {
if (FileIdx && StaticIdx) {
MergedIndex = mergeIndex(FileIdx.get(), StaticIdx);
Index = MergedIndex.get();
} else if (FileIdx)
Index = FileIdx.get();
else if (StaticIdx)
Index = StaticIdx;
else
Index = nullptr;
}
void ClangdServer::setRootPath(PathRef RootPath) {
std::string NewRootPath = llvm::sys::path::convert_to_slash(
RootPath, llvm::sys::path::Style::posix);
if (llvm::sys::fs::is_directory(NewRootPath))
this->RootPath = NewRootPath;
}
void ClangdServer::addDocument(PathRef File, StringRef Contents,
WantDiagnostics WantDiags) {
DocVersion Version = DraftMgr.updateDraft(File, Contents);
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()},
WantDiags, std::move(TaggedFS));
}
void ClangdServer::removeDocument(PathRef File) {
DraftMgr.removeDraft(File);
CompileArgs.invalidate(File);
WorkScheduler.remove(File);
}
void ClangdServer::forceReparse(PathRef File) {
auto FileContents = DraftMgr.getDraft(File);
assert(FileContents.Draft &&
"forceReparse() was called for non-added document");
// forceReparse promises to request new compilation flags from CDB, so we
// remove any cahced flags.
CompileArgs.invalidate(File);
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
scheduleReparseAndDiags(File, std::move(FileContents), WantDiagnostics::Yes,
std::move(TaggedFS));
}
void ClangdServer::codeComplete(
PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts,
UniqueFunction<void(Tagged<CompletionList>)> Callback,
llvm::Optional<StringRef> OverridenContents,
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
using CallbackType = UniqueFunction<void(Tagged<CompletionList>)>;
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
if (UsedFS)
*UsedFS = TaggedFS.Value;
// Copy completion options for passing them to async task handler.
auto CodeCompleteOpts = Opts;
if (!CodeCompleteOpts.Index) // Respect overridden index.
CodeCompleteOpts.Index = Index;
std::string Contents;
if (OverridenContents) {
Contents = OverridenContents->str();
} else {
VersionedDraft Latest = DraftMgr.getDraft(File);
assert(Latest.Draft && "codeComplete called for non-added document");
Contents = *Latest.Draft;
}
// Copy PCHs to avoid accessing this->PCHs concurrently
std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
auto Task = [PCHs, Pos, TaggedFS, CodeCompleteOpts](
std::string Contents, Path File, CallbackType Callback,
llvm::Expected<InputsAndPreamble> IP) {
assert(IP && "error when trying to read preamble for codeComplete");
auto PreambleData = IP->Preamble;
auto &Command = IP->Inputs.CompileCommand;
// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
// both the old and the new version in case only one of them matches.
CompletionList Result = clangd::codeComplete(
File, Command, PreambleData ? &PreambleData->Preamble : nullptr,
Contents, Pos, TaggedFS.Value, PCHs, CodeCompleteOpts);
Callback(make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
};
WorkScheduler.runWithPreamble("CodeComplete", File,
BindWithForward(Task, std::move(Contents),
File.str(),
std::move(Callback)));
}
void ClangdServer::signatureHelp(
PathRef File, Position Pos,
UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback,
llvm::Optional<StringRef> OverridenContents,
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
if (UsedFS)
*UsedFS = TaggedFS.Value;
std::string Contents;
if (OverridenContents) {
Contents = OverridenContents->str();
} else {
VersionedDraft Latest = DraftMgr.getDraft(File);
if (!Latest.Draft)
return Callback(llvm::make_error<llvm::StringError>(
"signatureHelp is called for non-added document",
llvm::errc::invalid_argument));
Contents = std::move(*Latest.Draft);
}
auto PCHs = this->PCHs;
auto Action = [Contents, Pos, TaggedFS,
PCHs](Path File, decltype(Callback) Callback,
llvm::Expected<InputsAndPreamble> IP) {
if (!IP)
return Callback(IP.takeError());
auto PreambleData = IP->Preamble;
auto &Command = IP->Inputs.CompileCommand;
Callback(make_tagged(
clangd::signatureHelp(File, Command,
PreambleData ? &PreambleData->Preamble : nullptr,
Contents, Pos, TaggedFS.Value, PCHs),
TaggedFS.Tag));
};
WorkScheduler.runWithPreamble(
"SignatureHelp", File,
BindWithForward(Action, File.str(), std::move(Callback)));
}
llvm::Expected<tooling::Replacements>
ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
size_t Begin = positionToOffset(Code, Rng.start);
size_t Len = positionToOffset(Code, Rng.end) - Begin;
return formatCode(Code, File, {tooling::Range(Begin, Len)});
}
llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
PathRef File) {
// Format everything.
return formatCode(Code, File, {tooling::Range(0, Code.size())});
}
llvm::Expected<tooling::Replacements>
ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
// Look for the previous opening brace from the character position and
// format starting from there.
size_t CursorPos = positionToOffset(Code, Pos);
size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
if (PreviousLBracePos == StringRef::npos)
PreviousLBracePos = CursorPos;
size_t Len = CursorPos - PreviousLBracePos;
return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
}
void ClangdServer::rename(
PathRef File, Position Pos, llvm::StringRef NewName,
UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)>
Callback) {
auto Action = [Pos](Path File, std::string NewName,
decltype(Callback) Callback,
Expected<InputsAndAST> InpAST) {
if (!InpAST)
return Callback(InpAST.takeError());
auto &AST = InpAST->AST;
RefactoringResultCollector ResultCollector;
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
const FileEntry *FE =
SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
if (!FE)
return Callback(llvm::make_error<llvm::StringError>(
"rename called for non-added document",
llvm::errc::invalid_argument));
SourceLocation SourceLocationBeg =
clangd::getBeginningOfIdentifier(AST, Pos, FE);
tooling::RefactoringRuleContext Context(
AST.getASTContext().getSourceManager());
Context.setASTContext(AST.getASTContext());
auto Rename = clang::tooling::RenameOccurrences::initiate(
Context, SourceRange(SourceLocationBeg), NewName);
if (!Rename)
return Callback(Rename.takeError());
Rename->invoke(ResultCollector, Context);
assert(ResultCollector.Result.hasValue());
if (!ResultCollector.Result.getValue())
return Callback(ResultCollector.Result->takeError());
std::vector<tooling::Replacement> Replacements;
for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
tooling::Replacements ChangeReps = Change.getReplacements();
for (const auto &Rep : ChangeReps) {
// FIXME: Right now we only support renaming the main file, so we
// drop replacements not for the main file. In the future, we might
// consider to support:
// * rename in any included header
// * rename only in the "main" header
// * provide an error if there are symbols we won't rename (e.g.
// std::vector)
// * rename globally in project
// * rename in open files
if (Rep.getFilePath() == File)
Replacements.push_back(Rep);
}
}
return Callback(Replacements);
};
WorkScheduler.runWithAST(
"Rename", File,
BindWithForward(Action, File.str(), NewName.str(), std::move(Callback)));
}
Expected<tooling::Replacements>
ClangdServer::insertInclude(PathRef File, StringRef Code,
llvm::StringRef Header) {
std::string ToInclude;
if (Header.startswith("<") || Header.startswith("\"")) {
ToInclude = Header;
} else {
auto U = URI::parse(Header);
if (!U)
return U.takeError();
auto Resolved = URI::resolve(*U, /*HintPath=*/File);
if (!Resolved)
return Resolved.takeError();
tooling::CompileCommand CompileCommand =
CompileArgs.getCompileCommand(File);
auto Include =
calculateIncludePath(File, Code, *Resolved, CompileCommand,
FSProvider.getTaggedFileSystem(File).Value);
if (!Include)
return Include.takeError();
if (Include->empty())
return tooling::Replacements();
ToInclude = std::move(*Include);
}
auto Style = format::getStyle("file", File, "llvm");
if (!Style) {
llvm::consumeError(Style.takeError());
// FIXME(ioeric): needs more consistent style support in clangd server.
Style = format::getLLVMStyle();
}
// Replacement with offset UINT_MAX and length 0 will be treated as include
// insertion.
tooling::Replacement R(File, /*Offset=*/UINT_MAX, 0, "#include " + ToInclude);
return format::cleanupAroundReplacements(Code, tooling::Replacements(R),
*Style);
}
llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) {
auto Latest = DraftMgr.getDraft(File);
if (!Latest.Draft)
return llvm::None;
return std::move(*Latest.Draft);
}
void ClangdServer::dumpAST(PathRef File,
UniqueFunction<void(std::string)> Callback) {
auto Action = [](decltype(Callback) Callback,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST) {
ignoreError(InpAST.takeError());
return Callback("<no-ast>");
}
std::string Result;
llvm::raw_string_ostream ResultOS(Result);
clangd::dumpAST(InpAST->AST, ResultOS);
ResultOS.flush();
Callback(Result);
};
WorkScheduler.runWithAST("DumpAST", File,
BindWithForward(Action, std::move(Callback)));
}
void ClangdServer::findDefinitions(
PathRef File, Position Pos,
UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)>
Callback) {
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
auto Action = [Pos, TaggedFS](decltype(Callback) Callback,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return Callback(InpAST.takeError());
auto Result = clangd::findDefinitions(InpAST->AST, Pos);
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
};
WorkScheduler.runWithAST("Definitions", File,
BindWithForward(Action, std::move(Callback)));
}
llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
".c++", ".m", ".mm"};
StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
StringRef PathExt = llvm::sys::path::extension(Path);
// Lookup in a list of known extensions.
auto SourceIter =
std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
[&PathExt](PathRef SourceExt) {
return SourceExt.equals_lower(PathExt);
});
bool IsSource = SourceIter != std::end(SourceExtensions);
auto HeaderIter =
std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
[&PathExt](PathRef HeaderExt) {
return HeaderExt.equals_lower(PathExt);
});
bool IsHeader = HeaderIter != std::end(HeaderExtensions);
// We can only switch between extensions known extensions.
if (!IsSource && !IsHeader)
return llvm::None;
// Array to lookup extensions for the switch. An opposite of where original
// extension was found.
ArrayRef<StringRef> NewExts;
if (IsSource)
NewExts = HeaderExtensions;
else
NewExts = SourceExtensions;
// Storage for the new path.
SmallString<128> NewPath = StringRef(Path);
// Instance of vfs::FileSystem, used for file existence checks.
auto FS = FSProvider.getTaggedFileSystem(Path).Value;
// Loop through switched extension candidates.
for (StringRef NewExt : NewExts) {
llvm::sys::path::replace_extension(NewPath, NewExt);
if (FS->exists(NewPath))
return NewPath.str().str(); // First str() to convert from SmallString to
// StringRef, second to convert from StringRef
// to std::string
// Also check NewExt in upper-case, just in case.
llvm::sys::path::replace_extension(NewPath, NewExt.upper());
if (FS->exists(NewPath))
return NewPath.str().str();
}
return llvm::None;
}
llvm::Expected<tooling::Replacements>
ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
ArrayRef<tooling::Range> Ranges) {
// Call clang-format.
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
auto StyleOrError =
format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
if (!StyleOrError) {
return StyleOrError.takeError();
} else {
return format::reformat(StyleOrError.get(), Code, Ranges, File);
}
}
void ClangdServer::findDocumentHighlights(
PathRef File, Position Pos,
UniqueFunction<void(llvm::Expected<Tagged<std::vector<DocumentHighlight>>>)>
Callback) {
auto FileContents = DraftMgr.getDraft(File);
if (!FileContents.Draft)
return Callback(llvm::make_error<llvm::StringError>(
"findDocumentHighlights called on non-added file",
llvm::errc::invalid_argument));
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
auto Action = [TaggedFS, Pos](decltype(Callback) Callback,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return Callback(InpAST.takeError());
auto Result = clangd::findDocumentHighlights(InpAST->AST, Pos);
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
};
WorkScheduler.runWithAST("Highlights", File,
BindWithForward(Action, std::move(Callback)));
}
void ClangdServer::findHover(
PathRef File, Position Pos,
UniqueFunction<void(llvm::Expected<Tagged<Hover>>)> Callback) {
Hover FinalHover;
auto FileContents = DraftMgr.getDraft(File);
if (!FileContents.Draft)
return Callback(llvm::make_error<llvm::StringError>(
"findHover called on non-added file", llvm::errc::invalid_argument));
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
auto Action = [Pos, TaggedFS](decltype(Callback) Callback,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return Callback(InpAST.takeError());
Hover Result = clangd::getHover(InpAST->AST, Pos);
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
};
WorkScheduler.runWithAST("Hover", File,
BindWithForward(Action, std::move(Callback)));
}
void ClangdServer::scheduleReparseAndDiags(
PathRef File, VersionedDraft Contents, WantDiagnostics WantDiags,
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
tooling::CompileCommand Command = CompileArgs.getCompileCommand(File);
DocVersion Version = Contents.Version;
Path FileStr = File.str();
VFSTag Tag = std::move(TaggedFS.Tag);
auto Callback = [this, Version, FileStr,
Tag](std::vector<DiagWithFixIts> Diags) {
// We need to serialize access to resulting diagnostics to avoid calling
// `onDiagnosticsReady` in the wrong order.
std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
// FIXME(ibiryukov): get rid of '<' comparison here. In the current
// implementation diagnostics will not be reported after version counters'
// overflow. This should not happen in practice, since DocVersion is a
// 64-bit unsigned integer.
if (Version < LastReportedDiagsVersion)
return;
LastReportedDiagsVersion = Version;
DiagConsumer.onDiagnosticsReady(
FileStr, make_tagged(std::move(Diags), std::move(Tag)));
};
WorkScheduler.update(File,
ParseInputs{std::move(Command),
std::move(TaggedFS.Value),
std::move(*Contents.Draft)},
WantDiags, std::move(Callback));
}
void ClangdServer::reparseOpenedFiles() {
for (const Path &FilePath : DraftMgr.getActiveFiles())
forceReparse(FilePath);
}
void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
// FIXME: Do nothing for now. This will be used for indexing and potentially
// invalidating other caches.
}
std::vector<std::pair<Path, std::size_t>>
ClangdServer::getUsedBytesPerFile() const {
return WorkScheduler.getUsedBytesPerFile();
}
LLVM_NODISCARD bool
ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds));
}