blob: cd7d44b32c3cea042c33476e0c0247f9e591dd7f [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"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000013#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000014#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000015#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000016#include "llvm/Support/Program.h"
Ilya Biryukove6dbb582017-10-10 09:08:47 +000017#include "llvm/Support/raw_ostream.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000018#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000019#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000020#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000021#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000022
23using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000024using namespace clang::clangd;
25
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000026static llvm::cl::opt<Path> CompileCommandsDir(
27 "compile-commands-dir",
28 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
29 "is invalid, clangd will look in the current directory and "
30 "parent paths of each source file."));
31
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000032static llvm::cl::opt<unsigned>
33 WorkerThreadsCount("j",
34 llvm::cl::desc("Number of async workers used by clangd"),
35 llvm::cl::init(getDefaultAsyncThreadsCount()));
36
Ilya Biryukovb33c1572017-09-12 13:57:14 +000037static llvm::cl::opt<bool> EnableSnippets(
38 "enable-snippets",
39 llvm::cl::desc(
40 "Present snippet completions instead of plaintext completions"),
41 llvm::cl::init(false));
42
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000043static llvm::cl::opt<bool> RunSynchronously(
44 "run-synchronously",
45 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
46 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000047
Ilya Biryukove6dbb582017-10-10 09:08:47 +000048static llvm::cl::opt<Path>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000049 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +000050 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000051 llvm::cl::init(""), llvm::cl::Hidden);
52
Ilya Biryukove6dbb582017-10-10 09:08:47 +000053static llvm::cl::opt<Path> InputMirrorFile(
54 "input-mirror-file",
55 llvm::cl::desc(
56 "Mirror all LSP input to the specified file. Useful for debugging."),
57 llvm::cl::init(""), llvm::cl::Hidden);
58
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000059int main(int argc, char *argv[]) {
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000060 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Ilya Biryukovafb55542017-05-16 14:40:30 +000061
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000062 if (!RunSynchronously && WorkerThreadsCount == 0) {
63 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
64 "specify -run-synchronously?";
65 return 1;
66 }
67
68 // Ignore -j option if -run-synchonously is used.
69 // FIXME: a warning should be shown here.
70 if (RunSynchronously)
71 WorkerThreadsCount = 0;
72
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000073 /// Validate command line arguments.
Ilya Biryukove6dbb582017-10-10 09:08:47 +000074 llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
75 if (!InputMirrorFile.empty()) {
76 std::error_code EC;
77 InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, llvm::sys::fs::F_RW);
78 if (EC) {
79 InputMirrorStream.reset();
80 llvm::errs() << "Error while opening an input mirror file: "
81 << EC.message();
82 }
83 }
84
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000085 llvm::raw_ostream &Outs = llvm::outs();
86 llvm::raw_ostream &Logs = llvm::errs();
Ilya Biryukove6dbb582017-10-10 09:08:47 +000087 JSONOutput Out(Outs, Logs,
88 InputMirrorStream ? InputMirrorStream.getPointer() : nullptr);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000089
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000090 // If --compile-commands-dir arg was invoked, check value and override default
91 // path.
92 namespace path = llvm::sys::path;
93 llvm::Optional<Path> CompileCommandsDirPath;
94
95 if (CompileCommandsDir.empty()) {
96 CompileCommandsDirPath = llvm::None;
97 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
98 !llvm::sys::fs::exists(CompileCommandsDir)) {
99 llvm::errs() << "Path specified by --compile-commands-dir either does not "
100 "exist or is not an absolute "
101 "path. The argument will be ignored.\n";
102 CompileCommandsDirPath = llvm::None;
103 } else {
104 CompileCommandsDirPath = CompileCommandsDir;
105 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +0000106
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +0000107 llvm::Optional<StringRef> ResourceDirRef = None;
108 if (!ResourceDir.empty())
109 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +0000110
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000111 /// Change stdin to binary to not lose \r\n on windows.
112 llvm::sys::ChangeStdinToBinary();
113
114 /// Initialize and run ClangdLSPServer.
Ilya Biryukovb33c1572017-09-12 13:57:14 +0000115 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets,
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000116 ResourceDirRef, CompileCommandsDirPath);
Ilya Biryukovafb55542017-05-16 14:40:30 +0000117 LSPServer.run(std::cin);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000118}