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"
|
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
|
|
|
// Issues an async read of AST and waits for results.
|
|
|
|
template <class Ret, class Func>
|
|
|
|
Ret blockingRunWithAST(TUScheduler &S, PathRef File, Func &&F) {
|
2018-01-31 19:26:43 +08:00
|
|
|
// Using shared_ptr to workaround MSVC bug. It requires future<> arguments to
|
|
|
|
// have default and copy ctor.
|
|
|
|
auto SharedPtrFunc = [&](llvm::Expected<InputsAndAST> Arg) {
|
|
|
|
return std::make_shared<Ret>(F(std::move(Arg)));
|
|
|
|
};
|
|
|
|
std::packaged_task<std::shared_ptr<Ret>(llvm::Expected<InputsAndAST>)> Task(
|
|
|
|
SharedPtrFunc);
|
2018-01-31 16:51:16 +08:00
|
|
|
auto Future = Task.get_future();
|
|
|
|
S.runWithAST(File, std::move(Task));
|
2018-01-31 19:26:43 +08:00
|
|
|
return std::move(*Future.get());
|
2018-01-31 16:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Issues an async read of preamble and waits for results.
|
|
|
|
template <class Ret, class Func>
|
|
|
|
Ret blockingRunWithPreamble(TUScheduler &S, PathRef File, Func &&F) {
|
2018-01-31 19:26:43 +08:00
|
|
|
// Using shared_ptr to workaround MSVC bug. It requires future<> arguments to
|
|
|
|
// have default and copy ctor.
|
|
|
|
auto SharedPtrFunc = [&](llvm::Expected<InputsAndPreamble> Arg) {
|
|
|
|
return std::make_shared<Ret>(F(std::move(Arg)));
|
|
|
|
};
|
|
|
|
std::packaged_task<std::shared_ptr<Ret>(llvm::Expected<InputsAndPreamble>)>
|
|
|
|
Task(SharedPtrFunc);
|
2018-01-31 16:51:16 +08:00
|
|
|
auto Future = Task.get_future();
|
|
|
|
S.runWithPreamble(File, std::move(Task));
|
2018-01-31 19:26:43 +08:00
|
|
|
return std::move(*Future.get());
|
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-13 16:59:23 +08:00
|
|
|
void ClangdServer::addDocument(PathRef File, StringRef Contents) {
|
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()},
|
|
|
|
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-13 16:59:23 +08:00
|
|
|
scheduleReparseAndDiags(File, std::move(FileContents), 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
|
|
|
};
|
|
|
|
|
[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.runWithPreamble(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
|
|
|
|
2017-10-26 20:28:13 +08:00
|
|
|
llvm::Expected<Tagged<SignatureHelp>>
|
[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
|
|
|
ClangdServer::signatureHelp(PathRef File, Position Pos,
|
2017-10-06 19:54:17 +08:00
|
|
|
llvm::Optional<StringRef> OverridenContents,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS) {
|
|
|
|
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)
|
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
"signatureHelp is called for non-added document",
|
|
|
|
llvm::errc::invalid_argument);
|
|
|
|
Contents = std::move(*Latest.Draft);
|
|
|
|
}
|
2017-10-06 19:54:17 +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
|
|
|
auto Action = [=](llvm::Expected<InputsAndPreamble> IP)
|
2018-01-31 16:51:16 +08:00
|
|
|
-> Expected<Tagged<SignatureHelp>> {
|
|
|
|
if (!IP)
|
|
|
|
return IP.takeError();
|
|
|
|
auto PreambleData = IP->Preamble;
|
|
|
|
auto &Command = IP->Inputs.CompileCommand;
|
|
|
|
|
|
|
|
return 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),
|
|
|
|
TaggedFS.Tag);
|
|
|
|
};
|
|
|
|
return blockingRunWithPreamble<Expected<Tagged<SignatureHelp>>>(WorkScheduler,
|
|
|
|
File, Action);
|
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
|
|
|
|
2017-11-09 19:30:04 +08:00
|
|
|
Expected<std::vector<tooling::Replacement>>
|
[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
|
|
|
ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName) {
|
2018-01-31 16:51:16 +08:00
|
|
|
using RetType = Expected<std::vector<tooling::Replacement>>;
|
|
|
|
auto Action = [=](Expected<InputsAndAST> InpAST) -> RetType {
|
|
|
|
if (!InpAST)
|
|
|
|
return InpAST.takeError();
|
|
|
|
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-01-31 16:51:16 +08:00
|
|
|
return 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(
|
|
|
|
Context, SourceRange(SourceLocationBeg), NewName.str());
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!Rename)
|
|
|
|
return Rename.takeError();
|
|
|
|
|
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())
|
|
|
|
return ResultCollector.Result->takeError();
|
|
|
|
|
|
|
|
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-01-31 16:51:16 +08:00
|
|
|
return Replacements;
|
|
|
|
};
|
|
|
|
return blockingRunWithAST<RetType>(WorkScheduler, File, std::move(Action));
|
2017-11-09 19:30:04 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
std::string ClangdServer::dumpAST(PathRef File) {
|
2018-01-31 16:51:16 +08:00
|
|
|
auto Action = [](llvm::Expected<InputsAndAST> InpAST) -> std::string {
|
|
|
|
if (!InpAST) {
|
|
|
|
ignoreError(InpAST.takeError());
|
|
|
|
return "<no-ast>";
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return Result;
|
|
|
|
};
|
|
|
|
return blockingRunWithAST<std::string>(WorkScheduler, File,
|
|
|
|
std::move(Action));
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2017-10-26 20:28:13 +08:00
|
|
|
llvm::Expected<Tagged<std::vector<Location>>>
|
[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
|
|
|
ClangdServer::findDefinitions(PathRef File, Position Pos) {
|
2017-06-29 00:12:10 +08:00
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
using RetType = llvm::Expected<Tagged<std::vector<Location>>>;
|
[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 Action = [=](llvm::Expected<InputsAndAST> InpAST) -> RetType {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST)
|
|
|
|
return 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-01-31 16:51:16 +08:00
|
|
|
return make_tagged(std::move(Result), TaggedFS.Tag);
|
|
|
|
};
|
|
|
|
return blockingRunWithAST<RetType>(WorkScheduler, File, Action);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
llvm::Expected<Tagged<std::vector<DocumentHighlight>>>
|
[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
|
|
|
ClangdServer::findDocumentHighlights(PathRef File, Position Pos) {
|
[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)
|
|
|
|
return llvm::make_error<llvm::StringError>(
|
|
|
|
"findDocumentHighlights called on non-added file",
|
|
|
|
llvm::errc::invalid_argument);
|
|
|
|
|
|
|
|
auto TaggedFS = FSProvider.getTaggedFileSystem(File);
|
|
|
|
|
2018-01-31 16:51:16 +08:00
|
|
|
using RetType = llvm::Expected<Tagged<std::vector<DocumentHighlight>>>;
|
[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 Action = [=](llvm::Expected<InputsAndAST> InpAST) -> RetType {
|
2018-01-31 16:51:16 +08:00
|
|
|
if (!InpAST)
|
|
|
|
return 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-01-31 16:51:16 +08:00
|
|
|
return make_tagged(std::move(Result), TaggedFS.Tag);
|
|
|
|
};
|
|
|
|
return blockingRunWithAST<RetType>(WorkScheduler, File, Action);
|
[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(
|
[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, VersionedDraft Contents,
|
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);
|
|
|
|
|
|
|
|
using OptDiags = llvm::Optional<std::vector<DiagWithFixIts>>;
|
|
|
|
|
|
|
|
DocVersion Version = Contents.Version;
|
|
|
|
Path FileStr = File.str();
|
|
|
|
VFSTag Tag = std::move(TaggedFS.Tag);
|
2018-01-25 22:19:21 +08:00
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
auto Callback = [this, Version, FileStr, Tag](OptDiags Diags) {
|
2017-08-14 16:17:24 +08:00
|
|
|
if (!Diags)
|
|
|
|
return; // A new reparse was requested before this one completed.
|
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(
|
[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
|
|
|
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-13 16:59:23 +08:00
|
|
|
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));
|
|
|
|
}
|