blob: 75ad029aadf99cfc954e47bdab9b42423e4a34eb [file] [log] [blame]
Krasimir Georgiev95ef1712017-04-12 17:13:08 +00001//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Ilya Biryukov38d79772017-05-16 09:38:59 +000010#include "ClangdLSPServer.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000011#include "JSONRPCDispatcher.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000012#include "Path.h"
Sam McCall8567cb32017-11-02 09:21:51 +000013#include "Trace.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000014#include "index/SymbolYAML.h"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000015#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000016#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000017#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000018#include "llvm/Support/Program.h"
Eric Liuc5105f92018-02-16 14:15:55 +000019#include "llvm/Support/Signals.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000020#include "llvm/Support/raw_ostream.h"
Sam McCalled2717a2018-02-14 03:20:07 +000021#include <cstdlib>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000022#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000023#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000024#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000025#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000026
27using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000028using namespace clang::clangd;
29
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000030namespace {
31enum class PCHStorageFlag { Disk, Memory };
Haojian Wuba28e9a2018-01-10 14:44:34 +000032
33// Build an in-memory static index for global symbols from a YAML-format file.
34// The size of global symbols should be relatively small, so that all symbols
35// can be managed in memory.
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +000036std::unique_ptr<SymbolIndex> buildStaticIndex(llvm::StringRef YamlSymbolFile) {
Haojian Wuba28e9a2018-01-10 14:44:34 +000037 auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
38 if (!Buffer) {
39 llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
40 return nullptr;
41 }
Sam McCall60039512018-02-09 14:42:01 +000042 auto Slab = SymbolsFromYAML(Buffer.get()->getBuffer());
Haojian Wuba28e9a2018-01-10 14:44:34 +000043 SymbolSlab::Builder SymsBuilder;
44 for (auto Sym : Slab)
45 SymsBuilder.insert(Sym);
46
47 return MemIndex::build(std::move(SymsBuilder).build());
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000048}
Haojian Wuba28e9a2018-01-10 14:44:34 +000049} // namespace
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000050
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000051static llvm::cl::opt<Path> CompileCommandsDir(
52 "compile-commands-dir",
53 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
54 "is invalid, clangd will look in the current directory and "
55 "parent paths of each source file."));
56
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000057static llvm::cl::opt<unsigned>
58 WorkerThreadsCount("j",
59 llvm::cl::desc("Number of async workers used by clangd"),
60 llvm::cl::init(getDefaultAsyncThreadsCount()));
61
Sam McCallc18c2802018-06-15 11:06:29 +000062// FIXME: also support "plain" style where signatures are always omitted.
63enum CompletionStyleFlag {
64 Detailed,
65 Bundled,
66};
67static llvm::cl::opt<CompletionStyleFlag> CompletionStyle(
68 "completion-style",
69 llvm::cl::desc("Granularity of code completion suggestions"),
70 llvm::cl::values(
71 clEnumValN(Detailed, "detailed",
72 "One completion item for each semantically distinct "
73 "completion, with full type information."),
74 clEnumValN(Bundled, "bundled",
75 "Similar completion items (e.g. function overloads) are "
76 "combined. Type information shown where possible.")),
77 llvm::cl::init(Detailed));
78
Sam McCalladccab62017-11-23 16:58:22 +000079// FIXME: Flags are the wrong mechanism for user preferences.
80// We should probably read a dotfile or similar.
81static llvm::cl::opt<bool> IncludeIneligibleResults(
82 "include-ineligible-results",
83 llvm::cl::desc(
84 "Include ineligible completion results (e.g. private members)"),
85 llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
86 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000087
Sam McCall5ed599e2018-02-06 10:47:30 +000088static llvm::cl::opt<JSONStreamStyle> InputStyle(
89 "input-style", llvm::cl::desc("Input JSON stream encoding"),
90 llvm::cl::values(
91 clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
92 clEnumValN(JSONStreamStyle::Delimited, "delimited",
93 "messages delimited by --- lines, with # comment support")),
94 llvm::cl::init(JSONStreamStyle::Standard));
95
Sam McCalldd0566b2017-11-06 15:40:30 +000096static llvm::cl::opt<bool>
97 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
98 llvm::cl::init(false));
99
Sam McCall5ed599e2018-02-06 10:47:30 +0000100static llvm::cl::opt<bool> Test(
101 "lit-test",
102 llvm::cl::desc(
103 "Abbreviation for -input-style=delimited -pretty -run-synchronously. "
104 "Intended to simplify lit tests."),
105 llvm::cl::init(false), llvm::cl::Hidden);
106
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000107static llvm::cl::opt<PCHStorageFlag> PCHStorage(
108 "pch-storage",
109 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
110 "improve performance"),
111 llvm::cl::values(
112 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
113 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
114 llvm::cl::init(PCHStorageFlag::Disk));
115
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000116static llvm::cl::opt<int> LimitResults(
117 "limit-results",
118 llvm::cl::desc("Limit the number of results returned by clangd. "
Haojian Wu48b48652018-01-25 09:20:09 +0000119 "0 means no limit."),
Sam McCallea283c72018-01-30 09:21:30 +0000120 llvm::cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000121
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000122static llvm::cl::opt<bool> RunSynchronously(
123 "run-synchronously",
124 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
125 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000126
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000127static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000128 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +0000129 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000130 llvm::cl::init(""), llvm::cl::Hidden);
131
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000132static llvm::cl::opt<Path> InputMirrorFile(
133 "input-mirror-file",
134 llvm::cl::desc(
135 "Mirror all LSP input to the specified file. Useful for debugging."),
136 llvm::cl::init(""), llvm::cl::Hidden);
137
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000138static llvm::cl::opt<bool> EnableIndex(
139 "index",
140 llvm::cl::desc("Enable index-based features such as global code completion "
141 "and searching for symbols."
142 "Clang uses an index built from symbols in opened files"),
Sam McCallea283c72018-01-30 09:21:30 +0000143 llvm::cl::init(true));
Eric Liubfac8f72017-12-19 18:00:37 +0000144
Haojian Wuba28e9a2018-01-10 14:44:34 +0000145static llvm::cl::opt<Path> YamlSymbolFile(
146 "yaml-symbol-file",
147 llvm::cl::desc(
148 "YAML-format global symbol file to build the static index. Clangd will "
149 "use the static index for global code completion.\n"
150 "WARNING: This option is experimental only, and will be removed "
151 "eventually. Don't rely on it."),
152 llvm::cl::init(""), llvm::cl::Hidden);
153
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000154int main(int argc, char *argv[]) {
Sam McCall3ebf7602018-02-20 11:46:39 +0000155 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000156 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Sam McCall5ed599e2018-02-06 10:47:30 +0000157 if (Test) {
158 RunSynchronously = true;
159 InputStyle = JSONStreamStyle::Delimited;
160 PrettyPrint = true;
161 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000162
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000163 if (!RunSynchronously && WorkerThreadsCount == 0) {
164 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
165 "specify -run-synchronously?";
166 return 1;
167 }
168
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000169 if (RunSynchronously) {
170 if (WorkerThreadsCount.getNumOccurrences())
171 llvm::errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000172 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000173 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000174
Benjamin Kramer74a18952017-10-26 10:07:04 +0000175 // Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000176 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
177 if (!InputMirrorFile.empty()) {
178 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000179 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
180 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000181 if (EC) {
182 InputMirrorStream.reset();
183 llvm::errs() << "Error while opening an input mirror file: "
184 << EC.message();
185 }
186 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000187
Sam McCalled2717a2018-02-14 03:20:07 +0000188 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
189 // trace flag in your editor's config is annoying, launching with
190 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCall8567cb32017-11-02 09:21:51 +0000191 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000192 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000193 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000194 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000195 TraceStream.emplace(TraceFile, /*ref*/ EC,
196 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000197 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000198 TraceStream.reset();
199 llvm::errs() << "Error while opening trace file " << TraceFile << ": "
200 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000201 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000202 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000203 }
204 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000205
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000206 llvm::Optional<trace::Session> TracingSession;
207 if (Tracer)
208 TracingSession.emplace(*Tracer);
209
Sam McCall7363a2f2018-03-05 17:28:54 +0000210 JSONOutput Out(llvm::outs(), llvm::errs(),
Sam McCalldd0566b2017-11-06 15:40:30 +0000211 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
212 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000213
Ilya Biryukov940901e2017-12-13 12:51:22 +0000214 clangd::LoggingSession LoggingSession(Out);
215
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000216 // If --compile-commands-dir arg was invoked, check value and override default
217 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000218 llvm::Optional<Path> CompileCommandsDirPath;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000219 if (CompileCommandsDir.empty()) {
220 CompileCommandsDirPath = llvm::None;
221 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
222 !llvm::sys::fs::exists(CompileCommandsDir)) {
223 llvm::errs() << "Path specified by --compile-commands-dir either does not "
224 "exist or is not an absolute "
225 "path. The argument will be ignored.\n";
226 CompileCommandsDirPath = llvm::None;
227 } else {
228 CompileCommandsDirPath = CompileCommandsDir;
229 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000230
Sam McCall7363a2f2018-03-05 17:28:54 +0000231 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000232 switch (PCHStorage) {
233 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000234 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000235 break;
236 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000237 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000238 break;
239 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000240 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000241 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000242 Opts.BuildDynamicSymbolIndex = EnableIndex;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000243 std::unique_ptr<SymbolIndex> StaticIdx;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000244 if (EnableIndex && !YamlSymbolFile.empty()) {
Kirill Bobyrev5a267ed2018-05-29 11:50:51 +0000245 StaticIdx = buildStaticIndex(YamlSymbolFile);
Sam McCall7363a2f2018-03-05 17:28:54 +0000246 Opts.StaticIndex = StaticIdx.get();
247 }
248 Opts.AsyncThreadsCount = WorkerThreadsCount;
249
Sam McCalladccab62017-11-23 16:58:22 +0000250 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000251 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000252 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000253 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall7363a2f2018-03-05 17:28:54 +0000254
Benjamin Kramer74a18952017-10-26 10:07:04 +0000255 // Initialize and run ClangdLSPServer.
Sam McCall7363a2f2018-03-05 17:28:54 +0000256 ClangdLSPServer LSPServer(Out, CCOpts, CompileCommandsDirPath, Opts);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000257 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000258 llvm::set_thread_name("clangd.main");
Sam McCall7363a2f2018-03-05 17:28:54 +0000259 // Change stdin to binary to not lose \r\n on windows.
260 llvm::sys::ChangeStdinToBinary();
Sam McCall27a07cf2018-06-05 09:34:46 +0000261 return LSPServer.run(stdin, InputStyle) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000262}