2017-04-13 01:13:08 +08:00
|
|
|
//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
|
2017-02-07 18:28:20 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "Path.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
#include "Transport.h"
|
[clangd] Merge binary + YAML serialization behind a (mostly) common interface.
Summary:
Interface is in one file, implementation in two as they have little in common.
A couple of ad-hoc YAML functions left exposed:
- symbol -> YAML I expect to keep for tools like dexp
- YAML -> symbol is used for the MR-style indexer, I think we can eliminate
this (merge-on-the-fly, else use a different serialization)
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D52453
llvm-svn: 342999
2018-09-26 02:06:43 +08:00
|
|
|
#include "index/Serialization.h"
|
2018-07-30 03:12:42 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2017-03-02 00:16:29 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-02-07 18:28:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-10-02 23:13:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-02-07 20:40:59 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-02-14 11:20:07 +08:00
|
|
|
#include <cstdlib>
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <iostream>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <memory>
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <string>
|
2017-08-14 16:45:47 +08:00
|
|
|
#include <thread>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
using namespace llvm;
|
2017-05-16 17:38:59 +08:00
|
|
|
using namespace clang;
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2018-10-16 16:53:52 +08:00
|
|
|
// FIXME: remove this option when Dex is cheap enough.
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> UseDex("use-dex-index",
|
|
|
|
cl::desc("Use experimental Dex dynamic index."),
|
|
|
|
cl::init(false), cl::Hidden);
|
2018-08-21 18:40:19 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<Path> CompileCommandsDir(
|
2017-10-02 23:13:20 +08:00
|
|
|
"compile-commands-dir",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc("Specify a path to look for compile_commands.json. If path "
|
|
|
|
"is invalid, clangd will look in the current directory and "
|
|
|
|
"parent paths of each source file."));
|
2017-10-02 23:13:20 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
WorkerThreadsCount("j", cl::desc("Number of async workers used by clangd"),
|
|
|
|
cl::init(getDefaultAsyncThreadsCount()));
|
2017-08-14 16:45:47 +08:00
|
|
|
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
// FIXME: also support "plain" style where signatures are always omitted.
|
2018-09-05 18:39:58 +08:00
|
|
|
enum CompletionStyleFlag { Detailed, Bundled };
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<CompletionStyleFlag> CompletionStyle(
|
|
|
|
"completion-style", cl::desc("Granularity of code completion suggestions"),
|
|
|
|
cl::values(
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
clEnumValN(Detailed, "detailed",
|
|
|
|
"One completion item for each semantically distinct "
|
|
|
|
"completion, with full type information."),
|
|
|
|
clEnumValN(Bundled, "bundled",
|
|
|
|
"Similar completion items (e.g. function overloads) are "
|
|
|
|
"combined. Type information shown where possible.")),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(Detailed));
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
|
2017-11-24 00:58:22 +08:00
|
|
|
// FIXME: Flags are the wrong mechanism for user preferences.
|
|
|
|
// We should probably read a dotfile or similar.
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> IncludeIneligibleResults(
|
2017-11-24 00:58:22 +08:00
|
|
|
"include-ineligible-results",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc("Include ineligible completion results (e.g. private members)"),
|
|
|
|
cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<JSONStreamStyle> InputStyle(
|
|
|
|
"input-style", cl::desc("Input JSON stream encoding"),
|
|
|
|
cl::values(
|
2018-02-06 18:47:30 +08:00
|
|
|
clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
|
|
|
|
clEnumValN(JSONStreamStyle::Delimited, "delimited",
|
|
|
|
"messages delimited by --- lines, with # comment support")),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(JSONStreamStyle::Standard));
|
2018-02-06 18:47:30 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> PrettyPrint("pretty", cl::desc("Pretty-print JSON output"),
|
|
|
|
cl::init(false));
|
Adds a json::Expr type to represent intermediate JSON expressions.
Summary:
This form can be created with a nice clang-format-friendly literal syntax,
and gets escaping right. It knows how to call unparse() on our Protocol types.
All the places where we pass around JSON internally now use this type.
Object properties are sorted (stored as std::map) and so serialization is
canonicalized, with optional prettyprinting (triggered by a -pretty flag).
This makes the lit tests much nicer to read and somewhat nicer to debug.
(Unfortunately the completion tests use CHECK-DAG, which only has
line-granularity, so pretty-printing is disabled there. In future we
could make completion ordering deterministic, or switch to unittests).
Compared to the current approach, it has some efficiencies like avoiding copies
of string literals used as object keys, but is probably slower overall.
I think the code/test quality benefits are worth it.
This patch doesn't attempt to do anything about JSON *parsing*.
It takes direction from the proposal in this doc[1], but is limited in scope
and visibility, for now.
I am of half a mind just to use Expr as the target of a parser, and maybe do a
little string deduplication, but not bother with clever memory allocation.
That would be simple, and fast enough for clangd...
[1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit
+cc d0k so he can tell me not to use std::map.
Reviewers: ioeric, malaperle
Subscribers: bkramer, ilya-biryukov, mgorny, klimek
Differential Revision: https://reviews.llvm.org/D39435
llvm-svn: 317486
2017-11-06 23:40:30 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<Logger::Level> LogLevel(
|
|
|
|
"log", cl::desc("Verbosity of log messages written to stderr"),
|
|
|
|
cl::values(clEnumValN(Logger::Error, "error", "Error messages only"),
|
|
|
|
clEnumValN(Logger::Info, "info", "High level execution tracing"),
|
|
|
|
clEnumValN(Logger::Debug, "verbose", "Low level details")),
|
|
|
|
cl::init(Logger::Info));
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> Test(
|
2018-02-06 18:47:30 +08:00
|
|
|
"lit-test",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc(
|
2018-02-06 18:47:30 +08:00
|
|
|
"Abbreviation for -input-style=delimited -pretty -run-synchronously. "
|
|
|
|
"Intended to simplify lit tests."),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(false), cl::Hidden);
|
2018-02-06 18:47:30 +08:00
|
|
|
|
2018-09-05 18:39:58 +08:00
|
|
|
enum PCHStorageFlag { Disk, Memory };
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<PCHStorageFlag> PCHStorage(
|
2017-11-17 00:25:18 +08:00
|
|
|
"pch-storage",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc("Storing PCHs in memory increases memory usages, but may "
|
|
|
|
"improve performance"),
|
|
|
|
cl::values(clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
|
|
|
|
clEnumValN(PCHStorageFlag::Memory, "memory",
|
|
|
|
"store PCHs in memory")),
|
|
|
|
cl::init(PCHStorageFlag::Disk));
|
|
|
|
|
|
|
|
static cl::opt<int>
|
|
|
|
LimitResults("limit-results",
|
|
|
|
cl::desc("Limit the number of results returned by clangd. "
|
|
|
|
"0 means no limit."),
|
|
|
|
cl::init(100));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
RunSynchronously("run-synchronously",
|
|
|
|
cl::desc("Parse on main thread. If set, -j is ignored"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<Path> ResourceDir("resource-dir",
|
|
|
|
cl::desc("Directory for system clang headers"),
|
|
|
|
cl::init(""), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<Path> InputMirrorFile(
|
2017-10-10 17:08:47 +08:00
|
|
|
"input-mirror-file",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc(
|
2017-10-10 17:08:47 +08:00
|
|
|
"Mirror all LSP input to the specified file. Useful for debugging."),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(""), cl::Hidden);
|
2017-10-10 17:08:47 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> EnableIndex(
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
"index",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc(
|
2018-09-13 20:53:23 +08:00
|
|
|
"Enable index-based features. By default, clangd maintains an index "
|
|
|
|
"built from symbols in opened files. Global index support needs to "
|
|
|
|
"enabled separatedly."),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(true), cl::Hidden);
|
2017-12-20 02:00:37 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> AllScopesCompletion(
|
2018-09-28 02:46:00 +08:00
|
|
|
"all-scopes-completion",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc(
|
2018-09-28 02:46:00 +08:00
|
|
|
"If set to true, code completion will include index symbols that are "
|
|
|
|
"not defined in the scopes (e.g. "
|
|
|
|
"namespaces) visible from the code completion point. Such completions "
|
|
|
|
"can insert scope qualifiers."),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(false), cl::Hidden);
|
2018-09-28 02:46:00 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ShowOrigins("debug-origin", cl::desc("Show origins of completion items"),
|
|
|
|
cl::init(clangd::CodeCompleteOptions().ShowOrigins),
|
|
|
|
cl::Hidden);
|
2018-07-05 14:20:41 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<bool> HeaderInsertionDecorators(
|
2018-07-30 03:12:42 +08:00
|
|
|
"header-insertion-decorators",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc("Prepend a circular dot or space before the completion "
|
|
|
|
"label, depending on whether "
|
|
|
|
"an include line will be inserted or not."),
|
|
|
|
cl::init(true));
|
2018-07-30 03:12:42 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<Path> IndexFile(
|
2018-10-08 18:44:54 +08:00
|
|
|
"index-file",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc(
|
2018-10-08 18:44:54 +08:00
|
|
|
"Index file to build the static index. The file must have been created "
|
|
|
|
"by a compatible clangd-index.\n"
|
2018-01-10 22:44:34 +08:00
|
|
|
"WARNING: This option is experimental only, and will be removed "
|
|
|
|
"eventually. Don't rely on it."),
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::init(""), cl::Hidden);
|
2018-01-10 22:44:34 +08:00
|
|
|
|
2018-08-02 01:39:29 +08:00
|
|
|
enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
|
2018-10-20 23:30:37 +08:00
|
|
|
static cl::opt<CompileArgsFrom> CompileArgsFrom(
|
|
|
|
"compile_args_from", cl::desc("The source of compile commands"),
|
|
|
|
cl::values(clEnumValN(LSPCompileArgs, "lsp",
|
|
|
|
"All compile commands come from LSP and "
|
|
|
|
"'compile_commands.json' files are ignored"),
|
|
|
|
clEnumValN(FilesystemCompileArgs, "filesystem",
|
|
|
|
"All compile commands come from the "
|
|
|
|
"'compile_commands.json' files")),
|
|
|
|
cl::init(FilesystemCompileArgs), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> EnableFunctionArgSnippets(
|
2018-09-19 18:16:44 +08:00
|
|
|
"function-arg-placeholders",
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::desc("When disabled, completions contain only parentheses for "
|
|
|
|
"function calls. When enabled, completions also contain "
|
|
|
|
"placeholders for method parameters."),
|
|
|
|
cl::init(clangd::CodeCompleteOptions().EnableFunctionArgSnippets));
|
2018-09-19 18:16:44 +08:00
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-10-20 23:30:37 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
|
|
|
cl::SetVersionPrinter([](raw_ostream &OS) {
|
2018-06-29 21:24:20 +08:00
|
|
|
OS << clang::getClangToolFullVersion("clangd") << "\n";
|
|
|
|
});
|
2018-10-20 23:30:37 +08:00
|
|
|
cl::ParseCommandLineOptions(
|
2018-06-29 21:24:20 +08:00
|
|
|
argc, argv,
|
|
|
|
"clangd is a language server that provides IDE-like features to editors. "
|
2018-10-20 23:30:37 +08:00
|
|
|
"\n\nIt should be used via an editor plugin rather than invoked "
|
|
|
|
"directly. "
|
2018-06-29 21:24:20 +08:00
|
|
|
"For more information, see:"
|
|
|
|
"\n\thttps://clang.llvm.org/extra/clangd.html"
|
|
|
|
"\n\thttps://microsoft.github.io/language-server-protocol/");
|
2018-02-06 18:47:30 +08:00
|
|
|
if (Test) {
|
|
|
|
RunSynchronously = true;
|
|
|
|
InputStyle = JSONStreamStyle::Delimited;
|
|
|
|
PrettyPrint = true;
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
if (!RunSynchronously && WorkerThreadsCount == 0) {
|
2018-10-20 23:30:37 +08:00
|
|
|
errs() << "A number of worker threads cannot be 0. Did you mean to "
|
|
|
|
"specify -run-synchronously?";
|
2017-08-14 16:45:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-25 15:21:16 +08:00
|
|
|
if (RunSynchronously) {
|
|
|
|
if (WorkerThreadsCount.getNumOccurrences())
|
2018-10-20 23:30:37 +08:00
|
|
|
errs() << "Ignoring -j because -run-synchronously is set.\n";
|
2017-08-14 16:45:47 +08:00
|
|
|
WorkerThreadsCount = 0;
|
2018-02-25 15:21:16 +08:00
|
|
|
}
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Validate command line arguments.
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<raw_fd_ostream> InputMirrorStream;
|
2017-10-10 17:08:47 +08:00
|
|
|
if (!InputMirrorFile.empty()) {
|
|
|
|
std::error_code EC;
|
2018-06-08 03:58:58 +08:00
|
|
|
InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
|
2018-10-20 23:30:37 +08:00
|
|
|
sys::fs::FA_Read | sys::fs::FA_Write);
|
2017-10-10 17:08:47 +08:00
|
|
|
if (EC) {
|
|
|
|
InputMirrorStream.reset();
|
2018-10-20 23:30:37 +08:00
|
|
|
errs() << "Error while opening an input mirror file: " << EC.message();
|
2017-10-10 17:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 23:04:59 +08:00
|
|
|
|
2018-02-14 11:20:07 +08:00
|
|
|
// Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
|
|
|
|
// trace flag in your editor's config is annoying, launching with
|
|
|
|
// `CLANGD_TRACE=trace.json vim` is easier.
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<raw_fd_ostream> TraceStream;
|
2017-12-14 23:04:59 +08:00
|
|
|
std::unique_ptr<trace::EventTracer> Tracer;
|
2018-02-14 11:20:07 +08:00
|
|
|
if (auto *TraceFile = getenv("CLANGD_TRACE")) {
|
2017-11-02 17:21:51 +08:00
|
|
|
std::error_code EC;
|
2018-06-08 03:58:58 +08:00
|
|
|
TraceStream.emplace(TraceFile, /*ref*/ EC,
|
2018-10-20 23:30:37 +08:00
|
|
|
sys::fs::FA_Read | sys::fs::FA_Write);
|
2017-11-02 17:21:51 +08:00
|
|
|
if (EC) {
|
2018-02-14 11:20:07 +08:00
|
|
|
TraceStream.reset();
|
2018-10-20 23:30:37 +08:00
|
|
|
errs() << "Error while opening trace file " << TraceFile << ": "
|
|
|
|
<< EC.message();
|
2017-11-02 17:21:51 +08:00
|
|
|
} else {
|
2017-12-14 23:04:59 +08:00
|
|
|
Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 17:08:47 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<trace::Session> TracingSession;
|
2017-12-14 23:04:59 +08:00
|
|
|
if (Tracer)
|
|
|
|
TracingSession.emplace(*Tracer);
|
|
|
|
|
2018-08-28 21:15:50 +08:00
|
|
|
// Use buffered stream to stderr (we still flush each log message). Unbuffered
|
|
|
|
// stream can cause significant (non-deterministic) latency for the logger.
|
2018-10-20 23:30:37 +08:00
|
|
|
errs().SetBuffered();
|
|
|
|
StreamLogger Logger(errs(), LogLevel);
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
clangd::LoggingSession LoggingSession(Logger);
|
2017-12-13 20:51:22 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
// If --compile-commands-dir arg was invoked, check value and override default
|
|
|
|
// path.
|
2018-10-20 23:30:37 +08:00
|
|
|
Optional<Path> CompileCommandsDirPath;
|
2018-10-23 19:54:36 +08:00
|
|
|
if (!CompileCommandsDir.empty()) {
|
|
|
|
if (sys::fs::exists(CompileCommandsDir)) {
|
|
|
|
// We support passing both relative and absolute paths to the
|
|
|
|
// --compile-commands-dir argument, but we assume the path is absolute in
|
|
|
|
// the rest of clangd so we make sure the path is absolute before
|
|
|
|
// continuing.
|
|
|
|
SmallString<128> Path(CompileCommandsDir);
|
|
|
|
if (std::error_code EC = sys::fs::make_absolute(Path)) {
|
|
|
|
errs() << "Error while converting the relative path specified by "
|
|
|
|
"--compile-commands-dir to an absolute path: "
|
|
|
|
<< EC.message() << ". The argument will be ignored.\n";
|
|
|
|
} else {
|
|
|
|
CompileCommandsDirPath = Path.str();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errs() << "Path specified by --compile-commands-dir does not exist. The "
|
|
|
|
"argument will be ignored.\n";
|
|
|
|
}
|
2017-10-02 23:13:20 +08:00
|
|
|
}
|
2017-02-07 20:40:59 +08:00
|
|
|
|
2018-03-06 01:28:54 +08:00
|
|
|
ClangdServer::Options Opts;
|
2017-11-17 00:25:18 +08:00
|
|
|
switch (PCHStorage) {
|
|
|
|
case PCHStorageFlag::Memory:
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.StorePreamblesInMemory = true;
|
2017-11-17 00:25:18 +08:00
|
|
|
break;
|
|
|
|
case PCHStorageFlag::Disk:
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.StorePreamblesInMemory = false;
|
2017-11-17 00:25:18 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-19 23:43:35 +08:00
|
|
|
if (!ResourceDir.empty())
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.ResourceDir = ResourceDir;
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
Opts.BuildDynamicSymbolIndex = EnableIndex;
|
2018-10-16 16:53:52 +08:00
|
|
|
Opts.HeavyweightDynamicSymbolIndex = UseDex;
|
2018-01-10 22:44:34 +08:00
|
|
|
std::unique_ptr<SymbolIndex> StaticIdx;
|
[clangd] Fix async index loading (from r341376).
Summary:
This wasn't actually async (due to std::future destructor blocking).
If it were, we would have clean shutdown issues if main returned
and destroyed Placeholder before the thread is done with it.
We could attempt to avoid any blocking by using shared_ptr or weak_ptr tricks so
the thread can detect Placeholder's destruction, but there are other potential
issues (e.g. loadIndex does tracing, and we'll destroy the tracer...)
Instead, once LSPServer::run returns, we wait for the index to finish loading
before exiting. Performance is not critical in this situation.
Reviewers: ilya-biryukov
Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51674
llvm-svn: 341797
2018-09-10 18:00:47 +08:00
|
|
|
std::future<void> AsyncIndexLoad; // Block exit while loading the index.
|
2018-10-08 18:44:54 +08:00
|
|
|
if (EnableIndex && !IndexFile.empty()) {
|
2018-09-05 00:19:40 +08:00
|
|
|
// Load the index asynchronously. Meanwhile SwapIndex returns no results.
|
|
|
|
SwapIndex *Placeholder;
|
|
|
|
StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
|
[clangd] Fix async index loading (from r341376).
Summary:
This wasn't actually async (due to std::future destructor blocking).
If it were, we would have clean shutdown issues if main returned
and destroyed Placeholder before the thread is done with it.
We could attempt to avoid any blocking by using shared_ptr or weak_ptr tricks so
the thread can detect Placeholder's destruction, but there are other potential
issues (e.g. loadIndex does tracing, and we'll destroy the tracer...)
Instead, once LSPServer::run returns, we wait for the index to finish loading
before exiting. Performance is not critical in this situation.
Reviewers: ilya-biryukov
Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51674
llvm-svn: 341797
2018-09-10 18:00:47 +08:00
|
|
|
AsyncIndexLoad = runAsync<void>([Placeholder, &Opts] {
|
2018-10-16 16:53:52 +08:00
|
|
|
if (auto Idx = loadIndex(IndexFile, Opts.URISchemes, /*UseDex=*/true))
|
2018-09-05 00:19:40 +08:00
|
|
|
Placeholder->reset(std::move(Idx));
|
|
|
|
});
|
[clangd] Fix async index loading (from r341376).
Summary:
This wasn't actually async (due to std::future destructor blocking).
If it were, we would have clean shutdown issues if main returned
and destroyed Placeholder before the thread is done with it.
We could attempt to avoid any blocking by using shared_ptr or weak_ptr tricks so
the thread can detect Placeholder's destruction, but there are other potential
issues (e.g. loadIndex does tracing, and we'll destroy the tracer...)
Instead, once LSPServer::run returns, we wait for the index to finish loading
before exiting. Performance is not critical in this situation.
Reviewers: ilya-biryukov
Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D51674
llvm-svn: 341797
2018-09-10 18:00:47 +08:00
|
|
|
if (RunSynchronously)
|
|
|
|
AsyncIndexLoad.wait();
|
2018-03-06 01:28:54 +08:00
|
|
|
}
|
2018-09-05 00:19:40 +08:00
|
|
|
Opts.StaticIndex = StaticIdx.get();
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.AsyncThreadsCount = WorkerThreadsCount;
|
|
|
|
|
2017-11-24 00:58:22 +08:00
|
|
|
clangd::CodeCompleteOptions CCOpts;
|
|
|
|
CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
|
[clangd] Implementation of workspace/symbol request
Summary:
This is a basic implementation of the "workspace/symbol" request which is
used to find symbols by a string query. Since this is similar to code completion
in terms of result, this implementation reuses the "fuzzyFind" in order to get
matches. For now, the scoring algorithm is the same as code completion and
improvements could be done in the future.
The index model doesn't contain quite enough symbols for this to cover
common symbols like methods, enum class enumerators, functions in unamed
namespaces, etc. The index model will be augmented separately to achieve this.
Reviewers: sammccall, ilya-biryukov
Reviewed By: sammccall
Subscribers: jkorous, hokein, simark, sammccall, klimek, mgorny, ilya-biryukov, mgrang, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44882
llvm-svn: 330637
2018-04-24 04:00:52 +08:00
|
|
|
CCOpts.Limit = LimitResults;
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
CCOpts.BundleOverloads = CompletionStyle != Detailed;
|
2018-07-05 14:20:41 +08:00
|
|
|
CCOpts.ShowOrigins = ShowOrigins;
|
2018-07-30 03:12:42 +08:00
|
|
|
if (!HeaderInsertionDecorators) {
|
|
|
|
CCOpts.IncludeIndicator.Insert.clear();
|
|
|
|
CCOpts.IncludeIndicator.NoInsert.clear();
|
|
|
|
}
|
[clangd] Speculative code completion index request before Sema is run.
Summary:
For index-based code completion, send an asynchronous speculative index
request, based on the index request for the last code completion on the same
file and the filter text typed before the cursor, before sema code completion
is invoked. This can reduce the code completion latency (by roughly latency of
sema code completion) if the speculative request is the same as the one
generated for the ongoing code completion from sema. As a sequence of code
completions often have the same scopes and proximity paths etc, this should be
effective for a number of code completions.
Trace with speculative index request:{F6997544}
Reviewers: hokein, ilya-biryukov
Reviewed By: ilya-biryukov
Subscribers: javed.absar, jfb, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D50962
llvm-svn: 340604
2018-08-24 19:23:56 +08:00
|
|
|
CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
|
2018-09-19 18:16:44 +08:00
|
|
|
CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
|
2018-09-28 02:46:00 +08:00
|
|
|
CCOpts.AllScopes = AllScopesCompletion;
|
2018-03-06 01:28:54 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Initialize and run ClangdLSPServer.
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
// Change stdin to binary to not lose \r\n on windows.
|
2018-10-20 23:30:37 +08:00
|
|
|
sys::ChangeStdinToBinary();
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
auto Transport = newJSONTransport(
|
2018-10-20 23:30:37 +08:00
|
|
|
stdin, outs(),
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, PrettyPrint,
|
|
|
|
InputStyle);
|
2018-08-02 01:39:29 +08:00
|
|
|
ClangdLSPServer LSPServer(
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
*Transport, CCOpts, CompileCommandsDirPath,
|
2018-08-02 01:39:29 +08:00
|
|
|
/*ShouldUseInMemoryCDB=*/CompileArgsFrom == LSPCompileArgs, Opts);
|
2017-10-25 16:45:41 +08:00
|
|
|
constexpr int NoShutdownRequestErrorCode = 1;
|
2018-10-20 23:30:37 +08:00
|
|
|
set_thread_name("clangd.main");
|
[clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. (re-land r344620)
Summary:
This paves the way for alternative transports (mac XPC, maybe messagepack?),
and also generally improves layering: testing ClangdLSPServer becomes less of
a pipe dream, we split up the JSONOutput monolith, etc.
This isn't a final state, much of what remains in JSONRPCDispatcher can go away,
handlers can call reply() on the transport directly, JSONOutput can be renamed
to StreamLogger and removed, etc. But this patch is sprawling already.
The main observable change (see tests) is that hitting EOF on input is now an
error: the client should send the 'exit' notification.
This is defensible: the protocol doesn't spell this case out. Reproducing the
current behavior for all combinations of shutdown/exit/EOF clutters interfaces.
We can iterate on this if desired.
Reviewers: jkorous, ioeric, hokein
Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53286
llvm-svn: 344672
2018-10-17 15:32:05 +08:00
|
|
|
return LSPServer.run() ? 0 : NoShutdownRequestErrorCode;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|