| Krasimir Georgiev | 95ef171 | 2017-04-12 17:13:08 +0000 | [diff] [blame] | 1 | //===--- ClangdMain.cpp - clangd server loop ------------------------------===// |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 2 | // |
| 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 Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 10 | #include "ClangdLSPServer.h" |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 11 | #include "JSONRPCDispatcher.h" |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 12 | #include "Path.h" |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 13 | #include "llvm/Support/CommandLine.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FileSystem.h" |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Program.h" |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 17 | #include "llvm/Support/raw_ostream.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 18 | #include <iostream> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 19 | #include <memory> |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 20 | #include <string> |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 21 | #include <thread> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 24 | using namespace clang::clangd; |
| 25 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 26 | static 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 Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 32 | static 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 Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 37 | static 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 Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 43 | static 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 Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 47 | |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 48 | static llvm::cl::opt<Path> |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 49 | ResourceDir("resource-dir", |
| Ilya Biryukov | 4ca7d85 | 2017-08-02 08:53:48 +0000 | [diff] [blame] | 50 | llvm::cl::desc("Directory for system clang headers"), |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 51 | llvm::cl::init(""), llvm::cl::Hidden); |
| 52 | |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 53 | static 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 Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 59 | int main(int argc, char *argv[]) { |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 60 | llvm::cl::ParseCommandLineOptions(argc, argv, "clangd"); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 61 | |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 62 | 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 Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 73 | /// Validate command line arguments. |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 74 | 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 Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 85 | llvm::raw_ostream &Outs = llvm::outs(); |
| 86 | llvm::raw_ostream &Logs = llvm::errs(); |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame^] | 87 | JSONOutput Out(Outs, Logs, |
| 88 | InputMirrorStream ? InputMirrorStream.getPointer() : nullptr); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 89 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 90 | // 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 Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 106 | |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 107 | llvm::Optional<StringRef> ResourceDirRef = None; |
| 108 | if (!ResourceDir.empty()) |
| 109 | ResourceDirRef = ResourceDir; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 110 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 111 | /// Change stdin to binary to not lose \r\n on windows. |
| 112 | llvm::sys::ChangeStdinToBinary(); |
| 113 | |
| 114 | /// Initialize and run ClangdLSPServer. |
| Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 115 | ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets, |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 116 | ResourceDirRef, CompileCommandsDirPath); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 117 | LSPServer.run(std::cin); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 118 | } |