1 //===--- ConfigCompile.cpp - Translating Fragments into Config ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Fragments are applied to Configs in two steps:
11 // 1. (When the fragment is first loaded)
12 // FragmentCompiler::compile() traverses the Fragment and creates
13 // function objects that know how to apply the configuration.
14 // 2. (Every time a config is required)
15 // CompiledFragment() executes these functions to populate the Config.
17 // Work could be split between these steps in different ways. We try to
18 // do as much work as possible in the first step. For example, regexes are
19 // compiled in stage 1 and captured by the apply function. This is because:
21 // - it's more efficient, as the work done in stage 1 must only be done once
22 // - problems can be reported in stage 1, in stage 2 we must silently recover
24 //===----------------------------------------------------------------------===//
26 #include "CompileCommands.h"
28 #include "ConfigFragment.h"
29 #include "ConfigProvider.h"
30 #include "Diagnostics.h"
32 #include "TidyProvider.h"
33 #include "support/Logger.h"
34 #include "support/Path.h"
35 #include "support/Trace.h"
36 #include "llvm/ADT/None.h"
37 #include "llvm/ADT/Optional.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/ADT/SmallString.h"
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/Support/FileSystem.h"
42 #include "llvm/Support/FormatVariadic.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/Regex.h"
45 #include "llvm/Support/SMLoc.h"
46 #include "llvm/Support/SourceMgr.h"
57 // Returns an empty stringref if Path is not under FragmentDir. Returns Path
58 // as-is when FragmentDir is empty.
59 llvm::StringRef
configRelative(llvm::StringRef Path
,
60 llvm::StringRef FragmentDir
) {
61 if (FragmentDir
.empty())
63 if (!Path
.consume_front(FragmentDir
))
64 return llvm::StringRef();
65 return Path
.empty() ? "." : Path
;
68 struct CompiledFragmentImpl
{
69 // The independent conditions to check before using settings from this config.
70 // The following fragment has *two* conditions:
71 // If: { Platform: [mac, linux], PathMatch: foo/.* }
72 // All of them must be satisfied: the platform and path conditions are ANDed.
73 // The OR logic for the platform condition is implemented inside the function.
74 std::vector
<llvm::unique_function
<bool(const Params
&) const>> Conditions
;
75 // Mutations that this fragment will apply to the configuration.
76 // These are invoked only if the conditions are satisfied.
77 std::vector
<llvm::unique_function
<void(const Params
&, Config
&) const>>
80 bool operator()(const Params
&P
, Config
&C
) const {
81 for (const auto &C
: Conditions
) {
83 dlog("Config fragment {0}: condition not met", this);
87 dlog("Config fragment {0}: applying {1} rules", this, Apply
.size());
88 for (const auto &A
: Apply
)
94 // Wrapper around condition compile() functions to reduce arg-passing.
95 struct FragmentCompiler
{
96 FragmentCompiler(CompiledFragmentImpl
&Out
, DiagnosticCallback D
,
98 : Out(Out
), Diagnostic(D
), SourceMgr(SM
) {}
99 CompiledFragmentImpl
&Out
;
100 DiagnosticCallback Diagnostic
;
101 llvm::SourceMgr
*SourceMgr
;
102 // Normalized Fragment::SourceInfo::Directory.
103 std::string FragmentDirectory
;
104 bool Trusted
= false;
106 llvm::Optional
<llvm::Regex
>
107 compileRegex(const Located
<std::string
> &Text
,
108 llvm::Regex::RegexFlags Flags
= llvm::Regex::NoFlags
) {
109 std::string Anchored
= "^(" + *Text
+ ")$";
110 llvm::Regex
Result(Anchored
, Flags
);
111 std::string RegexError
;
112 if (!Result
.isValid(RegexError
)) {
113 diag(Error
, "Invalid regex " + Anchored
+ ": " + RegexError
, Text
.Range
);
119 llvm::Optional
<std::string
> makeAbsolute(Located
<std::string
> Path
,
120 llvm::StringLiteral Description
,
121 llvm::sys::path::Style Style
) {
122 if (llvm::sys::path::is_absolute(*Path
))
124 if (FragmentDirectory
.empty()) {
127 "{0} must be an absolute path, because this fragment is not "
128 "associated with any directory.",
134 llvm::SmallString
<256> AbsPath
= llvm::StringRef(*Path
);
135 llvm::sys::fs::make_absolute(FragmentDirectory
, AbsPath
);
136 llvm::sys::path::native(AbsPath
, Style
);
137 return AbsPath
.str().str();
140 // Helper with similar API to StringSwitch, for parsing enum values.
141 template <typename T
> class EnumSwitch
{
142 FragmentCompiler
&Outer
;
143 llvm::StringRef EnumName
;
144 const Located
<std::string
> &Input
;
145 llvm::Optional
<T
> Result
;
146 llvm::SmallVector
<llvm::StringLiteral
> ValidValues
;
149 EnumSwitch(llvm::StringRef EnumName
, const Located
<std::string
> &In
,
150 FragmentCompiler
&Outer
)
151 : Outer(Outer
), EnumName(EnumName
), Input(In
) {}
153 EnumSwitch
&map(llvm::StringLiteral Name
, T Value
) {
154 assert(!llvm::is_contained(ValidValues
, Name
) && "Duplicate value!");
155 ValidValues
.push_back(Name
);
156 if (!Result
&& *Input
== Name
)
161 llvm::Optional
<T
> value() {
165 llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
166 EnumName
, *Input
, llvm::join(ValidValues
, ", "))
173 // Attempt to parse a specified string into an enum.
174 // Yields llvm::None and produces a diagnostic on failure.
176 // Optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
177 // .map("Foo", Enum::Foo)
178 // .map("Bar", Enum::Bar)
180 template <typename T
>
181 EnumSwitch
<T
> compileEnum(llvm::StringRef EnumName
,
182 const Located
<std::string
> &In
) {
183 return EnumSwitch
<T
>(EnumName
, In
, *this);
186 void compile(Fragment
&&F
) {
187 Trusted
= F
.Source
.Trusted
;
188 if (!F
.Source
.Directory
.empty()) {
189 FragmentDirectory
= llvm::sys::path::convert_to_slash(F
.Source
.Directory
);
190 if (FragmentDirectory
.back() != '/')
191 FragmentDirectory
+= '/';
193 compile(std::move(F
.If
));
194 compile(std::move(F
.CompileFlags
));
195 compile(std::move(F
.Index
));
196 compile(std::move(F
.Diagnostics
));
197 compile(std::move(F
.Completion
));
198 compile(std::move(F
.Hover
));
199 compile(std::move(F
.InlayHints
));
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
) {
215 if (auto RE
= compileRegex(Entry
, Flags
))
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
);
225 // Ignore the file if it is not nested under Fragment.
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
) {
236 if (auto RE
= compileRegex(Entry
, Flags
))
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
);
246 // Ignore the file if it is not nested under Fragment.
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
) {
286 // The point to insert at. Just append when `--` isn't present.
287 auto It
= llvm::find(Args
, "--");
288 Args
.insert(It
, Add
.begin(), Add
.end());
293 if (F
.CompilationDatabase
) {
294 llvm::Optional
<Config::CDBSearchSpec
> Spec
;
295 if (**F
.CompilationDatabase
== "Ancestors") {
297 Spec
->Policy
= Config::CDBSearchSpec::Ancestors
;
298 } else if (**F
.CompilationDatabase
== "None") {
300 Spec
->Policy
= Config::CDBSearchSpec::NoCDBSearch
;
303 makeAbsolute(*F
.CompilationDatabase
, "CompilationDatabase",
304 llvm::sys::path::Style::native
)) {
305 // Drop trailing slash to put the path in canonical form.
306 // Should makeAbsolute do this?
307 llvm::StringRef Rel
= llvm::sys::path::relative_path(*Path
);
308 if (!Rel
.empty() && llvm::sys::path::is_separator(Rel
.back()))
312 Spec
->Policy
= Config::CDBSearchSpec::FixedDir
;
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
) {
326 if (auto Val
= compileEnum
<Config::BackgroundPolicy
>("Background",
328 .map("Build", Config::BackgroundPolicy::Build
)
329 .map("Skip", Config::BackgroundPolicy::Skip
)
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();
360 // Make sure exactly one of the Sources is set.
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
) {
370 Spec
.Kind
= Config::ExternalIndexSpec::Server
;
371 Spec
.Location
= std::move(**External
.Server
);
372 } else if (External
.File
) {
373 Spec
.Kind
= Config::ExternalIndexSpec::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
);
381 Spec
.Kind
= Config::ExternalIndexSpec::None
;
383 if (Spec
.Kind
!= Config::ExternalIndexSpec::None
) {
384 // Make sure MountPoint is an absolute path with forward slashes.
385 if (!External
.MountPoint
)
386 External
.MountPoint
.emplace(FragmentDirectory
);
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
) {
398 if (Spec
.Kind
== Config::ExternalIndexSpec::None
) {
399 C
.Index
.External
= Spec
;
402 if (P
.Path
.empty() || !pathStartsWith(Spec
.MountPoint
, P
.Path
,
403 llvm::sys::path::Style::posix
))
405 C
.Index
.External
= Spec
;
406 // Disable background indexing for the files under the mountpoint.
407 // Note that this will overwrite statements in any previous fragments
408 // (including the current one).
409 C
.Index
.Background
= Config::BackgroundPolicy::Skip
;
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();
423 Normalized
.push_back(normalizeSuppressedCode(*Suppressed
).str());
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 if (auto Val
= compileEnum
<Config::UnusedIncludesPolicy
>(
436 "UnusedIncludes", **F
.UnusedIncludes
)
437 .map("Strict", Config::UnusedIncludesPolicy::Strict
)
438 .map("None", Config::UnusedIncludesPolicy::None
)
440 Out
.Apply
.push_back([Val
](const Params
&, Config
&C
) {
441 C
.Diagnostics
.UnusedIncludes
= *Val
;
443 compile(std::move(F
.Includes
));
445 compile(std::move(F
.ClangTidy
));
448 void compile(Fragment::StyleBlock
&&F
) {
449 if (!F
.FullyQualifiedNamespaces
.empty()) {
450 std::vector
<std::string
> FullyQualifiedNamespaces
;
451 for (auto &N
: F
.FullyQualifiedNamespaces
) {
452 // Normalize the data by dropping both leading and trailing ::
453 StringRef
Namespace(*N
);
454 Namespace
.consume_front("::");
455 Namespace
.consume_back("::");
456 FullyQualifiedNamespaces
.push_back(Namespace
.str());
458 Out
.Apply
.push_back([FullyQualifiedNamespaces(
459 std::move(FullyQualifiedNamespaces
))](
460 const Params
&, Config
&C
) {
461 C
.Style
.FullyQualifiedNamespaces
.insert(
462 C
.Style
.FullyQualifiedNamespaces
.begin(),
463 FullyQualifiedNamespaces
.begin(), FullyQualifiedNamespaces
.end());
468 void appendTidyCheckSpec(std::string
&CurSpec
,
469 const Located
<std::string
> &Arg
, bool IsPositive
) {
470 StringRef Str
= StringRef(*Arg
).trim();
471 // Don't support negating here, its handled if the item is in the Add or
473 if (Str
.startswith("-") || Str
.contains(',')) {
474 diag(Error
, "Invalid clang-tidy check name", Arg
.Range
);
477 if (!Str
.contains('*') && !isRegisteredTidyCheck(Str
)) {
479 llvm::formatv("clang-tidy check '{0}' was not found", Str
).str(),
489 void compile(Fragment::DiagnosticsBlock::ClangTidyBlock
&&F
) {
491 for (auto &CheckGlob
: F
.Add
)
492 appendTidyCheckSpec(Checks
, CheckGlob
, true);
494 for (auto &CheckGlob
: F
.Remove
)
495 appendTidyCheckSpec(Checks
, CheckGlob
, false);
499 [Checks
= std::move(Checks
)](const Params
&, Config
&C
) {
500 C
.Diagnostics
.ClangTidy
.Checks
.append(
502 C
.Diagnostics
.ClangTidy
.Checks
.empty() ? /*skip comma*/ 1 : 0,
505 if (!F
.CheckOptions
.empty()) {
506 std::vector
<std::pair
<std::string
, std::string
>> CheckOptions
;
507 for (auto &Opt
: F
.CheckOptions
)
508 CheckOptions
.emplace_back(std::move(*Opt
.first
),
509 std::move(*Opt
.second
));
511 [CheckOptions
= std::move(CheckOptions
)](const Params
&, Config
&C
) {
512 for (auto &StringPair
: CheckOptions
)
513 C
.Diagnostics
.ClangTidy
.CheckOptions
.insert_or_assign(
514 StringPair
.first
, StringPair
.second
);
519 void compile(Fragment::DiagnosticsBlock::IncludesBlock
&&F
) {
520 #ifdef CLANGD_PATH_CASE_INSENSITIVE
521 static llvm::Regex::RegexFlags Flags
= llvm::Regex::IgnoreCase
;
523 static llvm::Regex::RegexFlags Flags
= llvm::Regex::NoFlags
;
525 auto Filters
= std::make_shared
<std::vector
<llvm::Regex
>>();
526 for (auto &HeaderPattern
: F
.IgnoreHeader
) {
527 // Anchor on the right.
528 std::string AnchoredPattern
= "(" + *HeaderPattern
+ ")$";
529 llvm::Regex
CompiledRegex(AnchoredPattern
, Flags
);
530 std::string RegexError
;
531 if (!CompiledRegex
.isValid(RegexError
)) {
533 llvm::formatv("Invalid regular expression '{0}': {1}",
534 *HeaderPattern
, RegexError
)
536 HeaderPattern
.Range
);
539 Filters
->push_back(std::move(CompiledRegex
));
541 if (Filters
->empty())
543 auto Filter
= [Filters
](llvm::StringRef Path
) {
544 for (auto &Regex
: *Filters
)
545 if (Regex
.match(Path
))
549 Out
.Apply
.push_back([Filter
](const Params
&, Config
&C
) {
550 C
.Diagnostics
.Includes
.IgnoreHeader
.emplace_back(Filter
);
554 void compile(Fragment::CompletionBlock
&&F
) {
557 [AllScopes(**F
.AllScopes
)](const Params
&, Config
&C
) {
558 C
.Completion
.AllScopes
= AllScopes
;
563 void compile(Fragment::HoverBlock
&&F
) {
565 Out
.Apply
.push_back([ShowAKA(**F
.ShowAKA
)](const Params
&, Config
&C
) {
566 C
.Hover
.ShowAKA
= ShowAKA
;
571 void compile(Fragment::InlayHintsBlock
&&F
) {
573 Out
.Apply
.push_back([Value(**F
.Enabled
)](const Params
&, Config
&C
) {
574 C
.InlayHints
.Enabled
= Value
;
576 if (F
.ParameterNames
)
578 [Value(**F
.ParameterNames
)](const Params
&, Config
&C
) {
579 C
.InlayHints
.Parameters
= Value
;
582 Out
.Apply
.push_back([Value(**F
.DeducedTypes
)](const Params
&, Config
&C
) {
583 C
.InlayHints
.DeducedTypes
= Value
;
586 Out
.Apply
.push_back([Value(**F
.Designators
)](const Params
&, Config
&C
) {
587 C
.InlayHints
.Designators
= Value
;
591 constexpr static llvm::SourceMgr::DiagKind Error
= llvm::SourceMgr::DK_Error
;
592 constexpr static llvm::SourceMgr::DiagKind Warning
=
593 llvm::SourceMgr::DK_Warning
;
594 void diag(llvm::SourceMgr::DiagKind Kind
, llvm::StringRef Message
,
595 llvm::SMRange Range
) {
596 if (Range
.isValid() && SourceMgr
!= nullptr)
597 Diagnostic(SourceMgr
->GetMessage(Range
.Start
, Kind
, Message
, Range
));
599 Diagnostic(llvm::SMDiagnostic("", Kind
, Message
));
605 CompiledFragment
Fragment::compile(DiagnosticCallback D
) && {
606 llvm::StringRef ConfigFile
= "<unknown>";
607 std::pair
<unsigned, unsigned> LineCol
= {0, 0};
608 if (auto *SM
= Source
.Manager
.get()) {
609 unsigned BufID
= SM
->getMainFileID();
610 LineCol
= SM
->getLineAndColumn(Source
.Location
, BufID
);
611 ConfigFile
= SM
->getBufferInfo(BufID
).Buffer
->getBufferIdentifier();
613 trace::Span
Tracer("ConfigCompile");
614 SPAN_ATTACH(Tracer
, "ConfigFile", ConfigFile
);
615 auto Result
= std::make_shared
<CompiledFragmentImpl
>();
616 vlog("Config fragment: compiling {0}:{1} -> {2} (trusted={3})", ConfigFile
,
617 LineCol
.first
, Result
.get(), Source
.Trusted
);
619 FragmentCompiler
{*Result
, D
, Source
.Manager
.get()}.compile(std::move(*this));
620 // Return as cheaply-copyable wrapper.
621 return [Result(std::move(Result
))](const Params
&P
, Config
&C
) {
622 return (*Result
)(P
, C
);
626 } // namespace config
627 } // namespace clangd