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" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame^] | 11 | #include "clang/Format/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/ASTUnit.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/ArrayRef.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
| 18 | |
Ilya Biryukov | 2f31410 | 2017-05-16 10:06:20 +0000 | [diff] [blame] | 19 | using namespace clang; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | using namespace clang::clangd; |
| 21 | |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame^] | 22 | namespace { |
| 23 | |
| 24 | std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename, |
| 25 | ArrayRef<tooling::Range> Ranges) { |
| 26 | // Call clang-format. |
| 27 | // FIXME: Don't ignore style. |
| 28 | format::FormatStyle Style = format::getLLVMStyle(); |
| 29 | auto Result = format::reformat(Style, Code, Ranges, Filename); |
| 30 | |
| 31 | return std::vector<tooling::Replacement>(Result.begin(), Result.end()); |
| 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | size_t clangd::positionToOffset(StringRef Code, Position P) { |
| 37 | size_t Offset = 0; |
| 38 | for (int I = 0; I != P.line; ++I) { |
| 39 | // FIXME: \r\n |
| 40 | // FIXME: UTF-8 |
| 41 | size_t F = Code.find('\n', Offset); |
| 42 | if (F == StringRef::npos) |
| 43 | return 0; // FIXME: Is this reasonable? |
| 44 | Offset = F + 1; |
| 45 | } |
| 46 | return (Offset == 0 ? 0 : (Offset - 1)) + P.character; |
| 47 | } |
| 48 | |
| 49 | /// Turn an offset in Code into a [line, column] pair. |
| 50 | Position clangd::offsetToPosition(StringRef Code, size_t Offset) { |
| 51 | StringRef JustBefore = Code.substr(0, Offset); |
| 52 | // FIXME: \r\n |
| 53 | // FIXME: UTF-8 |
| 54 | int Lines = JustBefore.count('\n'); |
| 55 | int Cols = JustBefore.size() - JustBefore.rfind('\n') - 1; |
| 56 | return {Lines, Cols}; |
| 57 | } |
| 58 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 59 | WorkerRequest::WorkerRequest(WorkerRequestKind Kind, Path File, |
| 60 | DocVersion Version) |
| 61 | : Kind(Kind), File(File), Version(Version) {} |
| 62 | |
| 63 | ClangdScheduler::ClangdScheduler(ClangdServer &Server, bool RunSynchronously) |
| 64 | : RunSynchronously(RunSynchronously) { |
| 65 | if (RunSynchronously) { |
| 66 | // Don't start the worker thread if we're running synchronously |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Initialize Worker in ctor body, rather than init list to avoid potentially |
| 71 | // using not-yet-initialized members |
| 72 | Worker = std::thread([&Server, this]() { |
| 73 | while (true) { |
| 74 | WorkerRequest Request; |
| 75 | |
| 76 | // Pick request from the queue |
| 77 | { |
| 78 | std::unique_lock<std::mutex> Lock(Mutex); |
| 79 | // Wait for more requests. |
| 80 | RequestCV.wait(Lock, [this] { return !RequestQueue.empty() || Done; }); |
| 81 | if (Done) |
| 82 | return; |
| 83 | |
| 84 | assert(!RequestQueue.empty() && "RequestQueue was empty"); |
| 85 | |
| 86 | Request = std::move(RequestQueue.back()); |
| 87 | RequestQueue.pop_back(); |
| 88 | |
| 89 | // Skip outdated requests |
| 90 | if (Request.Version != Server.DraftMgr.getVersion(Request.File)) { |
| 91 | // FIXME(ibiryukov): Logging |
| 92 | // Output.log("Version for " + Twine(Request.File) + |
| 93 | // " in request is outdated, skipping request\n"); |
| 94 | continue; |
| 95 | } |
| 96 | } // unlock Mutex |
| 97 | |
| 98 | Server.handleRequest(std::move(Request)); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | ClangdScheduler::~ClangdScheduler() { |
| 104 | if (RunSynchronously) |
| 105 | return; // no worker thread is running in that case |
| 106 | |
| 107 | { |
| 108 | std::lock_guard<std::mutex> Lock(Mutex); |
| 109 | // Wake up the worker thread |
| 110 | Done = true; |
| 111 | RequestCV.notify_one(); |
| 112 | } // unlock Mutex |
| 113 | Worker.join(); |
| 114 | } |
| 115 | |
| 116 | void ClangdScheduler::enqueue(ClangdServer &Server, WorkerRequest Request) { |
| 117 | if (RunSynchronously) { |
| 118 | Server.handleRequest(Request); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | std::lock_guard<std::mutex> Lock(Mutex); |
| 123 | RequestQueue.push_back(Request); |
| 124 | RequestCV.notify_one(); |
| 125 | } |
| 126 | |
| 127 | ClangdServer::ClangdServer(std::unique_ptr<GlobalCompilationDatabase> CDB, |
| 128 | std::unique_ptr<DiagnosticsConsumer> DiagConsumer, |
| 129 | bool RunSynchronously) |
| 130 | : CDB(std::move(CDB)), DiagConsumer(std::move(DiagConsumer)), |
| 131 | PCHs(std::make_shared<PCHContainerOperations>()), |
| 132 | WorkScheduler(*this, RunSynchronously) {} |
| 133 | |
| 134 | void ClangdServer::addDocument(PathRef File, StringRef Contents) { |
| 135 | DocVersion NewVersion = DraftMgr.updateDraft(File, Contents); |
| 136 | WorkScheduler.enqueue( |
| 137 | *this, WorkerRequest(WorkerRequestKind::ParseAndPublishDiagnostics, File, |
| 138 | NewVersion)); |
| 139 | } |
| 140 | |
| 141 | void ClangdServer::removeDocument(PathRef File) { |
| 142 | auto NewVersion = DraftMgr.removeDraft(File); |
| 143 | WorkScheduler.enqueue( |
| 144 | *this, WorkerRequest(WorkerRequestKind::RemoveDocData, File, NewVersion)); |
| 145 | } |
| 146 | |
| 147 | std::vector<CompletionItem> ClangdServer::codeComplete(PathRef File, |
| 148 | Position Pos) { |
| 149 | auto FileContents = DraftMgr.getDraft(File); |
| 150 | assert(FileContents.Draft && "codeComplete is called for non-added document"); |
| 151 | |
| 152 | std::vector<CompletionItem> Result; |
| 153 | Units.runOnUnitWithoutReparse( |
| 154 | File, *FileContents.Draft, *CDB, PCHs, [&](ClangdUnit &Unit) { |
| 155 | Result = Unit.codeComplete(*FileContents.Draft, Pos); |
| 156 | }); |
| 157 | return Result; |
| 158 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame^] | 159 | std::vector<tooling::Replacement> ClangdServer::formatRange(PathRef File, |
| 160 | Range Rng) { |
| 161 | std::string Code = getDocument(File); |
| 162 | |
| 163 | size_t Begin = positionToOffset(Code, Rng.start); |
| 164 | size_t Len = positionToOffset(Code, Rng.end) - Begin; |
| 165 | return formatCode(Code, File, {tooling::Range(Begin, Len)}); |
| 166 | } |
| 167 | |
| 168 | std::vector<tooling::Replacement> ClangdServer::formatFile(PathRef File) { |
| 169 | // Format everything. |
| 170 | std::string Code = getDocument(File); |
| 171 | return formatCode(Code, File, {tooling::Range(0, Code.size())}); |
| 172 | } |
| 173 | |
| 174 | std::vector<tooling::Replacement> ClangdServer::formatOnType(PathRef File, |
| 175 | Position Pos) { |
| 176 | // Look for the previous opening brace from the character position and |
| 177 | // format starting from there. |
| 178 | std::string Code = getDocument(File); |
| 179 | size_t CursorPos = positionToOffset(Code, Pos); |
| 180 | size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos); |
| 181 | if (PreviousLBracePos == StringRef::npos) |
| 182 | PreviousLBracePos = CursorPos; |
| 183 | size_t Len = 1 + CursorPos - PreviousLBracePos; |
| 184 | |
| 185 | return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)}); |
| 186 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 187 | |
| 188 | std::string ClangdServer::getDocument(PathRef File) { |
| 189 | auto draft = DraftMgr.getDraft(File); |
| 190 | assert(draft.Draft && "File is not tracked, cannot get contents"); |
| 191 | return *draft.Draft; |
| 192 | } |
| 193 | |
| 194 | void ClangdServer::handleRequest(WorkerRequest Request) { |
| 195 | switch (Request.Kind) { |
| 196 | case WorkerRequestKind::ParseAndPublishDiagnostics: { |
| 197 | auto FileContents = DraftMgr.getDraft(Request.File); |
| 198 | if (FileContents.Version != Request.Version) |
| 199 | return; // This request is outdated, do nothing |
| 200 | |
| 201 | assert(FileContents.Draft && |
| 202 | "No contents inside a file that was scheduled for reparse"); |
| 203 | Units.runOnUnit(Request.File, *FileContents.Draft, *CDB, PCHs, |
| 204 | [&](ClangdUnit const &Unit) { |
| 205 | DiagConsumer->onDiagnosticsReady( |
| 206 | Request.File, Unit.getLocalDiagnostics()); |
| 207 | }); |
| 208 | break; |
| 209 | } |
| 210 | case WorkerRequestKind::RemoveDocData: |
| 211 | if (Request.Version != DraftMgr.getVersion(Request.File)) |
| 212 | return; // This request is outdated, do nothing |
| 213 | |
| 214 | Units.removeUnitIfPresent(Request.File); |
| 215 | break; |
| 216 | } |
| 217 | } |