blob: c4cc7a6a99fed4032ec8fdb26c4b6491247bfd54 [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.
36std::unique_ptr<SymbolIndex> BuildStaticIndex(llvm::StringRef YamlSymbolFile) {
37 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 McCalladccab62017-11-23 16:58:22 +000062// FIXME: Flags are the wrong mechanism for user preferences.
63// We should probably read a dotfile or similar.
64static llvm::cl::opt<bool> IncludeIneligibleResults(
65 "include-ineligible-results",
66 llvm::cl::desc(
67 "Include ineligible completion results (e.g. private members)"),
68 llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
69 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000070
Sam McCall5ed599e2018-02-06 10:47:30 +000071static llvm::cl::opt<JSONStreamStyle> InputStyle(
72 "input-style", llvm::cl::desc("Input JSON stream encoding"),
73 llvm::cl::values(
74 clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
75 clEnumValN(JSONStreamStyle::Delimited, "delimited",
76 "messages delimited by --- lines, with # comment support")),
77 llvm::cl::init(JSONStreamStyle::Standard));
78
Sam McCalldd0566b2017-11-06 15:40:30 +000079static llvm::cl::opt<bool>
80 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
81 llvm::cl::init(false));
82
Sam McCall5ed599e2018-02-06 10:47:30 +000083static llvm::cl::opt<bool> Test(
84 "lit-test",
85 llvm::cl::desc(
86 "Abbreviation for -input-style=delimited -pretty -run-synchronously. "
87 "Intended to simplify lit tests."),
88 llvm::cl::init(false), llvm::cl::Hidden);
89
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000090static llvm::cl::opt<PCHStorageFlag> PCHStorage(
91 "pch-storage",
92 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
93 "improve performance"),
94 llvm::cl::values(
95 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
96 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
97 llvm::cl::init(PCHStorageFlag::Disk));
98
Haojian Wu48b48652018-01-25 09:20:09 +000099static llvm::cl::opt<int> LimitCompletionResult(
Sam McCallea283c72018-01-30 09:21:30 +0000100 "completion-limit",
Haojian Wu48b48652018-01-25 09:20:09 +0000101 llvm::cl::desc("Limit the number of completion results returned by clangd. "
102 "0 means no limit."),
Sam McCallea283c72018-01-30 09:21:30 +0000103 llvm::cl::init(100));
Haojian Wu48b48652018-01-25 09:20:09 +0000104
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000105static llvm::cl::opt<bool> RunSynchronously(
106 "run-synchronously",
107 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
108 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000109
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000110static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000111 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +0000112 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000113 llvm::cl::init(""), llvm::cl::Hidden);
114
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000115static llvm::cl::opt<Path> InputMirrorFile(
116 "input-mirror-file",
117 llvm::cl::desc(
118 "Mirror all LSP input to the specified file. Useful for debugging."),
119 llvm::cl::init(""), llvm::cl::Hidden);
120
Eric Liubfac8f72017-12-19 18:00:37 +0000121static llvm::cl::opt<bool> EnableIndexBasedCompletion(
122 "enable-index-based-completion",
123 llvm::cl::desc(
Sam McCallea283c72018-01-30 09:21:30 +0000124 "Enable index-based global code completion. "
125 "Clang uses an index built from symbols in opened files"),
126 llvm::cl::init(true));
Eric Liubfac8f72017-12-19 18:00:37 +0000127
Haojian Wuba28e9a2018-01-10 14:44:34 +0000128static llvm::cl::opt<Path> YamlSymbolFile(
129 "yaml-symbol-file",
130 llvm::cl::desc(
131 "YAML-format global symbol file to build the static index. Clangd will "
132 "use the static index for global code completion.\n"
133 "WARNING: This option is experimental only, and will be removed "
134 "eventually. Don't rely on it."),
135 llvm::cl::init(""), llvm::cl::Hidden);
136
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000137int main(int argc, char *argv[]) {
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000138 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Sam McCall5ed599e2018-02-06 10:47:30 +0000139 if (Test) {
140 RunSynchronously = true;
141 InputStyle = JSONStreamStyle::Delimited;
142 PrettyPrint = true;
143 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000144
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000145 if (!RunSynchronously && WorkerThreadsCount == 0) {
146 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
147 "specify -run-synchronously?";
148 return 1;
149 }
150
151 // Ignore -j option if -run-synchonously is used.
152 // FIXME: a warning should be shown here.
153 if (RunSynchronously)
154 WorkerThreadsCount = 0;
155
Benjamin Kramer74a18952017-10-26 10:07:04 +0000156 // Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000157 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
158 if (!InputMirrorFile.empty()) {
159 std::error_code EC;
160 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
161 if (EC) {
162 InputMirrorStream.reset();
163 llvm::errs() << "Error while opening an input mirror file: "
164 << EC.message();
165 }
166 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000167
Sam McCalled2717a2018-02-14 03:20:07 +0000168 // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
169 // trace flag in your editor's config is annoying, launching with
170 // `CLANGD_TRACE=trace.json vim` is easier.
Sam McCall8567cb32017-11-02 09:21:51 +0000171 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000172 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCalled2717a2018-02-14 03:20:07 +0000173 if (auto *TraceFile = getenv("CLANGD_TRACE")) {
Sam McCall8567cb32017-11-02 09:21:51 +0000174 std::error_code EC;
175 TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
176 if (EC) {
Sam McCalled2717a2018-02-14 03:20:07 +0000177 TraceStream.reset();
178 llvm::errs() << "Error while opening trace file " << TraceFile << ": "
179 << EC.message();
Sam McCall8567cb32017-11-02 09:21:51 +0000180 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000181 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000182 }
183 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000184
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000185 llvm::Optional<trace::Session> TracingSession;
186 if (Tracer)
187 TracingSession.emplace(*Tracer);
188
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000189 llvm::raw_ostream &Outs = llvm::outs();
190 llvm::raw_ostream &Logs = llvm::errs();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000191 JSONOutput Out(Outs, Logs,
Sam McCalldd0566b2017-11-06 15:40:30 +0000192 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
193 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000194
Ilya Biryukov940901e2017-12-13 12:51:22 +0000195 clangd::LoggingSession LoggingSession(Out);
196
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000197 // If --compile-commands-dir arg was invoked, check value and override default
198 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000199 llvm::Optional<Path> CompileCommandsDirPath;
200
201 if (CompileCommandsDir.empty()) {
202 CompileCommandsDirPath = llvm::None;
203 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
204 !llvm::sys::fs::exists(CompileCommandsDir)) {
205 llvm::errs() << "Path specified by --compile-commands-dir either does not "
206 "exist or is not an absolute "
207 "path. The argument will be ignored.\n";
208 CompileCommandsDirPath = llvm::None;
209 } else {
210 CompileCommandsDirPath = CompileCommandsDir;
211 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000212
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000213 bool StorePreamblesInMemory;
214 switch (PCHStorage) {
215 case PCHStorageFlag::Memory:
216 StorePreamblesInMemory = true;
217 break;
218 case PCHStorageFlag::Disk:
219 StorePreamblesInMemory = false;
220 break;
221 }
222
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000223 llvm::Optional<StringRef> ResourceDirRef = None;
224 if (!ResourceDir.empty())
225 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000226
Benjamin Kramer74a18952017-10-26 10:07:04 +0000227 // Change stdin to binary to not lose \r\n on windows.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000228 llvm::sys::ChangeStdinToBinary();
229
Haojian Wuba28e9a2018-01-10 14:44:34 +0000230 std::unique_ptr<SymbolIndex> StaticIdx;
231 if (EnableIndexBasedCompletion && !YamlSymbolFile.empty())
232 StaticIdx = BuildStaticIndex(YamlSymbolFile);
Sam McCalladccab62017-11-23 16:58:22 +0000233 clangd::CodeCompleteOptions CCOpts;
Sam McCalladccab62017-11-23 16:58:22 +0000234 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Haojian Wu48b48652018-01-25 09:20:09 +0000235 CCOpts.Limit = LimitCompletionResult;
Benjamin Kramer74a18952017-10-26 10:07:04 +0000236 // Initialize and run ClangdLSPServer.
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000237 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory,
Eric Liubfac8f72017-12-19 18:00:37 +0000238 CCOpts, ResourceDirRef, CompileCommandsDirPath,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000239 EnableIndexBasedCompletion, StaticIdx.get());
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000240 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000241 llvm::set_thread_name("clangd.main");
Sam McCall5ed599e2018-02-06 10:47:30 +0000242 return LSPServer.run(std::cin, InputStyle) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000243}