blob: 7dde22774ea03c5402b643ec00898c3a717402b8 [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 }
Sam McCall8567cb32017-11-02 09:21:51 +0000118 llvm::Optional<llvm::raw_fd_ostream> TraceStream;
119 std::unique_ptr<trace::Session> TraceSession;
120 if (!TraceFile.empty()) {
121 std::error_code EC;
122 TraceStream.emplace(TraceFile, /*ref*/ EC, llvm::sys::fs::F_RW);
123 if (EC) {
124 TraceFile.reset();
125 llvm::errs() << "Error while opening trace file: " << EC.message();
126 } else {
Sam McCall9cfd9c92017-11-23 17:12:04 +0000127 TraceSession = trace::Session::create(*TraceStream, PrettyPrint);
Sam McCall8567cb32017-11-02 09:21:51 +0000128 }
129 }
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000130
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000131 llvm::raw_ostream &Outs = llvm::outs();
132 llvm::raw_ostream &Logs = llvm::errs();
Ilya Biryukove6dbb582017-10-10 09:08:47 +0000133 JSONOutput Out(Outs, Logs,
Sam McCalldd0566b2017-11-06 15:40:30 +0000134 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
135 PrettyPrint);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000136
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000137 // If --compile-commands-dir arg was invoked, check value and override default
138 // path.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000139 llvm::Optional<Path> CompileCommandsDirPath;
140
141 if (CompileCommandsDir.empty()) {
142 CompileCommandsDirPath = llvm::None;
143 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
144 !llvm::sys::fs::exists(CompileCommandsDir)) {
145 llvm::errs() << "Path specified by --compile-commands-dir either does not "
146 "exist or is not an absolute "
147 "path. The argument will be ignored.\n";
148 CompileCommandsDirPath = llvm::None;
149 } else {
150 CompileCommandsDirPath = CompileCommandsDir;
151 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000152
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000153 bool StorePreamblesInMemory;
154 switch (PCHStorage) {
155 case PCHStorageFlag::Memory:
156 StorePreamblesInMemory = true;
157 break;
158 case PCHStorageFlag::Disk:
159 StorePreamblesInMemory = false;
160 break;
161 }
162
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000163 llvm::Optional<StringRef> ResourceDirRef = None;
164 if (!ResourceDir.empty())
165 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000166
Benjamin Kramer74a18952017-10-26 10:07:04 +0000167 // Change stdin to binary to not lose \r\n on windows.
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000168 llvm::sys::ChangeStdinToBinary();
169
Sam McCalladccab62017-11-23 16:58:22 +0000170 clangd::CodeCompleteOptions CCOpts;
171 CCOpts.EnableSnippets = EnableSnippets;
172 CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Benjamin Kramer74a18952017-10-26 10:07:04 +0000173 // Initialize and run ClangdLSPServer.
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000174 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, StorePreamblesInMemory,
Sam McCalladccab62017-11-23 16:58:22 +0000175 CCOpts, ResourceDirRef,
Ilya Biryukove9eb7f02017-11-16 16:25:18 +0000176 CompileCommandsDirPath);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000177 constexpr int NoShutdownRequestErrorCode = 1;
Sam McCall8567cb32017-11-02 09:21:51 +0000178 llvm::set_thread_name("clangd.main");
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +0000179 return LSPServer.run(std::cin) ? 0 : NoShutdownRequestErrorCode;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000180}