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-12-20 01:06:07 +08:00
|
|
|
#include "CodeComplete.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "Headers.h"
|
2017-12-19 20:23:48 +08:00
|
|
|
#include "SourceCode.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "XRefs.h"
|
2018-01-15 20:33:00 +08:00
|
|
|
#include "index/Merge.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/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2017-11-16 02:04:56 +08:00
|
|
|
#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
|
|
|
|
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2018-01-26 01:01:39 +08:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2017-10-26 20:28:13 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-09-27 23:31:17 +08:00
|
|
|
#include "llvm/Support/Path.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 {
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
void ignoreError(llvm::Error Err) {
|
|
|
|
handleAllErrors(std::move(Err), [](const llvm::ErrorInfoBase &) {});
|
|
|
|
}
|
|
|
|
|
2017-06-28 18:34:50 +08:00
|
|
|
std::string getStandardResourceDir() {
|
|
|
|
static int Dummy; // Just an address in this process.
|
|
|
|
return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:30:04 +08:00
|
|
|
class RefactoringResultCollector final
|
|
|
|
: public tooling::RefactoringResultConsumer {
|
|
|
|
public:
|
|
|
|
void handleError(llvm::Error Err) override {
|
|
|
|
assert(!Result.hasValue());
|
|
|
|
// FIXME: figure out a way to return better message for DiagnosticError.
|
|
|
|
// clangd uses llvm::toString to convert the Err to string, however, for
|
|
|
|
// DiagnosticError, only "clang diagnostic" will be generated.
|
|
|
|
Result = std::move(Err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the handle(SymbolOccurrences) from parent class.
|
|
|
|
using tooling::RefactoringResultConsumer::handle;
|
|
|
|
|
|
|
|
void handle(tooling::AtomicChanges SourceReplacements) override {
|
|
|
|
assert(!Result.hasValue());
|
|
|
|
Result = std::move(SourceReplacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Expected<tooling::AtomicChanges>> Result;
|
|
|
|
};
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
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-11-24 00:58:22 +08:00
|
|
|
ClangdServer::ClangdServer(GlobalCompilationDatabase &CDB,
|
|
|
|
DiagnosticsConsumer &DiagConsumer,
|
|
|
|
FileSystemProvider &FSProvider,
|
|
|
|
unsigned AsyncThreadsCount,
|
2017-12-13 20:51:22 +08:00
|
|
|
bool StorePreamblesInMemory,
|
2018-01-10 22:44:34 +08:00
|
|
|
bool BuildDynamicSymbolIndex, SymbolIndex *StaticIdx,
|
2017-11-24 00:58:22 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDir)
|
2018-01-25 22:19:21 +08:00
|
|
|
: CompileArgs(CDB,
|
|
|
|
ResourceDir ? ResourceDir->str() : getStandardResourceDir()),
|
|
|
|
DiagConsumer(DiagConsumer), FSProvider(FSProvider),
|
2017-12-20 02:00:37 +08:00
|
|
|
FileIdx(BuildDynamicSymbolIndex ? new FileIndex() : nullptr),
|
2018-01-31 16:51:16 +08:00
|
|
|
PCHs(std::make_shared<PCHContainerOperations>()),
|
|
|
|
// Pass a callback into `WorkScheduler` to extract symbols from a newly
|
|
|
|
// parsed file and rebuild the file index synchronously each time an AST
|
|
|
|
// is parsed.
|
2017-12-20 02:00:37 +08:00
|
|
|
// FIXME(ioeric): this can be slow and we may be able to index on less
|
|
|
|
// critical paths.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
WorkScheduler(AsyncThreadsCount, StorePreamblesInMemory,
|
|
|
|
FileIdx
|
|
|
|
? [this](PathRef Path,
|
|
|
|
ParsedAST *AST) { FileIdx->update(Path, AST); }
|
|
|
|
: ASTParsedCallback()) {
|
2018-01-15 20:33:00 +08:00
|
|
|
if (FileIdx && StaticIdx) {
|
|
|
|
MergedIndex = mergeIndex(FileIdx.get(), StaticIdx);
|
|
|
|
Index = MergedIndex.get();
|
|
|
|
} else if (FileIdx)
|
|
|
|
Index = FileIdx.get();
|
|
|
|
else if (StaticIdx)
|
|
|
|
Index = StaticIdx;
|
|
|
|
else
|
|
|
|
Index = nullptr;
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-09-27 23:31:17 +08:00
|
|
|
void ClangdServer::setRootPath(PathRef RootPath) {
|
|
|
|
std::string NewRootPath = llvm::sys::path::convert_to_slash(
|
|
|
|
RootPath, llvm::sys::path::Style::posix);
|
|
|
|
if (llvm::sys::fs::is_directory(NewRootPath))
|
|
|
|
this->RootPath = NewRootPath;
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
void ClangdServer::addDocument(PathRef File, StringRef Contents,
|
|
|
|
WantDiagnostics WantDiags) {
|
2017-05-23 21:42:59 +08:00
|
|
|
DocVersion Version = DraftMgr.updateDraft(File, Contents);
|
2017-08-01 23:51:38 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2018-02-13 16:59:23 +08:00
|
|
|
scheduleReparseAndDiags(File, VersionedDraft{Version, Contents.str()},
|
2018-02-22 21:11:12 +08:00
|
|
|
WantDiags, std::move(TaggedFS));
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 15:37:35 +08:00
|
|
|
void ClangdServer::removeDocument(PathRef File) {
|
2017-08-14 16:17:24 +08:00
|
|
|
DraftMgr.removeDraft(File);
|
2018-01-25 22:19:21 +08:00
|
|
|
CompileArgs.invalidate(File);
|
2018-02-08 15:37:35 +08:00
|
|
|
WorkScheduler.remove(File);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
void ClangdServer::forceReparse(PathRef File) {
|
2017-08-14 16:37:32 +08:00
|
|
|
auto FileContents = DraftMgr.getDraft(File);
|
|
|
|
assert(FileContents.Draft &&
|
|
|
|
"forceReparse() was called for non-added document");
|
|
|
|
|
2018-01-25 22:19:21 +08:00
|
|
|
// forceReparse promises to request new compilation flags from CDB, so we
|
|
|
|
// remove any cahced flags.
|
|
|
|
CompileArgs.invalidate(File);
|
|
|
|
|
2017-08-14 16:37:32 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2018-02-22 21:11:12 +08:00
|
|
|
scheduleReparseAndDiags(File, std::move(FileContents), WantDiagnostics::Yes,
|
|
|
|
std::move(TaggedFS));
|
2017-05-26 20:26:51 +08:00
|
|
|
}
|
|
|
|
|
2017-10-25 17:35:10 +08:00
|
|
|
void ClangdServer::codeComplete(
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts,
|
|
|
|
UniqueFunction<void(Tagged<CompletionList>)> Callback,
|
2017-12-05 18:42:57 +08:00
|
|
|
llvm::Optional<StringRef> OverridenContents,
|
2017-10-25 17:35:10 +08:00
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
using CallbackType = UniqueFunction<void(Tagged<CompletionList>)>;
|
2017-10-25 17:35:10 +08:00
|
|
|
|
2017-06-14 17:46:44 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2017-08-01 01:09:29 +08:00
|
|
|
if (UsedFS)
|
|
|
|
*UsedFS = TaggedFS.Value;
|
|
|
|
|
2017-12-05 18:42:57 +08:00
|
|
|
// Copy completion options for passing them to async task handler.
|
|
|
|
auto CodeCompleteOpts = Opts;
|
2018-01-15 20:33:00 +08:00
|
|
|
if (!CodeCompleteOpts.Index) // Respect overridden index.
|
|
|
|
CodeCompleteOpts.Index = Index;
|
2018-01-09 22:39:27 +08:00
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
std::string Contents;
|
|
|
|
if (OverridenContents) {
|
|
|
|
Contents = OverridenContents->str();
|
|
|
|
} else {
|
|
|
|
VersionedDraft Latest = DraftMgr.getDraft(File);
|
|
|
|
assert(Latest.Draft && "codeComplete called for non-added document");
|
|
|
|
Contents = *Latest.Draft;
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:39:27 +08:00
|
|
|
// Copy PCHs to avoid accessing this->PCHs concurrently
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs = this->PCHs;
|
2018-01-31 16:51:16 +08:00
|
|
|
auto Task = [PCHs, Pos, TaggedFS, CodeCompleteOpts](
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
std::string Contents, Path File, CallbackType Callback,
|
|
|
|
llvm::Expected<InputsAndPreamble> IP) {
|
2018-01-31 16:51:16 +08:00
|
|
|
assert(IP && "error when trying to read preamble for codeComplete");
|
|
|
|
auto PreambleData = IP->Preamble;
|
|
|
|
auto &Command = IP->Inputs.CompileCommand;
|
|
|
|
|
|
|
|
// FIXME(ibiryukov): even if Preamble is non-null, we may want to check
|
|
|
|
// both the old and the new version in case only one of them matches.
|
|
|
|
CompletionList Result = clangd::codeComplete(
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
File, Command, PreambleData ? &PreambleData->Preamble : nullptr,
|
2018-01-31 16:51:16 +08:00
|
|
|
Contents, Pos, TaggedFS.Value, PCHs, CodeCompleteOpts);
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
Callback(make_tagged(std::move(Result), std::move(TaggedFS.Tag)));
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
WorkScheduler.runWithPreamble("CodeComplete", File,
|
|
|
|
BindWithForward(Task, std::move(Contents),
|
|
|
|
File.str(),
|
|
|
|
std::move(Callback)));
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
void ClangdServer::signatureHelp(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback,
|
|
|
|
llvm::Optional<StringRef> OverridenContents,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
|
2017-10-06 19:54:17 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
|
|
|
if (UsedFS)
|
|
|
|
*UsedFS = TaggedFS.Value;
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
std::string Contents;
|
|
|
|
if (OverridenContents) {
|
|
|
|
Contents = OverridenContents->str();
|
|
|
|
} else {
|
|
|
|
VersionedDraft Latest = DraftMgr.getDraft(File);
|
|
|
|
if (!Latest.Draft)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(llvm::make_error<llvm::StringError>(
|
2018-01-31 16:51:16 +08:00
|
|
|
"signatureHelp is called for non-added document",
|
2018-02-15 21:15:47 +08:00
|
|
|
llvm::errc::invalid_argument));
|
2018-01-31 16:51:16 +08:00
|
|
|
Contents = std::move(*Latest.Draft);
|
|
|
|
}
|
2017-10-06 19:54:17 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
auto PCHs = this->PCHs;
|
|
|
|
auto Action = [Contents, Pos, TaggedFS,
|
|
|
|
PCHs](Path File, decltype(Callback) Callback,
|
|
|
|
llvm::Expected<InputsAndPreamble> IP) {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!IP)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(IP.takeError());
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
auto PreambleData = IP->Preamble;
|
|
|
|
auto &Command = IP->Inputs.CompileCommand;
|
2018-02-15 21:15:47 +08:00
|
|
|
Callback(make_tagged(
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
clangd::signatureHelp(File, Command,
|
2018-01-31 16:51:16 +08:00
|
|
|
PreambleData ? &PreambleData->Preamble : nullptr,
|
|
|
|
Contents, Pos, TaggedFS.Value, PCHs),
|
2018-02-15 21:15:47 +08:00
|
|
|
TaggedFS.Tag));
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
2018-02-15 21:15:47 +08:00
|
|
|
|
|
|
|
WorkScheduler.runWithPreamble(
|
2018-02-19 17:56:28 +08:00
|
|
|
"SignatureHelp", File,
|
|
|
|
BindWithForward(Action, File.str(), std::move(Callback)));
|
2017-10-06 19:54:17 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 04:25:06 +08:00
|
|
|
llvm::Expected<tooling::Replacements>
|
|
|
|
ClangdServer::formatRange(StringRef Code, PathRef File, Range Rng) {
|
2017-05-16 22:40:30 +08:00
|
|
|
size_t Begin = positionToOffset(Code, Rng.start);
|
|
|
|
size_t Len = positionToOffset(Code, Rng.end) - Begin;
|
|
|
|
return formatCode(Code, File, {tooling::Range(Begin, Len)});
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:25:06 +08:00
|
|
|
llvm::Expected<tooling::Replacements> ClangdServer::formatFile(StringRef Code,
|
|
|
|
PathRef File) {
|
2017-05-16 22:40:30 +08:00
|
|
|
// Format everything.
|
|
|
|
return formatCode(Code, File, {tooling::Range(0, Code.size())});
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:25:06 +08:00
|
|
|
llvm::Expected<tooling::Replacements>
|
|
|
|
ClangdServer::formatOnType(StringRef Code, PathRef File, Position Pos) {
|
2017-05-16 22:40:30 +08:00
|
|
|
// Look for the previous opening brace from the character position and
|
|
|
|
// format starting from there.
|
|
|
|
size_t CursorPos = positionToOffset(Code, Pos);
|
|
|
|
size_t PreviousLBracePos = StringRef(Code).find_last_of('{', CursorPos);
|
|
|
|
if (PreviousLBracePos == StringRef::npos)
|
|
|
|
PreviousLBracePos = CursorPos;
|
2017-12-19 20:23:48 +08:00
|
|
|
size_t Len = CursorPos - PreviousLBracePos;
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
return formatCode(Code, File, {tooling::Range(PreviousLBracePos, Len)});
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
void ClangdServer::rename(
|
|
|
|
PathRef File, Position Pos, llvm::StringRef NewName,
|
|
|
|
UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)>
|
|
|
|
Callback) {
|
|
|
|
auto Action = [Pos](Path File, std::string NewName,
|
|
|
|
decltype(Callback) Callback,
|
|
|
|
Expected<InputsAndAST> InpAST) {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(InpAST.takeError());
|
2018-01-31 16:51:16 +08:00
|
|
|
auto &AST = InpAST->AST;
|
|
|
|
|
|
|
|
RefactoringResultCollector ResultCollector;
|
|
|
|
const SourceManager &SourceMgr = AST.getASTContext().getSourceManager();
|
2017-11-09 19:30:04 +08:00
|
|
|
const FileEntry *FE =
|
|
|
|
SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
|
|
|
|
if (!FE)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(llvm::make_error<llvm::StringError>(
|
|
|
|
"rename called for non-added document",
|
|
|
|
llvm::errc::invalid_argument));
|
2017-11-09 19:30:04 +08:00
|
|
|
SourceLocation SourceLocationBeg =
|
2018-01-31 16:51:16 +08:00
|
|
|
clangd::getBeginningOfIdentifier(AST, Pos, FE);
|
2017-11-09 19:30:04 +08:00
|
|
|
tooling::RefactoringRuleContext Context(
|
2018-01-31 16:51:16 +08:00
|
|
|
AST.getASTContext().getSourceManager());
|
|
|
|
Context.setASTContext(AST.getASTContext());
|
2017-11-09 19:30:04 +08:00
|
|
|
auto Rename = clang::tooling::RenameOccurrences::initiate(
|
2018-02-15 21:15:47 +08:00
|
|
|
Context, SourceRange(SourceLocationBeg), NewName);
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!Rename)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(Rename.takeError());
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2017-11-09 19:30:04 +08:00
|
|
|
Rename->invoke(ResultCollector, Context);
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
assert(ResultCollector.Result.hasValue());
|
|
|
|
if (!ResultCollector.Result.getValue())
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(ResultCollector.Result->takeError());
|
2018-01-31 16:51:16 +08:00
|
|
|
|
|
|
|
std::vector<tooling::Replacement> Replacements;
|
|
|
|
for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
|
|
|
|
tooling::Replacements ChangeReps = Change.getReplacements();
|
|
|
|
for (const auto &Rep : ChangeReps) {
|
|
|
|
// FIXME: Right now we only support renaming the main file, so we
|
|
|
|
// drop replacements not for the main file. In the future, we might
|
|
|
|
// consider to support:
|
|
|
|
// * rename in any included header
|
|
|
|
// * rename only in the "main" header
|
|
|
|
// * provide an error if there are symbols we won't rename (e.g.
|
|
|
|
// std::vector)
|
|
|
|
// * rename globally in project
|
|
|
|
// * rename in open files
|
|
|
|
if (Rep.getFilePath() == File)
|
|
|
|
Replacements.push_back(Rep);
|
|
|
|
}
|
2017-11-09 19:30:04 +08:00
|
|
|
}
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(Replacements);
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
2018-02-15 21:15:47 +08:00
|
|
|
|
|
|
|
WorkScheduler.runWithAST(
|
2018-02-19 17:56:28 +08:00
|
|
|
"Rename", File,
|
2018-02-15 21:15:47 +08:00
|
|
|
BindWithForward(Action, File.str(), NewName.str(), std::move(Callback)));
|
2017-11-09 19:30:04 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
Expected<tooling::Replacements>
|
|
|
|
ClangdServer::insertInclude(PathRef File, StringRef Code,
|
|
|
|
llvm::StringRef Header) {
|
|
|
|
std::string ToInclude;
|
|
|
|
if (Header.startswith("<") || Header.startswith("\"")) {
|
|
|
|
ToInclude = Header;
|
|
|
|
} else {
|
|
|
|
auto U = URI::parse(Header);
|
|
|
|
if (!U)
|
|
|
|
return U.takeError();
|
|
|
|
auto Resolved = URI::resolve(*U, /*HintPath=*/File);
|
|
|
|
if (!Resolved)
|
|
|
|
return Resolved.takeError();
|
|
|
|
|
|
|
|
tooling::CompileCommand CompileCommand =
|
|
|
|
CompileArgs.getCompileCommand(File);
|
|
|
|
auto Include =
|
2018-02-20 02:48:44 +08:00
|
|
|
calculateIncludePath(File, Code, *Resolved, CompileCommand,
|
|
|
|
FSProvider.getTaggedFileSystem(File).Value);
|
2018-02-16 22:15:55 +08:00
|
|
|
if (!Include)
|
|
|
|
return Include.takeError();
|
|
|
|
if (Include->empty())
|
|
|
|
return tooling::Replacements();
|
|
|
|
ToInclude = std::move(*Include);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Style = format::getStyle("file", File, "llvm");
|
|
|
|
if (!Style) {
|
|
|
|
llvm::consumeError(Style.takeError());
|
|
|
|
// FIXME(ioeric): needs more consistent style support in clangd server.
|
|
|
|
Style = format::getLLVMStyle();
|
|
|
|
}
|
|
|
|
// Replacement with offset UINT_MAX and length 0 will be treated as include
|
|
|
|
// insertion.
|
|
|
|
tooling::Replacement R(File, /*Offset=*/UINT_MAX, 0, "#include " + ToInclude);
|
|
|
|
return format::cleanupAroundReplacements(Code, tooling::Replacements(R),
|
|
|
|
*Style);
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:30:24 +08:00
|
|
|
llvm::Optional<std::string> ClangdServer::getDocument(PathRef File) {
|
|
|
|
auto Latest = DraftMgr.getDraft(File);
|
|
|
|
if (!Latest.Draft)
|
|
|
|
return llvm::None;
|
|
|
|
return std::move(*Latest.Draft);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
void ClangdServer::dumpAST(PathRef File,
|
|
|
|
UniqueFunction<void(std::string)> Callback) {
|
|
|
|
auto Action = [](decltype(Callback) Callback,
|
|
|
|
llvm::Expected<InputsAndAST> InpAST) {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST) {
|
|
|
|
ignoreError(InpAST.takeError());
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback("<no-ast>");
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
std::string Result;
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
llvm::raw_string_ostream ResultOS(Result);
|
2018-01-31 16:51:16 +08:00
|
|
|
clangd::dumpAST(InpAST->AST, ResultOS);
|
2017-08-01 23:51:38 +08:00
|
|
|
ResultOS.flush();
|
2018-01-31 16:51:16 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
Callback(Result);
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
2018-02-15 21:15:47 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
WorkScheduler.runWithAST("DumpAST", File,
|
|
|
|
BindWithForward(Action, std::move(Callback)));
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
void ClangdServer::findDefinitions(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)>
|
|
|
|
Callback) {
|
2017-06-29 00:12:10 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2018-02-15 21:15:47 +08:00
|
|
|
auto Action = [Pos, TaggedFS](decltype(Callback) Callback,
|
|
|
|
llvm::Expected<InputsAndAST> InpAST) {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(InpAST.takeError());
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
auto Result = clangd::findDefinitions(InpAST->AST, Pos);
|
2018-02-15 21:15:47 +08:00
|
|
|
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
2018-02-15 21:15:47 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
WorkScheduler.runWithAST("Definitions", File,
|
|
|
|
BindWithForward(Action, std::move(Callback)));
|
2017-06-29 00:12:10 +08:00
|
|
|
}
|
2017-08-14 16:17:24 +08:00
|
|
|
|
2017-09-28 11:14:40 +08:00
|
|
|
llvm::Optional<Path> ClangdServer::switchSourceHeader(PathRef Path) {
|
|
|
|
|
|
|
|
StringRef SourceExtensions[] = {".cpp", ".c", ".cc", ".cxx",
|
|
|
|
".c++", ".m", ".mm"};
|
|
|
|
StringRef HeaderExtensions[] = {".h", ".hh", ".hpp", ".hxx", ".inc"};
|
|
|
|
|
|
|
|
StringRef PathExt = llvm::sys::path::extension(Path);
|
|
|
|
|
|
|
|
// Lookup in a list of known extensions.
|
|
|
|
auto SourceIter =
|
|
|
|
std::find_if(std::begin(SourceExtensions), std::end(SourceExtensions),
|
|
|
|
[&PathExt](PathRef SourceExt) {
|
|
|
|
return SourceExt.equals_lower(PathExt);
|
|
|
|
});
|
|
|
|
bool IsSource = SourceIter != std::end(SourceExtensions);
|
|
|
|
|
|
|
|
auto HeaderIter =
|
|
|
|
std::find_if(std::begin(HeaderExtensions), std::end(HeaderExtensions),
|
|
|
|
[&PathExt](PathRef HeaderExt) {
|
|
|
|
return HeaderExt.equals_lower(PathExt);
|
|
|
|
});
|
|
|
|
|
|
|
|
bool IsHeader = HeaderIter != std::end(HeaderExtensions);
|
|
|
|
|
|
|
|
// We can only switch between extensions known extensions.
|
|
|
|
if (!IsSource && !IsHeader)
|
|
|
|
return llvm::None;
|
|
|
|
|
|
|
|
// Array to lookup extensions for the switch. An opposite of where original
|
|
|
|
// extension was found.
|
|
|
|
ArrayRef<StringRef> NewExts;
|
|
|
|
if (IsSource)
|
|
|
|
NewExts = HeaderExtensions;
|
|
|
|
else
|
|
|
|
NewExts = SourceExtensions;
|
|
|
|
|
|
|
|
// Storage for the new path.
|
|
|
|
SmallString<128> NewPath = StringRef(Path);
|
|
|
|
|
|
|
|
// Instance of vfs::FileSystem, used for file existence checks.
|
|
|
|
auto FS = FSProvider.getTaggedFileSystem(Path).Value;
|
|
|
|
|
|
|
|
// Loop through switched extension candidates.
|
|
|
|
for (StringRef NewExt : NewExts) {
|
|
|
|
llvm::sys::path::replace_extension(NewPath, NewExt);
|
|
|
|
if (FS->exists(NewPath))
|
|
|
|
return NewPath.str().str(); // First str() to convert from SmallString to
|
|
|
|
// StringRef, second to convert from StringRef
|
|
|
|
// to std::string
|
2017-10-06 22:39:39 +08:00
|
|
|
|
2017-09-28 11:14:40 +08:00
|
|
|
// Also check NewExt in upper-case, just in case.
|
|
|
|
llvm::sys::path::replace_extension(NewPath, NewExt.upper());
|
|
|
|
if (FS->exists(NewPath))
|
|
|
|
return NewPath.str().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:25:06 +08:00
|
|
|
llvm::Expected<tooling::Replacements>
|
|
|
|
ClangdServer::formatCode(llvm::StringRef Code, PathRef File,
|
|
|
|
ArrayRef<tooling::Range> Ranges) {
|
|
|
|
// Call clang-format.
|
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
|
|
|
auto StyleOrError =
|
|
|
|
format::getStyle("file", File, "LLVM", Code, TaggedFS.Value.get());
|
|
|
|
if (!StyleOrError) {
|
|
|
|
return StyleOrError.takeError();
|
|
|
|
} else {
|
|
|
|
return format::reformat(StyleOrError.get(), Code, Ranges, File);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
void ClangdServer::findDocumentHighlights(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<std::vector<DocumentHighlight>>>)>
|
|
|
|
Callback) {
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
auto FileContents = DraftMgr.getDraft(File);
|
|
|
|
if (!FileContents.Draft)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(llvm::make_error<llvm::StringError>(
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
"findDocumentHighlights called on non-added file",
|
2018-02-15 21:15:47 +08:00
|
|
|
llvm::errc::invalid_argument));
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
|
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
|
|
|
|
2018-02-15 21:15:47 +08:00
|
|
|
auto Action = [TaggedFS, Pos](decltype(Callback) Callback,
|
|
|
|
llvm::Expected<InputsAndAST> InpAST) {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST)
|
2018-02-15 21:15:47 +08:00
|
|
|
return Callback(InpAST.takeError());
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
auto Result = clangd::findDocumentHighlights(InpAST->AST, Pos);
|
2018-02-15 21:15:47 +08:00
|
|
|
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
|
2018-01-31 16:51:16 +08:00
|
|
|
};
|
2018-02-15 21:15:47 +08:00
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
WorkScheduler.runWithAST("Highlights", File,
|
|
|
|
BindWithForward(Action, std::move(Callback)));
|
[clangd] Implement textDocument/hover
Summary: Implemention of textDocument/hover as described in LSP definition.
This patch adds a basic Hover implementation. When hovering a variable,
function, method or namespace, clangd will return a text containing the
declaration's scope, as well as the declaration of the hovered entity.
For example, for a variable:
Declared in class Foo::Bar
int hello = 2
For macros, the macro definition is returned.
This patch doesn't include:
- markdown support (the client I use doesn't support it yet)
- range support (optional in the Hover response)
- comments associated to variables/functions/classes
They are kept as future work to keep this patch simpler.
I added tests in XRefsTests.cpp. hover.test contains one simple
smoketest to make sure the feature works from a black box perspective.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: sammccall, mgrang, klimek, rwols, ilya-biryukov, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D35894
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325395
2018-02-17 05:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClangdServer::findHover(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<Hover>>)> Callback) {
|
|
|
|
Hover FinalHover;
|
|
|
|
auto FileContents = DraftMgr.getDraft(File);
|
|
|
|
if (!FileContents.Draft)
|
|
|
|
return Callback(llvm::make_error<llvm::StringError>(
|
|
|
|
"findHover called on non-added file", llvm::errc::invalid_argument));
|
|
|
|
|
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
|
|
|
|
|
|
|
auto Action = [Pos, TaggedFS](decltype(Callback) Callback,
|
|
|
|
llvm::Expected<InputsAndAST> InpAST) {
|
|
|
|
if (!InpAST)
|
|
|
|
return Callback(InpAST.takeError());
|
|
|
|
|
|
|
|
Hover Result = clangd::getHover(InpAST->AST, Pos);
|
|
|
|
Callback(make_tagged(std::move(Result), TaggedFS.Tag));
|
|
|
|
};
|
|
|
|
|
2018-02-19 17:56:28 +08:00
|
|
|
WorkScheduler.runWithAST("Hover", File,
|
|
|
|
BindWithForward(Action, std::move(Callback)));
|
[clangd] Document highlights for clangd
Summary: Implementation of Document Highlights Request as described in
LSP.
Contributed by William Enright (nebiroth).
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Reviewed By: malaperle
Subscribers: mgrang, sammccall, klimek, ioeric, rwols, cfe-commits, arphaman, ilya-biryukov
Differential Revision: https://reviews.llvm.org/D38425
llvm-svn: 320474
2017-12-12 20:27:47 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
void ClangdServer::scheduleReparseAndDiags(
|
2018-02-22 21:11:12 +08:00
|
|
|
PathRef File, VersionedDraft Contents, WantDiagnostics WantDiags,
|
2018-01-25 22:19:21 +08:00
|
|
|
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
|
2018-01-31 16:51:16 +08:00
|
|
|
tooling::CompileCommand Command = CompileArgs.getCompileCommand(File);
|
|
|
|
|
|
|
|
DocVersion Version = Contents.Version;
|
|
|
|
Path FileStr = File.str();
|
|
|
|
VFSTag Tag = std::move(TaggedFS.Tag);
|
2018-01-25 22:19:21 +08:00
|
|
|
|
2018-02-22 21:11:12 +08:00
|
|
|
auto Callback = [this, Version, FileStr,
|
|
|
|
Tag](std::vector<DiagWithFixIts> Diags) {
|
2017-09-20 20:58:55 +08:00
|
|
|
// We need to serialize access to resulting diagnostics to avoid calling
|
|
|
|
// `onDiagnosticsReady` in the wrong order.
|
|
|
|
std::lock_guard<std::mutex> DiagsLock(DiagnosticsMutex);
|
|
|
|
DocVersion &LastReportedDiagsVersion = ReportedDiagnosticVersions[FileStr];
|
|
|
|
// FIXME(ibiryukov): get rid of '<' comparison here. In the current
|
|
|
|
// implementation diagnostics will not be reported after version counters'
|
|
|
|
// overflow. This should not happen in practice, since DocVersion is a
|
|
|
|
// 64-bit unsigned integer.
|
|
|
|
if (Version < LastReportedDiagsVersion)
|
|
|
|
return;
|
|
|
|
LastReportedDiagsVersion = Version;
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
DiagConsumer.onDiagnosticsReady(
|
2018-02-22 21:11:12 +08:00
|
|
|
FileStr, make_tagged(std::move(Diags), std::move(Tag)));
|
2017-08-14 16:17:24 +08:00
|
|
|
};
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
WorkScheduler.update(File,
|
2018-01-31 16:51:16 +08:00
|
|
|
ParseInputs{std::move(Command),
|
|
|
|
std::move(TaggedFS.Value),
|
|
|
|
std::move(*Contents.Draft)},
|
2018-02-22 21:11:12 +08:00
|
|
|
WantDiags, std::move(Callback));
|
2017-08-14 16:17:24 +08:00
|
|
|
}
|
2017-10-03 02:00:37 +08:00
|
|
|
|
|
|
|
void ClangdServer::onFileEvent(const DidChangeWatchedFilesParams &Params) {
|
|
|
|
// FIXME: Do nothing for now. This will be used for indexing and potentially
|
|
|
|
// invalidating other caches.
|
|
|
|
}
|
2018-01-25 22:32:21 +08:00
|
|
|
|
|
|
|
std::vector<std::pair<Path, std::size_t>>
|
|
|
|
ClangdServer::getUsedBytesPerFile() const {
|
2018-01-31 16:51:16 +08:00
|
|
|
return WorkScheduler.getUsedBytesPerFile();
|
2018-01-25 22:32:21 +08:00
|
|
|
}
|
2018-02-13 16:59:23 +08:00
|
|
|
|
|
|
|
LLVM_NODISCARD bool
|
|
|
|
ClangdServer::blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds) {
|
|
|
|
return WorkScheduler.blockUntilIdle(timeoutSeconds(TimeoutSeconds));
|
|
|
|
}
|