| 1 | //===--- ClangdMain.cpp - clangd server loop ------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ClangdLSPServer.h" |
| 10 | #include "CodeComplete.h" |
| 11 | #include "Compiler.h" |
| 12 | #include "Config.h" |
| 13 | #include "ConfigProvider.h" |
| 14 | #include "Feature.h" |
| 15 | #include "IncludeCleaner.h" |
| 16 | #include "PathMapping.h" |
| 17 | #include "Protocol.h" |
| 18 | #include "TidyProvider.h" |
| 19 | #include "Transport.h" |
| 20 | #include "index/Background.h" |
| 21 | #include "index/Index.h" |
| 22 | #include "index/MemIndex.h" |
| 23 | #include "index/Merge.h" |
| 24 | #include "index/ProjectAware.h" |
| 25 | #include "index/remote/Client.h" |
| 26 | #include "support/Path.h" |
| 27 | #include "support/Shutdown.h" |
| 28 | #include "support/ThreadCrashReporter.h" |
| 29 | #include "support/ThreadsafeFS.h" |
| 30 | #include "support/Trace.h" |
| 31 | #include "clang/Format/Format.h" |
| 32 | #include "llvm/ADT/SmallString.h" |
| 33 | #include "llvm/ADT/StringRef.h" |
| 34 | #include "llvm/Support/CommandLine.h" |
| 35 | #include "llvm/Support/FileSystem.h" |
| 36 | #include "llvm/Support/Path.h" |
| 37 | #include "llvm/Support/Process.h" |
| 38 | #include "llvm/Support/Program.h" |
| 39 | #include "llvm/Support/Signals.h" |
| 40 | #include "llvm/Support/TargetSelect.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
| 42 | #include <chrono> |
| 43 | #include <cstdlib> |
| 44 | #include <memory> |
| 45 | #include <mutex> |
| 46 | #include <optional> |
| 47 | #include <string> |
| 48 | #include <thread> |
| 49 | #include <utility> |
| 50 | #include <vector> |
| 51 | |
| 52 | #ifndef _WIN32 |
| 53 | #include <unistd.h> |
| 54 | #endif |
| 55 | |
| 56 | #ifdef __GLIBC__ |
| 57 | #include <malloc.h> |
| 58 | #endif |
| 59 | |
| 60 | namespace clang { |
| 61 | namespace clangd { |
| 62 | |
| 63 | // Implemented in Check.cpp. |
| 64 | bool check(const llvm::StringRef File, const ThreadsafeFS &TFS, |
| 65 | const ClangdLSPServer::Options &Opts); |
| 66 | |
| 67 | namespace { |
| 68 | |
| 69 | using llvm::cl::cat; |
| 70 | using llvm::cl::CommaSeparated; |
| 71 | using llvm::cl::desc; |
| 72 | using llvm::cl::Hidden; |
| 73 | using llvm::cl::init; |
| 74 | using llvm::cl::list; |
| 75 | using llvm::cl::opt; |
| 76 | using llvm::cl::OptionCategory; |
| 77 | using llvm::cl::ValueOptional; |
| 78 | using llvm::cl::values; |
| 79 | |
| 80 | // All flags must be placed in a category, or they will be shown neither in |
| 81 | // --help, nor --help-hidden! |
| 82 | OptionCategory CompileCommands("clangd compilation flags options"); |
| 83 | OptionCategory Features("clangd feature options"); |
| 84 | OptionCategory Misc("clangd miscellaneous options"); |
| 85 | OptionCategory Protocol("clangd protocol and logging options"); |
| 86 | OptionCategory Retired("clangd flags no longer in use"); |
| 87 | const OptionCategory *ClangdCategories[] = {&Features, &Protocol, |
| 88 | &CompileCommands, &Misc, &Retired}; |
| 89 | |
| 90 | template <typename T> class RetiredFlag { |
| 91 | opt<T> Option; |
| 92 | |
| 93 | public: |
| 94 | RetiredFlag(llvm::StringRef Name) |
| 95 | : Option(Name, cat(Retired), desc("Obsolete flag, ignored"), Hidden, |
| 96 | llvm::cl::callback([Name](const T &) { |
| 97 | llvm::errs() |
| 98 | << "The flag `-" << Name << "` is obsolete and ignored.\n"; |
| 99 | })) {} |
| 100 | }; |
| 101 | |
| 102 | enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs }; |
| 103 | opt<CompileArgsFrom> CompileArgsFrom{ |
| 104 | "compile_args_from", |
| 105 | cat(CompileCommands), |
| 106 | desc("The source of compile commands"), |
| 107 | values(clEnumValN(LSPCompileArgs, "lsp", |
| 108 | "All compile commands come from LSP and " |
| 109 | "'compile_commands.json' files are ignored"), |
| 110 | clEnumValN(FilesystemCompileArgs, "filesystem", |
| 111 | "All compile commands come from the " |
| 112 | "'compile_commands.json' files")), |
| 113 | init(FilesystemCompileArgs), |
| 114 | Hidden, |
| 115 | }; |
| 116 | |
| 117 | opt<Path> CompileCommandsDir{ |
| 118 | "compile-commands-dir", |
| 119 | cat(CompileCommands), |
| 120 | desc("Specify a path to look for compile_commands.json. If path " |
| 121 | "is invalid, clangd will look in the current directory and " |
| 122 | "parent paths of each source file"), |
| 123 | }; |
| 124 | |
| 125 | opt<Path> ResourceDir{ |
| 126 | "resource-dir", |
| 127 | cat(CompileCommands), |
| 128 | desc("Directory for system clang headers"), |
| 129 | init(""), |
| 130 | Hidden, |
| 131 | }; |
| 132 | |
| 133 | list<std::string> QueryDriverGlobs{ |
| 134 | "query-driver", |
| 135 | cat(CompileCommands), |
| 136 | desc( |
| 137 | "Comma separated list of globs for white-listing gcc-compatible " |
| 138 | "drivers that are safe to execute. Drivers matching any of these globs " |
| 139 | "will be used to extract system includes. e.g. " |
| 140 | "/usr/bin/**/clang-*,/path/to/repo/**/g++-*"), |
| 141 | CommaSeparated, |
| 142 | }; |
| 143 | |
| 144 | // FIXME: Flags are the wrong mechanism for user preferences. |
| 145 | // We should probably read a dotfile or similar. |
| 146 | opt<bool> AllScopesCompletion{ |
| 147 | "all-scopes-completion", |
| 148 | cat(Features), |
| 149 | desc("If set to true, code completion will include index symbols that are " |
| 150 | "not defined in the scopes (e.g. " |
| 151 | "namespaces) visible from the code completion point. Such completions " |
| 152 | "can insert scope qualifiers"), |
| 153 | init(true), |
| 154 | }; |
| 155 | |
| 156 | opt<bool> ShowOrigins{ |
| 157 | "debug-origin", |
| 158 | cat(Features), |
| 159 | desc("Show origins of completion items"), |
| 160 | init(CodeCompleteOptions().ShowOrigins), |
| 161 | Hidden, |
| 162 | }; |
| 163 | |
| 164 | opt<bool> EnableBackgroundIndex{ |
| 165 | "background-index", |
| 166 | cat(Features), |
| 167 | desc("Index project code in the background and persist index on disk."), |
| 168 | init(true), |
| 169 | }; |
| 170 | |
| 171 | opt<llvm::ThreadPriority> BackgroundIndexPriority{ |
| 172 | "background-index-priority", |
| 173 | cat(Features), |
| 174 | desc("Thread priority for building the background index. " |
| 175 | "The effect of this flag is OS-specific."), |
| 176 | values(clEnumValN(llvm::ThreadPriority::Background, "background", |
| 177 | "Minimum priority, runs on idle CPUs. " |
| 178 | "May leave 'performance' cores unused."), |
| 179 | clEnumValN(llvm::ThreadPriority::Low, "low", |
| 180 | "Reduced priority compared to interactive work."), |
| 181 | clEnumValN(llvm::ThreadPriority::Default, "normal", |
| 182 | "Same priority as other clangd work.")), |
| 183 | init(llvm::ThreadPriority::Low), |
| 184 | }; |
| 185 | |
| 186 | opt<bool> EnableClangTidy{ |
| 187 | "clang-tidy", |
| 188 | cat(Features), |
| 189 | desc("Enable clang-tidy diagnostics"), |
| 190 | init(true), |
| 191 | }; |
| 192 | |
| 193 | opt<CodeCompleteOptions::CodeCompletionParse> CodeCompletionParse{ |
| 194 | "completion-parse", |
| 195 | cat(Features), |
| 196 | desc("Whether the clang-parser is used for code-completion"), |
| 197 | values(clEnumValN(CodeCompleteOptions::AlwaysParse, "always", |
| 198 | "Block until the parser can be used"), |
| 199 | clEnumValN(CodeCompleteOptions::ParseIfReady, "auto", |
| 200 | "Use text-based completion if the parser " |
| 201 | "is not ready"), |
| 202 | clEnumValN(CodeCompleteOptions::NeverParse, "never", |
| 203 | "Always used text-based completion")), |
| 204 | init(CodeCompleteOptions().RunParser), |
| 205 | Hidden, |
| 206 | }; |
| 207 | |
| 208 | opt<CodeCompleteOptions::CodeCompletionRankingModel> RankingModel{ |
| 209 | "ranking-model", |
| 210 | cat(Features), |
| 211 | desc("Model to use to rank code-completion items"), |
| 212 | values(clEnumValN(CodeCompleteOptions::Heuristics, "heuristics", |
| 213 | "Use heuristics to rank code completion items"), |
| 214 | clEnumValN(CodeCompleteOptions::DecisionForest, "decision_forest", |
| 215 | "Use Decision Forest model to rank completion items")), |
| 216 | init(CodeCompleteOptions().RankingModel), |
| 217 | Hidden, |
| 218 | }; |
| 219 | |
| 220 | // FIXME: also support "plain" style where signatures are always omitted. |
| 221 | enum CompletionStyleFlag { Detailed, Bundled }; |
| 222 | opt<CompletionStyleFlag> CompletionStyle{ |
| 223 | "completion-style", |
| 224 | cat(Features), |
| 225 | desc("Granularity of code completion suggestions"), |
| 226 | values(clEnumValN(Detailed, "detailed", |
| 227 | "One completion item for each semantically distinct " |
| 228 | "completion, with full type information"), |
| 229 | clEnumValN(Bundled, "bundled", |
| 230 | "Similar completion items (e.g. function overloads) are " |
| 231 | "combined. Type information shown where possible")), |
| 232 | }; |
| 233 | |
| 234 | opt<std::string> FallbackStyle{ |
| 235 | "fallback-style", |
| 236 | cat(Features), |
| 237 | desc("clang-format style to apply by default when " |
| 238 | "no .clang-format file is found"), |
| 239 | init(clang::format::DefaultFallbackStyle), |
| 240 | }; |
| 241 | |
| 242 | opt<bool> EnableFunctionArgSnippets{ |
| 243 | "function-arg-placeholders", |
| 244 | cat(Features), |
| 245 | desc("When disabled, completions contain only parentheses for " |
| 246 | "function calls. When enabled, completions also contain " |
| 247 | "placeholders for method parameters"), |
| 248 | init(CodeCompleteOptions().EnableFunctionArgSnippets), |
| 249 | }; |
| 250 | |
| 251 | opt<CodeCompleteOptions::IncludeInsertion> HeaderInsertion{ |
| 252 | "header-insertion", |
| 253 | cat(Features), |
| 254 | desc("Add #include directives when accepting code completions"), |
| 255 | init(CodeCompleteOptions().InsertIncludes), |
| 256 | values( |
| 257 | clEnumValN(CodeCompleteOptions::IWYU, "iwyu", |
| 258 | "Include what you use. " |
| 259 | "Insert the owning header for top-level symbols, unless the " |
| 260 | "header is already directly included or the symbol is " |
| 261 | "forward-declared"), |
| 262 | clEnumValN( |
| 263 | CodeCompleteOptions::NeverInsert, "never", |
| 264 | "Never insert #include directives as part of code completion")), |
| 265 | }; |
| 266 | |
| 267 | opt<bool> ImportInsertions{ |
| 268 | "import-insertions", |
| 269 | cat(Features), |
| 270 | desc("If header insertion is enabled, add #import directives when " |
| 271 | "accepting code completions or fixing includes in Objective-C code"), |
| 272 | init(CodeCompleteOptions().ImportInsertions), |
| 273 | }; |
| 274 | |
| 275 | opt<bool> IncludeCleanerStdlib{ |
| 276 | "include-cleaner-stdlib", |
| 277 | cat(Features), |
| 278 | desc("Apply include-cleaner analysis to standard library headers " |
| 279 | "(immature!)"), |
| 280 | init(false), |
| 281 | Hidden, |
| 282 | }; |
| 283 | |
| 284 | opt<bool> HeaderInsertionDecorators{ |
| 285 | "header-insertion-decorators", |
| 286 | cat(Features), |
| 287 | desc("Prepend a circular dot or space before the completion " |
| 288 | "label, depending on whether " |
| 289 | "an include line will be inserted or not"), |
| 290 | init(true), |
| 291 | }; |
| 292 | |
| 293 | opt<bool> HiddenFeatures{ |
| 294 | "hidden-features", |
| 295 | cat(Features), |
| 296 | desc("Enable hidden features mostly useful to clangd developers"), |
| 297 | init(false), |
| 298 | Hidden, |
| 299 | }; |
| 300 | |
| 301 | opt<bool> IncludeIneligibleResults{ |
| 302 | "include-ineligible-results", |
| 303 | cat(Features), |
| 304 | desc("Include ineligible completion results (e.g. private members)"), |
| 305 | init(CodeCompleteOptions().IncludeIneligibleResults), |
| 306 | Hidden, |
| 307 | }; |
| 308 | |
| 309 | RetiredFlag<bool> EnableIndex("index"); |
| 310 | RetiredFlag<bool> SuggestMissingIncludes("suggest-missing-includes"); |
| 311 | RetiredFlag<bool> RecoveryAST("recovery-ast"); |
| 312 | RetiredFlag<bool> RecoveryASTType("recovery-ast-type"); |
| 313 | RetiredFlag<bool> AsyncPreamble("async-preamble"); |
| 314 | RetiredFlag<bool> CollectMainFileRefs("collect-main-file-refs"); |
| 315 | RetiredFlag<bool> CrossFileRename("cross-file-rename"); |
| 316 | RetiredFlag<std::string> ClangTidyChecks("clang-tidy-checks"); |
| 317 | RetiredFlag<bool> InlayHints("inlay-hints"); |
| 318 | RetiredFlag<bool> FoldingRanges("folding-ranges"); |
| 319 | |
| 320 | |
| 321 | opt<int> LimitResults{ |
| 322 | "limit-results", |
| 323 | cat(Features), |
| 324 | desc("Limit the number of results returned by clangd. " |
| 325 | "0 means no limit (default=100)"), |
| 326 | init(100), |
| 327 | }; |
| 328 | |
| 329 | opt<int> ReferencesLimit{ |
| 330 | "limit-references", |
| 331 | cat(Features), |
| 332 | desc("Limit the number of references returned by clangd. " |
| 333 | "0 means no limit (default=1000)"), |
| 334 | init(1000), |
| 335 | }; |
| 336 | |
| 337 | opt<int> RenameFileLimit{ |
| 338 | "rename-file-limit", |
| 339 | cat(Features), |
| 340 | desc("Limit the number of files to be affected by symbol renaming. " |
| 341 | "0 means no limit (default=50)"), |
| 342 | init(50), |
| 343 | }; |
| 344 | |
| 345 | list<std::string> TweakList{ |
| 346 | "tweaks", |
| 347 | cat(Features), |
| 348 | desc("Specify a list of Tweaks to enable (only for clangd developers)."), |
| 349 | Hidden, |
| 350 | CommaSeparated, |
| 351 | }; |
| 352 | |
| 353 | opt<unsigned> WorkerThreadsCount{ |
| 354 | "j", |
| 355 | cat(Misc), |
| 356 | desc("Number of async workers used by clangd. Background index also " |
| 357 | "uses this many workers."), |
| 358 | init(getDefaultAsyncThreadsCount()), |
| 359 | }; |
| 360 | |
| 361 | opt<Path> IndexFile{ |
| 362 | "index-file", |
| 363 | cat(Misc), |
| 364 | desc( |
| 365 | "Index file to build the static index. The file must have been created " |
| 366 | "by a compatible clangd-indexer\n" |
| 367 | "WARNING: This option is experimental only, and will be removed " |
| 368 | "eventually. Don't rely on it"), |
| 369 | init(""), |
| 370 | Hidden, |
| 371 | }; |
| 372 | |
| 373 | opt<bool> Test{ |
| 374 | "lit-test", |
| 375 | cat(Misc), |
| 376 | desc("Abbreviation for -input-style=delimited -pretty -sync " |
| 377 | "-enable-test-scheme -enable-config=0 -log=verbose -crash-pragmas. " |
| 378 | "Also sets config options: Index.StandardLibrary=false. " |
| 379 | "Intended to simplify lit tests"), |
| 380 | init(false), |
| 381 | Hidden, |
| 382 | }; |
| 383 | |
| 384 | opt<bool> CrashPragmas{ |
| 385 | "crash-pragmas", |
| 386 | cat(Misc), |
| 387 | desc("Respect `#pragma clang __debug crash` and friends."), |
| 388 | init(false), |
| 389 | Hidden, |
| 390 | }; |
| 391 | |
| 392 | opt<Path> CheckFile{ |
| 393 | "check", |
| 394 | cat(Misc), |
| 395 | desc("Parse one file in isolation instead of acting as a language server. " |
| 396 | "Useful to investigate/reproduce crashes or configuration problems. " |
| 397 | "With --check=<filename>, attempts to parse a particular file."), |
| 398 | init(""), |
| 399 | ValueOptional, |
| 400 | }; |
| 401 | |
| 402 | enum PCHStorageFlag { Disk, Memory }; |
| 403 | opt<PCHStorageFlag> PCHStorage{ |
| 404 | "pch-storage", |
| 405 | cat(Misc), |
| 406 | desc("Storing PCHs in memory increases memory usages, but may " |
| 407 | "improve performance"), |
| 408 | values( |
| 409 | clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"), |
| 410 | clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")), |
| 411 | init(PCHStorageFlag::Disk), |
| 412 | }; |
| 413 | |
| 414 | opt<bool> Sync{ |
| 415 | "sync", |
| 416 | cat(Misc), |
| 417 | desc("Handle client requests on main thread. Background index still uses " |
| 418 | "its own thread."), |
| 419 | init(false), |
| 420 | Hidden, |
| 421 | }; |
| 422 | |
| 423 | opt<JSONStreamStyle> InputStyle{ |
| 424 | "input-style", |
| 425 | cat(Protocol), |
| 426 | desc("Input JSON stream encoding"), |
| 427 | values( |
| 428 | clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"), |
| 429 | clEnumValN(JSONStreamStyle::Delimited, "delimited", |
| 430 | "messages delimited by --- lines, with # comment support")), |
| 431 | init(JSONStreamStyle::Standard), |
| 432 | Hidden, |
| 433 | }; |
| 434 | |
| 435 | opt<bool> EnableTestScheme{ |
| 436 | "enable-test-uri-scheme", |
| 437 | cat(Protocol), |
| 438 | desc("Enable 'test:' URI scheme. Only use in lit tests"), |
| 439 | init(false), |
| 440 | Hidden, |
| 441 | }; |
| 442 | |
| 443 | opt<std::string> PathMappingsArg{ |
| 444 | "path-mappings", |
| 445 | cat(Protocol), |
| 446 | desc( |
| 447 | "Translates between client paths (as seen by a remote editor) and " |
| 448 | "server paths (where clangd sees files on disk). " |
| 449 | "Comma separated list of '<client_path>=<server_path>' pairs, the " |
| 450 | "first entry matching a given path is used. " |
| 451 | "e.g. /home/project/incl=/opt/include,/home/project=/workarea/project"), |
| 452 | init(""), |
| 453 | }; |
| 454 | |
| 455 | opt<Path> InputMirrorFile{ |
| 456 | "input-mirror-file", |
| 457 | cat(Protocol), |
| 458 | desc("Mirror all LSP input to the specified file. Useful for debugging"), |
| 459 | init(""), |
| 460 | Hidden, |
| 461 | }; |
| 462 | |
| 463 | opt<Logger::Level> LogLevel{ |
| 464 | "log", |
| 465 | cat(Protocol), |
| 466 | desc("Verbosity of log messages written to stderr"), |
| 467 | values(clEnumValN(Logger::Error, "error", "Error messages only"), |
| 468 | clEnumValN(Logger::Info, "info", "High level execution tracing"), |
| 469 | clEnumValN(Logger::Debug, "verbose", "Low level details")), |
| 470 | init(Logger::Info), |
| 471 | }; |
| 472 | |
| 473 | opt<OffsetEncoding> ForceOffsetEncoding{ |
| 474 | "offset-encoding", |
| 475 | cat(Protocol), |
| 476 | desc("Force the offsetEncoding used for character positions. " |
| 477 | "This bypasses negotiation via client capabilities"), |
| 478 | values( |
| 479 | clEnumValN(OffsetEncoding::UTF8, "utf-8", "Offsets are in UTF-8 bytes"), |
| 480 | clEnumValN(OffsetEncoding::UTF16, "utf-16", |
| 481 | "Offsets are in UTF-16 code units"), |
| 482 | clEnumValN(OffsetEncoding::UTF32, "utf-32", |
| 483 | "Offsets are in unicode codepoints")), |
| 484 | init(OffsetEncoding::UnsupportedEncoding), |
| 485 | }; |
| 486 | |
| 487 | opt<bool> PrettyPrint{ |
| 488 | "pretty", |
| 489 | cat(Protocol), |
| 490 | desc("Pretty-print JSON output"), |
| 491 | init(false), |
| 492 | }; |
| 493 | |
| 494 | opt<bool> EnableConfig{ |
| 495 | "enable-config", |
| 496 | cat(Misc), |
| 497 | desc( |
| 498 | "Read user and project configuration from YAML files.\n" |
| 499 | "Project config is from a .clangd file in the project directory.\n" |
| 500 | "User config is from clangd/config.yaml in the following directories:\n" |
| 501 | "\tWindows: %USERPROFILE%\\AppData\\Local\n" |
| 502 | "\tMac OS: ~/Library/Preferences/\n" |
| 503 | "\tOthers: $XDG_CONFIG_HOME, usually ~/.config\n" |
| 504 | "Configuration is documented at https://clangd.llvm.org/config.html"), |
| 505 | init(true), |
| 506 | }; |
| 507 | |
| 508 | opt<bool> UseDirtyHeaders{"use-dirty-headers", cat(Misc), |
| 509 | desc("Use files open in the editor when parsing " |
| 510 | "headers instead of reading from the disk"), |
| 511 | Hidden, |
| 512 | init(ClangdServer::Options().UseDirtyHeaders)}; |
| 513 | |
| 514 | opt<bool> PreambleParseForwardingFunctions{ |
| 515 | "parse-forwarding-functions", |
| 516 | cat(Misc), |
| 517 | desc("Parse all emplace-like functions in included headers"), |
| 518 | Hidden, |
| 519 | init(ParseOptions().PreambleParseForwardingFunctions), |
| 520 | }; |
| 521 | |
| 522 | #if defined(__GLIBC__) && CLANGD_MALLOC_TRIM |
| 523 | opt<bool> EnableMallocTrim{ |
| 524 | "malloc-trim", |
| 525 | cat(Misc), |
| 526 | desc("Release memory periodically via malloc_trim(3)."), |
| 527 | init(true), |
| 528 | }; |
| 529 | |
| 530 | std::function<void()> getMemoryCleanupFunction() { |
| 531 | if (!EnableMallocTrim) |
| 532 | return nullptr; |
| 533 | // Leave a few MB at the top of the heap: it is insignificant |
| 534 | // and will most likely be needed by the main thread |
| 535 | constexpr size_t MallocTrimPad = 20'000'000; |
| 536 | return []() { |
| 537 | if (malloc_trim(MallocTrimPad)) |
| 538 | vlog("Released memory via malloc_trim"); |
| 539 | }; |
| 540 | } |
| 541 | #else |
| 542 | std::function<void()> getMemoryCleanupFunction() { return nullptr; } |
| 543 | #endif |
| 544 | |
| 545 | #if CLANGD_ENABLE_REMOTE |
| 546 | opt<std::string> RemoteIndexAddress{ |
| 547 | "remote-index-address", |
| 548 | cat(Features), |
| 549 | desc("Address of the remote index server"), |
| 550 | }; |
| 551 | |
| 552 | // FIXME(kirillbobyrev): Should this be the location of compile_commands.json? |
| 553 | opt<std::string> ProjectRoot{ |
| 554 | "project-root", |
| 555 | cat(Features), |
| 556 | desc("Path to the project root. Requires remote-index-address to be set."), |
| 557 | }; |
| 558 | #endif |
| 559 | |
| 560 | /// Supports a test URI scheme with relaxed constraints for lit tests. |
| 561 | /// The path in a test URI will be combined with a platform-specific fake |
| 562 | /// directory to form an absolute path. For example, test:///a.cpp is resolved |
| 563 | /// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix. |
| 564 | class TestScheme : public URIScheme { |
| 565 | public: |
| 566 | llvm::Expected<std::string> |
| 567 | getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, |
| 568 | llvm::StringRef /*HintPath*/) const override { |
| 569 | using namespace llvm::sys; |
| 570 | // Still require "/" in body to mimic file scheme, as we want lengths of an |
| 571 | // equivalent URI in both schemes to be the same. |
| 572 | if (!Body.startswith("/")) |
| 573 | return error( |
| 574 | "Expect URI body to be an absolute path starting with '/': {0}", |
| 575 | Body); |
| 576 | Body = Body.ltrim('/'); |
| 577 | llvm::SmallString<16> Path(Body); |
| 578 | path::native(Path); |
| 579 | fs::make_absolute(TestScheme::TestDir, Path); |
| 580 | return std::string(Path); |
| 581 | } |
| 582 | |
| 583 | llvm::Expected<URI> |
| 584 | uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { |
| 585 | llvm::StringRef Body = AbsolutePath; |
| 586 | if (!Body.consume_front(TestScheme::TestDir)) |
| 587 | return error("Path {0} doesn't start with root {1}", AbsolutePath, |
| 588 | TestDir); |
| 589 | |
| 590 | return URI("test", /*Authority=*/"", |
| 591 | llvm::sys::path::convert_to_slash(Body)); |
| 592 | } |
| 593 | |
| 594 | private: |
| 595 | const static char TestDir[]; |
| 596 | }; |
| 597 | |
| 598 | #ifdef _WIN32 |
| 599 | const char TestScheme::TestDir[] = "C:\\clangd-test"; |
| 600 | #else |
| 601 | const char TestScheme::TestDir[] = "/clangd-test"; |
| 602 | #endif |
| 603 | |
| 604 | std::unique_ptr<SymbolIndex> |
| 605 | loadExternalIndex(const Config::ExternalIndexSpec &External, |
| 606 | AsyncTaskRunner *Tasks) { |
| 607 | static const trace::Metric RemoteIndexUsed("used_remote_index", |
| 608 | trace::Metric::Value, "address"); |
| 609 | switch (External.Kind) { |
| 610 | case Config::ExternalIndexSpec::None: |
| 611 | break; |
| 612 | case Config::ExternalIndexSpec::Server: |
| 613 | RemoteIndexUsed.record(1, External.Location); |
| 614 | log("Associating {0} with remote index at {1}.", External.MountPoint, |
| 615 | External.Location); |
| 616 | return remote::getClient(External.Location, External.MountPoint); |
| 617 | case Config::ExternalIndexSpec::File: |
| 618 | log("Associating {0} with monolithic index at {1}.", External.MountPoint, |
| 619 | External.Location); |
| 620 | auto NewIndex = std::make_unique<SwapIndex>(std::make_unique<MemIndex>()); |
| 621 | auto IndexLoadTask = [File = External.Location, |
| 622 | PlaceHolder = NewIndex.get()] { |
| 623 | if (auto Idx = loadIndex(File, SymbolOrigin::Static, /*UseDex=*/true)) |
| 624 | PlaceHolder->reset(std::move(Idx)); |
| 625 | }; |
| 626 | if (Tasks) { |
| 627 | Tasks->runAsync("Load-index:" + External.Location, |
| 628 | std::move(IndexLoadTask)); |
| 629 | } else { |
| 630 | IndexLoadTask(); |
| 631 | } |
| 632 | return std::move(NewIndex); |
| 633 | } |
| 634 | llvm_unreachable("Invalid ExternalIndexKind."); |
| 635 | } |
| 636 | |
| 637 | class FlagsConfigProvider : public config::Provider { |
| 638 | private: |
| 639 | config::CompiledFragment Frag; |
| 640 | |
| 641 | std::vector<config::CompiledFragment> |
| 642 | getFragments(const config::Params &, |
| 643 | config::DiagnosticCallback) const override { |
| 644 | return {Frag}; |
| 645 | } |
| 646 | |
| 647 | public: |
| 648 | FlagsConfigProvider() { |
| 649 | std::optional<Config::CDBSearchSpec> CDBSearch; |
| 650 | std::optional<Config::ExternalIndexSpec> IndexSpec; |
| 651 | std::optional<Config::BackgroundPolicy> BGPolicy; |
| 652 | |
| 653 | // If --compile-commands-dir arg was invoked, check value and override |
| 654 | // default path. |
| 655 | if (!CompileCommandsDir.empty()) { |
| 656 | if (llvm::sys::fs::exists(CompileCommandsDir)) { |
| 657 | // We support passing both relative and absolute paths to the |
| 658 | // --compile-commands-dir argument, but we assume the path is absolute |
| 659 | // in the rest of clangd so we make sure the path is absolute before |
| 660 | // continuing. |
| 661 | llvm::SmallString<128> Path(CompileCommandsDir); |
| 662 | if (std::error_code EC = llvm::sys::fs::make_absolute(Path)) { |
| 663 | elog("Error while converting the relative path specified by " |
| 664 | "--compile-commands-dir to an absolute path: {0}. The argument " |
| 665 | "will be ignored.", |
| 666 | EC.message()); |
| 667 | } else { |
| 668 | CDBSearch = {Config::CDBSearchSpec::FixedDir, Path.str().str()}; |
| 669 | } |
| 670 | } else { |
| 671 | elog("Path specified by --compile-commands-dir does not exist. The " |
| 672 | "argument will be ignored."); |
| 673 | } |
| 674 | } |
| 675 | if (!IndexFile.empty()) { |
| 676 | Config::ExternalIndexSpec Spec; |
| 677 | Spec.Kind = Spec.File; |
| 678 | Spec.Location = IndexFile; |
| 679 | IndexSpec = std::move(Spec); |
| 680 | } |
| 681 | #if CLANGD_ENABLE_REMOTE |
| 682 | if (!RemoteIndexAddress.empty()) { |
| 683 | assert(!ProjectRoot.empty() && IndexFile.empty()); |
| 684 | Config::ExternalIndexSpec Spec; |
| 685 | Spec.Kind = Spec.Server; |
| 686 | Spec.Location = RemoteIndexAddress; |
| 687 | Spec.MountPoint = ProjectRoot; |
| 688 | IndexSpec = std::move(Spec); |
| 689 | BGPolicy = Config::BackgroundPolicy::Skip; |
| 690 | } |
| 691 | #endif |
| 692 | if (!EnableBackgroundIndex) { |
| 693 | BGPolicy = Config::BackgroundPolicy::Skip; |
| 694 | } |
| 695 | |
| 696 | Frag = [=](const config::Params &, Config &C) { |
| 697 | if (CDBSearch) |
| 698 | C.CompileFlags.CDBSearch = *CDBSearch; |
| 699 | if (IndexSpec) |
| 700 | C.Index.External = *IndexSpec; |
| 701 | if (BGPolicy) |
| 702 | C.Index.Background = *BGPolicy; |
| 703 | if (AllScopesCompletion.getNumOccurrences()) |
| 704 | C.Completion.AllScopes = AllScopesCompletion; |
| 705 | |
| 706 | if (Test) |
| 707 | C.Index.StandardLibrary = false; |
| 708 | return true; |
| 709 | }; |
| 710 | } |
| 711 | }; |
| 712 | } // namespace |
| 713 | } // namespace clangd |
| 714 | } // namespace clang |
| 715 | |
| 716 | enum class ErrorResultCode : int { |
| 717 | NoShutdownRequest = 1, |
| 718 | CantRunAsXPCService = 2, |
| 719 | CheckFailed = 3 |
| 720 | }; |
| 721 | |
| 722 | int main(int argc, char *argv[]) { |
| 723 | using namespace clang; |
| 724 | using namespace clang::clangd; |
| 725 | |
| 726 | llvm::InitializeAllTargetInfos(); |
| 727 | llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 728 | llvm::sys::AddSignalHandler( |
| 729 | [](void *) { |
| 730 | ThreadCrashReporter::runCrashHandlers(); |
| 731 | // Ensure ThreadCrashReporter and PrintStackTrace output is visible. |
| 732 | llvm::errs().flush(); |
| 733 | }, |
| 734 | nullptr); |
| 735 | llvm::sys::SetInterruptFunction(&requestShutdown); |
| 736 | llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) { |
| 737 | OS << versionString() << "\n" |
| 738 | << "Features: " << featureString() << "\n" |
| 739 | << "Platform: " << platformString() << "\n"; |
| 740 | }); |
| 741 | const char *FlagsEnvVar = "CLANGD_FLAGS"; |
| 742 | const char *Overview = |
| 743 | R"(clangd is a language server that provides IDE-like features to editors. |
| 744 | |
| 745 | It should be used via an editor plugin rather than invoked directly. For more information, see: |
| 746 | https://clangd.llvm.org/ |
| 747 | https://microsoft.github.io/language-server-protocol/ |
| 748 | |
| 749 | clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment variable. |
| 750 | )"; |
| 751 | llvm::cl::HideUnrelatedOptions(ClangdCategories); |
| 752 | llvm::cl::ParseCommandLineOptions(argc, argv, Overview, |
| 753 | /*Errs=*/nullptr, FlagsEnvVar); |
| 754 | if (Test) { |
| 755 | if (!Sync.getNumOccurrences()) |
| 756 | Sync = true; |
| 757 | if (!CrashPragmas.getNumOccurrences()) |
| 758 | CrashPragmas = true; |
| 759 | InputStyle = JSONStreamStyle::Delimited; |
| 760 | LogLevel = Logger::Verbose; |
| 761 | PrettyPrint = true; |
| 762 | // Disable config system by default to avoid external reads. |
| 763 | if (!EnableConfig.getNumOccurrences()) |
| 764 | EnableConfig = false; |
| 765 | // Disable background index on lit tests by default to prevent disk writes. |
| 766 | if (!EnableBackgroundIndex.getNumOccurrences()) |
| 767 | EnableBackgroundIndex = false; |
| 768 | // Ensure background index makes progress. |
| 769 | else if (EnableBackgroundIndex) |
| 770 | BackgroundQueue::preventThreadStarvationInTests(); |
| 771 | } |
| 772 | if (Test || EnableTestScheme) { |
| 773 | static URISchemeRegistry::Add<TestScheme> X( |
| 774 | "test", "Test scheme for clangd lit tests."); |
| 775 | } |
| 776 | if (CrashPragmas) |
| 777 | allowCrashPragmasForTest(); |
| 778 | |
| 779 | if (!Sync && WorkerThreadsCount == 0) { |
| 780 | llvm::errs() << "A number of worker threads cannot be 0. Did you mean to " |
| 781 | "specify -sync?"; |
| 782 | return 1; |
| 783 | } |
| 784 | |
| 785 | if (Sync) { |
| 786 | if (WorkerThreadsCount.getNumOccurrences()) |
| 787 | llvm::errs() << "Ignoring -j because -sync is set.\n"; |
| 788 | WorkerThreadsCount = 0; |
| 789 | } |
| 790 | if (FallbackStyle.getNumOccurrences()) |
| 791 | clang::format::DefaultFallbackStyle = FallbackStyle.c_str(); |
| 792 | |
| 793 | // Validate command line arguments. |
| 794 | std::optional<llvm::raw_fd_ostream> InputMirrorStream; |
| 795 | if (!InputMirrorFile.empty()) { |
| 796 | std::error_code EC; |
| 797 | InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC, |
| 798 | llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write); |
| 799 | if (EC) { |
| 800 | InputMirrorStream.reset(); |
| 801 | llvm::errs() << "Error while opening an input mirror file: " |
| 802 | << EC.message(); |
| 803 | } else { |
| 804 | InputMirrorStream->SetUnbuffered(); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | #if !CLANGD_DECISION_FOREST |
| 809 | if (RankingModel == clangd::CodeCompleteOptions::DecisionForest) { |
| 810 | llvm::errs() << "Clangd was compiled without decision forest support.\n"; |
| 811 | return 1; |
| 812 | } |
| 813 | #endif |
| 814 | |
| 815 | // Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a |
| 816 | // trace flag in your editor's config is annoying, launching with |
| 817 | // `CLANGD_TRACE=trace.json vim` is easier. |
| 818 | std::optional<llvm::raw_fd_ostream> TracerStream; |
| 819 | std::unique_ptr<trace::EventTracer> Tracer; |
| 820 | const char *JSONTraceFile = getenv("CLANGD_TRACE"); |
| 821 | const char *MetricsCSVFile = getenv("CLANGD_METRICS"); |
| 822 | const char *TracerFile = JSONTraceFile ? JSONTraceFile : MetricsCSVFile; |
| 823 | if (TracerFile) { |
| 824 | std::error_code EC; |
| 825 | TracerStream.emplace(TracerFile, /*ref*/ EC, |
| 826 | llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write); |
| 827 | if (EC) { |
| 828 | TracerStream.reset(); |
| 829 | llvm::errs() << "Error while opening trace file " << TracerFile << ": " |
| 830 | << EC.message(); |
| 831 | } else { |
| 832 | Tracer = (TracerFile == JSONTraceFile) |
| 833 | ? trace::createJSONTracer(*TracerStream, PrettyPrint) |
| 834 | : trace::createCSVMetricTracer(*TracerStream); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | std::optional<trace::Session> TracingSession; |
| 839 | if (Tracer) |
| 840 | TracingSession.emplace(*Tracer); |
| 841 | |
| 842 | // If a user ran `clangd` in a terminal without redirecting anything, |
| 843 | // it's somewhat likely they're confused about how to use clangd. |
| 844 | // Show them the help overview, which explains. |
| 845 | if (llvm::outs().is_displayed() && llvm::errs().is_displayed() && |
| 846 | !CheckFile.getNumOccurrences()) |
| 847 | llvm::errs() << Overview << "\n"; |
| 848 | // Use buffered stream to stderr (we still flush each log message). Unbuffered |
| 849 | // stream can cause significant (non-deterministic) latency for the logger. |
| 850 | llvm::errs().SetBuffered(); |
| 851 | // Don't flush stdout when logging, this would be both slow and racy! |
| 852 | llvm::errs().tie(nullptr); |
| 853 | StreamLogger Logger(llvm::errs(), LogLevel); |
| 854 | LoggingSession LoggingSession(Logger); |
| 855 | // Write some initial logs before we start doing any real work. |
| 856 | log("{0}", versionString()); |
| 857 | log("Features: {0}", featureString()); |
| 858 | log("PID: {0}", llvm::sys::Process::getProcessId()); |
| 859 | { |
| 860 | SmallString<128> CWD; |
| 861 | if (auto Err = llvm::sys::fs::current_path(CWD)) |
| 862 | log("Working directory unknown: {0}", Err.message()); |
| 863 | else |
| 864 | log("Working directory: {0}", CWD); |
| 865 | } |
| 866 | for (int I = 0; I < argc; ++I) |
| 867 | log("argv[{0}]: {1}", I, argv[I]); |
| 868 | if (auto EnvFlags = llvm::sys::Process::GetEnv(FlagsEnvVar)) |
| 869 | log("{0}: {1}", FlagsEnvVar, *EnvFlags); |
| 870 | |
| 871 | ClangdLSPServer::Options Opts; |
| 872 | Opts.UseDirBasedCDB = (CompileArgsFrom == FilesystemCompileArgs); |
| 873 | |
| 874 | switch (PCHStorage) { |
| 875 | case PCHStorageFlag::Memory: |
| 876 | Opts.StorePreamblesInMemory = true; |
| 877 | break; |
| 878 | case PCHStorageFlag::Disk: |
| 879 | Opts.StorePreamblesInMemory = false; |
| 880 | break; |
| 881 | } |
| 882 | if (!ResourceDir.empty()) |
| 883 | Opts.ResourceDir = ResourceDir; |
| 884 | Opts.BuildDynamicSymbolIndex = true; |
| 885 | std::vector<std::unique_ptr<SymbolIndex>> IdxStack; |
| 886 | std::unique_ptr<SymbolIndex> StaticIdx; |
| 887 | #if CLANGD_ENABLE_REMOTE |
| 888 | if (RemoteIndexAddress.empty() != ProjectRoot.empty()) { |
| 889 | llvm::errs() << "remote-index-address and project-path have to be " |
| 890 | "specified at the same time."; |
| 891 | return 1; |
| 892 | } |
| 893 | if (!RemoteIndexAddress.empty()) { |
| 894 | if (IndexFile.empty()) { |
| 895 | log("Connecting to remote index at {0}", RemoteIndexAddress); |
| 896 | } else { |
| 897 | elog("When enabling remote index, IndexFile should not be specified. " |
| 898 | "Only one can be used at time. Remote index will ignored."); |
| 899 | } |
| 900 | } |
| 901 | #endif |
| 902 | Opts.BackgroundIndex = EnableBackgroundIndex; |
| 903 | Opts.BackgroundIndexPriority = BackgroundIndexPriority; |
| 904 | Opts.ReferencesLimit = ReferencesLimit; |
| 905 | Opts.Rename.LimitFiles = RenameFileLimit; |
| 906 | auto PAI = createProjectAwareIndex(loadExternalIndex, Sync); |
| 907 | if (StaticIdx) { |
| 908 | IdxStack.emplace_back(std::move(StaticIdx)); |
| 909 | IdxStack.emplace_back( |
| 910 | std::make_unique<MergedIndex>(PAI.get(), IdxStack.back().get())); |
| 911 | Opts.StaticIndex = IdxStack.back().get(); |
| 912 | } else { |
| 913 | Opts.StaticIndex = PAI.get(); |
| 914 | } |
| 915 | Opts.AsyncThreadsCount = WorkerThreadsCount; |
| 916 | Opts.MemoryCleanup = getMemoryCleanupFunction(); |
| 917 | |
| 918 | Opts.CodeComplete.IncludeIneligibleResults = IncludeIneligibleResults; |
| 919 | Opts.CodeComplete.Limit = LimitResults; |
| 920 | if (CompletionStyle.getNumOccurrences()) |
| 921 | Opts.CodeComplete.BundleOverloads = CompletionStyle != Detailed; |
| 922 | Opts.CodeComplete.ShowOrigins = ShowOrigins; |
| 923 | Opts.CodeComplete.InsertIncludes = HeaderInsertion; |
| 924 | Opts.CodeComplete.ImportInsertions = ImportInsertions; |
| 925 | if (!HeaderInsertionDecorators) { |
| 926 | Opts.CodeComplete.IncludeIndicator.Insert.clear(); |
| 927 | Opts.CodeComplete.IncludeIndicator.NoInsert.clear(); |
| 928 | } |
| 929 | Opts.CodeComplete.EnableFunctionArgSnippets = EnableFunctionArgSnippets; |
| 930 | Opts.CodeComplete.RunParser = CodeCompletionParse; |
| 931 | Opts.CodeComplete.RankingModel = RankingModel; |
| 932 | |
| 933 | RealThreadsafeFS TFS; |
| 934 | std::vector<std::unique_ptr<config::Provider>> ProviderStack; |
| 935 | std::unique_ptr<config::Provider> Config; |
| 936 | if (EnableConfig) { |
| 937 | ProviderStack.push_back( |
| 938 | config::Provider::fromAncestorRelativeYAMLFiles(".clangd", TFS)); |
| 939 | llvm::SmallString<256> UserConfig; |
| 940 | if (llvm::sys::path::user_config_directory(UserConfig)) { |
| 941 | llvm::sys::path::append(UserConfig, "clangd", "config.yaml"); |
| 942 | vlog("User config file is {0}", UserConfig); |
| 943 | ProviderStack.push_back(config::Provider::fromYAMLFile( |
| 944 | UserConfig, /*Directory=*/"", TFS, /*Trusted=*/true)); |
| 945 | } else { |
| 946 | elog("Couldn't determine user config file, not loading"); |
| 947 | } |
| 948 | } |
| 949 | ProviderStack.push_back(std::make_unique<FlagsConfigProvider>()); |
| 950 | std::vector<const config::Provider *> ProviderPointers; |
| 951 | for (const auto &P : ProviderStack) |
| 952 | ProviderPointers.push_back(P.get()); |
| 953 | Config = config::Provider::combine(std::move(ProviderPointers)); |
| 954 | Opts.ConfigProvider = Config.get(); |
| 955 | |
| 956 | // Create an empty clang-tidy option. |
| 957 | TidyProvider ClangTidyOptProvider; |
| 958 | if (EnableClangTidy) { |
| 959 | std::vector<TidyProvider> Providers; |
| 960 | Providers.reserve(4 + EnableConfig); |
| 961 | Providers.push_back(provideEnvironment()); |
| 962 | Providers.push_back(provideClangTidyFiles(TFS)); |
| 963 | if (EnableConfig) |
| 964 | Providers.push_back(provideClangdConfig()); |
| 965 | Providers.push_back(provideDefaultChecks()); |
| 966 | Providers.push_back(disableUnusableChecks()); |
| 967 | ClangTidyOptProvider = combine(std::move(Providers)); |
| 968 | Opts.ClangTidyProvider = ClangTidyOptProvider; |
| 969 | } |
| 970 | Opts.UseDirtyHeaders = UseDirtyHeaders; |
| 971 | Opts.PreambleParseForwardingFunctions = PreambleParseForwardingFunctions; |
| 972 | Opts.ImportInsertions = ImportInsertions; |
| 973 | Opts.QueryDriverGlobs = std::move(QueryDriverGlobs); |
| 974 | Opts.TweakFilter = [&](const Tweak &T) { |
| 975 | if (T.hidden() && !HiddenFeatures) |
| 976 | return false; |
| 977 | if (TweakList.getNumOccurrences()) |
| 978 | return llvm::is_contained(TweakList, T.id()); |
| 979 | return true; |
| 980 | }; |
| 981 | if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding) |
| 982 | Opts.Encoding = ForceOffsetEncoding; |
| 983 | setIncludeCleanerAnalyzesStdlib(IncludeCleanerStdlib); |
| 984 | |
| 985 | if (CheckFile.getNumOccurrences()) { |
| 986 | llvm::SmallString<256> Path; |
| 987 | if (auto Error = |
| 988 | llvm::sys::fs::real_path(CheckFile, Path, /*expand_tilde=*/true)) { |
| 989 | elog("Failed to resolve path {0}: {1}", CheckFile, Error.message()); |
| 990 | return 1; |
| 991 | } |
| 992 | log("Entering check mode (no LSP server)"); |
| 993 | return check(Path, TFS, Opts) |
| 994 | ? 0 |
| 995 | : static_cast<int>(ErrorResultCode::CheckFailed); |
| 996 | } |
| 997 | |
| 998 | // Initialize and run ClangdLSPServer. |
| 999 | // Change stdin to binary to not lose \r\n on windows. |
| 1000 | llvm::sys::ChangeStdinToBinary(); |
| 1001 | std::unique_ptr<Transport> TransportLayer; |
| 1002 | if (getenv("CLANGD_AS_XPC_SERVICE")) { |
| 1003 | #if CLANGD_BUILD_XPC |
| 1004 | log("Starting LSP over XPC service"); |
| 1005 | TransportLayer = newXPCTransport(); |
| 1006 | #else |
| 1007 | llvm::errs() << "This clangd binary wasn't built with XPC support.\n"; |
| 1008 | return static_cast<int>(ErrorResultCode::CantRunAsXPCService); |
| 1009 | #endif |
| 1010 | } else { |
| 1011 | log("Starting LSP over stdin/stdout"); |
| 1012 | TransportLayer = newJSONTransport( |
| 1013 | stdin, llvm::outs(), InputMirrorStream ? &*InputMirrorStream : nullptr, |
| 1014 | PrettyPrint, InputStyle); |
| 1015 | } |
| 1016 | if (!PathMappingsArg.empty()) { |
| 1017 | auto Mappings = parsePathMappings(PathMappingsArg); |
| 1018 | if (!Mappings) { |
| 1019 | elog("Invalid -path-mappings: {0}", Mappings.takeError()); |
| 1020 | return 1; |
| 1021 | } |
| 1022 | TransportLayer = createPathMappingTransport(std::move(TransportLayer), |
| 1023 | std::move(*Mappings)); |
| 1024 | } |
| 1025 | |
| 1026 | ClangdLSPServer LSPServer(*TransportLayer, TFS, Opts); |
| 1027 | llvm::set_thread_name("clangd.main"); |
| 1028 | int ExitCode = LSPServer.run() |
| 1029 | ? 0 |
| 1030 | : static_cast<int>(ErrorResultCode::NoShutdownRequest); |
| 1031 | log("LSP finished, exiting with status {0}", ExitCode); |
| 1032 | |
| 1033 | // There may still be lingering background threads (e.g. slow requests |
| 1034 | // whose results will be dropped, background index shutting down). |
| 1035 | // |
| 1036 | // These should terminate quickly, and ~ClangdLSPServer blocks on them. |
| 1037 | // However if a bug causes them to run forever, we want to ensure the process |
| 1038 | // eventually exits. As clangd isn't directly user-facing, an editor can |
| 1039 | // "leak" clangd processes. Crashing in this case contains the damage. |
| 1040 | abortAfterTimeout(std::chrono::minutes(5)); |
| 1041 | |
| 1042 | return ExitCode; |
| 1043 | } |