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
|
|
|
|
|
|
|
|
#include "ClangdUnitStore.h"
|
|
|
|
#include "DraftStore.h"
|
|
|
|
#include "GlobalCompilationDatabase.h"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#include "ClangdUnit.h"
|
2017-10-10 00:26:26 +08:00
|
|
|
#include "Function.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "Protocol.h"
|
|
|
|
|
|
|
|
#include <condition_variable>
|
2017-05-23 21:42:59 +08:00
|
|
|
#include <functional>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
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-09-20 15:24:15 +08:00
|
|
|
class Logger;
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Turn a [line, column] pair into an offset in Code.
|
|
|
|
size_t positionToOffset(StringRef Code, Position P);
|
|
|
|
|
|
|
|
/// Turn an offset in Code into a [line, column] pair.
|
|
|
|
Position offsetToPosition(StringRef Code, size_t Offset);
|
|
|
|
|
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
|
|
|
|
onDiagnosticsReady(PathRef File,
|
|
|
|
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
|
|
|
class ClangdServer;
|
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
/// Returns a number of a default async threads to use for ClangdScheduler.
|
|
|
|
/// Returned value is always >= 1 (i.e. will not cause requests to be processed
|
|
|
|
/// synchronously).
|
|
|
|
unsigned getDefaultAsyncThreadsCount();
|
|
|
|
|
|
|
|
/// Handles running WorkerRequests of ClangdServer on a number of worker
|
|
|
|
/// threads.
|
2017-05-16 17:38:59 +08:00
|
|
|
class ClangdScheduler {
|
|
|
|
public:
|
2017-08-14 16:45:47 +08:00
|
|
|
/// If \p AsyncThreadsCount is 0, requests added using addToFront and addToEnd
|
|
|
|
/// will be processed synchronously on the calling thread.
|
|
|
|
// Otherwise, \p AsyncThreadsCount threads will be created to schedule the
|
|
|
|
// requests.
|
|
|
|
ClangdScheduler(unsigned AsyncThreadsCount);
|
2017-05-16 17:38:59 +08:00
|
|
|
~ClangdScheduler();
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Add a new request to run function \p F with args \p As to the start of the
|
|
|
|
/// queue. The request will be run on a separate thread.
|
|
|
|
template <class Func, class... Args>
|
|
|
|
void addToFront(Func &&F, Args &&... As) {
|
|
|
|
if (RunSynchronously) {
|
|
|
|
std::forward<Func>(F)(std::forward<Args>(As)...);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2017-10-10 00:26:26 +08:00
|
|
|
RequestQueue.push_front(
|
|
|
|
BindWithForward(std::forward<Func>(F), std::forward<Args>(As)...));
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
RequestCV.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a new request to run function \p F with args \p As to the end of the
|
|
|
|
/// queue. The request will be run on a separate thread.
|
|
|
|
template <class Func, class... Args> void addToEnd(Func &&F, Args &&... As) {
|
|
|
|
if (RunSynchronously) {
|
|
|
|
std::forward<Func>(F)(std::forward<Args>(As)...);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
2017-10-10 00:26:26 +08:00
|
|
|
RequestQueue.push_back(
|
|
|
|
BindWithForward(std::forward<Func>(F), std::forward<Args>(As)...));
|
2017-08-01 23:51:38 +08:00
|
|
|
}
|
|
|
|
RequestCV.notify_one();
|
|
|
|
}
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool RunSynchronously;
|
|
|
|
std::mutex Mutex;
|
2017-08-14 16:45:47 +08:00
|
|
|
/// We run some tasks on separate threads(parsing, CppFile cleanup).
|
|
|
|
/// These threads looks into RequestQueue to find requests to handle and
|
|
|
|
/// terminate when Done is set to true.
|
|
|
|
std::vector<std::thread> Workers;
|
|
|
|
/// Setting Done to true will make the worker threads terminate.
|
2017-05-16 17:38:59 +08:00
|
|
|
bool Done = false;
|
2017-08-14 16:45:47 +08:00
|
|
|
/// A queue of requests. Elements of this vector are async computations (i.e.
|
|
|
|
/// results of calling std::async(std::launch::deferred, ...)).
|
2017-10-10 00:26:26 +08:00
|
|
|
std::deque<UniqueFunction<void()>> RequestQueue;
|
2017-08-14 16:45:47 +08:00
|
|
|
/// Condition variable to wake up worker threads.
|
2017-05-16 17:38:59 +08:00
|
|
|
std::condition_variable RequestCV;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2017-09-12 21:57:14 +08:00
|
|
|
/// When \p SnippetCompletions is true, completion items will be presented
|
|
|
|
/// with embedded snippets. Otherwise, plaintext items will be presented.
|
|
|
|
///
|
2017-08-22 17:16:46 +08:00
|
|
|
/// 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
|
|
|
///
|
|
|
|
/// Various messages are logged using \p Logger.
|
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-10-23 22:46:48 +08:00
|
|
|
clangd::CodeCompleteOptions CodeCompleteOpts,
|
|
|
|
clangd::Logger &Logger,
|
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.
|
2017-08-01 23:51:38 +08:00
|
|
|
/// \return A future that will become ready when the rebuild (including
|
|
|
|
/// diagnostics) is finished.
|
|
|
|
std::future<void> addDocument(PathRef File, StringRef Contents);
|
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.
|
2017-08-14 16:37:32 +08:00
|
|
|
/// \return A future that will become ready when the file is removed and all
|
|
|
|
/// associated resources are freed.
|
2017-08-01 23:51:38 +08:00
|
|
|
std::future<void> removeDocument(PathRef File);
|
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.
|
2017-08-01 23:51:38 +08:00
|
|
|
std::future<void> forceReparse(PathRef File);
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-10-25 17:35:10 +08:00
|
|
|
/// DEPRECATED. Please use a callback-based version, this API is deprecated
|
|
|
|
/// and will soon be removed.
|
|
|
|
///
|
2017-10-06 01:04:13 +08:00
|
|
|
/// Run code completion for \p File at \p Pos.
|
|
|
|
///
|
|
|
|
/// Request is processed asynchronously. You can use the returned future to
|
|
|
|
/// wait for the results of the async request.
|
|
|
|
///
|
|
|
|
/// If \p OverridenContents is not None, they will used only for code
|
|
|
|
/// completion, i.e. no diagnostics update will be scheduled and a draft for
|
|
|
|
/// \p File will not be updated. If \p OverridenContents is None, contents of
|
|
|
|
/// the current draft for \p File will be used. If \p UsedFS is non-null, it
|
|
|
|
/// 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-11-15 17:16:29 +08:00
|
|
|
std::future<Tagged<CompletionList>>
|
2017-06-13 22:15:56 +08:00
|
|
|
codeComplete(PathRef File, Position Pos,
|
2017-08-01 01:09:29 +08:00
|
|
|
llvm::Optional<StringRef> OverridenContents = llvm::None,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr);
|
2017-10-06 01:04:13 +08:00
|
|
|
|
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.
|
2017-11-15 17:16:29 +08:00
|
|
|
void codeComplete(UniqueFunction<void(Tagged<CompletionList>)> Callback,
|
|
|
|
PathRef File, Position Pos,
|
|
|
|
llvm::Optional<StringRef> OverridenContents = llvm::None,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr);
|
2017-10-25 17:35:10 +08:00
|
|
|
|
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
|
|
|
|
/// \p OverridenContents is None, contents of the current draft for \p File
|
|
|
|
/// will be used. If \p UsedFS is non-null, it will be overwritten by
|
|
|
|
/// vfs::FileSystem used for signature help. This method should only be called
|
|
|
|
/// for currently tracked files.
|
2017-10-26 20:28:13 +08:00
|
|
|
llvm::Expected<Tagged<SignatureHelp>>
|
2017-10-06 19:54:17 +08:00
|
|
|
signatureHelp(PathRef File, Position Pos,
|
|
|
|
llvm::Optional<StringRef> OverridenContents = llvm::None,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> *UsedFS = nullptr);
|
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
/// Get definition of symbol at a specified \p Line and \p Column in \p File.
|
2017-10-26 20:28:13 +08:00
|
|
|
llvm::Expected<Tagged<std::vector<Location>>> findDefinitions(PathRef File,
|
|
|
|
Position Pos);
|
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);
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
/// Run formatting for \p Rng inside \p File.
|
|
|
|
std::vector<tooling::Replacement> formatRange(PathRef File, Range Rng);
|
|
|
|
/// Run formatting for the whole \p File.
|
|
|
|
std::vector<tooling::Replacement> formatFile(PathRef File);
|
|
|
|
/// Run formatting after a character was typed at \p Pos in \p File.
|
|
|
|
std::vector<tooling::Replacement> formatOnType(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.
|
|
|
|
Expected<std::vector<tooling::Replacement>> rename(PathRef File, Position Pos,
|
|
|
|
llvm::StringRef NewName);
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
/// Gets current document contents for \p File. \p File must point to a
|
|
|
|
/// currently tracked file.
|
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.
|
2017-05-16 17:38:59 +08:00
|
|
|
std::string getDocument(PathRef File);
|
|
|
|
|
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.
|
|
|
|
std::string dumpAST(PathRef File);
|
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
|
|
|
|
2017-05-23 21:42:59 +08:00
|
|
|
private:
|
2017-08-14 16:17:24 +08:00
|
|
|
std::future<void>
|
|
|
|
scheduleReparseAndDiags(PathRef File, VersionedDraft Contents,
|
|
|
|
std::shared_ptr<CppFile> Resources,
|
|
|
|
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS);
|
|
|
|
|
|
|
|
std::future<void> scheduleCancelRebuild(std::shared_ptr<CppFile> Resources);
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
clangd::Logger &Logger;
|
2017-06-13 23:59:43 +08:00
|
|
|
GlobalCompilationDatabase &CDB;
|
|
|
|
DiagnosticsConsumer &DiagConsumer;
|
|
|
|
FileSystemProvider &FSProvider;
|
2017-05-16 17:38:59 +08:00
|
|
|
DraftStore DraftMgr;
|
2017-08-01 23:51:38 +08:00
|
|
|
CppFileCollection Units;
|
2017-06-28 18:34:50 +08:00
|
|
|
std::string ResourceDir;
|
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-10-23 22:46:48 +08:00
|
|
|
clangd::CodeCompleteOptions CodeCompleteOpts;
|
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
|
|
|
|
// ClangdServer
|
|
|
|
ClangdScheduler WorkScheduler;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|