Bug Summary

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

Annotated Source Code

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

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