| 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 | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 11 | #include "Path.h" |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 12 | #include "Trace.h" |
| Sam McCall | 2c30fbc | 2018-10-18 12:32:04 +0000 | [diff] [blame] | 13 | #include "Transport.h" |
| Sam McCall | 02d600d | 2018-09-25 18:06:43 +0000 | [diff] [blame] | 14 | #include "index/Serialization.h" |
| Raoul Wols | 8f5e06f | 2018-07-29 19:12:42 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Version.h" |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Program.h" |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Signals.h" |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| Sam McCall | ed2717a | 2018-02-14 03:20:07 +0000 | [diff] [blame] | 22 | #include <cstdlib> |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 23 | #include <iostream> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 24 | #include <memory> |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 25 | #include <string> |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 26 | #include <thread> |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 27 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | using namespace clang; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 30 | using namespace clang::clangd; |
| 31 | |
| Sam McCall | 96f2489 | 2018-10-16 08:53:52 +0000 | [diff] [blame] | 32 | // FIXME: remove this option when Dex is cheap enough. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 33 | static cl::opt<bool> UseDex("use-dex-index", |
| 34 | cl::desc("Use experimental Dex dynamic index."), |
| 35 | cl::init(false), cl::Hidden); |
| Kirill Bobyrev | dc41bef | 2018-08-21 10:40:19 +0000 | [diff] [blame] | 36 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 37 | static cl::opt<Path> CompileCommandsDir( |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 38 | "compile-commands-dir", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 39 | cl::desc("Specify a path to look for compile_commands.json. If path " |
| 40 | "is invalid, clangd will look in the current directory and " |
| 41 | "parent paths of each source file.")); |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 42 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 43 | static cl::opt<unsigned> |
| 44 | WorkerThreadsCount("j", cl::desc("Number of async workers used by clangd"), |
| 45 | cl::init(getDefaultAsyncThreadsCount())); |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 46 | |
| Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 47 | // FIXME: also support "plain" style where signatures are always omitted. |
| Sam McCall | 47feb57 | 2018-09-05 10:39:58 +0000 | [diff] [blame] | 48 | enum CompletionStyleFlag { Detailed, Bundled }; |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 49 | static cl::opt<CompletionStyleFlag> CompletionStyle( |
| 50 | "completion-style", cl::desc("Granularity of code completion suggestions"), |
| 51 | cl::values( |
| Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 52 | clEnumValN(Detailed, "detailed", |
| 53 | "One completion item for each semantically distinct " |
| 54 | "completion, with full type information."), |
| 55 | clEnumValN(Bundled, "bundled", |
| 56 | "Similar completion items (e.g. function overloads) are " |
| 57 | "combined. Type information shown where possible.")), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 58 | cl::init(Detailed)); |
| Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 59 | |
| Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 60 | // FIXME: Flags are the wrong mechanism for user preferences. |
| 61 | // We should probably read a dotfile or similar. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 62 | static cl::opt<bool> IncludeIneligibleResults( |
| Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 63 | "include-ineligible-results", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 64 | cl::desc("Include ineligible completion results (e.g. private members)"), |
| 65 | cl::init(clangd::CodeCompleteOptions().IncludeIneligibleResults), |
| 66 | cl::Hidden); |
| Ilya Biryukov | b33c157 | 2017-09-12 13:57:14 +0000 | [diff] [blame] | 67 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 68 | static cl::opt<JSONStreamStyle> InputStyle( |
| 69 | "input-style", cl::desc("Input JSON stream encoding"), |
| 70 | cl::values( |
| Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 71 | clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"), |
| 72 | clEnumValN(JSONStreamStyle::Delimited, "delimited", |
| 73 | "messages delimited by --- lines, with # comment support")), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 74 | cl::init(JSONStreamStyle::Standard)); |
| Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 75 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 76 | static cl::opt<bool> PrettyPrint("pretty", cl::desc("Pretty-print JSON output"), |
| 77 | cl::init(false)); |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 78 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 79 | static cl::opt<Logger::Level> LogLevel( |
| 80 | "log", cl::desc("Verbosity of log messages written to stderr"), |
| 81 | cl::values(clEnumValN(Logger::Error, "error", "Error messages only"), |
| 82 | clEnumValN(Logger::Info, "info", "High level execution tracing"), |
| 83 | clEnumValN(Logger::Debug, "verbose", "Low level details")), |
| 84 | cl::init(Logger::Info)); |
| Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 85 | |
| Eric Liu | c0ac4bb | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 86 | static cl::opt<bool> |
| 87 | Test("lit-test", |
| 88 | cl::desc("Abbreviation for -input-style=delimited -pretty " |
| 89 | "-run-synchronously -enable-test-scheme. " |
| 90 | "Intended to simplify lit tests."), |
| 91 | cl::init(false), cl::Hidden); |
| 92 | |
| 93 | static cl::opt<bool> EnableTestScheme( |
| 94 | "enable-test-uri-scheme", |
| 95 | cl::desc("Enable 'test:' URI scheme. Only use in lit tests."), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 96 | cl::init(false), cl::Hidden); |
| Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 97 | |
| Sam McCall | 47feb57 | 2018-09-05 10:39:58 +0000 | [diff] [blame] | 98 | enum PCHStorageFlag { Disk, Memory }; |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 99 | static cl::opt<PCHStorageFlag> PCHStorage( |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 100 | "pch-storage", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 101 | cl::desc("Storing PCHs in memory increases memory usages, but may " |
| 102 | "improve performance"), |
| 103 | cl::values(clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"), |
| 104 | clEnumValN(PCHStorageFlag::Memory, "memory", |
| 105 | "store PCHs in memory")), |
| 106 | cl::init(PCHStorageFlag::Disk)); |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 107 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 108 | static cl::opt<int> |
| 109 | LimitResults("limit-results", |
| 110 | cl::desc("Limit the number of results returned by clangd. " |
| 111 | "0 means no limit."), |
| 112 | cl::init(100)); |
| Haojian Wu | 48b4865 | 2018-01-25 09:20:09 +0000 | [diff] [blame] | 113 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 114 | static cl::opt<bool> |
| 115 | RunSynchronously("run-synchronously", |
| 116 | cl::desc("Parse on main thread. If set, -j is ignored"), |
| 117 | cl::init(false), cl::Hidden); |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 118 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 119 | static cl::opt<Path> ResourceDir("resource-dir", |
| 120 | cl::desc("Directory for system clang headers"), |
| 121 | cl::init(""), cl::Hidden); |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 122 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 123 | static cl::opt<Path> InputMirrorFile( |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 124 | "input-mirror-file", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 125 | cl::desc( |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 126 | "Mirror all LSP input to the specified file. Useful for debugging."), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 127 | cl::init(""), cl::Hidden); |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 128 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 129 | static cl::opt<bool> EnableIndex( |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 130 | "index", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 131 | cl::desc( |
| Eric Liu | 76b88d8 | 2018-09-13 12:53:23 +0000 | [diff] [blame] | 132 | "Enable index-based features. By default, clangd maintains an index " |
| 133 | "built from symbols in opened files. Global index support needs to " |
| 134 | "enabled separatedly."), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 135 | cl::init(true), cl::Hidden); |
| Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 136 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 137 | static cl::opt<bool> AllScopesCompletion( |
| Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 138 | "all-scopes-completion", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 139 | cl::desc( |
| Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 140 | "If set to true, code completion will include index symbols that are " |
| 141 | "not defined in the scopes (e.g. " |
| 142 | "namespaces) visible from the code completion point. Such completions " |
| 143 | "can insert scope qualifiers."), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 144 | cl::init(false), cl::Hidden); |
| Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 145 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 146 | static cl::opt<bool> |
| 147 | ShowOrigins("debug-origin", cl::desc("Show origins of completion items"), |
| 148 | cl::init(clangd::CodeCompleteOptions().ShowOrigins), |
| 149 | cl::Hidden); |
| Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 150 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 151 | static cl::opt<bool> HeaderInsertionDecorators( |
| Raoul Wols | 8f5e06f | 2018-07-29 19:12:42 +0000 | [diff] [blame] | 152 | "header-insertion-decorators", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 153 | cl::desc("Prepend a circular dot or space before the completion " |
| 154 | "label, depending on whether " |
| 155 | "an include line will be inserted or not."), |
| 156 | cl::init(true)); |
| Raoul Wols | 8f5e06f | 2018-07-29 19:12:42 +0000 | [diff] [blame] | 157 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 158 | static cl::opt<Path> IndexFile( |
| Haojian Wu | 162510f | 2018-10-08 10:44:54 +0000 | [diff] [blame] | 159 | "index-file", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 160 | cl::desc( |
| Haojian Wu | 162510f | 2018-10-08 10:44:54 +0000 | [diff] [blame] | 161 | "Index file to build the static index. The file must have been created " |
| 162 | "by a compatible clangd-index.\n" |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 163 | "WARNING: This option is experimental only, and will be removed " |
| 164 | "eventually. Don't rely on it."), |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 165 | cl::init(""), cl::Hidden); |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 166 | |
| Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame^] | 167 | static cl::opt<bool> EnableBackgroundIndex( |
| 168 | "background-index", |
| 169 | cl::desc("Index project code in the background and persist index on disk. " |
| 170 | "Experimental"), |
| 171 | cl::init(false), cl::Hidden); |
| 172 | |
| Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 173 | enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs }; |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 174 | static cl::opt<CompileArgsFrom> CompileArgsFrom( |
| 175 | "compile_args_from", cl::desc("The source of compile commands"), |
| 176 | cl::values(clEnumValN(LSPCompileArgs, "lsp", |
| 177 | "All compile commands come from LSP and " |
| 178 | "'compile_commands.json' files are ignored"), |
| 179 | clEnumValN(FilesystemCompileArgs, "filesystem", |
| 180 | "All compile commands come from the " |
| 181 | "'compile_commands.json' files")), |
| 182 | cl::init(FilesystemCompileArgs), cl::Hidden); |
| Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 183 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 184 | static cl::opt<bool> EnableFunctionArgSnippets( |
| Kadir Cetinkaya | e8d8aee | 2018-09-19 10:16:44 +0000 | [diff] [blame] | 185 | "function-arg-placeholders", |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 186 | cl::desc("When disabled, completions contain only parentheses for " |
| 187 | "function calls. When enabled, completions also contain " |
| 188 | "placeholders for method parameters."), |
| 189 | cl::init(clangd::CodeCompleteOptions().EnableFunctionArgSnippets)); |
| Kadir Cetinkaya | e8d8aee | 2018-09-19 10:16:44 +0000 | [diff] [blame] | 190 | |
| Eric Liu | c0ac4bb | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 191 | namespace { |
| 192 | |
| 193 | /// \brief Supports a test URI scheme with relaxed constraints for lit tests. |
| 194 | /// The path in a test URI will be combined with a platform-specific fake |
| 195 | /// directory to form an absolute path. For example, test:///a.cpp is resolved |
| 196 | /// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix. |
| 197 | class TestScheme : public URIScheme { |
| 198 | public: |
| 199 | Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body, |
| 200 | StringRef /*HintPath*/) const override { |
| 201 | using namespace llvm::sys; |
| 202 | // Still require "/" in body to mimic file scheme, as we want lengths of an |
| 203 | // equivalent URI in both schemes to be the same. |
| 204 | if (!Body.startswith("/")) |
| 205 | return make_error<StringError>( |
| 206 | "Expect URI body to be an absolute path starting with '/': " + Body, |
| 207 | inconvertibleErrorCode()); |
| 208 | Body = Body.ltrim('/'); |
| 209 | SmallVector<char, 16> Path(Body.begin(), Body.end()); |
| 210 | path::native(Path); |
| 211 | auto Err = fs::make_absolute(TestScheme::TestDir, Path); |
| 212 | if (Err) |
| 213 | llvm_unreachable("Failed to make absolute path in test scheme."); |
| 214 | return std::string(Path.begin(), Path.end()); |
| 215 | } |
| 216 | |
| 217 | Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override { |
| 218 | StringRef Body = AbsolutePath; |
| 219 | if (!Body.consume_front(TestScheme::TestDir)) { |
| 220 | return make_error<StringError>("Path " + AbsolutePath + |
| 221 | " doesn't start with root " + TestDir, |
| 222 | inconvertibleErrorCode()); |
| 223 | } |
| 224 | |
| 225 | return URI("test", /*Authority=*/"", sys::path::convert_to_slash(Body)); |
| 226 | } |
| 227 | |
| 228 | private: |
| 229 | const static char TestDir[]; |
| 230 | }; |
| 231 | |
| 232 | #ifdef _WIN32 |
| 233 | const char TestScheme::TestDir[] = "C:\\clangd-test"; |
| 234 | #else |
| 235 | const char TestScheme::TestDir[] = "/clangd-test"; |
| 236 | #endif |
| 237 | |
| 238 | } |
| 239 | |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 240 | int main(int argc, char *argv[]) { |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 241 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 242 | cl::SetVersionPrinter([](raw_ostream &OS) { |
| Sam McCall | e72d097 | 2018-06-29 13:24:20 +0000 | [diff] [blame] | 243 | OS << clang::getClangToolFullVersion("clangd") << "\n"; |
| 244 | }); |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 245 | cl::ParseCommandLineOptions( |
| Sam McCall | e72d097 | 2018-06-29 13:24:20 +0000 | [diff] [blame] | 246 | argc, argv, |
| 247 | "clangd is a language server that provides IDE-like features to editors. " |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 248 | "\n\nIt should be used via an editor plugin rather than invoked " |
| 249 | "directly. " |
| Sam McCall | e72d097 | 2018-06-29 13:24:20 +0000 | [diff] [blame] | 250 | "For more information, see:" |
| 251 | "\n\thttps://clang.llvm.org/extra/clangd.html" |
| 252 | "\n\thttps://microsoft.github.io/language-server-protocol/"); |
| Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 253 | if (Test) { |
| 254 | RunSynchronously = true; |
| 255 | InputStyle = JSONStreamStyle::Delimited; |
| 256 | PrettyPrint = true; |
| 257 | } |
| Eric Liu | c0ac4bb | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 258 | if (Test || EnableTestScheme) { |
| 259 | static URISchemeRegistry::Add<TestScheme> X( |
| 260 | "test", "Test scheme for clangd lit tests."); |
| 261 | } |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 262 | |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 263 | if (!RunSynchronously && WorkerThreadsCount == 0) { |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 264 | errs() << "A number of worker threads cannot be 0. Did you mean to " |
| 265 | "specify -run-synchronously?"; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 266 | return 1; |
| 267 | } |
| 268 | |
| Kirill Bobyrev | bcaf380 | 2018-02-25 07:21:16 +0000 | [diff] [blame] | 269 | if (RunSynchronously) { |
| 270 | if (WorkerThreadsCount.getNumOccurrences()) |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 271 | errs() << "Ignoring -j because -run-synchronously is set.\n"; |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 272 | WorkerThreadsCount = 0; |
| Kirill Bobyrev | bcaf380 | 2018-02-25 07:21:16 +0000 | [diff] [blame] | 273 | } |
| Ilya Biryukov | db8b2d7 | 2017-08-14 08:45:47 +0000 | [diff] [blame] | 274 | |
| Benjamin Kramer | 74a1895 | 2017-10-26 10:07:04 +0000 | [diff] [blame] | 275 | // Validate command line arguments. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 276 | Optional<raw_fd_ostream> InputMirrorStream; |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 277 | if (!InputMirrorFile.empty()) { |
| 278 | std::error_code EC; |
| Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 279 | InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 280 | sys::fs::FA_Read | sys::fs::FA_Write); |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 281 | if (EC) { |
| 282 | InputMirrorStream.reset(); |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 283 | errs() << "Error while opening an input mirror file: " << EC.message(); |
| Sam McCall | 0d94618 | 2018-11-02 23:47:55 +0000 | [diff] [blame] | 284 | } else { |
| 285 | InputMirrorStream->SetUnbuffered(); |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 288 | |
| Sam McCall | ed2717a | 2018-02-14 03:20:07 +0000 | [diff] [blame] | 289 | // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a |
| 290 | // trace flag in your editor's config is annoying, launching with |
| 291 | // `CLANGD_TRACE=trace.json vim` is easier. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 292 | Optional<raw_fd_ostream> TraceStream; |
| Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 293 | std::unique_ptr<trace::EventTracer> Tracer; |
| Sam McCall | ed2717a | 2018-02-14 03:20:07 +0000 | [diff] [blame] | 294 | if (auto *TraceFile = getenv("CLANGD_TRACE")) { |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 295 | std::error_code EC; |
| Zachary Turner | 1f67a3c | 2018-06-07 19:58:58 +0000 | [diff] [blame] | 296 | TraceStream.emplace(TraceFile, /*ref*/ EC, |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 297 | sys::fs::FA_Read | sys::fs::FA_Write); |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 298 | if (EC) { |
| Sam McCall | ed2717a | 2018-02-14 03:20:07 +0000 | [diff] [blame] | 299 | TraceStream.reset(); |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 300 | errs() << "Error while opening trace file " << TraceFile << ": " |
| 301 | << EC.message(); |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 302 | } else { |
| Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 303 | Tracer = trace::createJSONTracer(*TraceStream, PrettyPrint); |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 306 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 307 | Optional<trace::Session> TracingSession; |
| Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 308 | if (Tracer) |
| 309 | TracingSession.emplace(*Tracer); |
| 310 | |
| Eric Liu | 4e4e5a4 | 2018-08-28 13:15:50 +0000 | [diff] [blame] | 311 | // Use buffered stream to stderr (we still flush each log message). Unbuffered |
| 312 | // stream can cause significant (non-deterministic) latency for the logger. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 313 | errs().SetBuffered(); |
| 314 | StreamLogger Logger(errs(), LogLevel); |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 315 | clangd::LoggingSession LoggingSession(Logger); |
| Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 316 | |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 317 | // If --compile-commands-dir arg was invoked, check value and override default |
| 318 | // path. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 319 | Optional<Path> CompileCommandsDirPath; |
| Sam McCall | f01ad10 | 2018-10-23 11:54:36 +0000 | [diff] [blame] | 320 | if (!CompileCommandsDir.empty()) { |
| 321 | if (sys::fs::exists(CompileCommandsDir)) { |
| 322 | // We support passing both relative and absolute paths to the |
| 323 | // --compile-commands-dir argument, but we assume the path is absolute in |
| 324 | // the rest of clangd so we make sure the path is absolute before |
| 325 | // continuing. |
| 326 | SmallString<128> Path(CompileCommandsDir); |
| 327 | if (std::error_code EC = sys::fs::make_absolute(Path)) { |
| 328 | errs() << "Error while converting the relative path specified by " |
| 329 | "--compile-commands-dir to an absolute path: " |
| 330 | << EC.message() << ". The argument will be ignored.\n"; |
| 331 | } else { |
| 332 | CompileCommandsDirPath = Path.str(); |
| 333 | } |
| 334 | } else { |
| 335 | errs() << "Path specified by --compile-commands-dir does not exist. The " |
| 336 | "argument will be ignored.\n"; |
| 337 | } |
| Ilya Biryukov | 0c1ca6b | 2017-10-02 15:13:20 +0000 | [diff] [blame] | 338 | } |
| Benjamin Kramer | 6a3d74e | 2017-02-07 12:40:59 +0000 | [diff] [blame] | 339 | |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 340 | ClangdServer::Options Opts; |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 341 | switch (PCHStorage) { |
| 342 | case PCHStorageFlag::Memory: |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 343 | Opts.StorePreamblesInMemory = true; |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 344 | break; |
| 345 | case PCHStorageFlag::Disk: |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 346 | Opts.StorePreamblesInMemory = false; |
| Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
| Krasimir Georgiev | 0dcb48e | 2017-07-19 15:43:35 +0000 | [diff] [blame] | 349 | if (!ResourceDir.empty()) |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 350 | Opts.ResourceDir = ResourceDir; |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 351 | Opts.BuildDynamicSymbolIndex = EnableIndex; |
| Sam McCall | 96f2489 | 2018-10-16 08:53:52 +0000 | [diff] [blame] | 352 | Opts.HeavyweightDynamicSymbolIndex = UseDex; |
| Sam McCall | 422c828 | 2018-11-26 16:00:11 +0000 | [diff] [blame^] | 353 | Opts.BackgroundIndex = EnableBackgroundIndex; |
| Haojian Wu | ba28e9a | 2018-01-10 14:44:34 +0000 | [diff] [blame] | 354 | std::unique_ptr<SymbolIndex> StaticIdx; |
| Sam McCall | f469c64 | 2018-09-10 10:00:47 +0000 | [diff] [blame] | 355 | std::future<void> AsyncIndexLoad; // Block exit while loading the index. |
| Haojian Wu | 162510f | 2018-10-08 10:44:54 +0000 | [diff] [blame] | 356 | if (EnableIndex && !IndexFile.empty()) { |
| Sam McCall | 76c4c3a | 2018-09-04 16:19:40 +0000 | [diff] [blame] | 357 | // Load the index asynchronously. Meanwhile SwapIndex returns no results. |
| 358 | SwapIndex *Placeholder; |
| 359 | StaticIdx.reset(Placeholder = new SwapIndex(llvm::make_unique<MemIndex>())); |
| Eric Liu | c0ac4bb | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 360 | AsyncIndexLoad = runAsync<void>([Placeholder] { |
| 361 | if (auto Idx = loadIndex(IndexFile, /*UseDex=*/true)) |
| Sam McCall | 76c4c3a | 2018-09-04 16:19:40 +0000 | [diff] [blame] | 362 | Placeholder->reset(std::move(Idx)); |
| 363 | }); |
| Sam McCall | f469c64 | 2018-09-10 10:00:47 +0000 | [diff] [blame] | 364 | if (RunSynchronously) |
| 365 | AsyncIndexLoad.wait(); |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 366 | } |
| Sam McCall | 76c4c3a | 2018-09-04 16:19:40 +0000 | [diff] [blame] | 367 | Opts.StaticIndex = StaticIdx.get(); |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 368 | Opts.AsyncThreadsCount = WorkerThreadsCount; |
| 369 | |
| Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 370 | clangd::CodeCompleteOptions CCOpts; |
| Sam McCall | adccab6 | 2017-11-23 16:58:22 +0000 | [diff] [blame] | 371 | CCOpts.IncludeIneligibleResults = IncludeIneligibleResults; |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 372 | CCOpts.Limit = LimitResults; |
| Sam McCall | c18c280 | 2018-06-15 11:06:29 +0000 | [diff] [blame] | 373 | CCOpts.BundleOverloads = CompletionStyle != Detailed; |
| Sam McCall | 2161ec7 | 2018-07-05 06:20:41 +0000 | [diff] [blame] | 374 | CCOpts.ShowOrigins = ShowOrigins; |
| Raoul Wols | 8f5e06f | 2018-07-29 19:12:42 +0000 | [diff] [blame] | 375 | if (!HeaderInsertionDecorators) { |
| 376 | CCOpts.IncludeIndicator.Insert.clear(); |
| 377 | CCOpts.IncludeIndicator.NoInsert.clear(); |
| 378 | } |
| Eric Liu | 25d74e9 | 2018-08-24 11:23:56 +0000 | [diff] [blame] | 379 | CCOpts.SpeculativeIndexRequest = Opts.StaticIndex; |
| Kadir Cetinkaya | e8d8aee | 2018-09-19 10:16:44 +0000 | [diff] [blame] | 380 | CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets; |
| Eric Liu | 670c147 | 2018-09-27 18:46:00 +0000 | [diff] [blame] | 381 | CCOpts.AllScopes = AllScopesCompletion; |
| Sam McCall | 7363a2f | 2018-03-05 17:28:54 +0000 | [diff] [blame] | 382 | |
| Benjamin Kramer | 74a1895 | 2017-10-26 10:07:04 +0000 | [diff] [blame] | 383 | // Initialize and run ClangdLSPServer. |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 384 | // Change stdin to binary to not lose \r\n on windows. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 385 | sys::ChangeStdinToBinary(); |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 386 | auto Transport = newJSONTransport( |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 387 | stdin, outs(), |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 388 | InputMirrorStream ? InputMirrorStream.getPointer() : nullptr, PrettyPrint, |
| 389 | InputStyle); |
| Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 390 | ClangdLSPServer LSPServer( |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 391 | *Transport, CCOpts, CompileCommandsDirPath, |
| Sam McCall | c55d09a | 2018-11-02 13:09:36 +0000 | [diff] [blame] | 392 | /*UseDirBasedCDB=*/CompileArgsFrom == FilesystemCompileArgs, Opts); |
| Ilya Biryukov | 0d9b8a3 | 2017-10-25 08:45:41 +0000 | [diff] [blame] | 393 | constexpr int NoShutdownRequestErrorCode = 1; |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 394 | set_thread_name("clangd.main"); |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 395 | return LSPServer.run() ? 0 : NoShutdownRequestErrorCode; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 396 | } |