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"
|
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"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
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;
|
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
namespace {
|
|
|
|
enum class PCHStorageFlag { Disk, Memory };
|
2018-01-10 22:44:34 +08:00
|
|
|
|
|
|
|
// Build an in-memory static index for global symbols from a YAML-format file.
|
|
|
|
// The size of global symbols should be relatively small, so that all symbols
|
|
|
|
// can be managed in memory.
|
|
|
|
std::unique_ptr<SymbolIndex> BuildStaticIndex(llvm::StringRef YamlSymbolFile) {
|
|
|
|
auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
|
|
|
|
if (!Buffer) {
|
|
|
|
llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto Slab = SymbolFromYAML(Buffer.get()->getBuffer());
|
|
|
|
SymbolSlab::Builder SymsBuilder;
|
|
|
|
for (auto Sym : Slab)
|
|
|
|
SymsBuilder.insert(Sym);
|
|
|
|
|
|
|
|
return MemIndex::build(std::move(SymsBuilder).build());
|
2017-11-17 00:25:18 +08:00
|
|
|
}
|
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()));
|
|
|
|
|
2017-09-12 21:57:14 +08:00
|
|
|
static llvm::cl::opt<bool> EnableSnippets(
|
|
|
|
"enable-snippets",
|
|
|
|
llvm::cl::desc(
|
2017-11-24 00:58:22 +08:00
|
|
|
"Present snippet completions instead of plaintext completions. "
|
|
|
|
"This also enables code pattern results." /* FIXME: should it? */),
|
|
|
|
llvm::cl::init(clangd::CodeCompleteOptions().EnableSnippets));
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
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));
|
|
|
|
|
2018-01-25 17:20:09 +08:00
|
|
|
static llvm::cl::opt<int> LimitCompletionResult(
|
2018-01-30 17:21:30 +08:00
|
|
|
"completion-limit",
|
2018-01-25 17:20:09 +08:00
|
|
|
llvm::cl::desc("Limit the number of completion results returned by clangd. "
|
|
|
|
"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);
|
|
|
|
|
2017-11-02 17:21:51 +08:00
|
|
|
static llvm::cl::opt<Path> TraceFile(
|
|
|
|
"trace",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Trace internal events and timestamps in chrome://tracing JSON format"),
|
|
|
|
llvm::cl::init(""), llvm::cl::Hidden);
|
|
|
|
|
2017-12-20 02:00:37 +08:00
|
|
|
static llvm::cl::opt<bool> EnableIndexBasedCompletion(
|
|
|
|
"enable-index-based-completion",
|
|
|
|
llvm::cl::desc(
|
2018-01-30 17:21:30 +08:00
|
|
|
"Enable index-based global code completion. "
|
|
|
|
"Clang uses an index built from symbols in opened files"),
|
|
|
|
llvm::cl::init(true));
|
2017-12-20 02:00:37 +08:00
|
|
|
|
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);
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2017-03-02 00:16:29 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore -j option if -run-synchonously is used.
|
|
|
|
// FIXME: a warning should be shown here.
|
|
|
|
if (RunSynchronously)
|
|
|
|
WorkerThreadsCount = 0;
|
|
|
|
|
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;
|
|
|
|
InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
|
|
|
|
if (EC) {
|
|
|
|
InputMirrorStream.reset();
|
|
|
|
llvm::errs() << "Error while opening an input mirror file: "
|
|
|
|
<< EC.message();
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 23:04:59 +08:00
|
|
|
|
|
|
|
// Setup tracing facilities.
|
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;
|
2017-11-02 17:21:51 +08:00
|
|
|
if (!TraceFile.empty()) {
|
|
|
|
std::error_code EC;
|
|
|
|
TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
|
|
|
|
if (EC) {
|
|
|
|
TraceFile.reset();
|
|
|
|
llvm::errs() << "Error while opening trace file: " << EC.message();
|
|
|
|
} 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);
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::raw_ostream &Outs = llvm::outs();
|
|
|
|
llvm::raw_ostream &Logs = llvm::errs();
|
2017-10-10 17:08:47 +08:00
|
|
|
JSONOutput Out(Outs, Logs,
|
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
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
bool StorePreamblesInMemory;
|
|
|
|
switch (PCHStorage) {
|
|
|
|
case PCHStorageFlag::Memory:
|
|
|
|
StorePreamblesInMemory = true;
|
|
|
|
break;
|
|
|
|
case PCHStorageFlag::Disk:
|
|
|
|
StorePreamblesInMemory = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-19 23:43:35 +08:00
|
|
|
llvm::Optional<StringRef> ResourceDirRef = None;
|
|
|
|
if (!ResourceDir.empty())
|
|
|
|
ResourceDirRef = ResourceDir;
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Change stdin to binary to not lose \r\n on windows.
|
2017-10-02 23:13:20 +08:00
|
|
|
llvm::sys::ChangeStdinToBinary();
|
|
|
|
|
2018-01-10 22:44:34 +08:00
|
|
|
std::unique_ptr<SymbolIndex> StaticIdx;
|
|
|
|
if (EnableIndexBasedCompletion && !YamlSymbolFile.empty())
|
|
|
|
StaticIdx = BuildStaticIndex(YamlSymbolFile);
|
2017-11-24 00:58:22 +08:00
|
|
|
clangd::CodeCompleteOptions CCOpts;
|
|
|
|
CCOpts.EnableSnippets = EnableSnippets;
|
|
|
|
CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
|
2018-01-25 17:20:09 +08:00
|
|
|
CCOpts.Limit = LimitCompletionResult;
|
2017-10-26 18:07:04 +08:00
|
|
|
// Initialize and run ClangdLSPServer.
|
2017-11-17 00:25:18 +08:00
|
|
|
ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory,
|
2017-12-20 02:00:37 +08:00
|
|
|
CCOpts, ResourceDirRef, CompileCommandsDirPath,
|
2018-01-10 22:44:34 +08:00
|
|
|
EnableIndexBasedCompletion, StaticIdx.get());
|
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-02-06 18:47:30 +08:00
|
|
|
return LSPServer.run(std::cin, InputStyle) ? 0 : NoShutdownRequestErrorCode;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|