2017-05-16 17:38:59 +08:00
|
|
|
//===--- ClangdServer.h - 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H
|
|
|
|
|
2017-12-15 05:22:03 +08:00
|
|
|
#include "ClangdUnit.h"
|
|
|
|
#include "CodeComplete.h"
|
2018-01-25 22:19:21 +08:00
|
|
|
#include "CompileArgsCache.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "DraftStore.h"
|
2017-12-15 05:22:03 +08:00
|
|
|
#include "Function.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2017-12-15 05:22:03 +08:00
|
|
|
#include "Protocol.h"
|
2018-01-31 16:51:16 +08:00
|
|
|
#include "TUScheduler.h"
|
2017-12-20 02:00:37 +08:00
|
|
|
#include "index/FileIndex.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-05-23 21:42:59 +08:00
|
|
|
#include <functional>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <future>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <string>
|
2017-05-30 23:11:02 +08:00
|
|
|
#include <type_traits>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class PCHContainerOperations;
|
|
|
|
|
|
|
|
namespace clangd {
|
|
|
|
|
2017-05-30 23:11:02 +08:00
|
|
|
/// A tag supplied by the FileSytemProvider.
|
2017-06-13 16:24:48 +08:00
|
|
|
typedef std::string VFSTag;
|
2017-05-30 23:11:02 +08:00
|
|
|
|
|
|
|
/// A value of an arbitrary type and VFSTag that was supplied by the
|
|
|
|
/// FileSystemProvider when this value was computed.
|
|
|
|
template <class T> class Tagged {
|
|
|
|
public:
|
2017-10-06 06:15:15 +08:00
|
|
|
// MSVC requires future<> arguments to be default-constructible.
|
|
|
|
Tagged() = default;
|
|
|
|
|
2017-05-30 23:11:02 +08:00
|
|
|
template <class U>
|
2017-06-13 16:24:48 +08:00
|
|
|
Tagged(U &&Value, VFSTag Tag)
|
|
|
|
: Value(std::forward<U>(Value)), Tag(std::move(Tag)) {}
|
2017-05-30 23:11:02 +08:00
|
|
|
|
|
|
|
template <class U>
|
|
|
|
Tagged(const Tagged<U> &Other) : Value(Other.Value), Tag(Other.Tag) {}
|
|
|
|
|
|
|
|
template <class U>
|
2017-06-13 16:24:48 +08:00
|
|
|
Tagged(Tagged<U> &&Other)
|
|
|
|
: Value(std::move(Other.Value)), Tag(std::move(Other.Tag)) {}
|
2017-05-30 23:11:02 +08:00
|
|
|
|
2017-10-06 06:15:15 +08:00
|
|
|
T Value = T();
|
|
|
|
VFSTag Tag = VFSTag();
|
2017-05-30 23:11:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Tagged<typename std::decay<T>::type> make_tagged(T &&Value, VFSTag Tag) {
|
2017-08-09 20:55:13 +08:00
|
|
|
return Tagged<typename std::decay<T>::type>(std::forward<T>(Value), Tag);
|
2017-05-30 23:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
class DiagnosticsConsumer {
|
|
|
|
public:
|
|
|
|
virtual ~DiagnosticsConsumer() = default;
|
|
|
|
|
|
|
|
/// Called by ClangdServer when \p Diagnostics for \p File are ready.
|
2017-05-30 23:11:02 +08:00
|
|
|
virtual void
|
[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
|
|
|
onDiagnosticsReady(PathRef File,
|
2017-05-30 23:11:02 +08:00
|
|
|
Tagged<std::vector<DiagWithFixIts>> Diagnostics) = 0;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
class FileSystemProvider {
|
|
|
|
public:
|
|
|
|
virtual ~FileSystemProvider() = default;
|
2017-06-14 17:46:44 +08:00
|
|
|
/// Called by ClangdServer to obtain a vfs::FileSystem to be used for parsing.
|
|
|
|
/// Name of the file that will be parsed is passed in \p File.
|
|
|
|
///
|
2017-05-30 23:11:02 +08:00
|
|
|
/// \return A filesystem that will be used for all file accesses in clangd.
|
|
|
|
/// A Tag returned by this method will be propagated to all results of clangd
|
|
|
|
/// that will use this filesystem.
|
2017-06-14 17:46:44 +08:00
|
|
|
virtual Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
|
|
|
|
getTaggedFileSystem(PathRef File) = 0;
|
2017-05-26 20:26:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class RealFileSystemProvider : public FileSystemProvider {
|
|
|
|
public:
|
2017-05-30 23:11:02 +08:00
|
|
|
/// \return getRealFileSystem() tagged with default tag, i.e. VFSTag()
|
2017-06-14 17:46:44 +08:00
|
|
|
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>>
|
|
|
|
getTaggedFileSystem(PathRef File) override;
|
2017-05-26 20:26:51 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
/// Provides API to manage ASTs for a collection of C++ files and request
|
2017-08-22 17:16:46 +08:00
|
|
|
/// various language features.
|
|
|
|
/// Currently supports async diagnostics, code completion, formatting and goto
|
|
|
|
/// definition.
|
2017-05-16 17:38:59 +08:00
|
|
|
class ClangdServer {
|
|
|
|
public:
|
2017-08-22 17:16:46 +08:00
|
|
|
/// Creates a new ClangdServer instance.
|
|
|
|
/// To process parsing requests asynchronously, ClangdServer will spawn \p
|
|
|
|
/// AsyncThreadsCount worker threads. However, if \p AsyncThreadsCount is 0,
|
|
|
|
/// all requests will be processed on the calling thread.
|
|
|
|
///
|
|
|
|
/// ClangdServer uses \p FSProvider to get an instance of vfs::FileSystem for
|
|
|
|
/// each parsing request. Results of code completion and diagnostics also
|
|
|
|
/// include a tag, that \p FSProvider returns along with the vfs::FileSystem.
|
|
|
|
///
|
|
|
|
/// The value of \p ResourceDir will be used to search for internal headers
|
|
|
|
/// (overriding defaults and -resource-dir compiler flag). If \p ResourceDir
|
|
|
|
/// is None, ClangdServer will call CompilerInvocation::GetResourcePath() to
|
|
|
|
/// obtain the standard resource directory.
|
|
|
|
///
|
|
|
|
/// ClangdServer uses \p CDB to obtain compilation arguments for parsing. Note
|
|
|
|
/// that ClangdServer only obtains compilation arguments once for each newly
|
|
|
|
/// added file (i.e., when processing a first call to addDocument) and reuses
|
|
|
|
/// those arguments for subsequent reparses. However, ClangdServer will check
|
|
|
|
/// if compilation arguments changed on calls to forceReparse().
|
|
|
|
///
|
|
|
|
/// After each parsing request finishes, ClangdServer reports diagnostics to
|
|
|
|
/// \p DiagConsumer. Note that a callback to \p DiagConsumer happens on a
|
|
|
|
/// worker thread. Therefore, instances of \p DiagConsumer must properly
|
|
|
|
/// synchronize access to shared state.
|
2017-09-20 15:24:15 +08:00
|
|
|
///
|
2017-11-17 00:25:18 +08:00
|
|
|
/// \p StorePreamblesInMemory defines whether the Preambles generated by
|
|
|
|
/// clangd are stored in-memory or on disk.
|
2017-12-20 02:00:37 +08:00
|
|
|
///
|
|
|
|
/// If \p BuildDynamicSymbolIndex is true, ClangdServer builds a dynamic
|
|
|
|
/// in-memory index for symbols in all opened files and uses the index to
|
|
|
|
/// augment code completion results.
|
2018-01-10 22:44:34 +08:00
|
|
|
///
|
|
|
|
/// If \p StaticIdx is set, ClangdServer uses the index for global code
|
|
|
|
/// completion.
|
2017-06-13 23:59:43 +08:00
|
|
|
ClangdServer(GlobalCompilationDatabase &CDB,
|
|
|
|
DiagnosticsConsumer &DiagConsumer,
|
2017-08-14 16:45:47 +08:00
|
|
|
FileSystemProvider &FSProvider, unsigned AsyncThreadsCount,
|
2017-12-13 20:51:22 +08:00
|
|
|
bool StorePreamblesInMemory,
|
2017-12-20 02:00:37 +08:00
|
|
|
bool BuildDynamicSymbolIndex = false,
|
2018-01-10 22:44:34 +08:00
|
|
|
SymbolIndex *StaticIdx = nullptr,
|
2017-06-28 18:34:50 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDir = llvm::None);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-09-27 23:31:17 +08:00
|
|
|
/// Set the root path of the workspace.
|
|
|
|
void setRootPath(PathRef RootPath);
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
/// Add a \p File to the list of tracked C++ files or update the contents if
|
|
|
|
/// \p File is already tracked. Also schedules parsing of the AST for it on a
|
|
|
|
/// separate thread. When the parsing is complete, DiagConsumer passed in
|
|
|
|
/// constructor will receive onDiagnosticsReady callback.
|
2018-02-22 21:11:12 +08:00
|
|
|
void addDocument(PathRef File, StringRef Contents,
|
|
|
|
WantDiagnostics WD = WantDiagnostics::Auto);
|
[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
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
/// Remove \p File from list of tracked files, schedule a request to free
|
|
|
|
/// resources associated with it.
|
2018-02-08 15:37:35 +08:00
|
|
|
void removeDocument(PathRef File);
|
[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
|
|
|
|
2017-05-26 20:26:51 +08:00
|
|
|
/// Force \p File to be reparsed using the latest contents.
|
2017-08-14 16:37:32 +08:00
|
|
|
/// Will also check if CompileCommand, provided by GlobalCompilationDatabase
|
|
|
|
/// for \p File has changed. If it has, will remove currently stored Preamble
|
|
|
|
/// and AST and rebuild them from scratch.
|
2018-02-13 16:59:23 +08:00
|
|
|
void forceReparse(PathRef File);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
/// Calls forceReparse() on all currently opened files.
|
|
|
|
/// As a result, this method may be very expensive.
|
|
|
|
/// This method is normally called when the compilation database is changed.
|
|
|
|
void reparseOpenedFiles();
|
|
|
|
|
2017-10-06 01:04:13 +08:00
|
|
|
/// Run code completion for \p File at \p Pos.
|
[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
|
|
|
/// Request is processed asynchronously.
|
2017-10-06 01:04:13 +08:00
|
|
|
///
|
2018-02-28 01:15:50 +08:00
|
|
|
/// The current draft for \p File will be used. If \p UsedFS is non-null, it
|
2017-10-06 01:04:13 +08:00
|
|
|
/// will be overwritten by vfs::FileSystem used for completion.
|
|
|
|
///
|
|
|
|
/// This method should only be called for currently tracked files. However, it
|
|
|
|
/// is safe to call removeDocument for \p File after this method returns, even
|
|
|
|
/// while returned future is not yet ready.
|
2017-10-25 17:35:10 +08:00
|
|
|
/// A version of `codeComplete` that runs \p Callback on the processing thread
|
|
|
|
/// when codeComplete results become available.
|
[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
|
|
|
void codeComplete(PathRef File, Position Pos,
|
|
|
|
const clangd::CodeCompleteOptions &Opts,
|
|
|
|
UniqueFunction<void(Tagged<CompletionList>)> Callback,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr);
|
|
|
|
|
2017-10-06 19:54:17 +08:00
|
|
|
/// Provide signature help for \p File at \p Pos. If \p OverridenContents is
|
|
|
|
/// not None, they will used only for signature help, i.e. no diagnostics
|
|
|
|
/// update will be scheduled and a draft for \p File will not be updated. If
|
2018-02-28 01:15:50 +08:00
|
|
|
/// If \p UsedFS is non-null, it will be overwritten by vfs::FileSystem used
|
|
|
|
/// for signature help. This method should only be called for tracked files.
|
2018-02-15 21:15:47 +08:00
|
|
|
void signatureHelp(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<SignatureHelp>>)> Callback,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr);
|
2017-10-06 19:54:17 +08:00
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
/// Get definition of symbol at a specified \p Line and \p Column in \p File.
|
2018-02-15 21:15:47 +08:00
|
|
|
void findDefinitions(
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<std::vector<Location>>>)>
|
|
|
|
Callback);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-09-28 11:14:40 +08:00
|
|
|
/// Helper function that returns a path to the corresponding source file when
|
|
|
|
/// given a header file and vice versa.
|
|
|
|
llvm::Optional<Path> switchSourceHeader(PathRef Path);
|
|
|
|
|
[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
|
|
|
/// Get document highlights for a given position.
|
2018-02-15 21:15:47 +08:00
|
|
|
void 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
|
|
|
|
[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
|
|
|
/// Get code hover for a given position.
|
|
|
|
void findHover(PathRef File, Position Pos,
|
|
|
|
UniqueFunction<void(llvm::Expected<Tagged<Hover>>)> Callback);
|
|
|
|
|
2017-12-13 04:25:06 +08:00
|
|
|
/// Run formatting for \p Rng inside \p File with content \p Code.
|
|
|
|
llvm::Expected<tooling::Replacements> formatRange(StringRef Code,
|
|
|
|
PathRef File, Range Rng);
|
|
|
|
|
|
|
|
/// Run formatting for the whole \p File with content \p Code.
|
|
|
|
llvm::Expected<tooling::Replacements> formatFile(StringRef Code,
|
|
|
|
PathRef File);
|
|
|
|
|
|
|
|
/// Run formatting after a character was typed at \p Pos in \p File with
|
|
|
|
/// content \p Code.
|
|
|
|
llvm::Expected<tooling::Replacements>
|
|
|
|
formatOnType(StringRef Code, PathRef File, Position Pos);
|
|
|
|
|
2017-11-09 19:30:04 +08:00
|
|
|
/// Rename all occurrences of the symbol at the \p Pos in \p File to
|
|
|
|
/// \p NewName.
|
2018-02-15 21:15:47 +08:00
|
|
|
void rename(PathRef File, Position Pos, llvm::StringRef NewName,
|
|
|
|
UniqueFunction<void(Expected<std::vector<tooling::Replacement>>)>
|
|
|
|
Callback);
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2018-02-26 16:32:13 +08:00
|
|
|
/// Inserts a new #include into \p File, if it's not present in \p Code.
|
|
|
|
///
|
|
|
|
/// \p DeclaringHeader The original header corresponding to this insertion
|
|
|
|
/// e.g. the header that declared a symbol. This can be either a URI or a
|
|
|
|
/// literal string quoted with <> or "" that can be #included directly.
|
|
|
|
/// \p InsertedHeader The preferred header to be inserted. This may be
|
|
|
|
/// different from \p DeclaringHeader as a header file can have a different
|
|
|
|
/// canonical include. This can be either a URI or a literal string quoted
|
|
|
|
/// with <> or "" that can be #included directly.
|
|
|
|
///
|
|
|
|
/// Both OriginalHeader and InsertedHeader will be considered to determine
|
|
|
|
/// whether an include needs to be added.
|
2018-02-16 22:15:55 +08:00
|
|
|
Expected<tooling::Replacements> insertInclude(PathRef File, StringRef Code,
|
2018-02-26 16:32:13 +08:00
|
|
|
StringRef DeclaringHeader,
|
|
|
|
StringRef InsertedHeader);
|
2018-02-16 22:15:55 +08:00
|
|
|
|
2018-01-17 20:30:24 +08:00
|
|
|
/// Gets current document contents for \p File. Returns None if \p File is not
|
|
|
|
/// currently tracked.
|
2017-05-16 22:40:30 +08:00
|
|
|
/// FIXME(ibiryukov): This function is here to allow offset-to-Position
|
|
|
|
/// conversions in outside code, maybe there's a way to get rid of it.
|
2018-01-17 20:30:24 +08:00
|
|
|
llvm::Optional<std::string> getDocument(PathRef File);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
/// Only for testing purposes.
|
|
|
|
/// Waits until all requests to worker thread are finished and dumps AST for
|
|
|
|
/// \p File. \p File must be in the list of added documents.
|
2018-02-15 21:15:47 +08:00
|
|
|
void dumpAST(PathRef File, UniqueFunction<void(std::string)> Callback);
|
2017-10-03 02:00:37 +08:00
|
|
|
/// Called when an event occurs for a watched file in the workspace.
|
|
|
|
void onFileEvent(const DidChangeWatchedFilesParams &Params);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
/// Returns estimated memory usage for each of the currently open files.
|
|
|
|
/// The order of results is unspecified.
|
|
|
|
/// Overall memory usage of clangd may be significantly more than reported
|
|
|
|
/// here, as this metric does not account (at least) for:
|
|
|
|
/// - memory occupied by static and dynamic index,
|
|
|
|
/// - memory required for in-flight requests,
|
|
|
|
/// FIXME: those metrics might be useful too, we should add them.
|
|
|
|
std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const;
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
// Blocks the main thread until the server is idle. Only for use in tests.
|
|
|
|
// Returns false if the timeout expires.
|
|
|
|
LLVM_NODISCARD bool
|
|
|
|
blockUntilIdleForTest(llvm::Optional<double> TimeoutSeconds = 10);
|
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
private:
|
2017-12-13 04:25:06 +08:00
|
|
|
/// FIXME: This stats several files to find a .clang-format file. I/O can be
|
|
|
|
/// slow. Think of a way to cache this.
|
|
|
|
llvm::Expected<tooling::Replacements>
|
|
|
|
formatCode(llvm::StringRef Code, PathRef File,
|
|
|
|
ArrayRef<tooling::Range> Ranges);
|
|
|
|
|
2018-02-13 16:59:23 +08:00
|
|
|
void
|
[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
|
|
|
scheduleReparseAndDiags(PathRef File, VersionedDraft Contents,
|
2018-02-22 21:11:12 +08:00
|
|
|
WantDiagnostics WD,
|
2018-01-25 22:19:21 +08:00
|
|
|
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS);
|
2017-08-14 16:17:24 +08:00
|
|
|
|
2018-01-25 22:19:21 +08:00
|
|
|
CompileArgsCache CompileArgs;
|
2017-06-13 23:59:43 +08:00
|
|
|
DiagnosticsConsumer &DiagConsumer;
|
|
|
|
FileSystemProvider &FSProvider;
|
2017-05-16 17:38:59 +08:00
|
|
|
DraftStore DraftMgr;
|
2018-01-15 20:33:00 +08:00
|
|
|
// The index used to look up symbols. This could be:
|
|
|
|
// - null (all index functionality is optional)
|
|
|
|
// - the dynamic index owned by ClangdServer (FileIdx)
|
|
|
|
// - the static index passed to the constructor
|
|
|
|
// - a merged view of a static and dynamic index (MergedIndex)
|
|
|
|
SymbolIndex *Index;
|
|
|
|
// If present, an up-to-date of symbols in open files. Read via Index.
|
2017-12-20 02:00:37 +08:00
|
|
|
std::unique_ptr<FileIndex> FileIdx;
|
2018-01-15 20:33:00 +08:00
|
|
|
// If present, a merged view of FileIdx and an external index. Read via Index.
|
|
|
|
std::unique_ptr<SymbolIndex> MergedIndex;
|
2017-09-27 23:31:17 +08:00
|
|
|
// If set, this represents the workspace path.
|
|
|
|
llvm::Optional<std::string> RootPath;
|
2017-05-16 17:38:59 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs;
|
2017-09-20 20:58:55 +08:00
|
|
|
/// Used to serialize diagnostic callbacks.
|
|
|
|
/// FIXME(ibiryukov): get rid of an extra map and put all version counters
|
|
|
|
/// into CppFile.
|
|
|
|
std::mutex DiagnosticsMutex;
|
|
|
|
/// Maps from a filename to the latest version of reported diagnostics.
|
|
|
|
llvm::StringMap<DocVersion> ReportedDiagnosticVersions;
|
2017-09-21 03:32:06 +08:00
|
|
|
// WorkScheduler has to be the last member, because its destructor has to be
|
|
|
|
// called before all other members to stop the worker thread that references
|
2018-01-31 16:51:16 +08:00
|
|
|
// ClangdServer.
|
|
|
|
TUScheduler WorkScheduler;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|