| 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" |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 12 | #include "llvm/Support/CommandLine.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Path.h" |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Program.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 16 | #include <iostream> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 17 | #include <memory> |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 18 | #include <string> |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 19 | #include <thread> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 22 | using namespace clang::clangd; |
| 23 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 24 | static 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 Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 30 | static 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 Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 35 | static 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 Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 41 | static 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 Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 45 | |
| Bruno Cardoso Lopes | c084589 | 2017-10-09 20:22:05 +0000 | [diff] [blame] | 46 | static llvm::cl::opt<std::string> |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 47 | ResourceDir("resource-dir", |
| Ilya Biryukov | 4ca7d85 | 2017-08-02 08:53:48 +0000 | [diff] [blame] | 48 | llvm::cl::desc("Directory for system clang headers"), |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 49 | llvm::cl::init(""), llvm::cl::Hidden); |
| 50 | |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 51 | int main(int argc, char *argv[]) { |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 52 | llvm::cl::ParseCommandLineOptions(argc, argv, "clangd"); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 53 | |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 54 | 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 Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 65 | /// Validate command line arguments. |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 66 | llvm::raw_ostream &Outs = llvm::outs(); |
| 67 | llvm::raw_ostream &Logs = llvm::errs(); |
| Bruno Cardoso Lopes | c084589 | 2017-10-09 20:22:05 +0000 | [diff] [blame] | 68 | JSONOutput Out(Outs, Logs); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 69 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 70 | // 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 Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 86 | |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 87 | llvm::Optional<StringRef> ResourceDirRef = None; |
| 88 | if (!ResourceDir.empty()) |
| 89 | ResourceDirRef = ResourceDir; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 90 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 91 | /// Change stdin to binary to not lose \r\n on windows. |
| 92 | llvm::sys::ChangeStdinToBinary(); |
| 93 | |
| 94 | /// Initialize and run ClangdLSPServer. |
| Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 95 | ClangdLSPServer LSPServer(Out, WorkerThreadsCount, EnableSnippets, |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 96 | ResourceDirRef, CompileCommandsDirPath); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 97 | LSPServer.run(std::cin); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 98 | } |