Bug Summary

File:tools/clang/tools/extra/clangd/ClangdServer.cpp
Warning:line 345, column 38
Called C++ object pointer is null

Annotated Source Code

[?] Use j/k keys for keyboard navigation

/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp

1//===--- 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 "CodeComplete.h"
12#include "SourceCode.h"
13#include "XRefs.h"
14#include "clang/Format/Format.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/CompilerInvocation.h"
17#include "clang/Tooling/CompilationDatabase.h"
18#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
19#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/Support/Errc.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/FormatProviders.h"
24#include "llvm/Support/FormatVariadic.h"
25#include "llvm/Support/Path.h"
26#include "llvm/Support/raw_ostream.h"
27#include <future>
28
29using namespace clang;
30using namespace clang::clangd;
31
32namespace {
33
34std::string getStandardResourceDir() {
35 static int Dummy; // Just an address in this process.
36 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
37}
38
39class RefactoringResultCollector final
40 : public tooling::RefactoringResultConsumer {
41public:
42 void handleError(llvm::Error Err) override {
43 assert(!Result.hasValue())(static_cast <bool> (!Result.hasValue()) ? void (0) : __assert_fail
("!Result.hasValue()", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 43, __extension__ __PRETTY_FUNCTION__))
;
44 // FIXME: figure out a way to return better message for DiagnosticError.
45 // clangd uses llvm::toString to convert the Err to string, however, for
46 // DiagnosticError, only "clang diagnostic" will be generated.
47 Result = std::move(Err);
48 }
49
50 // Using the handle(SymbolOccurrences) from parent class.
51 using tooling::RefactoringResultConsumer::handle;
52
53 void handle(tooling::AtomicChanges SourceReplacements) override {
54 assert(!Result.hasValue())(static_cast <bool> (!Result.hasValue()) ? void (0) : __assert_fail
("!Result.hasValue()", "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 54, __extension__ __PRETTY_FUNCTION__))
;
55 Result = std::move(SourceReplacements);
56 }
57
58 Optional<Expected<tooling::AtomicChanges>> Result;
59};
60
61} // namespace
62
63Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
64RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
65 return make_tagged(vfs::getRealFileSystem(), VFSTag());
66}
67
68unsigned clangd::getDefaultAsyncThreadsCount() {
69 unsigned HardwareConcurrency = std::thread::hardware_concurrency();
70 // C++ standard says that hardware_concurrency()
71 // may return 0, fallback to 1 worker thread in
72 // that case.
73 if (HardwareConcurrency == 0)
74 return 1;
75 return HardwareConcurrency;
76}
77
78ClangdScheduler::ClangdScheduler(unsigned AsyncThreadsCount)
79 : RunSynchronously(AsyncThreadsCount == 0) {
80 if (RunSynchronously) {
81 // Don't start the worker thread if we're running synchronously
82 return;
83 }
84
85 Workers.reserve(AsyncThreadsCount);
86 for (unsigned I = 0; I < AsyncThreadsCount; ++I) {
87 Workers.push_back(std::thread([this, I]() {
88 llvm::set_thread_name(llvm::formatv("scheduler/{0}", I));
89 while (true) {
90 UniqueFunction<void()> Request;
91
92 // Pick request from the queue
93 {
94 std::unique_lock<std::mutex> Lock(Mutex);
95 // Wait for more requests.
96 RequestCV.wait(Lock,
97 [this] { return !RequestQueue.empty() || Done; });
98 if (Done)
99 return;
100
101 assert(!RequestQueue.empty() && "RequestQueue was empty")(static_cast <bool> (!RequestQueue.empty() && "RequestQueue was empty"
) ? void (0) : __assert_fail ("!RequestQueue.empty() && \"RequestQueue was empty\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 101, __extension__ __PRETTY_FUNCTION__))
;
102
103 // We process requests starting from the front of the queue. Users of
104 // ClangdScheduler have a way to prioritise their requests by putting
105 // them to the either side of the queue (using either addToEnd or
106 // addToFront).
107 Request = std::move(RequestQueue.front());
108 RequestQueue.pop_front();
109 } // unlock Mutex
110
111 Request();
112 }
113 }));
114 }
115}
116
117ClangdScheduler::~ClangdScheduler() {
118 if (RunSynchronously)
119 return; // no worker thread is running in that case
120
121 {
122 std::lock_guard<std::mutex> Lock(Mutex);
123 // Wake up the worker thread
124 Done = true;
125 } // unlock Mutex
126 RequestCV.notify_all();
127
128 for (auto &Worker : Workers)
129 Worker.join();
130}
131
132ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
133 DiagnosticsConsumer &DiagConsumer,
134 FileSystemProvider &FSProvider,
135 unsigned AsyncThreadsCount,
136 bool StorePreamblesInMemory,
137 bool BuildDynamicSymbolIndex,
138 llvm::Optional<StringRef> ResourceDir)
139 : CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider),
140 FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
141 // Pass a callback into `Units` to extract symbols from a newly parsed
142 // file and rebuild the file index synchronously each time an AST is
143 // parsed.
144 // FIXME(ioeric): this can be slow and we may be able to index on less
145 // critical paths.
146 Units(FileIdx
147 ? [this](const Context &Ctx, PathRef Path,
148 ParsedAST *AST) { FileIdx->update(Ctx, Path, AST); }
149 : ASTParsedCallback()),
150 ResourceDir(ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
151 PCHs(std::make_shared<PCHContainerOperations>()),
152 StorePreamblesInMemory(StorePreamblesInMemory),
153 WorkScheduler(AsyncThreadsCount) {}
154
155void ClangdServer::setRootPath(PathRef RootPath) {
156 std::string NewRootPath = llvm::sys::path::convert_to_slash(
157 RootPath, llvm::sys::path::Style::posix);
158 if (llvm::sys::fs::is_directory(NewRootPath))
159 this->RootPath = NewRootPath;
160}
161
162std::future<Context> ClangdServer::addDocument(Context Ctx, PathRef File,
163 StringRef Contents) {
164 DocVersion Version = DraftMgr.updateDraft(File, Contents);
165
166 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
167 std::shared_ptr<CppFile> Resources = Units.getOrCreateFile(
168 File, ResourceDir, CDB, StorePreamblesInMemory, PCHs);
169 return scheduleReparseAndDiags(std::move(Ctx), File,
170 VersionedDraft{Version, Contents.str()},
171 std::move(Resources), std::move(TaggedFS));
172}
173
174std::future<Context> ClangdServer::removeDocument(Context Ctx, PathRef File) {
175 DraftMgr.removeDraft(File);
176 std::shared_ptr<CppFile> Resources = Units.removeIfPresent(File);
177 return scheduleCancelRebuild(std::move(Ctx), std::move(Resources));
178}
179
180std::future<Context> ClangdServer::forceReparse(Context Ctx, PathRef File) {
181 auto FileContents = DraftMgr.getDraft(File);
182 assert(FileContents.Draft &&(static_cast <bool> (FileContents.Draft && "forceReparse() was called for non-added document"
) ? void (0) : __assert_fail ("FileContents.Draft && \"forceReparse() was called for non-added document\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 183, __extension__ __PRETTY_FUNCTION__))
183 "forceReparse() was called for non-added document")(static_cast <bool> (FileContents.Draft && "forceReparse() was called for non-added document"
) ? void (0) : __assert_fail ("FileContents.Draft && \"forceReparse() was called for non-added document\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 183, __extension__ __PRETTY_FUNCTION__))
;
184
185 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
186 auto Recreated = Units.recreateFileIfCompileCommandChanged(
187 File, ResourceDir, CDB, StorePreamblesInMemory, PCHs);
188
189 // Note that std::future from this cleanup action is ignored.
190 scheduleCancelRebuild(Ctx.clone(), std::move(Recreated.RemovedFile));
191 // Schedule a reparse.
192 return scheduleReparseAndDiags(std::move(Ctx), File, std::move(FileContents),
193 std::move(Recreated.FileInCollection),
194 std::move(TaggedFS));
195}
196
197std::future<std::pair<Context, Tagged<CompletionList>>>
198ClangdServer::codeComplete(Context Ctx, PathRef File, Position Pos,
199 const clangd::CodeCompleteOptions &Opts,
200 llvm::Optional<StringRef> OverridenContents,
201 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
202 using ResultType = std::pair<Context, Tagged<CompletionList>>;
203
204 std::promise<ResultType> ResultPromise;
205
206 auto Callback = [](std::promise<ResultType> ResultPromise, Context Ctx,
207 Tagged<CompletionList> Result) -> void {
208 ResultPromise.set_value({std::move(Ctx), std::move(Result)});
209 };
210
211 std::future<ResultType> ResultFuture = ResultPromise.get_future();
212 codeComplete(std::move(Ctx), File, Pos, Opts,
213 BindWithForward(Callback, std::move(ResultPromise)),
214 OverridenContents, UsedFS);
215 return ResultFuture;
216}
217
218void ClangdServer::codeComplete(
219 Context Ctx, PathRef File, Position Pos,
220 const clangd::CodeCompleteOptions &Opts,
221 UniqueFunction<void(Context, Tagged<CompletionList>)> Callback,
222 llvm::Optional<StringRef> OverridenContents,
223 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
224 using CallbackType = UniqueFunction<void(Context, Tagged<CompletionList>)>;
225
226 std::string Contents;
227 if (OverridenContents) {
228 Contents = *OverridenContents;
229 } else {
230 auto FileContents = DraftMgr.getDraft(File);
231 assert(FileContents.Draft &&(static_cast <bool> (FileContents.Draft && "codeComplete is called for non-added document"
) ? void (0) : __assert_fail ("FileContents.Draft && \"codeComplete is called for non-added document\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 232, __extension__ __PRETTY_FUNCTION__))
232 "codeComplete is called for non-added document")(static_cast <bool> (FileContents.Draft && "codeComplete is called for non-added document"
) ? void (0) : __assert_fail ("FileContents.Draft && \"codeComplete is called for non-added document\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 232, __extension__ __PRETTY_FUNCTION__))
;
233
234 Contents = std::move(*FileContents.Draft);
235 }
236
237 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
238 if (UsedFS)
239 *UsedFS = TaggedFS.Value;
240
241 std::shared_ptr<CppFile> Resources = Units.getFile(File);
242 assert(Resources && "Calling completion on non-added file")(static_cast <bool> (Resources && "Calling completion on non-added file"
) ? void (0) : __assert_fail ("Resources && \"Calling completion on non-added file\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 242, __extension__ __PRETTY_FUNCTION__))
;
243
244 // Remember the current Preamble and use it when async task starts executing.
245 // At the point when async task starts executing, we may have a different
246 // Preamble in Resources. However, we assume the Preamble that we obtain here
247 // is reusable in completion more often.
248 std::shared_ptr<const PreambleData> Preamble =
249 Resources->getPossiblyStalePreamble();
250 // Copy completion options for passing them to async task handler.
251 auto CodeCompleteOpts = Opts;
252 if (FileIdx)
253 CodeCompleteOpts.Index = FileIdx.get();
254 // A task that will be run asynchronously.
255 auto Task =
256 // 'mutable' to reassign Preamble variable.
257 [=](Context Ctx, CallbackType Callback) mutable {
258 if (!Preamble) {
259 // Maybe we built some preamble before processing this request.
260 Preamble = Resources->getPossiblyStalePreamble();
261 }
262 // FIXME(ibiryukov): even if Preamble is non-null, we may want to check
263 // both the old and the new version in case only one of them matches.
264
265 CompletionList Result = clangd::codeComplete(
266 Ctx, File, Resources->getCompileCommand(),
267 Preamble ? &Preamble->Preamble : nullptr, Contents, Pos,
268 TaggedFS.Value, PCHs, CodeCompleteOpts);
269
270 Callback(std::move(Ctx),
271 make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
272 };
273
274 WorkScheduler.addToFront(std::move(Task), std::move(Ctx),
275 std::move(Callback));
276}
277
278llvm::Expected<Tagged<SignatureHelp>>
279ClangdServer::signatureHelp(const Context &Ctx, PathRef File, Position Pos,
280 llvm::Optional<StringRef> OverridenContents,
281 IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
282 std::string DraftStorage;
283 if (!OverridenContents) {
284 auto FileContents = DraftMgr.getDraft(File);
285 if (!FileContents.Draft)
286 return llvm::make_error<llvm::StringError>(
287 "signatureHelp is called for non-added document",
288 llvm::errc::invalid_argument);
289
290 DraftStorage = std::move(*FileContents.Draft);
291 OverridenContents = DraftStorage;
292 }
293
294 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
295 if (UsedFS)
296 *UsedFS = TaggedFS.Value;
297
298 std::shared_ptr<CppFile> Resources = Units.getFile(File);
299 if (!Resources)
300 return llvm::make_error<llvm::StringError>(
301 "signatureHelp is called for non-added document",
302 llvm::errc::invalid_argument);
303
304 auto Preamble = Resources->getPossiblyStalePreamble();
305 auto Result =
306 clangd::signatureHelp(Ctx, File, Resources->getCompileCommand(),
307 Preamble ? &Preamble->Preamble : nullptr,
308 *OverridenContents, Pos, TaggedFS.Value, PCHs);
309 return make_tagged(std::move(Result), TaggedFS.Tag);
310}
311
312llvm::Expected<tooling::Replacements>
313ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
314 size_t Begin = positionToOffset(Code, Rng.start);
315 size_t Len = positionToOffset(Code, Rng.end) - Begin;
316 return formatCode(Code, File, {tooling::Range(Begin, Len)});
317}
318
319llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
320 PathRef File) {
321 // Format everything.
322 return formatCode(Code, File, {tooling::Range(0, Code.size())});
323}
324
325llvm::Expected<tooling::Replacements>
326ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
327 // Look for the previous opening brace from the character position and
328 // format starting from there.
329 size_t CursorPos = positionToOffset(Code, Pos);
330 size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
331 if (PreviousLBracePos == StringRef::npos)
332 PreviousLBracePos = CursorPos;
333 size_t Len = CursorPos - PreviousLBracePos;
334
335 return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
336}
337
338Expected<std::vector<tooling::Replacement>>
339ClangdServer::rename(const Context &Ctx, PathRef File, Position Pos,
340 llvm::StringRef NewName) {
341 std::string Code = getDocument(File);
342 std::shared_ptr<CppFile> Resources = Units.getFile(File);
343 RefactoringResultCollector ResultCollector;
344 Resources->getAST().get()->runUnderLock([&](ParsedAST *AST) {
1
Calling 'ParsedASTWrapper::runUnderLock'
345 const SourceManager &SourceMgr = AST->getASTContext().getSourceManager();
6
Called C++ object pointer is null
346 const FileEntry *FE =
347 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
348 if (!FE)
349 return;
350 SourceLocation SourceLocationBeg =
351 clangd::getBeginningOfIdentifier(*AST, Pos, FE);
352 tooling::RefactoringRuleContext Context(
353 AST->getASTContext().getSourceManager());
354 Context.setASTContext(AST->getASTContext());
355 auto Rename = clang::tooling::RenameOccurrences::initiate(
356 Context, SourceRange(SourceLocationBeg), NewName.str());
357 if (!Rename) {
358 ResultCollector.Result = Rename.takeError();
359 return;
360 }
361 Rename->invoke(ResultCollector, Context);
362 });
363 assert(ResultCollector.Result.hasValue())(static_cast <bool> (ResultCollector.Result.hasValue())
? void (0) : __assert_fail ("ResultCollector.Result.hasValue()"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 363, __extension__ __PRETTY_FUNCTION__))
;
364 if (!ResultCollector.Result.getValue())
365 return ResultCollector.Result->takeError();
366
367 std::vector<tooling::Replacement> Replacements;
368 for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
369 tooling::Replacements ChangeReps = Change.getReplacements();
370 for (const auto &Rep : ChangeReps) {
371 // FIXME: Right now we only support renaming the main file, so we drop
372 // replacements not for the main file. In the future, we might consider to
373 // support:
374 // * rename in any included header
375 // * rename only in the "main" header
376 // * provide an error if there are symbols we won't rename (e.g.
377 // std::vector)
378 // * rename globally in project
379 // * rename in open files
380 if (Rep.getFilePath() == File)
381 Replacements.push_back(Rep);
382 }
383 }
384 return Replacements;
385}
386
387std::string ClangdServer::getDocument(PathRef File) {
388 auto draft = DraftMgr.getDraft(File);
389 assert(draft.Draft && "File is not tracked, cannot get contents")(static_cast <bool> (draft.Draft && "File is not tracked, cannot get contents"
) ? void (0) : __assert_fail ("draft.Draft && \"File is not tracked, cannot get contents\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 389, __extension__ __PRETTY_FUNCTION__))
;
390 return *draft.Draft;
391}
392
393std::string ClangdServer::dumpAST(PathRef File) {
394 std::shared_ptr<CppFile> Resources = Units.getFile(File);
395 assert(Resources && "dumpAST is called for non-added document")(static_cast <bool> (Resources && "dumpAST is called for non-added document"
) ? void (0) : __assert_fail ("Resources && \"dumpAST is called for non-added document\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 395, __extension__ __PRETTY_FUNCTION__))
;
396
397 std::string Result;
398 Resources->getAST().get()->runUnderLock([&Result](ParsedAST *AST) {
399 llvm::raw_string_ostream ResultOS(Result);
400 if (AST) {
401 clangd::dumpAST(*AST, ResultOS);
402 } else {
403 ResultOS << "<no-ast>";
404 }
405 ResultOS.flush();
406 });
407 return Result;
408}
409
410llvm::Expected<Tagged<std::vector<Location>>>
411ClangdServer::findDefinitions(const Context &Ctx, PathRef File, Position Pos) {
412 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
413
414 std::shared_ptr<CppFile> Resources = Units.getFile(File);
415 if (!Resources)
416 return llvm::make_error<llvm::StringError>(
417 "findDefinitions called on non-added file",
418 llvm::errc::invalid_argument);
419
420 std::vector<Location> Result;
421 Resources->getAST().get()->runUnderLock([Pos, &Result, &Ctx](ParsedAST *AST) {
422 if (!AST)
423 return;
424 Result = clangd::findDefinitions(Ctx, *AST, Pos);
425 });
426 return make_tagged(std::move(Result), TaggedFS.Tag);
427}
428
429llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
430
431 StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
432 ".c++", ".m", ".mm"};
433 StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
434
435 StringRef PathExt = llvm::sys::path::extension(Path);
436
437 // Lookup in a list of known extensions.
438 auto SourceIter =
439 std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
440 [&PathExt](PathRef SourceExt) {
441 return SourceExt.equals_lower(PathExt);
442 });
443 bool IsSource = SourceIter != std::end(SourceExtensions);
444
445 auto HeaderIter =
446 std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
447 [&PathExt](PathRef HeaderExt) {
448 return HeaderExt.equals_lower(PathExt);
449 });
450
451 bool IsHeader = HeaderIter != std::end(HeaderExtensions);
452
453 // We can only switch between extensions known extensions.
454 if (!IsSource && !IsHeader)
455 return llvm::None;
456
457 // Array to lookup extensions for the switch. An opposite of where original
458 // extension was found.
459 ArrayRef<StringRef> NewExts;
460 if (IsSource)
461 NewExts = HeaderExtensions;
462 else
463 NewExts = SourceExtensions;
464
465 // Storage for the new path.
466 SmallString<128> NewPath = StringRef(Path);
467
468 // Instance of vfs::FileSystem, used for file existence checks.
469 auto FS = FSProvider.getTaggedFileSystem(Path).Value;
470
471 // Loop through switched extension candidates.
472 for (StringRef NewExt : NewExts) {
473 llvm::sys::path::replace_extension(NewPath, NewExt);
474 if (FS->exists(NewPath))
475 return NewPath.str().str(); // First str() to convert from SmallString to
476 // StringRef, second to convert from StringRef
477 // to std::string
478
479 // Also check NewExt in upper-case, just in case.
480 llvm::sys::path::replace_extension(NewPath, NewExt.upper());
481 if (FS->exists(NewPath))
482 return NewPath.str().str();
483 }
484
485 return llvm::None;
486}
487
488llvm::Expected<tooling::Replacements>
489ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
490 ArrayRef<tooling::Range> Ranges) {
491 // Call clang-format.
492 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
493 auto StyleOrError =
494 format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
495 if (!StyleOrError) {
496 return StyleOrError.takeError();
497 } else {
498 return format::reformat(StyleOrError.get(), Code, Ranges, File);
499 }
500}
501
502llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
503ClangdServer::findDocumentHighlights(const Context &Ctx, PathRef File,
504 Position Pos) {
505 auto FileContents = DraftMgr.getDraft(File);
506 if (!FileContents.Draft)
507 return llvm::make_error<llvm::StringError>(
508 "findDocumentHighlights called on non-added file",
509 llvm::errc::invalid_argument);
510
511 auto TaggedFS = FSProvider.getTaggedFileSystem(File);
512
513 std::shared_ptr<CppFile> Resources = Units.getFile(File);
514 if (!Resources)
515 return llvm::make_error<llvm::StringError>(
516 "findDocumentHighlights called on non-added file",
517 llvm::errc::invalid_argument);
518
519 std::vector<DocumentHighlight> Result;
520 llvm::Optional<llvm::Error> Err;
521 Resources->getAST().get()->runUnderLock([Pos, &Ctx, &Err,
522 &Result](ParsedAST *AST) {
523 if (!AST) {
524 Err = llvm::make_error<llvm::StringError>("Invalid AST",
525 llvm::errc::invalid_argument);
526 return;
527 }
528 Result = clangd::findDocumentHighlights(Ctx, *AST, Pos);
529 });
530
531 if (Err)
532 return std::move(*Err);
533 return make_tagged(Result, TaggedFS.Tag);
534}
535
536std::future<Context> ClangdServer::scheduleReparseAndDiags(
537 Context Ctx, PathRef File, VersionedDraft Contents,
538 std::shared_ptr<CppFile> Resources,
539 Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
540
541 assert(Contents.Draft && "Draft must have contents")(static_cast <bool> (Contents.Draft && "Draft must have contents"
) ? void (0) : __assert_fail ("Contents.Draft && \"Draft must have contents\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/ClangdServer.cpp"
, 541, __extension__ __PRETTY_FUNCTION__))
;
542 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)>
543 DeferredRebuild =
544 Resources->deferRebuild(*Contents.Draft, TaggedFS.Value);
545 std::promise<Context> DonePromise;
546 std::future<Context> DoneFuture = DonePromise.get_future();
547
548 DocVersion Version = Contents.Version;
549 Path FileStr = File;
550 VFSTag Tag = TaggedFS.Tag;
551 auto ReparseAndPublishDiags =
552 [this, FileStr, Version,
553 Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(
554 const Context &)>
555 DeferredRebuild,
556 std::promise<Context> DonePromise, Context Ctx) -> void {
557 auto Guard = onScopeExit([&]() { DonePromise.set_value(std::move(Ctx)); });
558
559 auto CurrentVersion = DraftMgr.getVersion(FileStr);
560 if (CurrentVersion != Version)
561 return; // This request is outdated
562
563 auto Diags = DeferredRebuild(Ctx);
564 if (!Diags)
565 return; // A new reparse was requested before this one completed.
566
567 // We need to serialize access to resulting diagnostics to avoid calling
568 // `onDiagnosticsReady` in the wrong order.
569 std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
570 DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
571 // FIXME(ibiryukov): get rid of '<' comparison here. In the current
572 // implementation diagnostics will not be reported after version counters'
573 // overflow. This should not happen in practice, since DocVersion is a
574 // 64-bit unsigned integer.
575 if (Version < LastReportedDiagsVersion)
576 return;
577 LastReportedDiagsVersion = Version;
578
579 DiagConsumer.onDiagnosticsReady(FileStr,
580 make_tagged(std::move(*Diags), Tag));
581 };
582
583 WorkScheduler.addToFront(std::move(ReparseAndPublishDiags),
584 std::move(DeferredRebuild), std::move(DonePromise),
585 std::move(Ctx));
586 return DoneFuture;
587}
588
589std::future<Context>
590ClangdServer::scheduleCancelRebuild(Context Ctx,
591 std::shared_ptr<CppFile> Resources) {
592 std::promise<Context> DonePromise;
593 std::future<Context> DoneFuture = DonePromise.get_future();
594 if (!Resources) {
595 // No need to schedule any cleanup.
596 DonePromise.set_value(std::move(Ctx));
597 return DoneFuture;
598 }
599
600 UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
601 auto CancelReparses = [Resources](std::promise<Context> DonePromise,
602 UniqueFunction<void()> DeferredCancel,
603 Context Ctx) {
604 DeferredCancel();
605 DonePromise.set_value(std::move(Ctx));
606 };
607 WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
608 std::move(DeferredCancel), std::move(Ctx));
609 return DoneFuture;
610}
611
612void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
613 // FIXME: Do nothing for now. This will be used for indexing and potentially
614 // invalidating other caches.
615}

/build/llvm-toolchain-snapshot-6.0~svn321639/tools/clang/tools/extra/clangd/index/../ClangdUnit.h

1//===--- ClangdUnit.h -------------------------------------------*- 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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
12
13#include "Context.h"
14#include "Function.h"
15#include "Path.h"
16#include "Protocol.h"
17#include "clang/Frontend/FrontendAction.h"
18#include "clang/Frontend/PrecompiledPreamble.h"
19#include "clang/Serialization/ASTBitCodes.h"
20#include "clang/Tooling/CompilationDatabase.h"
21#include "clang/Tooling/Core/Replacement.h"
22#include <atomic>
23#include <future>
24#include <memory>
25#include <mutex>
26
27namespace llvm {
28class raw_ostream;
29}
30
31namespace clang {
32class PCHContainerOperations;
33
34namespace vfs {
35class FileSystem;
36}
37
38namespace tooling {
39struct CompileCommand;
40}
41
42namespace clangd {
43
44/// A diagnostic with its FixIts.
45struct DiagWithFixIts {
46 clangd::Diagnostic Diag;
47 llvm::SmallVector<TextEdit, 1> FixIts;
48};
49
50// Stores Preamble and associated data.
51struct PreambleData {
52 PreambleData(PrecompiledPreamble Preamble,
53 std::vector<serialization::DeclID> TopLevelDeclIDs,
54 std::vector<DiagWithFixIts> Diags);
55
56 PrecompiledPreamble Preamble;
57 std::vector<serialization::DeclID> TopLevelDeclIDs;
58 std::vector<DiagWithFixIts> Diags;
59};
60
61/// Stores and provides access to parsed AST.
62class ParsedAST {
63public:
64 /// Attempts to run Clang and store parsed AST. If \p Preamble is non-null
65 /// it is reused during parsing.
66 static llvm::Optional<ParsedAST>
67 Build(const Context &Ctx, std::unique_ptr<clang::CompilerInvocation> CI,
68 std::shared_ptr<const PreambleData> Preamble,
69 std::unique_ptr<llvm::MemoryBuffer> Buffer,
70 std::shared_ptr<PCHContainerOperations> PCHs,
71 IntrusiveRefCntPtr<vfs::FileSystem> VFS);
72
73 ParsedAST(ParsedAST &&Other);
74 ParsedAST &operator=(ParsedAST &&Other);
75
76 ~ParsedAST();
77
78 ASTContext &getASTContext();
79 const ASTContext &getASTContext() const;
80
81 Preprocessor &getPreprocessor();
82 const Preprocessor &getPreprocessor() const;
83
84 /// This function returns all top-level decls, including those that come
85 /// from Preamble. Decls, coming from Preamble, have to be deserialized, so
86 /// this call might be expensive.
87 ArrayRef<const Decl *> getTopLevelDecls();
88
89 const std::vector<DiagWithFixIts> &getDiagnostics() const;
90
91private:
92 ParsedAST(std::shared_ptr<const PreambleData> Preamble,
93 std::unique_ptr<CompilerInstance> Clang,
94 std::unique_ptr<FrontendAction> Action,
95 std::vector<const Decl *> TopLevelDecls,
96 std::vector<DiagWithFixIts> Diags);
97
98private:
99 void ensurePreambleDeclsDeserialized();
100
101 // In-memory preambles must outlive the AST, it is important that this member
102 // goes before Clang and Action.
103 std::shared_ptr<const PreambleData> Preamble;
104 // We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
105 // on it) and CompilerInstance used to run it. That way we don't have to do
106 // complex memory management of all Clang structures on our own. (They are
107 // stored in CompilerInstance and cleaned up by
108 // FrontendAction.EndSourceFile).
109 std::unique_ptr<CompilerInstance> Clang;
110 std::unique_ptr<FrontendAction> Action;
111
112 // Data, stored after parsing.
113 std::vector<DiagWithFixIts> Diags;
114 std::vector<const Decl *> TopLevelDecls;
115 bool PreambleDeclsDeserialized;
116};
117
118// Provides thread-safe access to ParsedAST.
119class ParsedASTWrapper {
120public:
121 ParsedASTWrapper(ParsedASTWrapper &&Wrapper);
122 ParsedASTWrapper(llvm::Optional<ParsedAST> AST);
123
124 /// Runs \p F on wrapped ParsedAST under lock. Ensures it is not accessed
125 /// concurrently.
126 template <class Func> void runUnderLock(Func F) const {
127 std::lock_guard<std::mutex> Lock(Mutex);
128 F(AST ? AST.getPointer() : nullptr);
2
Assuming the condition is false
3
'?' condition is false
4
Passing null pointer value via 1st parameter 'AST'
5
Calling 'operator()'
129 }
130
131private:
132 // This wrapper is used as an argument to std::shared_future (and it returns a
133 // const ref in get()), but we need to have non-const ref in order to
134 // implement some features.
135 mutable std::mutex Mutex;
136 mutable llvm::Optional<ParsedAST> AST;
137};
138
139using ASTParsedCallback =
140 std::function<void(const Context &Ctx, PathRef Path, ParsedAST *)>;
141
142/// Manages resources, required by clangd. Allows to rebuild file with new
143/// contents, and provides AST and Preamble for it.
144class CppFile : public std::enable_shared_from_this<CppFile> {
145public:
146 // We only allow to create CppFile as shared_ptr, because a future returned by
147 // deferRebuild will hold references to it.
148 static std::shared_ptr<CppFile>
149 Create(PathRef FileName, tooling::CompileCommand Command,
150 bool StorePreamblesInMemory,
151 std::shared_ptr<PCHContainerOperations> PCHs,
152 ASTParsedCallback ASTCallback);
153
154private:
155 CppFile(PathRef FileName, tooling::CompileCommand Command,
156 bool StorePreamblesInMemory,
157 std::shared_ptr<PCHContainerOperations> PCHs,
158 ASTParsedCallback ASTCallback);
159
160public:
161 CppFile(CppFile const &) = delete;
162 CppFile(CppFile &&) = delete;
163
164 /// Cancels a scheduled rebuild, if any, and sets AST and Preamble to nulls.
165 /// If a rebuild is in progress, will wait for it to finish.
166 void cancelRebuild();
167
168 /// Similar to deferRebuild, but sets both Preamble and AST to nulls instead
169 /// of doing an actual parsing. Returned function is a deferred computation
170 /// that will wait for any ongoing rebuilds to finish and actually set the AST
171 /// and Preamble to nulls. It can be run on a different thread. This function
172 /// is useful to cancel ongoing rebuilds, if any, before removing CppFile.
173 UniqueFunction<void()> deferCancelRebuild();
174
175 /// Rebuild AST and Preamble synchronously on the calling thread.
176 /// Returns a list of diagnostics or a llvm::None, if another rebuild was
177 /// requested in parallel (effectively cancelling this rebuild) before
178 /// diagnostics were produced.
179 llvm::Optional<std::vector<DiagWithFixIts>>
180 rebuild(const Context &Ctx, StringRef NewContents,
181 IntrusiveRefCntPtr<vfs::FileSystem> VFS);
182
183 /// Schedule a rebuild and return a deferred computation that will finish the
184 /// rebuild, that can be called on a different thread.
185 /// After calling this method, resources, available via futures returned by
186 /// getPreamble() and getAST(), will be waiting for rebuild to finish. A
187 /// continuation fininshing rebuild, returned by this function, must be
188 /// computed(i.e., operator() must be called on it) in order to make those
189 /// resources ready. If deferRebuild is called again before the rebuild is
190 /// finished (either because returned future had not been called or because it
191 /// had not returned yet), the previous rebuild request is cancelled and the
192 /// resource futures (returned by getPreamble() or getAST()) that were not
193 /// ready will be waiting for the last rebuild to finish instead.
194 /// The future to finish rebuild returns a list of diagnostics built during
195 /// reparse, or None, if another deferRebuild was called before this
196 /// rebuild was finished.
197 UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>(const Context &)>
198 deferRebuild(StringRef NewContents, IntrusiveRefCntPtr<vfs::FileSystem> VFS);
199
200 /// Returns a future to get the most fresh PreambleData for a file. The
201 /// future will wait until the Preamble is rebuilt.
202 std::shared_future<std::shared_ptr<const PreambleData>> getPreamble() const;
203 /// Return some preamble for a file. It might be stale, but won't wait for
204 /// rebuild to finish.
205 std::shared_ptr<const PreambleData> getPossiblyStalePreamble() const;
206
207 /// Returns a future to get the most fresh AST for a file. Returned AST is
208 /// wrapped to prevent concurrent accesses.
209 /// We use std::shared_ptr here because MVSC fails to compile non-copyable
210 /// classes as template arguments of promise/future. It is guaranteed to
211 /// always be non-null.
212 std::shared_future<std::shared_ptr<ParsedASTWrapper>> getAST() const;
213
214 /// Get CompileCommand used to build this CppFile.
215 tooling::CompileCommand const &getCompileCommand() const;
216
217private:
218 /// A helper guard that manages the state of CppFile during rebuild.
219 class RebuildGuard {
220 public:
221 RebuildGuard(CppFile &File, unsigned RequestRebuildCounter);
222 ~RebuildGuard();
223
224 bool wasCancelledBeforeConstruction() const;
225
226 private:
227 CppFile &File;
228 unsigned RequestRebuildCounter;
229 bool WasCancelledBeforeConstruction;
230 };
231
232 Path FileName;
233 tooling::CompileCommand Command;
234 bool StorePreamblesInMemory;
235
236 /// Mutex protects all fields, declared below it, FileName and Command are not
237 /// mutated.
238 mutable std::mutex Mutex;
239 /// A counter to cancel old rebuilds.
240 unsigned RebuildCounter;
241 /// Used to wait when rebuild is finished before starting another one.
242 bool RebuildInProgress;
243 /// Condition variable to indicate changes to RebuildInProgress.
244 std::condition_variable RebuildCond;
245
246 /// Promise and future for the latests AST. Fulfilled during rebuild.
247 /// We use std::shared_ptr here because MVSC fails to compile non-copyable
248 /// classes as template arguments of promise/future.
249 std::promise<std::shared_ptr<ParsedASTWrapper>> ASTPromise;
250 std::shared_future<std::shared_ptr<ParsedASTWrapper>> ASTFuture;
251
252 /// Promise and future for the latests Preamble. Fulfilled during rebuild.
253 std::promise<std::shared_ptr<const PreambleData>> PreamblePromise;
254 std::shared_future<std::shared_ptr<const PreambleData>> PreambleFuture;
255 /// Latest preamble that was built. May be stale, but always available without
256 /// waiting for rebuild to finish.
257 std::shared_ptr<const PreambleData> LatestAvailablePreamble;
258 /// Utility class, required by clang.
259 std::shared_ptr<PCHContainerOperations> PCHs;
260 /// This is called after the file is parsed. This can be nullptr if there is
261 /// no callback.
262 ASTParsedCallback ASTCallback;
263};
264
265/// Get the beginning SourceLocation at a specified \p Pos.
266SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos,
267 const FileEntry *FE);
268
269/// For testing/debugging purposes. Note that this method deserializes all
270/// unserialized Decls, so use with care.
271void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS);
272
273} // namespace clangd
274} // namespace clang
275#endif