2017-04-13 01:13:08 +08:00
|
|
|
//===--- ClangdMain.cpp - clangd server loop ------------------------------===//
|
2017-02-07 18:28:20 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-07 18:28:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2019-04-10 20:15:35 +08:00
|
|
|
#include "CodeComplete.h"
|
2021-03-05 19:07:25 +08:00
|
|
|
#include "Config.h"
|
|
|
|
#include "ConfigProvider.h"
|
2019-04-10 20:15:35 +08:00
|
|
|
#include "Features.inc"
|
2020-01-07 18:28:05 +08:00
|
|
|
#include "PathMapping.h"
|
2019-03-28 01:47:49 +08:00
|
|
|
#include "Protocol.h"
|
2020-11-26 02:35:34 +08:00
|
|
|
#include "TidyProvider.h"
|
[clangd] Lay JSONRPCDispatcher to rest.
Summary:
Most of its functionality is moved into ClangdLSPServer.
The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer
was never real, and only served to obfuscate.
Some previous implicit/magic stuff is now explicit:
- the return type of LSP method calls are now in the signature
- no more reply() that gets the ID using global context magic
- arg tracing no longer relies on RequestArgs::stash context magic either
This is mostly refactoring, but some deliberate fixes while here:
- LSP method params are now by const reference
- notifications and calls are now distinct namespaces.
(some tests had protocol errors and needed updating)
- we now reply to calls we failed to decode
- outgoing calls use distinct IDs
A few error codes and message IDs changed in unimportant ways (see tests).
Reviewers: ioeric
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D53387
llvm-svn: 344737
2018-10-18 20:32:04 +08:00
|
|
|
#include "Transport.h"
|
2019-04-18 21:46:40 +08:00
|
|
|
#include "index/Background.h"
|
2020-11-06 06:55:04 +08:00
|
|
|
#include "index/Index.h"
|
|
|
|
#include "index/Merge.h"
|
|
|
|
#include "index/ProjectAware.h"
|
[clangd] Merge binary + YAML serialization behind a (mostly) common interface.
Summary:
Interface is in one file, implementation in two as they have little in common.
A couple of ad-hoc YAML functions left exposed:
- symbol -> YAML I expect to keep for tools like dexp
- YAML -> symbol is used for the MR-style indexer, I think we can eliminate
this (merge-on-the-fly, else use a different serialization)
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D52453
llvm-svn: 342999
2018-09-26 02:06:43 +08:00
|
|
|
#include "index/Serialization.h"
|
2020-07-27 17:21:55 +08:00
|
|
|
#include "index/remote/Client.h"
|
2020-02-19 22:37:36 +08:00
|
|
|
#include "refactor/Rename.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Path.h"
|
|
|
|
#include "support/Shutdown.h"
|
2020-06-17 17:53:32 +08:00
|
|
|
#include "support/ThreadsafeFS.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Trace.h"
|
2018-07-30 03:12:42 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2019-05-06 16:11:59 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2019-03-28 01:47:49 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2020-06-16 18:16:24 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2019-05-21 01:30:46 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-03-02 00:16:29 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-02-07 18:28:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-10-02 23:13:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2019-07-25 15:54:48 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2017-02-07 20:40:59 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2019-06-26 15:39:14 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2017-10-10 17:08:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
[clangd] abort if shutdown takes more than a minute.
Summary:
A certain class of bug (e.g. infloop on an AST worker thread) currently means
clangd never terminates, even if the editor shuts down the protocol and closes
our stdin, and the main thread recognizes that.
Instead, let's wait 60 seconds for threads to finish cleanly, and then crash
if they haven't.
(Obviously, we should still fix these bugs).
Reviewers: kadircet
Subscribers: MaskRay, jkorous, arphaman, jfb, usaxena95, cfe-commits, ilya-biryukov
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69329
2019-10-23 17:11:18 +08:00
|
|
|
#include <chrono>
|
2018-02-14 11:20:07 +08:00
|
|
|
#include <cstdlib>
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <memory>
|
2019-05-21 01:30:46 +08:00
|
|
|
#include <mutex>
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <string>
|
2017-08-14 16:45:47 +08:00
|
|
|
#include <thread>
|
2021-03-05 19:07:25 +08:00
|
|
|
#include <utility>
|
2020-11-06 06:55:04 +08:00
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-11-26 02:51:07 +08:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2020-12-22 15:44:20 +08:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2020-10-01 22:14:31 +08:00
|
|
|
|
|
|
|
// Implemented in Check.cpp.
|
2021-03-19 04:14:02 +08:00
|
|
|
bool check(const llvm::StringRef File,
|
|
|
|
llvm::function_ref<bool(const Position &)> ShouldCheckLine,
|
2021-06-03 01:45:11 +08:00
|
|
|
const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts,
|
|
|
|
bool EnableCodeCompletion);
|
2020-10-01 22:14:31 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
namespace {
|
2018-08-21 18:40:19 +08:00
|
|
|
|
2019-07-24 17:33:27 +08:00
|
|
|
using llvm::cl::cat;
|
|
|
|
using llvm::cl::CommaSeparated;
|
|
|
|
using llvm::cl::desc;
|
|
|
|
using llvm::cl::Hidden;
|
|
|
|
using llvm::cl::init;
|
|
|
|
using llvm::cl::list;
|
|
|
|
using llvm::cl::opt;
|
2019-07-24 20:41:52 +08:00
|
|
|
using llvm::cl::OptionCategory;
|
2020-10-01 22:14:31 +08:00
|
|
|
using llvm::cl::ValueOptional;
|
2019-07-24 17:33:27 +08:00
|
|
|
using llvm::cl::values;
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
// All flags must be placed in a category, or they will be shown neither in
|
|
|
|
// --help, nor --help-hidden!
|
|
|
|
OptionCategory CompileCommands("clangd compilation flags options");
|
|
|
|
OptionCategory Features("clangd feature options");
|
|
|
|
OptionCategory Misc("clangd miscellaneous options");
|
|
|
|
OptionCategory Protocol("clangd protocol and logging options");
|
2021-01-15 06:50:35 +08:00
|
|
|
OptionCategory Retired("clangd flags no longer in use");
|
2019-07-24 20:41:52 +08:00
|
|
|
const OptionCategory *ClangdCategories[] = {&Features, &Protocol,
|
2021-01-15 06:50:35 +08:00
|
|
|
&CompileCommands, &Misc, &Retired};
|
|
|
|
|
|
|
|
template <typename T> class RetiredFlag {
|
|
|
|
opt<T> Option;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RetiredFlag(llvm::StringRef Name)
|
|
|
|
: Option(Name, cat(Retired), desc("Obsolete flag, ignored"), Hidden,
|
|
|
|
llvm::cl::callback([Name](const T &) {
|
|
|
|
llvm::errs()
|
|
|
|
<< "The flag `-" << Name << "` is obsolete and ignored.\n";
|
|
|
|
})) {}
|
|
|
|
};
|
2019-07-24 20:41:52 +08:00
|
|
|
|
|
|
|
enum CompileArgsFrom { LSPCompileArgs, FilesystemCompileArgs };
|
|
|
|
opt<CompileArgsFrom> CompileArgsFrom{
|
|
|
|
"compile_args_from",
|
|
|
|
cat(CompileCommands),
|
|
|
|
desc("The source of compile commands"),
|
|
|
|
values(clEnumValN(LSPCompileArgs, "lsp",
|
|
|
|
"All compile commands come from LSP and "
|
|
|
|
"'compile_commands.json' files are ignored"),
|
|
|
|
clEnumValN(FilesystemCompileArgs, "filesystem",
|
|
|
|
"All compile commands come from the "
|
|
|
|
"'compile_commands.json' files")),
|
|
|
|
init(FilesystemCompileArgs),
|
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
|
|
|
opt<Path> CompileCommandsDir{
|
2017-10-02 23:13:20 +08:00
|
|
|
"compile-commands-dir",
|
2019-07-24 20:41:52 +08:00
|
|
|
cat(CompileCommands),
|
2019-07-24 17:33:27 +08:00
|
|
|
desc("Specify a path to look for compile_commands.json. If path "
|
|
|
|
"is invalid, clangd will look in the current directory and "
|
|
|
|
"parent paths of each source file"),
|
|
|
|
};
|
2017-10-02 23:13:20 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<Path> ResourceDir{
|
|
|
|
"resource-dir",
|
|
|
|
cat(CompileCommands),
|
|
|
|
desc("Directory for system clang headers"),
|
|
|
|
init(""),
|
|
|
|
Hidden,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
list<std::string> QueryDriverGlobs{
|
|
|
|
"query-driver",
|
|
|
|
cat(CompileCommands),
|
|
|
|
desc(
|
|
|
|
"Comma separated list of globs for white-listing gcc-compatible "
|
|
|
|
"drivers that are safe to execute. Drivers matching any of these globs "
|
|
|
|
"will be used to extract system includes. e.g. "
|
|
|
|
"/usr/bin/**/clang-*,/path/to/repo/**/g++-*"),
|
|
|
|
CommaSeparated,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
|
2017-11-24 00:58:22 +08:00
|
|
|
// FIXME: Flags are the wrong mechanism for user preferences.
|
|
|
|
// We should probably read a dotfile or similar.
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> AllScopesCompletion{
|
|
|
|
"all-scopes-completion",
|
|
|
|
cat(Features),
|
|
|
|
desc("If set to true, code completion will include index symbols that are "
|
|
|
|
"not defined in the scopes (e.g. "
|
|
|
|
"namespaces) visible from the code completion point. Such completions "
|
|
|
|
"can insert scope qualifiers"),
|
|
|
|
init(true),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> ShowOrigins{
|
|
|
|
"debug-origin",
|
|
|
|
cat(Features),
|
|
|
|
desc("Show origins of completion items"),
|
|
|
|
init(CodeCompleteOptions().ShowOrigins),
|
2019-07-24 17:33:27 +08:00
|
|
|
Hidden,
|
|
|
|
};
|
2018-02-06 18:47:30 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> EnableBackgroundIndex{
|
|
|
|
"background-index",
|
|
|
|
cat(Features),
|
2019-08-29 22:38:02 +08:00
|
|
|
desc("Index project code in the background and persist index on disk."),
|
2019-07-24 20:41:52 +08:00
|
|
|
init(true),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> EnableClangTidy{
|
|
|
|
"clang-tidy",
|
|
|
|
cat(Features),
|
|
|
|
desc("Enable clang-tidy diagnostics"),
|
|
|
|
init(true),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<CodeCompleteOptions::CodeCompletionParse> CodeCompletionParse{
|
|
|
|
"completion-parse",
|
|
|
|
cat(Features),
|
|
|
|
desc("Whether the clang-parser is used for code-completion"),
|
|
|
|
values(clEnumValN(CodeCompleteOptions::AlwaysParse, "always",
|
|
|
|
"Block until the parser can be used"),
|
|
|
|
clEnumValN(CodeCompleteOptions::ParseIfReady, "auto",
|
|
|
|
"Use text-based completion if the parser "
|
|
|
|
"is not ready"),
|
|
|
|
clEnumValN(CodeCompleteOptions::NeverParse, "never",
|
|
|
|
"Always used text-based completion")),
|
|
|
|
init(CodeCompleteOptions().RunParser),
|
2019-07-24 17:33:27 +08:00
|
|
|
Hidden,
|
|
|
|
};
|
2017-10-10 17:08:47 +08:00
|
|
|
|
[clangd] Use Decision Forest to score code completions.
By default clangd will score a code completion item using heuristics model.
Scoring can be done by Decision Forest model by passing `--ranking_model=decision_forest` to
clangd.
Features omitted from the model:
- `NameMatch` is excluded because the final score must be multiplicative in `NameMatch` to allow rescoring by the editor.
- `NeedsFixIts` is excluded because the generating dataset that needs 'fixits' is non-trivial.
There are multiple ways (heuristics) to combine the above two features with the prediction of the DF:
- `NeedsFixIts` is used as is with a penalty of `0.5`.
Various alternatives of combining NameMatch `N` and Decision forest Prediction `P`
- N * scale(P, 0, 1): Linearly scale the output of model to range [0, 1]
- N * a^P:
- More natural: Prediction of each Decision Tree can be considered as a multiplicative boost (like NameMatch)
- Ordering is independent of the absolute value of P. Order of two items is proportional to `a^{difference in model prediction score}`. Higher `a` gives higher weightage to model output as compared to NameMatch score.
Baseline MRR = 0.619
MRR for various combinations:
N * P = 0.6346, advantage%=2.5768
N * 1.1^P = 0.6600, advantage%=6.6853
N * **1.2**^P = 0.6669, advantage%=**7.8005**
N * **1.3**^P = 0.6668, advantage%=**7.7795**
N * **1.4**^P = 0.6659, advantage%=**7.6270**
N * 1.5^P = 0.6646, advantage%=7.4200
N * 1.6^P = 0.6636, advantage%=7.2671
N * 1.7^P = 0.6629, advantage%=7.1450
N * 2^P = 0.6612, advantage%=6.8673
N * 2.5^P = 0.6598, advantage%=6.6491
N * 3^P = 0.6590, advantage%=6.5242
N * scaled[0, 1] = 0.6465, advantage%=4.5054
Differential Revision: https://reviews.llvm.org/D88281
2020-09-22 13:56:08 +08:00
|
|
|
opt<CodeCompleteOptions::CodeCompletionRankingModel> RankingModel{
|
|
|
|
"ranking-model",
|
|
|
|
cat(Features),
|
|
|
|
desc("Model to use to rank code-completion items"),
|
|
|
|
values(clEnumValN(CodeCompleteOptions::Heuristics, "heuristics",
|
|
|
|
"Use hueristics to rank code completion items"),
|
|
|
|
clEnumValN(CodeCompleteOptions::DecisionForest, "decision_forest",
|
|
|
|
"Use Decision Forest model to rank completion items")),
|
|
|
|
init(CodeCompleteOptions().RankingModel),
|
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
// FIXME: also support "plain" style where signatures are always omitted.
|
|
|
|
enum CompletionStyleFlag { Detailed, Bundled };
|
|
|
|
opt<CompletionStyleFlag> CompletionStyle{
|
|
|
|
"completion-style",
|
|
|
|
cat(Features),
|
|
|
|
desc("Granularity of code completion suggestions"),
|
|
|
|
values(clEnumValN(Detailed, "detailed",
|
|
|
|
"One completion item for each semantically distinct "
|
|
|
|
"completion, with full type information"),
|
|
|
|
clEnumValN(Bundled, "bundled",
|
|
|
|
"Similar completion items (e.g. function overloads) are "
|
|
|
|
"combined. Type information shown where possible")),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<std::string> FallbackStyle{
|
|
|
|
"fallback-style",
|
|
|
|
cat(Features),
|
|
|
|
desc("clang-format style to apply by default when "
|
|
|
|
"no .clang-format file is found"),
|
|
|
|
init(clang::format::DefaultFallbackStyle),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> EnableFunctionArgSnippets{
|
|
|
|
"function-arg-placeholders",
|
|
|
|
cat(Features),
|
|
|
|
desc("When disabled, completions contain only parentheses for "
|
|
|
|
"function calls. When enabled, completions also contain "
|
|
|
|
"placeholders for method parameters"),
|
|
|
|
init(CodeCompleteOptions().EnableFunctionArgSnippets),
|
2019-07-24 17:33:27 +08:00
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<CodeCompleteOptions::IncludeInsertion> HeaderInsertion{
|
2019-04-10 20:15:35 +08:00
|
|
|
"header-insertion",
|
2019-07-24 20:41:52 +08:00
|
|
|
cat(Features),
|
2019-07-24 17:33:27 +08:00
|
|
|
desc("Add #include directives when accepting code completions"),
|
|
|
|
init(CodeCompleteOptions().InsertIncludes),
|
|
|
|
values(
|
2019-04-10 20:15:35 +08:00
|
|
|
clEnumValN(CodeCompleteOptions::IWYU, "iwyu",
|
|
|
|
"Include what you use. "
|
|
|
|
"Insert the owning header for top-level symbols, unless the "
|
|
|
|
"header is already directly included or the symbol is "
|
2019-05-28 17:31:27 +08:00
|
|
|
"forward-declared"),
|
2019-04-10 20:15:35 +08:00
|
|
|
clEnumValN(
|
|
|
|
CodeCompleteOptions::NeverInsert, "never",
|
2019-07-24 17:33:27 +08:00
|
|
|
"Never insert #include directives as part of code completion")),
|
|
|
|
};
|
2019-04-10 20:15:35 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> HeaderInsertionDecorators{
|
2018-07-30 03:12:42 +08:00
|
|
|
"header-insertion-decorators",
|
2019-07-24 20:41:52 +08:00
|
|
|
cat(Features),
|
2019-07-24 17:33:27 +08:00
|
|
|
desc("Prepend a circular dot or space before the completion "
|
|
|
|
"label, depending on whether "
|
|
|
|
"an include line will be inserted or not"),
|
|
|
|
init(true),
|
|
|
|
};
|
2018-07-30 03:12:42 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> HiddenFeatures{
|
|
|
|
"hidden-features",
|
|
|
|
cat(Features),
|
|
|
|
desc("Enable hidden features mostly useful to clangd developers"),
|
|
|
|
init(false),
|
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
|
|
|
opt<bool> IncludeIneligibleResults{
|
|
|
|
"include-ineligible-results",
|
|
|
|
cat(Features),
|
|
|
|
desc("Include ineligible completion results (e.g. private members)"),
|
|
|
|
init(CodeCompleteOptions().IncludeIneligibleResults),
|
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
2021-01-15 06:50:35 +08:00
|
|
|
RetiredFlag<bool> EnableIndex("index");
|
2021-01-28 07:42:16 +08:00
|
|
|
RetiredFlag<bool> SuggestMissingIncludes("suggest-missing-includes");
|
|
|
|
RetiredFlag<bool> RecoveryAST("recovery-ast");
|
|
|
|
RetiredFlag<bool> RecoveryASTType("recovery-ast-type");
|
|
|
|
RetiredFlag<bool> AsyncPreamble("async-preamble");
|
|
|
|
RetiredFlag<bool> CollectMainFileRefs("collect-main-file-refs");
|
2021-02-11 21:58:16 +08:00
|
|
|
RetiredFlag<bool> CrossFileRename("cross-file-rename");
|
2021-02-13 22:14:21 +08:00
|
|
|
RetiredFlag<std::string> ClangTidyChecks("clang-tidy-checks");
|
2019-07-24 20:41:52 +08:00
|
|
|
|
|
|
|
opt<int> LimitResults{
|
|
|
|
"limit-results",
|
|
|
|
cat(Features),
|
|
|
|
desc("Limit the number of results returned by clangd. "
|
|
|
|
"0 means no limit (default=100)"),
|
|
|
|
init(100),
|
|
|
|
};
|
|
|
|
|
2021-05-06 05:39:37 +08:00
|
|
|
opt<int> ReferencesLimit{
|
|
|
|
"limit-references",
|
|
|
|
cat(Features),
|
|
|
|
desc("Limit the number of references returned by clangd. "
|
|
|
|
"0 means no limit (default=1000)"),
|
|
|
|
init(1000),
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
list<std::string> TweakList{
|
|
|
|
"tweaks",
|
|
|
|
cat(Features),
|
|
|
|
desc("Specify a list of Tweaks to enable (only for clangd developers)."),
|
|
|
|
Hidden,
|
|
|
|
CommaSeparated,
|
|
|
|
};
|
|
|
|
|
[clangd] Implement textDocument/foldingRange
Summary:
This patch introduces basic textDocument/foldingRange support. It relies on
textDocument/documentSymbols to collect all symbols and uses takes ranges
to create folds.
The next steps for textDocument/foldingRange support would be:
* Implementing FoldingRangeClientCapabilities and respecting respect client
preferences
* Specifying folding range kind
* Migrating from DocumentSymbol implementation to custom RecursiveASTVisitor flow that will allow more flexibility
* Supporting more folding range types: comments, PP conditional regions, includes and other code regions (e.g. public/private/protected sections of classes, control flow statement bodies)
Tested: (Neo)Vim (coc-clangd) and VSCode.
Related issue: https://github.com/clangd/clangd/issues/310
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82436
2020-07-14 15:28:38 +08:00
|
|
|
opt<bool> FoldingRanges{
|
|
|
|
"folding-ranges",
|
|
|
|
cat(Features),
|
|
|
|
desc("Enable preview of FoldingRanges feature"),
|
|
|
|
init(false),
|
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
2021-04-26 11:46:51 +08:00
|
|
|
opt<bool> InlayHints{"inlay-hints", cat(Features),
|
|
|
|
desc("Enable preview of InlayHints feature"), init(false)};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<unsigned> WorkerThreadsCount{
|
|
|
|
"j",
|
|
|
|
cat(Misc),
|
clangd: use -j for background index pool
Summary:
clangd supports a -j option to limit the amount of threads to use for parsing
TUs. However, when using -background-index (the default in later versions of
clangd), the parallelism used by clangd defaults to the hardware_parallelisn,
i.e. number of physical cores.
On shared hardware environments, with large projects, this can significantly
affect performance with no way to tune it down.
This change makes the -j parameter apply equally to parsing and background
index. It's not perfect, because the total number of threads is 2x the -j value,
which may still be unexpected. But at least this change allows users to prevent
clangd using all CPU cores.
Reviewers: kadircet, sammccall
Reviewed By: sammccall
Subscribers: javed.absar, jfb, sammccall, ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66031
llvm-svn: 368498
2019-08-10 07:03:32 +08:00
|
|
|
desc("Number of async workers used by clangd. Background index also "
|
|
|
|
"uses this many workers."),
|
2019-07-24 20:41:52 +08:00
|
|
|
init(getDefaultAsyncThreadsCount()),
|
|
|
|
};
|
|
|
|
|
|
|
|
opt<Path> IndexFile{
|
2018-10-08 18:44:54 +08:00
|
|
|
"index-file",
|
2019-07-24 20:41:52 +08:00
|
|
|
cat(Misc),
|
2019-07-24 17:33:27 +08:00
|
|
|
desc(
|
2018-10-08 18:44:54 +08:00
|
|
|
"Index file to build the static index. The file must have been created "
|
2019-05-28 17:31:27 +08:00
|
|
|
"by a compatible clangd-indexer\n"
|
2018-01-10 22:44:34 +08:00
|
|
|
"WARNING: This option is experimental only, and will be removed "
|
2019-05-28 17:31:27 +08:00
|
|
|
"eventually. Don't rely on it"),
|
2019-07-24 17:33:27 +08:00
|
|
|
init(""),
|
|
|
|
Hidden,
|
|
|
|
};
|
2018-01-10 22:44:34 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> Test{
|
|
|
|
"lit-test",
|
|
|
|
cat(Misc),
|
|
|
|
desc("Abbreviation for -input-style=delimited -pretty -sync "
|
2020-07-06 23:23:08 +08:00
|
|
|
"-enable-test-scheme -enable-config=0 -log=verbose. "
|
2019-07-24 20:41:52 +08:00
|
|
|
"Intended to simplify lit tests"),
|
|
|
|
init(false),
|
|
|
|
Hidden,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
[clangd] Enable auto-index behind a flag.
Summary:
Ownership and configuration:
The auto-index (background index) is maintained by ClangdServer, like Dynamic.
(This means ClangdServer will be able to enqueue preamble indexing in future).
For now it's enabled by a simple boolean flag in ClangdServer::Options, but
we probably want to eventually allow injecting the storage strategy.
New 'sync' command:
In order to meaningfully test the integration (not just unit-test components)
we need a way for tests to ensure the asynchronous index reads/writes occur
before a certain point.
Because these tests and assertions are few, I think exposing an explicit "sync"
command for use in tests is simpler than allowing threading to be completely
disabled in the background index (as we do for TUScheduler).
Bugs:
I fixed a couple of trivial bugs I found while testing, but there's one I can't.
JSONCompilationDatabase::getAllFiles() may return relative paths, and currently
we trigger an assertion that assumes they are absolute.
There's no efficient way to resolve them (you have to retrieve the corresponding
command and then resolve against its directory property). In general I think
this behavior is broken and we should fix it in JSONCompilationDatabase and
require CompilationDatabase::getAllFiles() to be absolute.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D54894
llvm-svn: 347567
2018-11-27 00:00:11 +08:00
|
|
|
|
2020-10-01 22:14:31 +08:00
|
|
|
opt<Path> CheckFile{
|
|
|
|
"check",
|
|
|
|
cat(Misc),
|
|
|
|
desc("Parse one file in isolation instead of acting as a language server. "
|
|
|
|
"Useful to investigate/reproduce crashes or configuration problems. "
|
|
|
|
"With --check=<filename>, attempts to parse a particular file."),
|
|
|
|
init(""),
|
|
|
|
ValueOptional,
|
|
|
|
};
|
|
|
|
|
2021-03-19 04:14:02 +08:00
|
|
|
opt<std::string> CheckFileLines{
|
|
|
|
"check-lines",
|
|
|
|
cat(Misc),
|
|
|
|
desc("If specified, limits the range of tokens in -check file on which "
|
|
|
|
"various features are tested. Example --check-lines=3-7 restricts "
|
|
|
|
"testing to lines 3 to 7 (inclusive) or --check-lines=5 to restrict "
|
|
|
|
"to one line. Default is testing entire file."),
|
|
|
|
init(""),
|
|
|
|
ValueOptional,
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
enum PCHStorageFlag { Disk, Memory };
|
|
|
|
opt<PCHStorageFlag> PCHStorage{
|
|
|
|
"pch-storage",
|
|
|
|
cat(Misc),
|
|
|
|
desc("Storing PCHs in memory increases memory usages, but may "
|
|
|
|
"improve performance"),
|
|
|
|
values(
|
|
|
|
clEnumValN(PCHStorageFlag::Disk, "disk", "store PCHs on disk"),
|
|
|
|
clEnumValN(PCHStorageFlag::Memory, "memory", "store PCHs in memory")),
|
|
|
|
init(PCHStorageFlag::Disk),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> Sync{
|
|
|
|
"sync",
|
|
|
|
cat(Misc),
|
clangd: use -j for background index pool
Summary:
clangd supports a -j option to limit the amount of threads to use for parsing
TUs. However, when using -background-index (the default in later versions of
clangd), the parallelism used by clangd defaults to the hardware_parallelisn,
i.e. number of physical cores.
On shared hardware environments, with large projects, this can significantly
affect performance with no way to tune it down.
This change makes the -j parameter apply equally to parsing and background
index. It's not perfect, because the total number of threads is 2x the -j value,
which may still be unexpected. But at least this change allows users to prevent
clangd using all CPU cores.
Reviewers: kadircet, sammccall
Reviewed By: sammccall
Subscribers: javed.absar, jfb, sammccall, ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66031
llvm-svn: 368498
2019-08-10 07:03:32 +08:00
|
|
|
desc("Handle client requests on main thread. Background index still uses "
|
|
|
|
"its own thread."),
|
2019-07-24 20:41:52 +08:00
|
|
|
init(false),
|
2019-07-24 17:33:27 +08:00
|
|
|
Hidden,
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<JSONStreamStyle> InputStyle{
|
|
|
|
"input-style",
|
|
|
|
cat(Protocol),
|
|
|
|
desc("Input JSON stream encoding"),
|
|
|
|
values(
|
|
|
|
clEnumValN(JSONStreamStyle::Standard, "standard", "usual LSP protocol"),
|
|
|
|
clEnumValN(JSONStreamStyle::Delimited, "delimited",
|
|
|
|
"messages delimited by --- lines, with # comment support")),
|
|
|
|
init(JSONStreamStyle::Standard),
|
|
|
|
Hidden,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> EnableTestScheme{
|
|
|
|
"enable-test-uri-scheme",
|
|
|
|
cat(Protocol),
|
|
|
|
desc("Enable 'test:' URI scheme. Only use in lit tests"),
|
|
|
|
init(false),
|
|
|
|
Hidden,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2020-01-07 18:28:05 +08:00
|
|
|
opt<std::string> PathMappingsArg{
|
|
|
|
"path-mappings",
|
|
|
|
cat(Protocol),
|
|
|
|
desc(
|
|
|
|
"Translates between client paths (as seen by a remote editor) and "
|
|
|
|
"server paths (where clangd sees files on disk). "
|
|
|
|
"Comma separated list of '<client_path>=<server_path>' pairs, the "
|
|
|
|
"first entry matching a given path is used. "
|
|
|
|
"e.g. /home/project/incl=/opt/include,/home/project=/workarea/project"),
|
|
|
|
init(""),
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<Path> InputMirrorFile{
|
|
|
|
"input-mirror-file",
|
|
|
|
cat(Protocol),
|
|
|
|
desc("Mirror all LSP input to the specified file. Useful for debugging"),
|
|
|
|
init(""),
|
|
|
|
Hidden,
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<Logger::Level> LogLevel{
|
|
|
|
"log",
|
|
|
|
cat(Protocol),
|
|
|
|
desc("Verbosity of log messages written to stderr"),
|
|
|
|
values(clEnumValN(Logger::Error, "error", "Error messages only"),
|
|
|
|
clEnumValN(Logger::Info, "info", "High level execution tracing"),
|
|
|
|
clEnumValN(Logger::Debug, "verbose", "Low level details")),
|
|
|
|
init(Logger::Info),
|
2019-07-24 17:33:27 +08:00
|
|
|
};
|
2019-01-28 22:01:55 +08:00
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<OffsetEncoding> ForceOffsetEncoding{
|
2019-03-28 01:47:49 +08:00
|
|
|
"offset-encoding",
|
2019-07-24 20:41:52 +08:00
|
|
|
cat(Protocol),
|
2019-07-24 17:33:27 +08:00
|
|
|
desc("Force the offsetEncoding used for character positions. "
|
|
|
|
"This bypasses negotiation via client capabilities"),
|
|
|
|
values(
|
|
|
|
clEnumValN(OffsetEncoding::UTF8, "utf-8", "Offsets are in UTF-8 bytes"),
|
|
|
|
clEnumValN(OffsetEncoding::UTF16, "utf-16",
|
2019-08-05 16:14:17 +08:00
|
|
|
"Offsets are in UTF-16 code units"),
|
|
|
|
clEnumValN(OffsetEncoding::UTF32, "utf-32",
|
|
|
|
"Offsets are in unicode codepoints")),
|
2019-07-24 17:33:27 +08:00
|
|
|
init(OffsetEncoding::UnsupportedEncoding),
|
|
|
|
};
|
|
|
|
|
2019-07-24 20:41:52 +08:00
|
|
|
opt<bool> PrettyPrint{
|
|
|
|
"pretty",
|
|
|
|
cat(Protocol),
|
|
|
|
desc("Pretty-print JSON output"),
|
2019-07-24 17:33:27 +08:00
|
|
|
init(false),
|
|
|
|
};
|
2019-06-18 21:37:54 +08:00
|
|
|
|
2020-07-06 23:23:08 +08:00
|
|
|
opt<bool> EnableConfig{
|
|
|
|
"enable-config",
|
|
|
|
cat(Misc),
|
|
|
|
desc(
|
|
|
|
"Read user and project configuration from YAML files.\n"
|
|
|
|
"Project config is from a .clangd file in the project directory.\n"
|
|
|
|
"User config is from clangd/config.yaml in the following directories:\n"
|
|
|
|
"\tWindows: %USERPROFILE%\\AppData\\Local\n"
|
|
|
|
"\tMac OS: ~/Library/Preferences/\n"
|
|
|
|
"\tOthers: $XDG_CONFIG_HOME, usually ~/.config\n"
|
|
|
|
"Configuration is documented at https://clangd.llvm.org/config.html"),
|
2020-07-15 01:04:55 +08:00
|
|
|
init(true),
|
2020-07-06 23:23:08 +08:00
|
|
|
};
|
|
|
|
|
2020-12-22 15:44:20 +08:00
|
|
|
#if defined(__GLIBC__) && CLANGD_MALLOC_TRIM
|
|
|
|
opt<bool> EnableMallocTrim{
|
|
|
|
"malloc-trim",
|
|
|
|
cat(Misc),
|
|
|
|
desc("Release memory periodically via malloc_trim(3)."),
|
|
|
|
init(true),
|
|
|
|
};
|
|
|
|
|
|
|
|
std::function<void()> getMemoryCleanupFunction() {
|
|
|
|
if (!EnableMallocTrim)
|
|
|
|
return nullptr;
|
|
|
|
// Leave a few MB at the top of the heap: it is insignificant
|
|
|
|
// and will most likely be needed by the main thread
|
|
|
|
constexpr size_t MallocTrimPad = 20'000'000;
|
|
|
|
return []() {
|
|
|
|
if (malloc_trim(MallocTrimPad))
|
|
|
|
vlog("Released memory via malloc_trim");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::function<void()> getMemoryCleanupFunction() { return nullptr; }
|
|
|
|
#endif
|
|
|
|
|
2020-07-27 20:11:29 +08:00
|
|
|
#if CLANGD_ENABLE_REMOTE
|
2020-07-27 17:21:55 +08:00
|
|
|
opt<std::string> RemoteIndexAddress{
|
|
|
|
"remote-index-address",
|
|
|
|
cat(Features),
|
|
|
|
desc("Address of the remote index server"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME(kirillbobyrev): Should this be the location of compile_commands.json?
|
|
|
|
opt<std::string> ProjectRoot{
|
|
|
|
"project-root",
|
|
|
|
cat(Features),
|
|
|
|
desc("Path to the project root. Requires remote-index-address to be set."),
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Supports a test URI scheme with relaxed constraints for lit tests.
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
/// The path in a test URI will be combined with a platform-specific fake
|
|
|
|
/// directory to form an absolute path. For example, test:///a.cpp is resolved
|
|
|
|
/// C:\clangd-test\a.cpp on Windows and /clangd-test/a.cpp on Unix.
|
|
|
|
class TestScheme : public URIScheme {
|
|
|
|
public:
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Expected<std::string>
|
|
|
|
getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body,
|
|
|
|
llvm::StringRef /*HintPath*/) const override {
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
using namespace llvm::sys;
|
|
|
|
// Still require "/" in body to mimic file scheme, as we want lengths of an
|
|
|
|
// equivalent URI in both schemes to be the same.
|
|
|
|
if (!Body.startswith("/"))
|
2020-09-14 17:33:12 +08:00
|
|
|
return error(
|
|
|
|
"Expect URI body to be an absolute path starting with '/': {0}",
|
|
|
|
Body);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
Body = Body.ltrim('/');
|
2020-11-28 04:36:09 +08:00
|
|
|
llvm::SmallString<16> Path(Body);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
path::native(Path);
|
2019-01-16 18:26:52 +08:00
|
|
|
fs::make_absolute(TestScheme::TestDir, Path);
|
2020-11-28 04:36:09 +08:00
|
|
|
return std::string(Path);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Expected<URI>
|
|
|
|
uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override {
|
|
|
|
llvm::StringRef Body = AbsolutePath;
|
2020-09-14 17:33:12 +08:00
|
|
|
if (!Body.consume_front(TestScheme::TestDir))
|
|
|
|
return error("Path {0} doesn't start with root {1}", AbsolutePath,
|
|
|
|
TestDir);
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
return URI("test", /*Authority=*/"",
|
|
|
|
llvm::sys::path::convert_to_slash(Body));
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const static char TestDir[];
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
const char TestScheme::TestDir[] = "C:\\clangd-test";
|
|
|
|
#else
|
|
|
|
const char TestScheme::TestDir[] = "/clangd-test";
|
|
|
|
#endif
|
|
|
|
|
2020-11-06 06:55:04 +08:00
|
|
|
std::unique_ptr<SymbolIndex>
|
|
|
|
loadExternalIndex(const Config::ExternalIndexSpec &External,
|
2021-03-08 17:23:56 +08:00
|
|
|
AsyncTaskRunner *Tasks) {
|
2020-11-06 06:55:04 +08:00
|
|
|
switch (External.Kind) {
|
2021-04-13 16:26:03 +08:00
|
|
|
case Config::ExternalIndexSpec::None:
|
|
|
|
break;
|
2020-11-06 06:55:04 +08:00
|
|
|
case Config::ExternalIndexSpec::Server:
|
|
|
|
log("Associating {0} with remote index at {1}.", External.MountPoint,
|
|
|
|
External.Location);
|
|
|
|
return remote::getClient(External.Location, External.MountPoint);
|
|
|
|
case Config::ExternalIndexSpec::File:
|
|
|
|
log("Associating {0} with monolithic index at {1}.", External.MountPoint,
|
|
|
|
External.Location);
|
|
|
|
auto NewIndex = std::make_unique<SwapIndex>(std::make_unique<MemIndex>());
|
2021-03-05 19:07:25 +08:00
|
|
|
auto IndexLoadTask = [File = External.Location,
|
|
|
|
PlaceHolder = NewIndex.get()] {
|
|
|
|
if (auto Idx = loadIndex(File, /*UseDex=*/true))
|
|
|
|
PlaceHolder->reset(std::move(Idx));
|
|
|
|
};
|
2021-03-08 17:23:56 +08:00
|
|
|
if (Tasks) {
|
|
|
|
Tasks->runAsync("Load-index:" + External.Location,
|
|
|
|
std::move(IndexLoadTask));
|
2021-03-05 19:07:25 +08:00
|
|
|
} else {
|
2021-03-08 17:23:56 +08:00
|
|
|
IndexLoadTask();
|
2021-03-05 19:07:25 +08:00
|
|
|
}
|
2020-11-06 06:55:04 +08:00
|
|
|
return std::move(NewIndex);
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid ExternalIndexKind.");
|
|
|
|
}
|
2021-03-05 19:07:25 +08:00
|
|
|
|
|
|
|
class FlagsConfigProvider : public config::Provider {
|
|
|
|
private:
|
|
|
|
config::CompiledFragment Frag;
|
|
|
|
|
|
|
|
std::vector<config::CompiledFragment>
|
|
|
|
getFragments(const config::Params &,
|
|
|
|
config::DiagnosticCallback) const override {
|
|
|
|
return {Frag};
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
FlagsConfigProvider() {
|
|
|
|
llvm::Optional<Config::CDBSearchSpec> CDBSearch;
|
|
|
|
llvm::Optional<Config::ExternalIndexSpec> IndexSpec;
|
|
|
|
llvm::Optional<Config::BackgroundPolicy> BGPolicy;
|
|
|
|
|
|
|
|
// If --compile-commands-dir arg was invoked, check value and override
|
|
|
|
// default path.
|
|
|
|
if (!CompileCommandsDir.empty()) {
|
|
|
|
if (llvm::sys::fs::exists(CompileCommandsDir)) {
|
|
|
|
// We support passing both relative and absolute paths to the
|
|
|
|
// --compile-commands-dir argument, but we assume the path is absolute
|
|
|
|
// in the rest of clangd so we make sure the path is absolute before
|
|
|
|
// continuing.
|
|
|
|
llvm::SmallString<128> Path(CompileCommandsDir);
|
|
|
|
if (std::error_code EC = llvm::sys::fs::make_absolute(Path)) {
|
|
|
|
elog("Error while converting the relative path specified by "
|
|
|
|
"--compile-commands-dir to an absolute path: {0}. The argument "
|
|
|
|
"will be ignored.",
|
|
|
|
EC.message());
|
|
|
|
} else {
|
|
|
|
CDBSearch = {Config::CDBSearchSpec::FixedDir, Path.str().str()};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
elog("Path specified by --compile-commands-dir does not exist. The "
|
|
|
|
"argument will be ignored.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IndexFile.empty()) {
|
|
|
|
Config::ExternalIndexSpec Spec;
|
|
|
|
Spec.Kind = Spec.File;
|
|
|
|
Spec.Location = IndexFile;
|
|
|
|
IndexSpec = std::move(Spec);
|
|
|
|
}
|
2021-03-11 20:46:52 +08:00
|
|
|
#if CLANGD_ENABLE_REMOTE
|
2021-03-05 19:07:25 +08:00
|
|
|
if (!RemoteIndexAddress.empty()) {
|
|
|
|
assert(!ProjectRoot.empty() && IndexFile.empty());
|
|
|
|
Config::ExternalIndexSpec Spec;
|
|
|
|
Spec.Kind = Spec.Server;
|
|
|
|
Spec.Location = RemoteIndexAddress;
|
|
|
|
Spec.MountPoint = ProjectRoot;
|
|
|
|
IndexSpec = std::move(Spec);
|
|
|
|
BGPolicy = Config::BackgroundPolicy::Skip;
|
|
|
|
}
|
2021-03-11 20:46:52 +08:00
|
|
|
#endif
|
2021-03-05 19:07:25 +08:00
|
|
|
if (!EnableBackgroundIndex) {
|
|
|
|
BGPolicy = Config::BackgroundPolicy::Skip;
|
|
|
|
}
|
2021-03-05 20:57:04 +08:00
|
|
|
|
2021-03-05 19:07:25 +08:00
|
|
|
Frag = [=](const config::Params &, Config &C) {
|
|
|
|
if (CDBSearch)
|
|
|
|
C.CompileFlags.CDBSearch = *CDBSearch;
|
|
|
|
if (IndexSpec)
|
|
|
|
C.Index.External = *IndexSpec;
|
|
|
|
if (BGPolicy)
|
|
|
|
C.Index.Background = *BGPolicy;
|
2021-03-05 20:57:04 +08:00
|
|
|
if (AllScopesCompletion.getNumOccurrences())
|
|
|
|
C.Completion.AllScopes = AllScopesCompletion;
|
2021-03-05 19:07:25 +08:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2019-01-07 23:45:19 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
|
2019-01-16 08:24:22 +08:00
|
|
|
enum class ErrorResultCode : int {
|
|
|
|
NoShutdownRequest = 1,
|
2020-10-01 22:14:31 +08:00
|
|
|
CantRunAsXPCService = 2,
|
|
|
|
CheckFailed = 3
|
2019-01-16 08:24:22 +08:00
|
|
|
};
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2019-01-07 23:45:19 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::clangd;
|
|
|
|
|
2019-06-26 15:39:14 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
2019-11-26 10:06:56 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2019-11-26 02:51:07 +08:00
|
|
|
llvm::sys::SetInterruptFunction(&requestShutdown);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::cl::SetVersionPrinter([](llvm::raw_ostream &OS) {
|
2018-06-29 21:24:20 +08:00
|
|
|
OS << clang::getClangToolFullVersion("clangd") << "\n";
|
|
|
|
});
|
2019-07-25 15:54:48 +08:00
|
|
|
const char *FlagsEnvVar = "CLANGD_FLAGS";
|
|
|
|
const char *Overview =
|
|
|
|
R"(clangd is a language server that provides IDE-like features to editors.
|
|
|
|
|
|
|
|
It should be used via an editor plugin rather than invoked directly. For more information, see:
|
2020-05-08 21:11:44 +08:00
|
|
|
https://clangd.llvm.org/
|
2019-07-25 15:54:48 +08:00
|
|
|
https://microsoft.github.io/language-server-protocol/
|
|
|
|
|
|
|
|
clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment variable.
|
|
|
|
)";
|
2019-07-24 20:41:52 +08:00
|
|
|
llvm::cl::HideUnrelatedOptions(ClangdCategories);
|
2019-07-25 15:54:48 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, Overview,
|
|
|
|
/*Errs=*/nullptr, FlagsEnvVar);
|
2018-02-06 18:47:30 +08:00
|
|
|
if (Test) {
|
2019-05-28 17:20:57 +08:00
|
|
|
Sync = true;
|
2018-02-06 18:47:30 +08:00
|
|
|
InputStyle = JSONStreamStyle::Delimited;
|
2019-04-18 18:32:08 +08:00
|
|
|
LogLevel = Logger::Verbose;
|
2018-02-06 18:47:30 +08:00
|
|
|
PrettyPrint = true;
|
2020-07-06 23:23:08 +08:00
|
|
|
// Disable config system by default to avoid external reads.
|
|
|
|
if (!EnableConfig.getNumOccurrences())
|
|
|
|
EnableConfig = false;
|
2019-07-19 21:40:30 +08:00
|
|
|
// Disable background index on lit tests by default to prevent disk writes.
|
|
|
|
if (!EnableBackgroundIndex.getNumOccurrences())
|
|
|
|
EnableBackgroundIndex = false;
|
2019-04-18 21:46:40 +08:00
|
|
|
// Ensure background index makes progress.
|
2019-07-19 21:40:30 +08:00
|
|
|
else if (EnableBackgroundIndex)
|
|
|
|
BackgroundQueue::preventThreadStarvationInTests();
|
2018-02-06 18:47:30 +08:00
|
|
|
}
|
[clangd] Cleanup: stop passing around list of supported URI schemes.
Summary:
Instead of passing around a list of supported URI schemes in clangd, we
expose an interface to convert a path to URI using any compatible scheme
that has been registered. It favors customized schemes and falls
back to "file" when no other scheme works.
Changes in this patch are:
- URI::create(AbsPath, URISchemes) -> URI::create(AbsPath). The new API finds a
compatible scheme from the registry.
- Remove URISchemes option everywhere (ClangdServer, SymbolCollecter, FileIndex etc).
- Unit tests will use "unittest" by default.
- Move "test" scheme from ClangdLSPServer to ClangdMain.cpp, and only
register the test scheme when lit-test or enable-lit-scheme is set.
(The new flag is added to make lit protocol.test work; I wonder if there
is alternative here.)
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D54800
llvm-svn: 347467
2018-11-22 23:02:05 +08:00
|
|
|
if (Test || EnableTestScheme) {
|
|
|
|
static URISchemeRegistry::Add<TestScheme> X(
|
|
|
|
"test", "Test scheme for clangd lit tests.");
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
2019-05-28 17:20:57 +08:00
|
|
|
if (!Sync && WorkerThreadsCount == 0) {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::errs() << "A number of worker threads cannot be 0. Did you mean to "
|
2019-05-28 17:20:57 +08:00
|
|
|
"specify -sync?";
|
2017-08-14 16:45:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-28 17:20:57 +08:00
|
|
|
if (Sync) {
|
2018-02-25 15:21:16 +08:00
|
|
|
if (WorkerThreadsCount.getNumOccurrences())
|
2019-05-28 17:20:57 +08:00
|
|
|
llvm::errs() << "Ignoring -j because -sync is set.\n";
|
2017-08-14 16:45:47 +08:00
|
|
|
WorkerThreadsCount = 0;
|
2018-02-25 15:21:16 +08:00
|
|
|
}
|
2019-05-06 16:11:59 +08:00
|
|
|
if (FallbackStyle.getNumOccurrences())
|
|
|
|
clang::format::DefaultFallbackStyle = FallbackStyle.c_str();
|
2017-08-14 16:45:47 +08:00
|
|
|
|
2017-10-26 18:07:04 +08:00
|
|
|
// Validate command line arguments.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<llvm::raw_fd_ostream> InputMirrorStream;
|
2017-10-10 17:08:47 +08:00
|
|
|
if (!InputMirrorFile.empty()) {
|
|
|
|
std::error_code EC;
|
2018-06-08 03:58:58 +08:00
|
|
|
InputMirrorStream.emplace(InputMirrorFile, /*ref*/ EC,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
|
2017-10-10 17:08:47 +08:00
|
|
|
if (EC) {
|
|
|
|
InputMirrorStream.reset();
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::errs() << "Error while opening an input mirror file: "
|
|
|
|
<< EC.message();
|
2018-11-03 07:47:55 +08:00
|
|
|
} else {
|
|
|
|
InputMirrorStream->SetUnbuffered();
|
2017-10-10 17:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 23:04:59 +08:00
|
|
|
|
2018-02-14 11:20:07 +08:00
|
|
|
// Setup tracing facilities if CLANGD_TRACE is set. In practice enabling a
|
|
|
|
// trace flag in your editor's config is annoying, launching with
|
|
|
|
// `CLANGD_TRACE=trace.json vim` is easier.
|
2020-05-10 04:43:35 +08:00
|
|
|
llvm::Optional<llvm::raw_fd_ostream> TracerStream;
|
2017-12-14 23:04:59 +08:00
|
|
|
std::unique_ptr<trace::EventTracer> Tracer;
|
2020-05-10 04:43:35 +08:00
|
|
|
const char *JSONTraceFile = getenv("CLANGD_TRACE");
|
|
|
|
const char *MetricsCSVFile = getenv("CLANGD_METRICS");
|
|
|
|
const char *TracerFile = JSONTraceFile ? JSONTraceFile : MetricsCSVFile;
|
|
|
|
if (TracerFile) {
|
2017-11-02 17:21:51 +08:00
|
|
|
std::error_code EC;
|
2020-05-10 04:43:35 +08:00
|
|
|
TracerStream.emplace(TracerFile, /*ref*/ EC,
|
|
|
|
llvm::sys::fs::FA_Read | llvm::sys::fs::FA_Write);
|
2017-11-02 17:21:51 +08:00
|
|
|
if (EC) {
|
2020-05-10 04:43:35 +08:00
|
|
|
TracerStream.reset();
|
|
|
|
llvm::errs() << "Error while opening trace file " << TracerFile << ": "
|
2019-01-07 23:45:19 +08:00
|
|
|
<< EC.message();
|
2017-11-02 17:21:51 +08:00
|
|
|
} else {
|
2020-05-10 04:43:35 +08:00
|
|
|
Tracer = (TracerFile == JSONTraceFile)
|
|
|
|
? trace::createJSONTracer(*TracerStream, PrettyPrint)
|
|
|
|
: trace::createCSVMetricTracer(*TracerStream);
|
2017-11-02 17:21:51 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 17:08:47 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<trace::Session> TracingSession;
|
2017-12-14 23:04:59 +08:00
|
|
|
if (Tracer)
|
|
|
|
TracingSession.emplace(*Tracer);
|
|
|
|
|
2019-07-25 16:00:54 +08:00
|
|
|
// If a user ran `clangd` in a terminal without redirecting anything,
|
|
|
|
// it's somewhat likely they're confused about how to use clangd.
|
|
|
|
// Show them the help overview, which explains.
|
2020-10-01 22:14:31 +08:00
|
|
|
if (llvm::outs().is_displayed() && llvm::errs().is_displayed() &&
|
|
|
|
!CheckFile.getNumOccurrences())
|
2019-07-25 16:00:54 +08:00
|
|
|
llvm::errs() << Overview << "\n";
|
2018-08-28 21:15:50 +08:00
|
|
|
// Use buffered stream to stderr (we still flush each log message). Unbuffered
|
|
|
|
// stream can cause significant (non-deterministic) latency for the logger.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::errs().SetBuffered();
|
2020-06-10 15:36:58 +08:00
|
|
|
// Don't flush stdout when logging, this would be both slow and racy!
|
|
|
|
llvm::errs().tie(nullptr);
|
2019-01-07 23:45:19 +08:00
|
|
|
StreamLogger Logger(llvm::errs(), LogLevel);
|
|
|
|
LoggingSession LoggingSession(Logger);
|
2019-07-23 22:30:28 +08:00
|
|
|
// Write some initial logs before we start doing any real work.
|
|
|
|
log("{0}", clang::getClangToolFullVersion("clangd"));
|
2020-04-17 07:00:42 +08:00
|
|
|
log("PID: {0}", llvm::sys::Process::getProcessId());
|
2019-07-23 22:30:28 +08:00
|
|
|
{
|
|
|
|
SmallString<128> CWD;
|
|
|
|
if (auto Err = llvm::sys::fs::current_path(CWD))
|
|
|
|
log("Working directory unknown: {0}", Err.message());
|
|
|
|
else
|
|
|
|
log("Working directory: {0}", CWD);
|
|
|
|
}
|
|
|
|
for (int I = 0; I < argc; ++I)
|
|
|
|
log("argv[{0}]: {1}", I, argv[I]);
|
2019-07-25 15:54:48 +08:00
|
|
|
if (auto EnvFlags = llvm::sys::Process::GetEnv(FlagsEnvVar))
|
|
|
|
log("{0}: {1}", FlagsEnvVar, *EnvFlags);
|
2017-12-13 20:51:22 +08:00
|
|
|
|
2020-09-29 16:37:46 +08:00
|
|
|
ClangdLSPServer::Options Opts;
|
|
|
|
Opts.UseDirBasedCDB = (CompileArgsFrom == FilesystemCompileArgs);
|
|
|
|
|
2017-11-17 00:25:18 +08:00
|
|
|
switch (PCHStorage) {
|
|
|
|
case PCHStorageFlag::Memory:
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.StorePreamblesInMemory = true;
|
2017-11-17 00:25:18 +08:00
|
|
|
break;
|
|
|
|
case PCHStorageFlag::Disk:
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.StorePreamblesInMemory = false;
|
2017-11-17 00:25:18 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-19 23:43:35 +08:00
|
|
|
if (!ResourceDir.empty())
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.ResourceDir = ResourceDir;
|
2021-01-15 06:50:35 +08:00
|
|
|
Opts.BuildDynamicSymbolIndex = true;
|
2020-11-06 06:55:04 +08:00
|
|
|
std::vector<std::unique_ptr<SymbolIndex>> IdxStack;
|
2018-01-10 22:44:34 +08:00
|
|
|
std::unique_ptr<SymbolIndex> StaticIdx;
|
2020-07-27 20:03:28 +08:00
|
|
|
#if CLANGD_ENABLE_REMOTE
|
2020-07-27 17:21:55 +08:00
|
|
|
if (RemoteIndexAddress.empty() != ProjectRoot.empty()) {
|
|
|
|
llvm::errs() << "remote-index-address and project-path have to be "
|
|
|
|
"specified at the same time.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!RemoteIndexAddress.empty()) {
|
|
|
|
if (IndexFile.empty()) {
|
|
|
|
log("Connecting to remote index at {0}", RemoteIndexAddress);
|
|
|
|
} else {
|
|
|
|
elog("When enabling remote index, IndexFile should not be specified. "
|
|
|
|
"Only one can be used at time. Remote index will ignored.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-10 20:02:22 +08:00
|
|
|
Opts.BackgroundIndex = EnableBackgroundIndex;
|
2021-05-06 05:39:37 +08:00
|
|
|
Opts.ReferencesLimit = ReferencesLimit;
|
2021-03-08 17:23:56 +08:00
|
|
|
auto PAI = createProjectAwareIndex(loadExternalIndex, Sync);
|
2020-11-06 06:55:04 +08:00
|
|
|
if (StaticIdx) {
|
|
|
|
IdxStack.emplace_back(std::move(StaticIdx));
|
|
|
|
IdxStack.emplace_back(
|
|
|
|
std::make_unique<MergedIndex>(PAI.get(), IdxStack.back().get()));
|
|
|
|
Opts.StaticIndex = IdxStack.back().get();
|
|
|
|
} else {
|
|
|
|
Opts.StaticIndex = PAI.get();
|
|
|
|
}
|
2018-03-06 01:28:54 +08:00
|
|
|
Opts.AsyncThreadsCount = WorkerThreadsCount;
|
[clangd] Implement textDocument/foldingRange
Summary:
This patch introduces basic textDocument/foldingRange support. It relies on
textDocument/documentSymbols to collect all symbols and uses takes ranges
to create folds.
The next steps for textDocument/foldingRange support would be:
* Implementing FoldingRangeClientCapabilities and respecting respect client
preferences
* Specifying folding range kind
* Migrating from DocumentSymbol implementation to custom RecursiveASTVisitor flow that will allow more flexibility
* Supporting more folding range types: comments, PP conditional regions, includes and other code regions (e.g. public/private/protected sections of classes, control flow statement bodies)
Tested: (Neo)Vim (coc-clangd) and VSCode.
Related issue: https://github.com/clangd/clangd/issues/310
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: nridge, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82436
2020-07-14 15:28:38 +08:00
|
|
|
Opts.FoldingRanges = FoldingRanges;
|
2021-04-26 11:46:51 +08:00
|
|
|
Opts.InlayHints = InlayHints;
|
2020-12-22 15:44:20 +08:00
|
|
|
Opts.MemoryCleanup = getMemoryCleanupFunction();
|
2018-03-06 01:28:54 +08:00
|
|
|
|
2020-09-29 16:37:46 +08:00
|
|
|
Opts.CodeComplete.IncludeIneligibleResults = IncludeIneligibleResults;
|
|
|
|
Opts.CodeComplete.Limit = LimitResults;
|
2019-07-09 01:27:15 +08:00
|
|
|
if (CompletionStyle.getNumOccurrences())
|
2020-09-29 16:37:46 +08:00
|
|
|
Opts.CodeComplete.BundleOverloads = CompletionStyle != Detailed;
|
|
|
|
Opts.CodeComplete.ShowOrigins = ShowOrigins;
|
|
|
|
Opts.CodeComplete.InsertIncludes = HeaderInsertion;
|
2018-07-30 03:12:42 +08:00
|
|
|
if (!HeaderInsertionDecorators) {
|
2020-09-29 16:37:46 +08:00
|
|
|
Opts.CodeComplete.IncludeIndicator.Insert.clear();
|
|
|
|
Opts.CodeComplete.IncludeIndicator.NoInsert.clear();
|
2018-07-30 03:12:42 +08:00
|
|
|
}
|
2020-09-29 16:37:46 +08:00
|
|
|
Opts.CodeComplete.EnableFunctionArgSnippets = EnableFunctionArgSnippets;
|
|
|
|
Opts.CodeComplete.RunParser = CodeCompletionParse;
|
|
|
|
Opts.CodeComplete.RankingModel = RankingModel;
|
2018-03-06 01:28:54 +08:00
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
RealThreadsafeFS TFS;
|
2020-07-15 02:44:59 +08:00
|
|
|
std::vector<std::unique_ptr<config::Provider>> ProviderStack;
|
2020-07-06 23:23:08 +08:00
|
|
|
std::unique_ptr<config::Provider> Config;
|
|
|
|
if (EnableConfig) {
|
|
|
|
ProviderStack.push_back(
|
|
|
|
config::Provider::fromAncestorRelativeYAMLFiles(".clangd", TFS));
|
|
|
|
llvm::SmallString<256> UserConfig;
|
|
|
|
if (llvm::sys::path::user_config_directory(UserConfig)) {
|
|
|
|
llvm::sys::path::append(UserConfig, "clangd", "config.yaml");
|
|
|
|
vlog("User config file is {0}", UserConfig);
|
2021-04-15 18:52:58 +08:00
|
|
|
ProviderStack.push_back(config::Provider::fromYAMLFile(
|
|
|
|
UserConfig, /*Directory=*/"", TFS, /*Trusted=*/true));
|
2020-07-06 23:23:08 +08:00
|
|
|
} else {
|
|
|
|
elog("Couldn't determine user config file, not loading");
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 19:07:25 +08:00
|
|
|
ProviderStack.push_back(std::make_unique<FlagsConfigProvider>());
|
|
|
|
std::vector<const config::Provider *> ProviderPointers;
|
|
|
|
for (const auto &P : ProviderStack)
|
|
|
|
ProviderPointers.push_back(P.get());
|
|
|
|
Config = config::Provider::combine(std::move(ProviderPointers));
|
|
|
|
Opts.ConfigProvider = Config.get();
|
2020-07-06 23:23:08 +08:00
|
|
|
|
2019-01-22 17:39:05 +08:00
|
|
|
// Create an empty clang-tidy option.
|
2020-11-26 02:35:34 +08:00
|
|
|
TidyProvider ClangTidyOptProvider;
|
2019-02-06 17:10:47 +08:00
|
|
|
if (EnableClangTidy) {
|
2020-11-26 02:35:34 +08:00
|
|
|
std::vector<TidyProvider> Providers;
|
|
|
|
Providers.reserve(4 + EnableConfig);
|
|
|
|
Providers.push_back(provideEnvironment());
|
|
|
|
Providers.push_back(provideClangTidyFiles(TFS));
|
|
|
|
if (EnableConfig)
|
|
|
|
Providers.push_back(provideClangdConfig());
|
2021-02-13 22:14:21 +08:00
|
|
|
Providers.push_back(provideDefaultChecks());
|
2020-11-26 02:35:34 +08:00
|
|
|
Providers.push_back(disableUnusableChecks());
|
|
|
|
ClangTidyOptProvider = combine(std::move(Providers));
|
|
|
|
Opts.ClangTidyProvider = ClangTidyOptProvider;
|
2019-02-06 17:10:47 +08:00
|
|
|
}
|
2019-06-26 15:45:27 +08:00
|
|
|
Opts.QueryDriverGlobs = std::move(QueryDriverGlobs);
|
2019-07-12 16:50:20 +08:00
|
|
|
Opts.TweakFilter = [&](const Tweak &T) {
|
|
|
|
if (T.hidden() && !HiddenFeatures)
|
|
|
|
return false;
|
|
|
|
if (TweakList.getNumOccurrences())
|
|
|
|
return llvm::is_contained(TweakList, T.id());
|
|
|
|
return true;
|
|
|
|
};
|
2019-03-28 01:47:49 +08:00
|
|
|
if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding)
|
2020-09-30 16:56:43 +08:00
|
|
|
Opts.Encoding = ForceOffsetEncoding;
|
2020-02-19 22:37:36 +08:00
|
|
|
|
2020-10-01 22:14:31 +08:00
|
|
|
if (CheckFile.getNumOccurrences()) {
|
|
|
|
llvm::SmallString<256> Path;
|
|
|
|
llvm::sys::fs::real_path(CheckFile, Path, /*expand_tilde=*/true);
|
|
|
|
log("Entering check mode (no LSP server)");
|
2021-03-19 04:14:02 +08:00
|
|
|
uint32_t Begin = 0, End = std::numeric_limits<uint32_t>::max();
|
|
|
|
if (!CheckFileLines.empty()) {
|
|
|
|
StringRef RangeStr(CheckFileLines);
|
|
|
|
bool ParseError = RangeStr.consumeInteger(0, Begin);
|
|
|
|
if (RangeStr.empty()) {
|
|
|
|
End = Begin;
|
|
|
|
} else {
|
|
|
|
ParseError |= !RangeStr.consume_front("-");
|
|
|
|
ParseError |= RangeStr.consumeInteger(0, End);
|
|
|
|
}
|
|
|
|
if (ParseError || !RangeStr.empty()) {
|
|
|
|
elog("Invalid --check-line specified. Use Begin-End format, e.g. 3-17");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto ShouldCheckLine = [&](const Position &Pos) {
|
|
|
|
uint32_t Line = Pos.line + 1; // Position::line is 0-based.
|
|
|
|
return Line >= Begin && Line <= End;
|
|
|
|
};
|
2021-06-03 01:45:11 +08:00
|
|
|
// For now code completion is enabled any time the range is limited via
|
|
|
|
// --check-lines. If it turns out to be to slow, we can introduce a
|
|
|
|
// dedicated flag for that instead.
|
|
|
|
return check(Path, ShouldCheckLine, TFS, Opts,
|
|
|
|
/*EnableCodeCompletion=*/!CheckFileLines.empty())
|
2020-10-01 22:14:31 +08:00
|
|
|
? 0
|
|
|
|
: static_cast<int>(ErrorResultCode::CheckFailed);
|
|
|
|
}
|
2021-03-19 04:14:02 +08:00
|
|
|
if (!CheckFileLines.empty()) {
|
|
|
|
elog("--check-lines requires --check");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-10-01 22:14:31 +08:00
|
|
|
|
2020-09-24 17:55:50 +08:00
|
|
|
// Initialize and run ClangdLSPServer.
|
|
|
|
// Change stdin to binary to not lose \r\n on windows.
|
|
|
|
llvm::sys::ChangeStdinToBinary();
|
|
|
|
std::unique_ptr<Transport> TransportLayer;
|
|
|
|
if (getenv("CLANGD_AS_XPC_SERVICE")) {
|
|
|
|
#if CLANGD_BUILD_XPC
|
|
|
|
log("Starting LSP over XPC service");
|
|
|
|
TransportLayer = newXPCTransport();
|
|
|
|
#else
|
|
|
|
llvm::errs() << "This clangd binary wasn't built with XPC support.\n";
|
2020-10-01 22:14:31 +08:00
|
|
|
return static_cast<int>(ErrorResultCode::CantRunAsXPCService);
|
2020-09-24 17:55:50 +08:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
log("Starting LSP over stdin/stdout");
|
|
|
|
TransportLayer = newJSONTransport(
|
|
|
|
stdin, llvm::outs(),
|
|
|
|
InputMirrorStream ? InputMirrorStream.getPointer() : nullptr,
|
|
|
|
PrettyPrint, InputStyle);
|
|
|
|
}
|
|
|
|
if (!PathMappingsArg.empty()) {
|
|
|
|
auto Mappings = parsePathMappings(PathMappingsArg);
|
|
|
|
if (!Mappings) {
|
|
|
|
elog("Invalid -path-mappings: {0}", Mappings.takeError());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
TransportLayer = createPathMappingTransport(std::move(TransportLayer),
|
|
|
|
std::move(*Mappings));
|
|
|
|
}
|
2020-06-04 05:50:26 +08:00
|
|
|
|
2020-09-29 16:37:46 +08:00
|
|
|
ClangdLSPServer LSPServer(*TransportLayer, TFS, Opts);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::set_thread_name("clangd.main");
|
[clangd] abort if shutdown takes more than a minute.
Summary:
A certain class of bug (e.g. infloop on an AST worker thread) currently means
clangd never terminates, even if the editor shuts down the protocol and closes
our stdin, and the main thread recognizes that.
Instead, let's wait 60 seconds for threads to finish cleanly, and then crash
if they haven't.
(Obviously, we should still fix these bugs).
Reviewers: kadircet
Subscribers: MaskRay, jkorous, arphaman, jfb, usaxena95, cfe-commits, ilya-biryukov
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69329
2019-10-23 17:11:18 +08:00
|
|
|
int ExitCode = LSPServer.run()
|
|
|
|
? 0
|
|
|
|
: static_cast<int>(ErrorResultCode::NoShutdownRequest);
|
|
|
|
log("LSP finished, exiting with status {0}", ExitCode);
|
|
|
|
|
|
|
|
// There may still be lingering background threads (e.g. slow requests
|
|
|
|
// whose results will be dropped, background index shutting down).
|
|
|
|
//
|
|
|
|
// These should terminate quickly, and ~ClangdLSPServer blocks on them.
|
|
|
|
// However if a bug causes them to run forever, we want to ensure the process
|
|
|
|
// eventually exits. As clangd isn't directly user-facing, an editor can
|
|
|
|
// "leak" clangd processes. Crashing in this case contains the damage.
|
2019-11-26 02:51:07 +08:00
|
|
|
abortAfterTimeout(std::chrono::minutes(5));
|
[clangd] abort if shutdown takes more than a minute.
Summary:
A certain class of bug (e.g. infloop on an AST worker thread) currently means
clangd never terminates, even if the editor shuts down the protocol and closes
our stdin, and the main thread recognizes that.
Instead, let's wait 60 seconds for threads to finish cleanly, and then crash
if they haven't.
(Obviously, we should still fix these bugs).
Reviewers: kadircet
Subscribers: MaskRay, jkorous, arphaman, jfb, usaxena95, cfe-commits, ilya-biryukov
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69329
2019-10-23 17:11:18 +08:00
|
|
|
|
|
|
|
return ExitCode;
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|