blob: 4c91fe86ae5c551473f8a3ca27cbf7d19171468f [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"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000014#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000015#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000016#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000017#include "llvm/Support/Program.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000018#include "llvm/Support/raw_ostream.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000019#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000020#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000021#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000022#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000023
24using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000025using namespace clang::clangd;
26
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000027namespace {
28enum class PCHStorageFlag { Disk, Memory };
29}
30
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000031static llvm::cl::opt<Path> CompileCommandsDir(
32 "compile-commands-dir",
33 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
34 "is invalid, clangd will look in the current directory and "
35 "parent paths of each source file."));
36
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000037static llvm::cl::opt<unsigned>
38 WorkerThreadsCount("j",
39 llvm::cl::desc("Number of async workers used by clangd"),
40 llvm::cl::init(getDefaultAsyncThreadsCount()));
41
Ilya Biryukovb33c1572017-09-12 13:57:14 +000042static llvm::cl::opt<bool> EnableSnippets(
43 "enable-snippets",
44 llvm::cl::desc(
Sam McCalladccab62017-11-23 16:58:22 +000045 "Present snippet completions instead of plaintext completions. "
46 "This also enables code pattern results." /* FIXME: should it? */),
47 llvm::cl::init(clangd::CodeCompleteOptions().EnableSnippets));
48
49// FIXME: Flags are the wrong mechanism for user preferences.
50// We should probably read a dotfile or similar.
51static llvm::cl::opt<bool> IncludeIneligibleResults(
52 "include-ineligible-results",
53 llvm::cl::desc(
54 "Include ineligible completion results (e.g. private members)"),
55 llvm::cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults),
56 llvm::cl::Hidden);
Ilya Biryukovb33c1572017-09-12 13:57:14 +000057
Sam McCalldd0566b2017-11-06 15:40:30 +000058static llvm::cl::opt<bool>
59 PrettyPrint("pretty", llvm::cl::desc("Pretty-print JSON output"),
60 llvm::cl::init(false));
61
Ilya Biryukove9eb7f02017-11-16 16:25:18 +000062static llvm::cl::opt<PCHStorageFlag> PCHStorage(
63 "pch-storage",
64 llvm::cl::desc("Storing PCHs in memory increases memory usages, but may "
65 "improve performance"),
66 llvm::cl::values(
67 clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
68 clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
69 llvm::cl::init(PCHStorageFlag::Disk));
70
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000071static llvm::cl::opt<bool> RunSynchronously(
72 "run-synchronously",
73 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
74 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000075
Ilya Biryukove6dbb582017-10-10 09:08:47 +000076static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000077 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +000078 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000079 llvm::cl::init(""), llvm::cl::Hidden);
80
Ilya Biryukove6dbb582017-10-10 09:08:47 +000081static llvm::cl::opt<Path> InputMirrorFile(
82 "input-mirror-file",
83 llvm::cl::desc(
84 "Mirror all LSP input to the specified file. Useful for debugging."),
85 llvm::cl::init(""), llvm::cl::Hidden);
86
Sam McCall8567cb32017-11-02 09:21:51 +000087static llvm::cl::opt<Path> TraceFile(
88 "trace",
89 llvm::cl::desc(
90 "Trace internal events and timestamps in chrome://tracing JSON format"),
91 llvm::cl::init(""), llvm::cl::Hidden);
92
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000093int main(int argc, char *argv[]) {
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000094 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Ilya Biryukovafb55542017-05-16 14:40:30 +000095
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000096 if (!RunSynchronously && WorkerThreadsCount == 0) {
97 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
98 "specify -run-synchronously?";
99 return 1;
100 }
101
102 // Ignore -j option if -run-synchonously is used.
103 // FIXME: a warning should be shown here.
104 if (RunSynchronously)
105 WorkerThreadsCount = 0;
106
Benjamin Kramer74a18952017-10-26 10:07:04 +0000107 // Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000108 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
109 if (!InputMirrorFile.empty()) {
110 std::error_code EC;
111 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
112 if (EC) {
113 InputMirrorStream.reset();
114 llvm::errs() << "Error while opening an input mirror file: "
115 << EC.message();
116 }
117 }
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000118
119 // Setup tracing facilities.
Sam McCall8567cb32017-11-02 09:21:51 +0000120 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000121 std::unique_ptr<trace::EventTracer> Tracer;
Sam McCall8567cb32017-11-02 09:21:51 +0000122 if (!TraceFile.empty()) {
123 std::error_code EC;
124 TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
125 if (EC) {
126 TraceFile.reset();
127 llvm::errs() << "Error while opening trace file: " << EC.message();
128 } else {
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000129 Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000130 }
131 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000132
Ilya Biryukovee27d2e2017-12-14 15:04:59 +0000133 llvm::Optional<trace::Session> TracingSession;
134 if (Tracer)
135 TracingSession.emplace(*Tracer);
136
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000137 llvm::raw_ostream &Outs = llvm::outs();
138 llvm::raw_ostream &Logs = llvm::errs();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000139 JSONOutput Out(Outs, Logs,
Sam McCalldd0566b2017-11-06 15:40:30 +0000140 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
141 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000142
Ilya Biryukov940901e2017-12-13 12:51:22 +0000143 clangd::LoggingSession LoggingSession(Out);
144
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000145 // If --compile-commands-dir arg was invoked, check value and override default
146 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000147 llvm::Optional<Path> CompileCommandsDirPath;
148
149 if (CompileCommandsDir.empty()) {
150 CompileCommandsDirPath = llvm::None;
151 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
152 !llvm::sys::fs::exists(CompileCommandsDir)) {
153 llvm::errs() << "Path specified by --compile-commands-dir either does not "
154 "exist or is not an absolute "
155 "path. The argument will be ignored.\n";
156 CompileCommandsDirPath = llvm::None;
157 } else {
158 CompileCommandsDirPath = CompileCommandsDir;
159 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000160
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000161 bool StorePreamblesInMemory;
162 switch (PCHStorage) {
163 case PCHStorageFlag::Memory:
164 StorePreamblesInMemory = true;
165 break;
166 case PCHStorageFlag::Disk:
167 StorePreamblesInMemory = false;
168 break;
169 }
170
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000171 llvm::Optional<StringRef> ResourceDirRef = None;
172 if (!ResourceDir.empty())
173 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000174
Benjamin Kramer74a18952017-10-26 10:07:04 +0000175 // Change stdin to binary to not lose \r\n on windows.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000176 llvm::sys::ChangeStdinToBinary();
177
Sam McCalladccab62017-11-23 16:58:22 +0000178 clangd::CodeCompleteOptions CCOpts;
179 CCOpts.EnableSnippets = EnableSnippets;
180 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Benjamin Kramer74a18952017-10-26 10:07:04 +0000181 // Initialize and run ClangdLSPServer.
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000182 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory,
Ilya Biryukov9b5ffc22017-12-12 12:56:46 +0000183 CCOpts, ResourceDirRef, CompileCommandsDirPath);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000184 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000185 llvm::set_thread_name("clangd.main");
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000186 return LSPServer.run(std::cin) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000187}