36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/SmallString.h"
38#include "llvm/ADT/StringExtras.h"
39#include "llvm/ADT/StringRef.h"
40#include "llvm/Support/FileSystem.h"
41#include "llvm/Support/FormatVariadic.h"
42#include "llvm/Support/Path.h"
43#include "llvm/Support/Regex.h"
44#include "llvm/Support/SMLoc.h"
45#include "llvm/Support/SourceMgr.h"
58llvm::StringRef configRelative(llvm::StringRef
Path,
59 llvm::StringRef FragmentDir) {
60 if (FragmentDir.empty())
62 if (!
Path.consume_front(FragmentDir))
63 return llvm::StringRef();
67struct CompiledFragmentImpl {
73 std::vector<llvm::unique_function<bool(
const Params &)
const>>
Conditions;
76 std::vector<llvm::unique_function<void(
const Params &, Config &)
const>>
79 bool operator()(
const Params &P, Config &
C)
const {
82 dlog(
"Config fragment {0}: condition not met",
this);
86 dlog(
"Config fragment {0}: applying {1} rules",
this,
Apply.size());
87 for (
const auto &A :
Apply)
94struct FragmentCompiler {
98 CompiledFragmentImpl &
Out;
100 llvm::SourceMgr *SourceMgr;
105 std::optional<llvm::Regex>
106 compileRegex(
const Located<std::string> &
Text,
107 llvm::Regex::RegexFlags
Flags = llvm::Regex::NoFlags) {
108 std::string Anchored =
"^(" + *
Text +
")$";
109 llvm::Regex Result(Anchored,
Flags);
110 std::string RegexError;
111 if (!Result.isValid(RegexError)) {
112 diag(
Error,
"Invalid regex " + Anchored +
": " + RegexError,
Text.Range);
115 return std::move(Result);
118 std::optional<std::string> makeAbsolute(Located<std::string>
Path,
119 llvm::StringLiteral Description,
120 llvm::sys::path::Style Style) {
121 if (llvm::sys::path::is_absolute(*
Path))
126 "{0} must be an absolute path, because this fragment is not "
127 "associated with any directory.",
133 llvm::SmallString<256> AbsPath = llvm::StringRef(*
Path);
134 llvm::sys::fs::make_absolute(FragmentDirectory, AbsPath);
135 llvm::sys::path::native(AbsPath, Style);
136 return AbsPath.str().str();
140 template <
typename T>
class EnumSwitch {
141 FragmentCompiler &Outer;
142 llvm::StringRef EnumName;
143 const Located<std::string> &Input;
144 std::optional<T> Result;
145 llvm::SmallVector<llvm::StringLiteral> ValidValues;
148 EnumSwitch(llvm::StringRef EnumName,
const Located<std::string> &In,
149 FragmentCompiler &Outer)
150 : Outer(Outer), EnumName(EnumName), Input(In) {}
152 EnumSwitch &map(llvm::StringLiteral
Name, T
Value) {
153 assert(!llvm::is_contained(ValidValues,
Name) &&
"Duplicate value!");
154 ValidValues.push_back(
Name);
155 if (!Result && *Input ==
Name)
160 std::optional<T> value() {
164 llvm::formatv(
"Invalid {0} value '{1}'. Valid values are {2}.",
165 EnumName, *Input, llvm::join(ValidValues,
", "))
179 template <
typename T>
180 EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
181 const Located<std::string> &In) {
182 return EnumSwitch<T>(EnumName, In, *
this);
185 void compile(Fragment &&F) {
187 if (!F.Source.Directory.empty()) {
192 compile(std::move(F.If));
193 compile(std::move(F.CompileFlags));
194 compile(std::move(F.Index));
195 compile(std::move(F.Diagnostics));
196 compile(std::move(F.Completion));
197 compile(std::move(F.Hover));
198 compile(std::move(F.InlayHints));
199 compile(std::move(F.SemanticTokens));
200 compile(std::move(F.Style));
203 void compile(Fragment::IfBlock &&F) {
204 if (F.HasUnrecognizedCondition)
205 Out.Conditions.push_back([&](
const Params &) {
return false; });
207#ifdef CLANGD_PATH_CASE_INSENSITIVE
208 llvm::Regex::RegexFlags
Flags = llvm::Regex::IgnoreCase;
210 llvm::Regex::RegexFlags
Flags = llvm::Regex::NoFlags;
213 auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
214 for (
auto &
Entry : F.PathMatch) {
216 PathMatch->push_back(std::move(*RE));
218 if (!PathMatch->empty()) {
219 Out.Conditions.push_back(
220 [PathMatch(std::move(PathMatch)),
221 FragmentDir(FragmentDirectory)](
const Params &P) {
224 llvm::StringRef
Path = configRelative(P.Path, FragmentDir);
228 return llvm::any_of(*PathMatch, [&](
const llvm::Regex &RE) {
229 return RE.match(
Path);
234 auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
235 for (
auto &
Entry : F.PathExclude) {
237 PathExclude->push_back(std::move(*RE));
239 if (!PathExclude->empty()) {
240 Out.Conditions.push_back(
241 [PathExclude(std::move(PathExclude)),
242 FragmentDir(FragmentDirectory)](
const Params &P) {
245 llvm::StringRef
Path = configRelative(P.Path, FragmentDir);
249 return llvm::none_of(*PathExclude, [&](
const llvm::Regex &RE) {
250 return RE.match(
Path);
256 void compile(Fragment::CompileFlagsBlock &&F) {
259 [Compiler(std::move(**F.Compiler))](
const Params &, Config &
C) {
260 C.CompileFlags.Edits.push_back(
261 [Compiler](std::vector<std::string> &Args) {
263 Args.front() = Compiler;
267 if (!F.Remove.empty()) {
268 auto Remove = std::make_shared<ArgStripper>();
269 for (
auto &A : F.Remove)
271 Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
272 std::move(Remove)))](
const Params &, Config &
C) {
273 C.CompileFlags.Edits.push_back(
274 [Remove](std::vector<std::string> &
Args) {
275 Remove->process(
Args);
280 if (!F.Add.empty()) {
281 std::vector<std::string> Add;
282 for (
auto &A : F.Add)
283 Add.push_back(std::move(*A));
284 Out.Apply.push_back([Add(std::move(Add))](
const Params &, Config &
C) {
285 C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &
Args) {
287 auto It = llvm::find(
Args,
"--");
288 Args.insert(It, Add.begin(), Add.end());
293 if (F.CompilationDatabase) {
294 std::optional<Config::CDBSearchSpec> Spec;
295 if (**F.CompilationDatabase ==
"Ancestors") {
298 }
else if (**F.CompilationDatabase ==
"None") {
303 makeAbsolute(*F.CompilationDatabase,
"CompilationDatabase",
304 llvm::sys::path::Style::native)) {
307 llvm::StringRef Rel = llvm::sys::path::relative_path(*
Path);
308 if (!Rel.empty() && llvm::sys::path::is_separator(Rel.back()))
313 Spec->FixedCDBPath = std::move(
Path);
318 [Spec(std::move(*Spec))](
const Params &, Config &
C) {
319 C.CompileFlags.CDBSearch = Spec;
324 void compile(Fragment::IndexBlock &&F) {
327 compileEnum<Config::BackgroundPolicy>(
"Background", *F.Background)
332 [Val](
const Params &, Config &
C) {
C.Index.Background = *Val; });
335 compile(std::move(**F.External), F.External->Range);
336 if (F.StandardLibrary)
338 [Val(**F.StandardLibrary)](
const Params &, Config &
C) {
339 C.Index.StandardLibrary = Val;
343 void compile(Fragment::IndexBlock::ExternalBlock &&External,
344 llvm::SMRange BlockRange) {
345 if (External.Server && !
Trusted) {
347 "Remote index may not be specified by untrusted configuration. "
348 "Copy this into user config to use it.",
349 External.Server->Range);
352#ifndef CLANGD_ENABLE_REMOTE
353 if (External.Server) {
354 elog(
"Clangd isn't compiled with remote index support, ignoring Server: "
357 External.Server.reset();
361 unsigned SourceCount = External.File.has_value() +
362 External.Server.has_value() + *External.IsNone;
363 if (SourceCount != 1) {
364 diag(
Error,
"Exactly one of File, Server or None must be set.",
368 Config::ExternalIndexSpec Spec;
369 if (External.Server) {
371 Spec.Location = std::move(**External.Server);
372 }
else if (External.File) {
374 auto AbsPath = makeAbsolute(std::move(*External.File),
"File",
375 llvm::sys::path::Style::native);
378 Spec.Location = std::move(*AbsPath);
380 assert(*External.IsNone);
385 if (!External.MountPoint)
387 if ((**External.MountPoint).empty()) {
388 diag(
Error,
"A mountpoint is required.", BlockRange);
391 auto AbsPath = makeAbsolute(std::move(*External.MountPoint),
"MountPoint",
392 llvm::sys::path::Style::posix);
395 Spec.MountPoint = std::move(*AbsPath);
397 Out.Apply.push_back([Spec(std::move(Spec))](
const Params &P, Config &
C) {
399 C.Index.External = Spec;
403 llvm::sys::path::Style::posix))
405 C.Index.External = Spec;
413 void compile(Fragment::DiagnosticsBlock &&F) {
414 std::vector<std::string> Normalized;
415 for (
const auto &Suppressed : F.Suppress) {
416 if (*Suppressed ==
"*") {
417 Out.Apply.push_back([&](
const Params &, Config &
C) {
418 C.Diagnostics.SuppressAll =
true;
419 C.Diagnostics.Suppress.clear();
425 if (!Normalized.empty())
427 [Normalized(std::move(Normalized))](
const Params &, Config &
C) {
428 if (
C.Diagnostics.SuppressAll)
430 for (llvm::StringRef N : Normalized)
431 C.Diagnostics.Suppress.insert(N);
434 if (F.UnusedIncludes) {
435 auto Val = compileEnum<Config::IncludesPolicy>(
"UnusedIncludes",
440 if (!Val && **F.UnusedIncludes ==
"Experiment") {
442 "Experiment is deprecated for UnusedIncludes, use Strict instead.",
443 F.UnusedIncludes->Range);
447 Out.Apply.push_back([Val](
const Params &, Config &
C) {
448 C.Diagnostics.UnusedIncludes = *Val;
453 if (F.MissingIncludes)
454 if (
auto Val = compileEnum<Config::IncludesPolicy>(
"MissingIncludes",
459 Out.Apply.push_back([Val](
const Params &, Config &
C) {
460 C.Diagnostics.MissingIncludes = *Val;
463 compile(std::move(F.Includes));
464 compile(std::move(F.ClangTidy));
467 void compile(Fragment::StyleBlock &&F) {
468 if (!F.FullyQualifiedNamespaces.empty()) {
469 std::vector<std::string> FullyQualifiedNamespaces;
470 for (
auto &N : F.FullyQualifiedNamespaces) {
475 FullyQualifiedNamespaces.push_back(
Namespace.str());
477 Out.Apply.push_back([FullyQualifiedNamespaces(
478 std::move(FullyQualifiedNamespaces))](
479 const Params &, Config &
C) {
480 C.Style.FullyQualifiedNamespaces.insert(
481 C.Style.FullyQualifiedNamespaces.begin(),
482 FullyQualifiedNamespaces.begin(), FullyQualifiedNamespaces.end());
487 void appendTidyCheckSpec(std::string &CurSpec,
488 const Located<std::string> &Arg,
bool IsPositive) {
489 StringRef Str = StringRef(*Arg).trim();
492 if (Str.starts_with(
"-") || Str.contains(
',')) {
493 diag(
Error,
"Invalid clang-tidy check name", Arg.Range);
496 if (!Str.contains(
'*')) {
499 llvm::formatv(
"clang-tidy check '{0}' was not found", Str).str(),
504 if (!Fast.has_value()) {
507 "Latency of clang-tidy check '{0}' is not known. "
508 "It will only run if ClangTidy.FastCheckFilter is Loose or None",
515 "clang-tidy check '{0}' is slow. "
516 "It will only run if ClangTidy.FastCheckFilter is None",
528 void compile(Fragment::DiagnosticsBlock::ClangTidyBlock &&F) {
530 for (
auto &CheckGlob : F.Add)
531 appendTidyCheckSpec(Checks, CheckGlob,
true);
533 for (
auto &CheckGlob : F.Remove)
534 appendTidyCheckSpec(Checks, CheckGlob,
false);
538 [Checks = std::move(Checks)](
const Params &, Config &
C) {
539 C.Diagnostics.ClangTidy.Checks.append(
541 C.Diagnostics.ClangTidy.Checks.empty() ? 1 : 0,
544 if (!F.CheckOptions.empty()) {
545 std::vector<std::pair<std::string, std::string>> CheckOptions;
546 for (
auto &Opt : F.CheckOptions)
547 CheckOptions.emplace_back(std::move(*Opt.first),
548 std::move(*Opt.second));
550 [CheckOptions = std::move(CheckOptions)](
const Params &, Config &
C) {
551 for (
auto &StringPair : CheckOptions)
552 C.Diagnostics.ClangTidy.CheckOptions.insert_or_assign(
553 StringPair.first, StringPair.second);
556 if (F.FastCheckFilter.has_value())
557 if (
auto Val = compileEnum<Config::FastCheckPolicy>(
"FastCheckFilter",
563 Out.Apply.push_back([Val](
const Params &, Config &
C) {
564 C.Diagnostics.ClangTidy.FastCheckFilter = *Val;
568 void compile(Fragment::DiagnosticsBlock::IncludesBlock &&F) {
569#ifdef CLANGD_PATH_CASE_INSENSITIVE
570 static llvm::Regex::RegexFlags
Flags = llvm::Regex::IgnoreCase;
572 static llvm::Regex::RegexFlags
Flags = llvm::Regex::NoFlags;
574 std::shared_ptr<std::vector<llvm::Regex>> Filters;
575 if (!F.IgnoreHeader.empty()) {
576 Filters = std::make_shared<std::vector<llvm::Regex>>();
577 for (
auto &HeaderPattern : F.IgnoreHeader) {
579 std::string AnchoredPattern =
"(" + *HeaderPattern +
")$";
580 llvm::Regex CompiledRegex(AnchoredPattern,
Flags);
581 std::string RegexError;
582 if (!CompiledRegex.isValid(RegexError)) {
584 llvm::formatv(
"Invalid regular expression '{0}': {1}",
585 *HeaderPattern, RegexError)
587 HeaderPattern.Range);
590 Filters->push_back(std::move(CompiledRegex));
596 std::optional<bool> AnalyzeAngledIncludes;
597 if (F.AnalyzeAngledIncludes.has_value())
598 AnalyzeAngledIncludes = **F.AnalyzeAngledIncludes;
599 if (!Filters && !AnalyzeAngledIncludes.has_value())
601 Out.Apply.push_back([Filters = std::move(Filters),
602 AnalyzeAngledIncludes](
const Params &, Config &
C) {
604 auto Filter = [Filters](llvm::StringRef
Path) {
605 for (
auto &Regex : *Filters)
606 if (Regex.match(
Path))
610 C.Diagnostics.Includes.IgnoreHeader.emplace_back(std::move(Filter));
612 if (AnalyzeAngledIncludes.has_value())
613 C.Diagnostics.Includes.AnalyzeAngledIncludes = *AnalyzeAngledIncludes;
617 void compile(Fragment::CompletionBlock &&F) {
620 [AllScopes(**F.AllScopes)](
const Params &, Config &
C) {
621 C.Completion.AllScopes = AllScopes;
624 if (F.ArgumentLists) {
626 compileEnum<Config::ArgumentListsPolicy>(
"ArgumentLists",
629 .map(
"OpenDelimiter",
632 .map(
"FullPlaceholders",
635 Out.Apply.push_back([Val](
const Params &, Config &
C) {
636 C.Completion.ArgumentLists = *Val;
641 void compile(Fragment::HoverBlock &&F) {
643 Out.Apply.push_back([ShowAKA(**F.ShowAKA)](
const Params &, Config &
C) {
644 C.Hover.ShowAKA = ShowAKA;
649 void compile(Fragment::InlayHintsBlock &&F) {
651 Out.Apply.push_back([
Value(**F.Enabled)](
const Params &, Config &
C) {
652 C.InlayHints.Enabled = Value;
654 if (F.ParameterNames)
656 [
Value(**F.ParameterNames)](
const Params &, Config &
C) {
657 C.InlayHints.Parameters = Value;
660 Out.Apply.push_back([
Value(**F.DeducedTypes)](
const Params &, Config &
C) {
661 C.InlayHints.DeducedTypes = Value;
664 Out.Apply.push_back([
Value(**F.Designators)](
const Params &, Config &
C) {
665 C.InlayHints.Designators = Value;
668 Out.Apply.push_back([
Value(**F.BlockEnd)](
const Params &, Config &
C) {
669 C.InlayHints.BlockEnd = Value;
671 if (F.DefaultArguments)
673 [
Value(**F.DefaultArguments)](
const Params &, Config &
C) {
674 C.InlayHints.DefaultArguments = Value;
678 [
Value(**F.TypeNameLimit)](
const Params &, Config &
C) {
679 C.InlayHints.TypeNameLimit = Value;
683 void compile(Fragment::SemanticTokensBlock &&F) {
684 if (!F.DisabledKinds.empty()) {
685 std::vector<std::string> DisabledKinds;
686 for (
auto &
Kind : F.DisabledKinds)
687 DisabledKinds.push_back(std::move(*
Kind));
690 [DisabledKinds(std::move(DisabledKinds))](
const Params &, Config &
C) {
691 for (
auto &
Kind : DisabledKinds) {
692 auto It = llvm::find(
C.SemanticTokens.DisabledKinds,
Kind);
693 if (It ==
C.SemanticTokens.DisabledKinds.end())
694 C.SemanticTokens.DisabledKinds.push_back(std::move(
Kind));
698 if (!F.DisabledModifiers.empty()) {
699 std::vector<std::string> DisabledModifiers;
700 for (
auto &
Kind : F.DisabledModifiers)
701 DisabledModifiers.push_back(std::move(*
Kind));
703 Out.Apply.push_back([DisabledModifiers(std::move(DisabledModifiers))](
704 const Params &, Config &
C) {
705 for (
auto &
Kind : DisabledModifiers) {
706 auto It = llvm::find(
C.SemanticTokens.DisabledModifiers,
Kind);
707 if (It ==
C.SemanticTokens.DisabledModifiers.end())
708 C.SemanticTokens.DisabledModifiers.push_back(std::move(
Kind));
714 constexpr static llvm::SourceMgr::DiagKind
Error = llvm::SourceMgr::DK_Error;
715 constexpr static llvm::SourceMgr::DiagKind
Warning =
716 llvm::SourceMgr::DK_Warning;
717 void diag(llvm::SourceMgr::DiagKind
Kind, llvm::StringRef Message,
718 llvm::SMRange Range) {
719 if (
Range.isValid() && SourceMgr !=
nullptr)
730 std::pair<unsigned, unsigned> LineCol = {0, 0};
731 if (
auto *SM = Source.Manager.get()) {
732 unsigned BufID = SM->getMainFileID();
733 LineCol = SM->getLineAndColumn(Source.Location, BufID);
734 ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
738 auto Result = std::make_shared<CompiledFragmentImpl>();
739 vlog(
"Config fragment: compiling {0}:{1} -> {2} (trusted={3})",
ConfigFile,
740 LineCol.first, Result.get(), Source.Trusted);
742 FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*
this));
744 return [Result(std::move(Result))](
const Params &P,
Config &
C) {
745 return (*Result)(P,
C);
llvm::SmallString< 256U > Name
static cl::opt< std::string > ConfigFile("config-file", desc(R"(
Specify the path of .clang-tidy or custom config file:
e.g. --config-file=/some/path/myTidyConfigFile
This option internally works exactly the same way as
--config option after reading specified config file.
Use either --config-file or --config, not both.
)"), cl::init(""), cl::cat(ClangTidyCategory))
static constexpr llvm::SourceMgr::DiagKind Error
CompiledFragmentImpl & Out
DiagnosticCallback Diagnostic
std::vector< llvm::unique_function< void(const Params &, Config &) const > > Apply
std::vector< llvm::unique_function< bool(const Params &) const > > Conditions
std::string FragmentDirectory
CharSourceRange Range
SourceRange for the file name.
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
Records an event whose duration is the lifetime of the Span object.
std::function< bool(const Params &, Config &)> CompiledFragment
A chunk of configuration that has been fully analyzed and is ready to apply.
llvm::function_ref< void(const llvm::SMDiagnostic &)> DiagnosticCallback
Used to report problems in parsing or interpreting a config.
@ Warning
A warning message.
std::string Path
A typedef to represent a file path.
bool isRegisteredTidyCheck(llvm::StringRef Check)
Returns if Check is a registered clang-tidy check.
void vlog(const char *Fmt, Ts &&... Vals)
bool pathStartsWith(PathRef Ancestor, PathRef Path, llvm::sys::path::Style Style)
Checks if Ancestor is a proper ancestor of Path.
std::optional< bool > isFastTidyCheck(llvm::StringRef Check)
Returns if Check is known-fast, known-slow, or its speed is unknown.
llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code)
Take a user-specified diagnostic code, and convert it to a normalized form stored in the config and c...
void elog(const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
@ None
nothing, no argument list and also NO Delimiters "()" or "<>".
@ Delimiters
empty pair of delimiters "()" or "<>".
@ OpenDelimiter
open, only opening delimiter "(" or "<".
@ FullPlaceholders
full name of both type and variable.
@ Strict
Diagnose missing and unused includes.
Describes the context used to evaluate configuration fragments.