2017-05-16 17:38:59 +08:00
|
|
|
//===--- 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"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-05-23 21:42:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <future>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-16 18:06:20 +08:00
|
|
|
using namespace clang;
|
2017-05-16 17:38:59 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::vector<tooling::Replacement> formatCode(StringRef Code, StringRef Filename,
|
|
|
|
ArrayRef<tooling::Range> Ranges) {
|
|
|
|
// Call clang-format.
|
|
|
|
// FIXME: Don't ignore style.
|
|
|
|
format::FormatStyle Style = format::getLLVMStyle();
|
|
|
|
auto Result = format::reformat(Style, Code, Ranges, Filename);
|
|
|
|
|
|
|
|
return std::vector<tooling::Replacement>(Result.begin(), Result.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
size_t clangd::positionToOffset(StringRef Code, Position P) {
|
|
|
|
size_t Offset = 0;
|
|
|
|
for (int I = 0; I != P.line; ++I) {
|
|
|
|
// FIXME: \r\n
|
|
|
|
// FIXME: UTF-8
|
|
|
|
size_t F = Code.find('\n', Offset);
|
|
|
|
if (F == StringRef::npos)
|
|
|
|
return 0; // FIXME: Is this reasonable?
|
|
|
|
Offset = F + 1;
|
|
|
|
}
|
|
|
|
return (Offset == 0 ? 0 : (Offset - 1)) + P.character;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn an offset in Code into a [line, column] pair.
|
|
|
|
Position clangd::offsetToPosition(StringRef Code, size_t Offset) {
|
|
|
|
StringRef JustBefore = Code.substr(0, Offset);
|
|
|
|
// FIXME: \r\n
|
|
|
|
// FIXME: UTF-8
|
|
|
|
int Lines = JustBefore.count('\n');
|
|
|
|
int Cols = JustBefore.size() - JustBefore.rfind('\n') - 1;
|
|
|
|
return {Lines, Cols};
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:11:02 +08:00
|
|
|
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
|
2017-06-14 17:46:44 +08:00
|
|
|
RealFileSystemProvider::getTaggedFileSystem(PathRef File) {
|
2017-05-30 23:11:02 +08:00
|
|
|
return make_tagged(vfs::getRealFileSystem(), VFSTag());
|
2017-05-26 20:26:51 +08:00
|
|
|
}
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
ClangdScheduler::ClangdScheduler(bool RunSynchronously)
|
2017-05-16 17:38:59 +08:00
|
|
|
: RunSynchronously(RunSynchronously) {
|
|
|
|
if (RunSynchronously) {
|
|
|
|
// Don't start the worker thread if we're running synchronously
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize Worker in ctor body, rather than init list to avoid potentially
|
|
|
|
// using not-yet-initialized members
|
2017-05-23 21:42:59 +08:00
|
|
|
Worker = std::thread([this]() {
|
2017-05-16 17:38:59 +08:00
|
|
|
while (true) {
|
2017-05-23 21:42:59 +08:00
|
|
|
std::function<void()> Request;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
// Pick request from the queue
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
// Wait for more requests.
|
|
|
|
RequestCV.wait(Lock, [this] { return !RequestQueue.empty() || Done; });
|
|
|
|
if (Done)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!RequestQueue.empty() && "RequestQueue was empty");
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
// We process requests starting from the front of the queue. Users of
|
|
|
|
// ClangdScheduler have a way to prioritise their requests by putting
|
|
|
|
// them to the either side of the queue (using either addToEnd or
|
|
|
|
// addToFront).
|
|
|
|
Request = std::move(RequestQueue.front());
|
|
|
|
RequestQueue.pop_front();
|
2017-05-16 17:38:59 +08:00
|
|
|
} // unlock Mutex
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
Request();
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ClangdScheduler::~ClangdScheduler() {
|
|
|
|
if (RunSynchronously)
|
|
|
|
return; // no worker thread is running in that case
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
// Wake up the worker thread
|
|
|
|
Done = true;
|
|
|
|
} // unlock Mutex
|
2017-05-23 21:42:59 +08:00
|
|
|
RequestCV.notify_one();
|
2017-05-16 17:38:59 +08:00
|
|
|
Worker.join();
|
|
|
|
}
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
void ClangdScheduler::addToFront(std::function<void()> Request) {
|
2017-05-16 17:38:59 +08:00
|
|
|
if (RunSynchronously) {
|
2017-05-23 21:42:59 +08:00
|
|
|
Request();
|
2017-05-16 17:38:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
RequestQueue.push_front(Request);
|
|
|
|
}
|
|
|
|
RequestCV.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdScheduler::addToEnd(std::function<void()> Request) {
|
|
|
|
if (RunSynchronously) {
|
|
|
|
Request();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
RequestQueue.push_back(Request);
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
RequestCV.notify_one();
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:59:43 +08:00
|
|
|
ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
|
|
|
|
DiagnosticsConsumer &DiagConsumer,
|
|
|
|
FileSystemProvider &FSProvider,
|
2017-05-16 17:38:59 +08:00
|
|
|
bool RunSynchronously)
|
2017-06-13 23:59:43 +08:00
|
|
|
: CDB(CDB), DiagConsumer(DiagConsumer), FSProvider(FSProvider),
|
2017-05-16 17:38:59 +08:00
|
|
|
PCHs(std::make_shared<PCHContainerOperations>()),
|
2017-05-23 21:42:59 +08:00
|
|
|
WorkScheduler(RunSynchronously) {}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
void ClangdServer::addDocument(PathRef File, StringRef Contents) {
|
2017-05-23 21:42:59 +08:00
|
|
|
DocVersion Version = DraftMgr.updateDraft(File, Contents);
|
|
|
|
Path FileStr = File;
|
|
|
|
WorkScheduler.addToFront([this, FileStr, Version]() {
|
|
|
|
auto FileContents = DraftMgr.getDraft(FileStr);
|
|
|
|
if (FileContents.Version != Version)
|
|
|
|
return; // This request is outdated, do nothing
|
|
|
|
|
|
|
|
assert(FileContents.Draft &&
|
|
|
|
"No contents inside a file that was scheduled for reparse");
|
2017-06-14 17:46:44 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(FileStr);
|
2017-05-30 23:11:02 +08:00
|
|
|
Units.runOnUnit(
|
2017-06-13 23:59:43 +08:00
|
|
|
FileStr, *FileContents.Draft, CDB, PCHs, TaggedFS.Value,
|
2017-05-30 23:11:02 +08:00
|
|
|
[&](ClangdUnit const &Unit) {
|
2017-06-13 23:59:43 +08:00
|
|
|
DiagConsumer.onDiagnosticsReady(
|
2017-05-30 23:11:02 +08:00
|
|
|
FileStr, make_tagged(Unit.getLocalDiagnostics(), TaggedFS.Tag));
|
|
|
|
});
|
2017-05-23 21:42:59 +08:00
|
|
|
});
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdServer::removeDocument(PathRef File) {
|
2017-05-23 21:42:59 +08:00
|
|
|
auto Version = DraftMgr.removeDraft(File);
|
|
|
|
Path FileStr = File;
|
|
|
|
WorkScheduler.addToFront([this, FileStr, Version]() {
|
|
|
|
if (Version != DraftMgr.getVersion(FileStr))
|
|
|
|
return; // This request is outdated, do nothing
|
|
|
|
|
|
|
|
Units.removeUnitIfPresent(FileStr);
|
|
|
|
});
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
void ClangdServer::forceReparse(PathRef File) {
|
|
|
|
// The addDocument schedules the reparse even if the contents of the file
|
|
|
|
// never changed, so we just call it here.
|
|
|
|
addDocument(File, getDocument(File));
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:15:56 +08:00
|
|
|
Tagged<std::vector<CompletionItem>>
|
|
|
|
ClangdServer::codeComplete(PathRef File, Position Pos,
|
|
|
|
llvm::Optional<StringRef> OverridenContents) {
|
|
|
|
std::string DraftStorage;
|
|
|
|
if (!OverridenContents) {
|
|
|
|
auto FileContents = DraftMgr.getDraft(File);
|
|
|
|
assert(FileContents.Draft &&
|
|
|
|
"codeComplete is called for non-added document");
|
|
|
|
|
|
|
|
DraftStorage = std::move(*FileContents.Draft);
|
|
|
|
OverridenContents = DraftStorage;
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
std::vector<CompletionItem> Result;
|
2017-06-14 17:46:44 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2017-06-13 22:15:56 +08:00
|
|
|
// It would be nice to use runOnUnitWithoutReparse here, but we can't
|
|
|
|
// guarantee the correctness of code completion cache here if we don't do the
|
|
|
|
// reparse.
|
2017-06-13 23:59:43 +08:00
|
|
|
Units.runOnUnit(File, *OverridenContents, CDB, PCHs, TaggedFS.Value,
|
2017-06-13 22:15:56 +08:00
|
|
|
[&](ClangdUnit &Unit) {
|
|
|
|
Result = Unit.codeComplete(*OverridenContents, Pos,
|
|
|
|
TaggedFS.Value);
|
|
|
|
});
|
2017-05-30 23:11:02 +08:00
|
|
|
return make_tagged(std::move(Result), TaggedFS.Tag);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
std::vector<tooling::Replacement> ClangdServer::formatRange(PathRef File,
|
|
|
|
Range Rng) {
|
|
|
|
std::string Code = getDocument(File);
|
|
|
|
|
|
|
|
size_t Begin = positionToOffset(Code, Rng.start);
|
|
|
|
size_t Len = positionToOffset(Code, Rng.end) - Begin;
|
|
|
|
return formatCode(Code, File, {tooling::Range(Begin, Len)});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<tooling::Replacement> ClangdServer::formatFile(PathRef File) {
|
|
|
|
// Format everything.
|
|
|
|
std::string Code = getDocument(File);
|
|
|
|
return formatCode(Code, File, {tooling::Range(0, Code.size())});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<tooling::Replacement> ClangdServer::formatOnType(PathRef File,
|
|
|
|
Position Pos) {
|
|
|
|
// Look for the previous opening brace from the character position and
|
|
|
|
// format starting from there.
|
|
|
|
std::string Code = getDocument(File);
|
|
|
|
size_t CursorPos = positionToOffset(Code, Pos);
|
|
|
|
size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
|
|
|
|
if (PreviousLBracePos == StringRef::npos)
|
|
|
|
PreviousLBracePos = CursorPos;
|
|
|
|
size_t Len = 1 + CursorPos - PreviousLBracePos;
|
|
|
|
|
|
|
|
return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
std::string ClangdServer::getDocument(PathRef File) {
|
|
|
|
auto draft = DraftMgr.getDraft(File);
|
|
|
|
assert(draft.Draft && "File is not tracked, cannot get contents");
|
|
|
|
return *draft.Draft;
|
|
|
|
}
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
std::string ClangdServer::dumpAST(PathRef File) {
|
|
|
|
std::promise<std::string> DumpPromise;
|
|
|
|
auto DumpFuture = DumpPromise.get_future();
|
|
|
|
auto Version = DraftMgr.getVersion(File);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
WorkScheduler.addToEnd([this, &DumpPromise, File, Version]() {
|
|
|
|
assert(DraftMgr.getVersion(File) == Version && "Version has changed");
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
Units.runOnExistingUnit(File, [&DumpPromise](ClangdUnit &Unit) {
|
|
|
|
std::string Result;
|
|
|
|
|
|
|
|
llvm::raw_string_ostream ResultOS(Result);
|
|
|
|
Unit.dumpAST(ResultOS);
|
|
|
|
ResultOS.flush();
|
|
|
|
|
|
|
|
DumpPromise.set_value(std::move(Result));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return DumpFuture.get();
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|