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