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-05-16 22:40:30 +08:00
|
|
|
#include "JSONRPCDispatcher.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "Path.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2018-01-10 22:44:34 +08:00
|
|
|
#include "index/SymbolYAML.h"
|
2018-08-21 18:32:27 +08:00
|
|
|
#include "index/dex/DexIndex.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
|
|
|
|
|
|
|
using namespace clang;
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2018-08-28 22:55:05 +08:00
|
|
|
// FIXME: remove this option when Dex is stable enough.
|
2018-08-21 18:40:19 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
UseDex("use-dex-index",
|
|
|
|
llvm::cl::desc("Use experimental Dex static index."),
|
2018-08-28 22:55:05 +08:00
|
|
|
llvm::cl::init(true), llvm::cl::Hidden);
|
2018-08-21 18:40:19 +08:00
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
namespace {
|
2018-08-21 18:32:27 +08:00
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
enum class PCHStorageFlag { Disk, Memory };
|
2018-01-10 22:44:34 +08:00
|
|
|
|
|
|
|
} // namespace
|
2017-11-17 00:25:18 +08:00
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
static llvm::cl::opt<Path> CompileCommandsDir(
|
|
|
|
"compile-commands-dir",
|
|
|
|
llvm::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-08-14 16:45:47 +08:00
|
|
|
static llvm::cl::opt<unsigned>
|
|
|
|
WorkerThreadsCount("j",
|
|
|
|
llvm::cl::desc("Number of async workers used by clangd"),
|
|
|
|
llvm::cl::init(getDefaultAsyncThreadsCount()));
|
|
|
|
|
[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.
|
|
|
|
enum CompletionStyleFlag {
|
|
|
|
Detailed,
|
|
|
|
Bundled,
|
|
|
|
};
|
|
|
|
static llvm::cl::opt<CompletionStyleFlag> CompletionStyle(
|
|
|
|
"completion-style",
|
|
|
|
llvm::cl::desc("Granularity of code completion suggestions"),
|
|
|
|
llvm::cl::values(
|
|
|
|
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.")),
|
|
|
|
llvm::cl::init(Detailed));
|
|
|
|
|
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.
|
|
|
|
static llvm::cl::opt<bool> IncludeIneligibleResults(
|
|
|
|
"include-ineligible-results",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Include ineligible completion results (e.g. private members)"),
|
|
|
|
llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
|
|
|
|
llvm::cl::Hidden);
|
2017-09-12 21:57:14 +08:00
|
|
|
|
2018-02-06 18:47:30 +08:00
|
|
|
static llvm::cl::opt<JSONStreamStyle> InputStyle(
|
|
|
|
"input-style", llvm::cl::desc("Input JSON stream encoding"),
|
|
|
|
llvm::cl::values(
|
|
|
|
clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
|
|
|
|
clEnumValN(JSONStreamStyle::Delimited, "delimited",
|
|
|
|
"messages delimited by --- lines, with # comment support")),
|
|
|
|
llvm::cl::init(JSONStreamStyle::Standard));
|
|
|
|
|
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
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
|
|
|
|
llvm::cl::init(false));
|
|
|
|
|
[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
|
|
|
static llvm::cl::opt<Logger::Level> LogLevel(
|
|
|
|
"log", llvm::cl::desc("Verbosity of log messages written to stderr"),
|
|
|
|
llvm::cl::values(clEnumValN(Logger::Error, "error", "Error messages only"),
|
|
|
|
clEnumValN(Logger::Info, "info",
|
|
|
|
"High level execution tracing"),
|
|
|
|
clEnumValN(Logger::Debug, "verbose", "Low level details")),
|
|
|
|
llvm::cl::init(Logger::Info));
|
|
|
|
|
2018-02-06 18:47:30 +08:00
|
|
|
static llvm::cl::opt<bool> Test(
|
|
|
|
"lit-test",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Abbreviation for -input-style=delimited -pretty -run-synchronously. "
|
|
|
|
"Intended to simplify lit tests."),
|
|
|
|
llvm::cl::init(false), llvm::cl::Hidden);
|
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
static llvm::cl::opt<PCHStorageFlag> PCHStorage(
|
|
|
|
"pch-storage",
|
|
|
|
llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
|
|
|
|
"improve performance"),
|
|
|
|
llvm::cl::values(
|
|
|
|
clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
|
|
|
|
clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
|
|
|
|
llvm::cl::init(PCHStorageFlag::Disk));
|
|
|
|
|
[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
|
|
|
static llvm::cl::opt<int> LimitResults(
|
|
|
|
"limit-results",
|
|
|
|
llvm::cl::desc("Limit the number of results returned by clangd. "
|
2018-01-25 17:20:09 +08:00
|
|
|
"0 means no limit."),
|
2018-01-30 17:21:30 +08:00
|
|
|
llvm::cl::init(100));
|
2018-01-25 17:20:09 +08:00
|
|
|
|
2017-08-14 16:45:47 +08:00
|
|
|
static llvm::cl::opt<bool> RunSynchronously(
|
|
|
|
"run-synchronously",
|
|
|
|
llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
|
|
|
|
llvm::cl::init(false), llvm::cl::Hidden);
|
2017-03-02 00:16:29 +08:00
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
static llvm::cl::opt<Path>
|
2017-07-19 23:43:35 +08:00
|
|
|
ResourceDir("resource-dir",
|
2017-08-02 16:53:48 +08:00
|
|
|
llvm::cl::desc("Directory for system clang headers"),
|
2017-07-19 23:43:35 +08:00
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
static llvm::cl::opt<Path> InputMirrorFile(
|
|
|
|
"input-mirror-file",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Mirror all LSP input to the specified file. Useful for debugging."),
|
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
[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
|
|
|
static llvm::cl::opt<bool> EnableIndex(
|
|
|
|
"index",
|
|
|
|
llvm::cl::desc("Enable index-based features such as global code completion "
|
2018-08-14 20:00:39 +08:00
|
|
|
"and searching for symbols. "
|
[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
|
|
|
"Clang uses an index built from symbols in opened files"),
|
2018-01-30 17:21:30 +08:00
|
|
|
llvm::cl::init(true));
|
2017-12-20 02:00:37 +08:00
|
|
|
|
2018-07-05 14:20:41 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
ShowOrigins("debug-origin",
|
|
|
|
llvm::cl::desc("Show origins of completion items"),
|
|
|
|
llvm::cl::init(clangd::CodeCompleteOptions().ShowOrigins),
|
|
|
|
llvm::cl::Hidden);
|
|
|
|
|
2018-07-30 03:12:42 +08:00
|
|
|
static llvm::cl::opt<bool> HeaderInsertionDecorators(
|
|
|
|
"header-insertion-decorators",
|
|
|
|
llvm::cl::desc("Prepend a circular dot or space before the completion "
|
2018-08-14 20:00:39 +08:00
|
|
|
"label, depending on whether "
|
2018-07-30 03:12:42 +08:00
|
|
|
"an include line will be inserted or not."),
|
|
|
|
llvm::cl::init(true));
|
|
|
|
|
2018-01-10 22:44:34 +08:00
|
|
|
static llvm::cl::opt<Path> YamlSymbolFile(
|
|
|
|
"yaml-symbol-file",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"YAML-format global symbol file to build the static index. Clangd will "
|
|
|
|
"use the static index for global code completion.\n"
|
|
|
|
"WARNING: This option is experimental only, and will be removed "
|
|
|
|
"eventually. Don't rely on it."),
|
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
2018-08-02 01:39:29 +08:00
|
|
|
enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
|
|
|
|
|
|
|
|
static llvm::cl::opt<CompileArgsFrom> CompileArgsFrom(
|
|
|
|
"compile_args_from", llvm::cl::desc("The source of compile commands"),
|
|
|
|
llvm::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")),
|
|
|
|
llvm::cl::init(FilesystemCompileArgs), llvm::cl::Hidden);
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-02-20 19:46:39 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2018-06-29 21:24:20 +08:00
|
|
|
llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) {
|
|
|
|
OS << clang::getClangToolFullVersion("clangd") << "\n";
|
|
|
|
});
|
|
|
|
llvm::cl::ParseCommandLineOptions(
|
|
|
|
argc, argv,
|
|
|
|
"clangd is a language server that provides IDE-like features to editors. "
|
|
|
|
"\n\nIt should be used via an editor plugin rather than invoked directly."
|
|
|
|
"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) {
|
|
|
|
llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
|
|
|
|
"specify -run-synchronously?";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-02-25 15:21:16 +08:00
|
|
|
if (RunSynchronously) {
|
|
|
|
if (WorkerThreadsCount.getNumOccurrences())
|
|
|
|
llvm::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.
|
2017-10-10 17:08:47 +08:00
|
|
|
llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
|
|
|
|
if (!InputMirrorFile.empty()) {
|
|
|
|
std::error_code EC;
|
2018-06-08 03:58:58 +08:00
|
|
|
InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
|
|
|
|
llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
|
2017-10-10 17:08:47 +08:00
|
|
|
if (EC) {
|
|
|
|
InputMirrorStream.reset();
|
|
|
|
llvm::errs() << "Error while opening an input mirror file: "
|
|
|
|
<< EC.message();
|
|
|
|
}
|
|
|
|
}
|
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.
|
2017-11-02 17:21:51 +08:00
|
|
|
llvm::Optional<llvm::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,
|
|
|
|
llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
|
2017-11-02 17:21:51 +08:00
|
|
|
if (EC) {
|
2018-02-14 11:20:07 +08:00
|
|
|
TraceStream.reset();
|
|
|
|
llvm::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
|
|
|
|
2017-12-14 23:04:59 +08:00
|
|
|
llvm::Optional<trace::Session> TracingSession;
|
|
|
|
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.
|
|
|
|
llvm::errs().SetBuffered();
|
[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
|
|
|
JSONOutput Out(llvm::outs(), llvm::errs(), LogLevel,
|
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
|
|
|
InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
|
|
|
|
PrettyPrint);
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
clangd::LoggingSession LoggingSession(Out);
|
|
|
|
|
2017-10-02 23:13:20 +08:00
|
|
|
// If --compile-commands-dir arg was invoked, check value and override default
|
|
|
|
// path.
|
|
|
|
llvm::Optional<Path> CompileCommandsDirPath;
|
|
|
|
if (CompileCommandsDir.empty()) {
|
|
|
|
CompileCommandsDirPath = llvm::None;
|
|
|
|
} else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
|
|
|
|
!llvm::sys::fs::exists(CompileCommandsDir)) {
|
|
|
|
llvm::errs() << "Path specified by --compile-commands-dir either does not "
|
|
|
|
"exist or is not an absolute "
|
|
|
|
"path. The argument will be ignored.\n";
|
|
|
|
CompileCommandsDirPath = llvm::None;
|
|
|
|
} else {
|
|
|
|
CompileCommandsDirPath = CompileCommandsDir;
|
|
|
|
}
|
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-01-10 22:44:34 +08:00
|
|
|
std::unique_ptr<SymbolIndex> StaticIdx;
|
[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
|
|
|
if (EnableIndex && !YamlSymbolFile.empty()) {
|
2018-09-04 23:10:40 +08:00
|
|
|
StaticIdx = loadIndex(YamlSymbolFile, UseDex);
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.StaticIndex = StaticIdx.get();
|
|
|
|
}
|
|
|
|
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-03-06 01:28:54 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Initialize and run ClangdLSPServer.
|
2018-08-02 01:39:29 +08:00
|
|
|
ClangdLSPServer LSPServer(
|
|
|
|
Out, CCOpts, CompileCommandsDirPath,
|
|
|
|
/*ShouldUseInMemoryCDB=*/CompileArgsFrom == LSPCompileArgs, Opts);
|
2017-10-25 16:45:41 +08:00
|
|
|
constexpr int NoShutdownRequestErrorCode = 1;
|
2017-11-02 17:21:51 +08:00
|
|
|
llvm::set_thread_name("clangd.main");
|
2018-03-06 01:28:54 +08:00
|
|
|
// Change stdin to binary to not lose \r\n on windows.
|
|
|
|
llvm::sys::ChangeStdinToBinary();
|
2018-06-05 17:34:46 +08:00
|
|
|
return LSPServer.run(stdin, InputStyle) ? 0 : NoShutdownRequestErrorCode;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|