blob: 2d965198f89ed9835420e10aabb34cde95da0e46 [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"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000019#include "llvm/Support/raw_ostream.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000020#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000021#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000022#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000023#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000024
25using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000026using namespace clang::clangd;
27
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000028namespace {
29enum class PCHStorageFlag { Disk, Memory };
Haojian Wuba28e9a2018-01-10 14:44:34 +000030
31// Build an in-memory static index for global symbols from a YAML-format file.
32// The size of global symbols should be relatively small, so that all symbols
33// can be managed in memory.
34std::unique_ptr<SymbolIndex> BuildStaticIndex(llvm::StringRef YamlSymbolFile) {
35 auto Buffer = llvm::MemoryBuffer::getFile(YamlSymbolFile);
36 if (!Buffer) {
37 llvm::errs() << "Can't open " << YamlSymbolFile << "\n";
38 return nullptr;
39 }
40 auto Slab = SymbolFromYAML(Buffer.get()->getBuffer());
41 SymbolSlab::Builder SymsBuilder;
42 for (auto Sym : Slab)
43 SymsBuilder.insert(Sym);
44
45 return MemIndex::build(std::move(SymsBuilder).build());
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000046}
Haojian Wuba28e9a2018-01-10 14:44:34 +000047} // namespace
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000048
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000049static llvm::cl::opt<Path> CompileCommandsDir(
50 "compile-commands-dir",
51 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
52 "is invalid, clangd will look in the current directory and "
53 "parent paths of each source file."));
54
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000055static llvm::cl::opt<unsigned>
56 WorkerThreadsCount("j",
57 llvm::cl::desc("Number of async workers used by clangd"),
58 llvm::cl::init(getDefaultAsyncThreadsCount()));
59
Ilya Biryukovb33c1572017-09-12 13:57:14 +000060static llvm::cl::opt<bool> EnableSnippets(
61 "enable-snippets",
62 llvm::cl::desc(
Sam McCalladccab62017-11-23 16:58:22 +000063 "Present snippet completions instead of plaintext completions. "
64 "This also enables code pattern results." /* FIXME: should it? */),
65 llvm::cl::init(clangd::CodeCompleteOptions().EnableSnippets));
66
67// FIXME: Flags are the wrong mechanism for user preferences.
68// We should probably read a dotfile or similar.
69static llvm::cl::opt<bool> IncludeIneligibleResults(
70 "include-ineligible-results",
71 llvm::cl::desc(
72 "Include ineligible completion results (e.g. private members)"),
73 llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
74 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000075
Sam McCalldd0566b2017-11-06 15:40:30 +000076static llvm::cl::opt<bool>
77 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
78 llvm::cl::init(false));
79
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000080static llvm::cl::opt<PCHStorageFlag> PCHStorage(
81 "pch-storage",
82 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
83 "improve performance"),
84 llvm::cl::values(
85 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
86 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
87 llvm::cl::init(PCHStorageFlag::Disk));
88
Haojian Wu48b48652018-01-25 09:20:09 +000089static llvm::cl::opt<int> LimitCompletionResult(
90 "limit-completion",
91 llvm::cl::desc("Limit the number of completion results returned by clangd. "
92 "0 means no limit."),
93 llvm::cl::init(0));
94
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000095static llvm::cl::opt<bool> RunSynchronously(
96 "run-synchronously",
97 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
98 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000099
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000100static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000101 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +0000102 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000103 llvm::cl::init(""), llvm::cl::Hidden);
104
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000105static llvm::cl::opt<Path> InputMirrorFile(
106 "input-mirror-file",
107 llvm::cl::desc(
108 "Mirror all LSP input to the specified file. Useful for debugging."),
109 llvm::cl::init(""), llvm::cl::Hidden);
110
Sam McCall8567cb32017-11-02 09:21:51 +0000111static llvm::cl::opt<Path> TraceFile(
112 "trace",
113 llvm::cl::desc(
114 "Trace internal events and timestamps in chrome://tracing JSON format"),
115 llvm::cl::init(""), llvm::cl::Hidden);
116
Eric Liubfac8f72017-12-19 18:00:37 +0000117static llvm::cl::opt<bool> EnableIndexBasedCompletion(
118 "enable-index-based-completion",
119 llvm::cl::desc(
120 "Enable index-based global code completion (experimental). Clangd will "
121 "use index built from symbols in opened files"),
122 llvm::cl::init(false), llvm::cl::Hidden);
123
Haojian Wuba28e9a2018-01-10 14:44:34 +0000124static llvm::cl::opt<Path> YamlSymbolFile(
125 "yaml-symbol-file",
126 llvm::cl::desc(
127 "YAML-format global symbol file to build the static index. Clangd will "
128 "use the static index for global code completion.\n"
129 "WARNING: This option is experimental only, and will be removed "
130 "eventually. Don't rely on it."),
131 llvm::cl::init(""), llvm::cl::Hidden);
132
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000133int main(int argc, char *argv[]) {
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000134 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Ilya Biryukovafb55542017-05-16 14:40:30 +0000135
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000136 if (!RunSynchronously && WorkerThreadsCount == 0) {
137 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
138 "specify -run-synchronously?";
139 return 1;
140 }
141
142 // Ignore -j option if -run-synchonously is used.
143 // FIXME: a warning should be shown here.
144 if (RunSynchronously)
145 WorkerThreadsCount = 0;
146
Benjamin Kramer74a18952017-10-26 10:07:04 +0000147 // Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000148 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
149 if (!InputMirrorFile.empty()) {
150 std::error_code EC;
151 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
152 if (EC) {
153 InputMirrorStream.reset();
154 llvm::errs() << "Error while opening an input mirror file: "
155 << EC.message();
156 }
157 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000158
159 // Setup tracing facilities.
Sam McCall8567cb32017-11-02 09:21:51 +0000160 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000161 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCall8567cb32017-11-02 09:21:51 +0000162 if (!TraceFile.empty()) {
163 std::error_code EC;
164 TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
165 if (EC) {
166 TraceFile.reset();
167 llvm::errs() << "Error while opening trace file: " << EC.message();
168 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000169 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000170 }
171 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000172
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000173 llvm::Optional<trace::Session> TracingSession;
174 if (Tracer)
175 TracingSession.emplace(*Tracer);
176
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000177 llvm::raw_ostream &Outs = llvm::outs();
178 llvm::raw_ostream &Logs = llvm::errs();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000179 JSONOutput Out(Outs, Logs,
Sam McCalldd0566b2017-11-06 15:40:30 +0000180 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
181 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000182
Ilya Biryukov940901e2017-12-13 12:51:22 +0000183 clangd::LoggingSession LoggingSession(Out);
184
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000185 // If --compile-commands-dir arg was invoked, check value and override default
186 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000187 llvm::Optional<Path> CompileCommandsDirPath;
188
189 if (CompileCommandsDir.empty()) {
190 CompileCommandsDirPath = llvm::None;
191 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
192 !llvm::sys::fs::exists(CompileCommandsDir)) {
193 llvm::errs() << "Path specified by --compile-commands-dir either does not "
194 "exist or is not an absolute "
195 "path. The argument will be ignored.\n";
196 CompileCommandsDirPath = llvm::None;
197 } else {
198 CompileCommandsDirPath = CompileCommandsDir;
199 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000200
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000201 bool StorePreamblesInMemory;
202 switch (PCHStorage) {
203 case PCHStorageFlag::Memory:
204 StorePreamblesInMemory = true;
205 break;
206 case PCHStorageFlag::Disk:
207 StorePreamblesInMemory = false;
208 break;
209 }
210
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000211 llvm::Optional<StringRef> ResourceDirRef = None;
212 if (!ResourceDir.empty())
213 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000214
Benjamin Kramer74a18952017-10-26 10:07:04 +0000215 // Change stdin to binary to not lose \r\n on windows.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000216 llvm::sys::ChangeStdinToBinary();
217
Haojian Wuba28e9a2018-01-10 14:44:34 +0000218 std::unique_ptr<SymbolIndex> StaticIdx;
219 if (EnableIndexBasedCompletion && !YamlSymbolFile.empty())
220 StaticIdx = BuildStaticIndex(YamlSymbolFile);
Sam McCalladccab62017-11-23 16:58:22 +0000221 clangd::CodeCompleteOptions CCOpts;
222 CCOpts.EnableSnippets = EnableSnippets;
223 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Haojian Wu48b48652018-01-25 09:20:09 +0000224 CCOpts.Limit = LimitCompletionResult;
Benjamin Kramer74a18952017-10-26 10:07:04 +0000225 // Initialize and run ClangdLSPServer.
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000226 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory,
Eric Liubfac8f72017-12-19 18:00:37 +0000227 CCOpts, ResourceDirRef, CompileCommandsDirPath,
Haojian Wuba28e9a2018-01-10 14:44:34 +0000228 EnableIndexBasedCompletion, StaticIdx.get());
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000229 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000230 llvm::set_thread_name("clangd.main");
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000231 return LSPServer.run(std::cin) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000232}