blob: c7d99d2a1967ba4a1b836628c14b662e92ccd301 [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 McCall50f36312018-09-04 16:16:50 +000013#include "RIFF.h"
Sam McCall8567cb32017-11-02 09:21:51 +000014#include "Trace.h"
Haojian Wuba28e9a2018-01-10 14:44:34 +000015#include "index/SymbolYAML.h"
Raoul Wols8f5e06f2018-07-29 19:12:42 +000016#include "clang/Basic/Version.h"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000017#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000018#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000019#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000020#include "llvm/Support/Program.h"
Eric Liuc5105f92018-02-16 14:15:55 +000021#include "llvm/Support/Signals.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000022#include "llvm/Support/raw_ostream.h"
Sam McCalled2717a2018-02-14 03:20:07 +000023#include <cstdlib>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000024#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000025#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000026#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000027#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000028
29using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000030using namespace clang::clangd;
31
Kirill Bobyrev8212eae2018-08-28 14:55:05 +000032// FIXME: remove this option when Dex is stable enough.
Kirill Bobyrevdc41bef2018-08-21 10:40:19 +000033static llvm::cl::opt<bool>
34 UseDex("use-dex-index",
35 llvm::cl::desc("Use experimental Dex static index."),
Kirill Bobyrev8212eae2018-08-28 14:55:05 +000036 llvm::cl::init(true), llvm::cl::Hidden);
Kirill Bobyrevdc41bef2018-08-21 10:40:19 +000037
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000038static llvm::cl::opt<Path> CompileCommandsDir(
39 "compile-commands-dir",
40 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
41 "is invalid, clangd will look in the current directory and "
42 "parent paths of each source file."));
43
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000044static llvm::cl::opt<unsigned>
45 WorkerThreadsCount("j",
46 llvm::cl::desc("Number of async workers used by clangd"),
47 llvm::cl::init(getDefaultAsyncThreadsCount()));
48
Sam McCallc18c2802018-06-15 11:06:29 +000049// FIXME: also support "plain" style where signatures are always omitted.
Sam McCall47feb572018-09-05 10:39:58 +000050enum CompletionStyleFlag { Detailed, Bundled };
Sam McCallc18c2802018-06-15 11:06:29 +000051static llvm::cl::opt<CompletionStyleFlag> CompletionStyle(
52 "completion-style",
53 llvm::cl::desc("Granularity of code completion suggestions"),
54 llvm::cl::values(
55 clEnumValN(Detailed, "detailed",
56 "One completion item for each semantically distinct "
57 "completion, with full type information."),
58 clEnumValN(Bundled, "bundled",
59 "Similar completion items (e.g. function overloads) are "
60 "combined. Type information shown where possible.")),
61 llvm::cl::init(Detailed));
62
Sam McCalladccab62017-11-23 16:58:22 +000063// FIXME: Flags are the wrong mechanism for user preferences.
64// We should probably read a dotfile or similar.
65static llvm::cl::opt<bool> IncludeIneligibleResults(
66 "include-ineligible-results",
67 llvm::cl::desc(
68 "Include ineligible completion results (e.g. private members)"),
69 llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
70 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000071
Sam McCall5ed599e2018-02-06 10:47:30 +000072static llvm::cl::opt<JSONStreamStyle> InputStyle(
73 "input-style", llvm::cl::desc("Input JSON stream encoding"),
74 llvm::cl::values(
75 clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
76 clEnumValN(JSONStreamStyle::Delimited, "delimited",
77 "messages delimited by --- lines, with # comment support")),
78 llvm::cl::init(JSONStreamStyle::Standard));
79
Sam McCalldd0566b2017-11-06 15:40:30 +000080static llvm::cl::opt<bool>
81 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
82 llvm::cl::init(false));
83
Sam McCallbed58852018-07-11 10:35:11 +000084static llvm::cl::opt<Logger::Level> LogLevel(
85 "log", llvm::cl::desc("Verbosity of log messages written to stderr"),
86 llvm::cl::values(clEnumValN(Logger::Error, "error", "Error messages only"),
87 clEnumValN(Logger::Info, "info",
88 "High level execution tracing"),
89 clEnumValN(Logger::Debug, "verbose", "Low level details")),
90 llvm::cl::init(Logger::Info));
91
Sam McCall5ed599e2018-02-06 10:47:30 +000092static llvm::cl::opt<bool> Test(
93 "lit-test",
94 llvm::cl::desc(
95 "Abbreviation for -input-style=delimited -pretty -run-synchronously. "
96 "Intended to simplify lit tests."),
97 llvm::cl::init(false), llvm::cl::Hidden);
98
Sam McCall47feb572018-09-05 10:39:58 +000099enum PCHStorageFlag { Disk, Memory };
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000100static llvm::cl::opt<PCHStorageFlag> PCHStorage(
101 "pch-storage",
102 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
103 "improve performance"),
104 llvm::cl::values(
105 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
106 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
107 llvm::cl::init(PCHStorageFlag::Disk));
108
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000109static llvm::cl::opt<int> LimitResults(
110 "limit-results",
111 llvm::cl::desc("Limit the number of results returned by clangd. "
Haojian Wu48b48652018-01-25 09:20:09 +0000112 "0 means no limit."),
Sam McCallea283c72018-01-30 09:21:30 +0000113 llvm::cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000114
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000115static llvm::cl::opt<bool> RunSynchronously(
116 "run-synchronously",
117 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
118 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000119
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000120static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000121 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +0000122 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000123 llvm::cl::init(""), llvm::cl::Hidden);
124
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000125static llvm::cl::opt<Path> InputMirrorFile(
126 "input-mirror-file",
127 llvm::cl::desc(
128 "Mirror all LSP input to the specified file. Useful for debugging."),
129 llvm::cl::init(""), llvm::cl::Hidden);
130
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000131static llvm::cl::opt<bool> EnableIndex(
132 "index",
133 llvm::cl::desc("Enable index-based features such as global code completion "
Kirill Bobyrev0ef813f2018-08-14 12:00:39 +0000134 "and searching for symbols. "
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000135 "Clang uses an index built from symbols in opened files"),
Sam McCallea283c72018-01-30 09:21:30 +0000136 llvm::cl::init(true));
Eric Liubfac8f72017-12-19 18:00:37 +0000137
Sam McCall2161ec72018-07-05 06:20:41 +0000138static llvm::cl::opt<bool>
139 ShowOrigins("debug-origin",
140 llvm::cl::desc("Show origins of completion items"),
141 llvm::cl::init(clangd::CodeCompleteOptions().ShowOrigins),
142 llvm::cl::Hidden);
143
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000144static llvm::cl::opt<bool> HeaderInsertionDecorators(
145 "header-insertion-decorators",
146 llvm::cl::desc("Prepend a circular dot or space before the completion "
Kirill Bobyrev0ef813f2018-08-14 12:00:39 +0000147 "label, depending on whether "
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000148 "an include line will be inserted or not."),
149 llvm::cl::init(true));
150
Haojian Wuba28e9a2018-01-10 14:44:34 +0000151static llvm::cl::opt<Path> YamlSymbolFile(
152 "yaml-symbol-file",
153 llvm::cl::desc(
154 "YAML-format global symbol file to build the static index. Clangd will "
155 "use the static index for global code completion.\n"
156 "WARNING: This option is experimental only, and will be removed "
157 "eventually. Don't rely on it."),
158 llvm::cl::init(""), llvm::cl::Hidden);
159
Alex Lorenzf8087862018-08-01 17:39:29 +0000160enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
Alex Lorenzf8087862018-08-01 17:39:29 +0000161static llvm::cl::opt<CompileArgsFrom> CompileArgsFrom(
162 "compile_args_from", llvm::cl::desc("The source of compile commands"),
163 llvm::cl::values(clEnumValN(LSPCompileArgs, "lsp",
164 "All compile commands come from LSP and "
165 "'compile_commands.json' files are ignored"),
166 clEnumValN(FilesystemCompileArgs, "filesystem",
167 "All compile commands come from the "
168 "'compile_commands.json' files")),
169 llvm::cl::init(FilesystemCompileArgs), llvm::cl::Hidden);
170
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000171int main(int argc, char *argv[]) {
Sam McCall3ebf7602018-02-20 11:46:39 +0000172 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Sam McCalle72d0972018-06-29 13:24:20 +0000173 llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) {
174 OS << clang::getClangToolFullVersion("clangd") << "\n";
175 });
176 llvm::cl::ParseCommandLineOptions(
177 argc, argv,
178 "clangd is a language server that provides IDE-like features to editors. "
179 "\n\nIt should be used via an editor plugin rather than invoked directly."
180 "For more information, see:"
181 "\n\thttps://clang.llvm.org/extra/clangd.html"
182 "\n\thttps://microsoft.github.io/language-server-protocol/");
Sam McCall5ed599e2018-02-06 10:47:30 +0000183 if (Test) {
184 RunSynchronously = true;
185 InputStyle = JSONStreamStyle::Delimited;
186 PrettyPrint = true;
187 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000188
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000189 if (!RunSynchronously && WorkerThreadsCount == 0) {
190 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
191 "specify -run-synchronously?";
192 return 1;
193 }
194
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000195 if (RunSynchronously) {
196 if (WorkerThreadsCount.getNumOccurrences())
197 llvm::errs() << "Ignoring -j because -run-synchronously is set.\n";
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000198 WorkerThreadsCount = 0;
Kirill Bobyrevbcaf3802018-02-25 07:21:16 +0000199 }
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000200
Benjamin Kramer74a18952017-10-26 10:07:04 +0000201 // Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000202 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
203 if (!InputMirrorFile.empty()) {
204 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000205 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
206 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000207 if (EC) {
208 InputMirrorStream.reset();
209 llvm::errs() << "Error while opening an input mirror file: "
210 << EC.message();
211 }
212 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000213
Sam McCalled2717a2018-02-14 03:20:07 +0000214 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
215 // trace flag in your editor's config is annoying, launching with
216 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCall8567cb32017-11-02 09:21:51 +0000217 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000218 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000219 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000220 std::error_code EC;
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000221 TraceStream.emplace(TraceFile, /*ref*/ EC,
222 llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
Sam McCall8567cb32017-11-02 09:21:51 +0000223 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000224 TraceStream.reset();
225 llvm::errs() << "Error while opening trace file " << TraceFile << ": "
226 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000227 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000228 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000229 }
230 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000231
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000232 llvm::Optional<trace::Session> TracingSession;
233 if (Tracer)
234 TracingSession.emplace(*Tracer);
235
Eric Liu4e4e5a42018-08-28 13:15:50 +0000236 // Use buffered stream to stderr (we still flush each log message). Unbuffered
237 // stream can cause significant (non-deterministic) latency for the logger.
238 llvm::errs().SetBuffered();
Sam McCallbed58852018-07-11 10:35:11 +0000239 JSONOutput Out(llvm::outs(), llvm::errs(), LogLevel,
Sam McCalldd0566b2017-11-06 15:40:30 +0000240 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
241 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000242
Ilya Biryukov940901e2017-12-13 12:51:22 +0000243 clangd::LoggingSession LoggingSession(Out);
244
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000245 // If --compile-commands-dir arg was invoked, check value and override default
246 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000247 llvm::Optional<Path> CompileCommandsDirPath;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000248 if (CompileCommandsDir.empty()) {
249 CompileCommandsDirPath = llvm::None;
250 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
251 !llvm::sys::fs::exists(CompileCommandsDir)) {
252 llvm::errs() << "Path specified by --compile-commands-dir either does not "
253 "exist or is not an absolute "
254 "path. The argument will be ignored.\n";
255 CompileCommandsDirPath = llvm::None;
256 } else {
257 CompileCommandsDirPath = CompileCommandsDir;
258 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000259
Sam McCall7363a2f2018-03-05 17:28:54 +0000260 ClangdServer::Options Opts;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000261 switch (PCHStorage) {
262 case PCHStorageFlag::Memory:
Sam McCall7363a2f2018-03-05 17:28:54 +0000263 Opts.StorePreamblesInMemory = true;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000264 break;
265 case PCHStorageFlag::Disk:
Sam McCall7363a2f2018-03-05 17:28:54 +0000266 Opts.StorePreamblesInMemory = false;
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000267 break;
268 }
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000269 if (!ResourceDir.empty())
Sam McCall7363a2f2018-03-05 17:28:54 +0000270 Opts.ResourceDir = ResourceDir;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000271 Opts.BuildDynamicSymbolIndex = EnableIndex;
Haojian Wuba28e9a2018-01-10 14:44:34 +0000272 std::unique_ptr<SymbolIndex> StaticIdx;
Sam McCallf469c642018-09-10 10:00:47 +0000273 std::future<void> AsyncIndexLoad; // Block exit while loading the index.
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000274 if (EnableIndex && !YamlSymbolFile.empty()) {
Sam McCall76c4c3a2018-09-04 16:19:40 +0000275 // Load the index asynchronously. Meanwhile SwapIndex returns no results.
276 SwapIndex *Placeholder;
277 StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>()));
Sam McCallf469c642018-09-10 10:00:47 +0000278 AsyncIndexLoad = runAsync<void>([Placeholder, &Opts] {
Kirill Bobyrev19a94612018-09-06 12:54:43 +0000279 if (auto Idx = loadIndex(YamlSymbolFile, Opts.URISchemes, UseDex))
Sam McCall76c4c3a2018-09-04 16:19:40 +0000280 Placeholder->reset(std::move(Idx));
281 });
Sam McCallf469c642018-09-10 10:00:47 +0000282 if (RunSynchronously)
283 AsyncIndexLoad.wait();
Sam McCall7363a2f2018-03-05 17:28:54 +0000284 }
Sam McCall76c4c3a2018-09-04 16:19:40 +0000285 Opts.StaticIndex = StaticIdx.get();
Sam McCall7363a2f2018-03-05 17:28:54 +0000286 Opts.AsyncThreadsCount = WorkerThreadsCount;
287
Sam McCalladccab62017-11-23 16:58:22 +0000288 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000289 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000290 CCOpts.Limit = LimitResults;
Sam McCallc18c2802018-06-15 11:06:29 +0000291 CCOpts.BundleOverloads = CompletionStyle != Detailed;
Sam McCall2161ec72018-07-05 06:20:41 +0000292 CCOpts.ShowOrigins = ShowOrigins;
Raoul Wols8f5e06f2018-07-29 19:12:42 +0000293 if (!HeaderInsertionDecorators) {
294 CCOpts.IncludeIndicator.Insert.clear();
295 CCOpts.IncludeIndicator.NoInsert.clear();
296 }
Eric Liu25d74e92018-08-24 11:23:56 +0000297 CCOpts.SpeculativeIndexRequest = Opts.StaticIndex;
Sam McCall7363a2f2018-03-05 17:28:54 +0000298
Benjamin Kramer74a18952017-10-26 10:07:04 +0000299 // Initialize and run ClangdLSPServer.
Alex Lorenzf8087862018-08-01 17:39:29 +0000300 ClangdLSPServer LSPServer(
301 Out, CCOpts, CompileCommandsDirPath,
302 /*ShouldUseInMemoryCDB=*/CompileArgsFrom == LSPCompileArgs, Opts);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000303 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000304 llvm::set_thread_name("clangd.main");
Sam McCall7363a2f2018-03-05 17:28:54 +0000305 // Change stdin to binary to not lose \r\n on windows.
306 llvm::sys::ChangeStdinToBinary();
Sam McCall27a07cf2018-06-05 09:34:46 +0000307 return LSPServer.run(stdin, InputStyle) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000308}