blob: 0634c94528eab14c8a7f066dc9b43950ae4bdea3 [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"
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000012#include "llvm/Support/CommandLine.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000013#include "llvm/Support/FileSystem.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000014#include "llvm/Support/Path.h"
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000015#include "llvm/Support/Program.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000016#include <iostream>
Ilya Biryukov38d79772017-05-16 09:38:59 +000017#include <memory>
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000018#include <string>
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000019#include <thread>
Ilya Biryukov38d79772017-05-16 09:38:59 +000020
21using namespace clang;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000022using namespace clang::clangd;
23
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000024static llvm::cl::opt<Path> CompileCommandsDir(
25 "compile-commands-dir",
26 llvm::cl::desc("Specify a path to look for compile_commands.json. If path "
27 "is invalid, clangd will look in the current directory and "
28 "parent paths of each source file."));
29
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000030static llvm::cl::opt<unsigned>
31 WorkerThreadsCount("j",
32 llvm::cl::desc("Number of async workers used by clangd"),
33 llvm::cl::init(getDefaultAsyncThreadsCount()));
34
Ilya Biryukovb33c1572017-09-12 13:57:14 +000035static llvm::cl::opt<bool> EnableSnippets(
36 "enable-snippets",
37 llvm::cl::desc(
38 "Present snippet completions instead of plaintext completions"),
39 llvm::cl::init(false));
40
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000041static llvm::cl::opt<bool> RunSynchronously(
42 "run-synchronously",
43 llvm::cl::desc("Parse on main thread. If set, -j is ignored"),
44 llvm::cl::init(false), llvm::cl::Hidden);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000045
Bruno Cardoso Lopesc0845892017-10-09 20:22:05 +000046static llvm::cl::opt<std::string>
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000047 ResourceDir("resource-dir",
Ilya Biryukov4ca7d852017-08-02 08:53:48 +000048 llvm::cl::desc("Directory for system clang headers"),
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000049 llvm::cl::init(""), llvm::cl::Hidden);
50
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000051int main(int argc, char *argv[]) {
Benjamin Kramerf0af3e62017-03-01 16:16:29 +000052 llvm::cl::ParseCommandLineOptions(argc, argv, "clangd");
Ilya Biryukovafb55542017-05-16 14:40:30 +000053
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000054 if (!RunSynchronously && WorkerThreadsCount == 0) {
55 llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
56 "specify -run-synchronously?";
57 return 1;
58 }
59
60 // Ignore -j option if -run-synchonously is used.
61 // FIXME: a warning should be shown here.
62 if (RunSynchronously)
63 WorkerThreadsCount = 0;
64
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000065 /// Validate command line arguments.
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000066 llvm::raw_ostream &Outs = llvm::outs();
67 llvm::raw_ostream &Logs = llvm::errs();
Bruno Cardoso Lopesc0845892017-10-09 20:22:05 +000068 JSONOutput Out(Outs, Logs);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000069
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000070 // If --compile-commands-dir arg was invoked, check value and override default
71 // path.
72 namespace path = llvm::sys::path;
73 llvm::Optional<Path> CompileCommandsDirPath;
74
75 if (CompileCommandsDir.empty()) {
76 CompileCommandsDirPath = llvm::None;
77 } else if (!llvm::sys::path::is_absolute(CompileCommandsDir) ||
78 !llvm::sys::fs::exists(CompileCommandsDir)) {
79 llvm::errs() << "Path specified by --compile-commands-dir either does not "
80 "exist or is not an absolute "
81 "path. The argument will be ignored.\n";
82 CompileCommandsDirPath = llvm::None;
83 } else {
84 CompileCommandsDirPath = CompileCommandsDir;
85 }
Benjamin Kramer6a3d74e2017-02-07 12:40:59 +000086
Krasimir Georgiev0dcb48e2017-07-19 15:43:35 +000087 llvm::Optional<StringRef> ResourceDirRef = None;
88 if (!ResourceDir.empty())
89 ResourceDirRef = ResourceDir;
Ilya Biryukovdb8b2d72017-08-14 08:45:47 +000090
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000091 /// Change stdin to binary to not lose \r\n on windows.
92 llvm::sys::ChangeStdinToBinary();
93
94 /// Initialize and run ClangdLSPServer.
Ilya Biryukovb33c1572017-09-12 13:57:14 +000095 ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets,
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000096 ResourceDirRef, CompileCommandsDirPath);
Ilya Biryukovafb55542017-05-16 14:40:30 +000097 LSPServer.run(std::cin);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000098}