1//===--- ConfigCompile.cpp - Translating Fragments into Config ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Fragments are applied to Configs in two steps:
10//
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.
16//
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:
20//
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
23//
24//===----------------------------------------------------------------------===//
25
26#include "CompileCommands.h"
27#include "Config.h"
28#include "ConfigFragment.h"
29#include "ConfigProvider.h"
30#include "Diagnostics.h"
31#include "Feature.h"
32#include "TidyProvider.h"
33#include "support/Logger.h"
34#include "support/Path.h"
35#include "support/Trace.h"
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"
46#include <algorithm>
47#include <memory>
48#include <optional>
49#include <string>
50#include <vector>
51
52namespace clang {
53namespace clangd {
54namespace config {
55namespace {
56
57// Returns an empty stringref if Path is not under FragmentDir. Returns Path
58// as-is when FragmentDir is empty.
59llvm::StringRef configRelative(llvm::StringRef Path,
60 llvm::StringRef FragmentDir) {
61 if (FragmentDir.empty())
62 return Path;
63 if (!Path.consume_front(FragmentDir))
64 return llvm::StringRef();
65 return Path.empty() ? "." : Path;
66}
67
68struct 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>>
78 Apply;
79
80 bool operator()(const Params &P, Config &C) const {
81 for (const auto &C : Conditions) {
82 if (!C(P)) {
83 dlog("Config fragment {0}: condition not met", this);
84 return false;
85 }
86 }
87 dlog("Config fragment {0}: applying {1} rules", this, Apply.size());
88 for (const auto &A : Apply)
89 A(P, C);
90 return true;
91 }
92};
93
94// Wrapper around condition compile() functions to reduce arg-passing.
95struct FragmentCompiler {
96 FragmentCompiler(CompiledFragmentImpl &Out, DiagnosticCallback D,
97 llvm::SourceMgr *SM)
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;
105
106 std::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);
114 return std::nullopt;
115 }
116 return std::move(Result);
117 }
118
119 std::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))
123 return *Path;
124 if (FragmentDirectory.empty()) {
125 diag(Error,
126 llvm::formatv(
127 "{0} must be an absolute path, because this fragment is not "
128 "associated with any directory.",
129 Description)
130 .str(),
131 Path.Range);
132 return std::nullopt;
133 }
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();
138 }
139
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 std::optional<T> Result;
146 llvm::SmallVector<llvm::StringLiteral> ValidValues;
147
148 public:
149 EnumSwitch(llvm::StringRef EnumName, const Located<std::string> &In,
150 FragmentCompiler &Outer)
151 : Outer(Outer), EnumName(EnumName), Input(In) {}
152
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)
157 Result = Value;
158 return *this;
159 }
160
161 std::optional<T> value() {
162 if (!Result)
163 Outer.diag(
164 Warning,
165 llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
166 EnumName, *Input, llvm::join(ValidValues, ", "))
167 .str(),
168 Input.Range);
169 return Result;
170 };
171 };
172
173 // Attempt to parse a specified string into an enum.
174 // Yields std::nullopt and produces a diagnostic on failure.
175 //
176 // std::optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
177 // .map("Foo", Enum::Foo)
178 // .map("Bar", Enum::Bar)
179 // .value();
180 template <typename T>
181 EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
182 const Located<std::string> &In) {
183 return EnumSwitch<T>(EnumName, In, *this);
184 }
185
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 += '/';
192 }
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.SemanticTokens));
201 compile(std::move(F.Style));
202 }
203
204 void compile(Fragment::IfBlock &&F) {
205 if (F.HasUnrecognizedCondition)
206 Out.Conditions.push_back([&](const Params &) { return false; });
207
208#ifdef CLANGD_PATH_CASE_INSENSITIVE
209 llvm::Regex::RegexFlags Flags = llvm::Regex::IgnoreCase;
210#else
211 llvm::Regex::RegexFlags Flags = llvm::Regex::NoFlags;
212#endif
213
214 auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
215 for (auto &Entry : F.PathMatch) {
216 if (auto RE = compileRegex(Entry, Flags))
217 PathMatch->push_back(std::move(*RE));
218 }
219 if (!PathMatch->empty()) {
220 Out.Conditions.push_back(
221 [PathMatch(std::move(PathMatch)),
222 FragmentDir(FragmentDirectory)](const Params &P) {
223 if (P.Path.empty())
224 return false;
225 llvm::StringRef Path = configRelative(P.Path, FragmentDir);
226 // Ignore the file if it is not nested under Fragment.
227 if (Path.empty())
228 return false;
229 return llvm::any_of(*PathMatch, [&](const llvm::Regex &RE) {
230 return RE.match(Path);
231 });
232 });
233 }
234
235 auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
236 for (auto &Entry : F.PathExclude) {
237 if (auto RE = compileRegex(Entry, Flags))
238 PathExclude->push_back(std::move(*RE));
239 }
240 if (!PathExclude->empty()) {
241 Out.Conditions.push_back(
242 [PathExclude(std::move(PathExclude)),
243 FragmentDir(FragmentDirectory)](const Params &P) {
244 if (P.Path.empty())
245 return false;
246 llvm::StringRef Path = configRelative(P.Path, FragmentDir);
247 // Ignore the file if it is not nested under Fragment.
248 if (Path.empty())
249 return true;
250 return llvm::none_of(*PathExclude, [&](const llvm::Regex &RE) {
251 return RE.match(Path);
252 });
253 });
254 }
255 }
256
257 void compile(Fragment::CompileFlagsBlock &&F) {
258 if (F.Compiler)
259 Out.Apply.push_back(
260 [Compiler(std::move(**F.Compiler))](const Params &, Config &C) {
261 C.CompileFlags.Edits.push_back(
262 [Compiler](std::vector<std::string> &Args) {
263 if (!Args.empty())
264 Args.front() = Compiler;
265 });
266 });
267
268 if (!F.Remove.empty()) {
269 auto Remove = std::make_shared<ArgStripper>();
270 for (auto &A : F.Remove)
271 Remove->strip(*A);
272 Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
273 std::move(Remove)))](const Params &, Config &C) {
274 C.CompileFlags.Edits.push_back(
275 [Remove](std::vector<std::string> &Args) {
276 Remove->process(Args);
277 });
278 });
279 }
280
281 if (!F.Add.empty()) {
282 std::vector<std::string> Add;
283 for (auto &A : F.Add)
284 Add.push_back(std::move(*A));
285 Out.Apply.push_back([Add(std::move(Add))](const Params &, Config &C) {
286 C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
287 // The point to insert at. Just append when `--` isn't present.
288 auto It = llvm::find(Args, "--");
289 Args.insert(It, Add.begin(), Add.end());
290 });
291 });
292 }
293
294 if (F.CompilationDatabase) {
295 std::optional<Config::CDBSearchSpec> Spec;
296 if (**F.CompilationDatabase == "Ancestors") {
297 Spec.emplace();
298 Spec->Policy = Config::CDBSearchSpec::Ancestors;
299 } else if (**F.CompilationDatabase == "None") {
300 Spec.emplace();
301 Spec->Policy = Config::CDBSearchSpec::NoCDBSearch;
302 } else {
303 if (auto Path =
304 makeAbsolute(*F.CompilationDatabase, "CompilationDatabase",
305 llvm::sys::path::Style::native)) {
306 // Drop trailing slash to put the path in canonical form.
307 // Should makeAbsolute do this?
308 llvm::StringRef Rel = llvm::sys::path::relative_path(*Path);
309 if (!Rel.empty() && llvm::sys::path::is_separator(Rel.back()))
310 Path->pop_back();
311
312 Spec.emplace();
313 Spec->Policy = Config::CDBSearchSpec::FixedDir;
314 Spec->FixedCDBPath = std::move(Path);
315 }
316 }
317 if (Spec)
318 Out.Apply.push_back(
319 [Spec(std::move(*Spec))](const Params &, Config &C) {
320 C.CompileFlags.CDBSearch = Spec;
321 });
322 }
323 }
324
325 void compile(Fragment::IndexBlock &&F) {
326 if (F.Background) {
327 if (auto Val = compileEnum<Config::BackgroundPolicy>("Background",
328 **F.Background)
329 .map("Build", Config::BackgroundPolicy::Build)
330 .map("Skip", Config::BackgroundPolicy::Skip)
331 .value())
332 Out.Apply.push_back(
333 [Val](const Params &, Config &C) { C.Index.Background = *Val; });
334 }
335 if (F.External)
336 compile(std::move(**F.External), F.External->Range);
337 if (F.StandardLibrary)
338 Out.Apply.push_back(
339 [Val(**F.StandardLibrary)](const Params &, Config &C) {
340 C.Index.StandardLibrary = Val;
341 });
342 }
343
344 void compile(Fragment::IndexBlock::ExternalBlock &&External,
345 llvm::SMRange BlockRange) {
346 if (External.Server && !Trusted) {
347 diag(Error,
348 "Remote index may not be specified by untrusted configuration. "
349 "Copy this into user config to use it.",
350 External.Server->Range);
351 return;
352 }
353#ifndef CLANGD_ENABLE_REMOTE
354 if (External.Server) {
355 elog("Clangd isn't compiled with remote index support, ignoring Server: "
356 "{0}",
357 *External.Server);
358 External.Server.reset();
359 }
360#endif
361 // Make sure exactly one of the Sources is set.
362 unsigned SourceCount = External.File.has_value() +
363 External.Server.has_value() + *External.IsNone;
364 if (SourceCount != 1) {
365 diag(Error, "Exactly one of File, Server or None must be set.",
366 BlockRange);
367 return;
368 }
369 Config::ExternalIndexSpec Spec;
370 if (External.Server) {
371 Spec.Kind = Config::ExternalIndexSpec::Server;
372 Spec.Location = std::move(**External.Server);
373 } else if (External.File) {
374 Spec.Kind = Config::ExternalIndexSpec::File;
375 auto AbsPath = makeAbsolute(std::move(*External.File), "File",
376 llvm::sys::path::Style::native);
377 if (!AbsPath)
378 return;
379 Spec.Location = std::move(*AbsPath);
380 } else {
381 assert(*External.IsNone);
382 Spec.Kind = Config::ExternalIndexSpec::None;
383 }
384 if (Spec.Kind != Config::ExternalIndexSpec::None) {
385 // Make sure MountPoint is an absolute path with forward slashes.
386 if (!External.MountPoint)
387 External.MountPoint.emplace(FragmentDirectory);
388 if ((**External.MountPoint).empty()) {
389 diag(Error, "A mountpoint is required.", BlockRange);
390 return;
391 }
392 auto AbsPath = makeAbsolute(std::move(*External.MountPoint), "MountPoint",
393 llvm::sys::path::Style::posix);
394 if (!AbsPath)
395 return;
396 Spec.MountPoint = std::move(*AbsPath);
397 }
398 Out.Apply.push_back([Spec(std::move(Spec))](const Params &P, Config &C) {
399 if (Spec.Kind == Config::ExternalIndexSpec::None) {
400 C.Index.External = Spec;
401 return;
402 }
403 if (P.Path.empty() || !pathStartsWith(Spec.MountPoint, P.Path,
404 llvm::sys::path::Style::posix))
405 return;
406 C.Index.External = Spec;
407 // Disable background indexing for the files under the mountpoint.
408 // Note that this will overwrite statements in any previous fragments
409 // (including the current one).
410 C.Index.Background = Config::BackgroundPolicy::Skip;
411 });
412 }
413
414 void compile(Fragment::DiagnosticsBlock &&F) {
415 std::vector<std::string> Normalized;
416 for (const auto &Suppressed : F.Suppress) {
417 if (*Suppressed == "*") {
418 Out.Apply.push_back([&](const Params &, Config &C) {
419 C.Diagnostics.SuppressAll = true;
420 C.Diagnostics.Suppress.clear();
421 });
422 return;
423 }
424 Normalized.push_back(normalizeSuppressedCode(*Suppressed).str());
425 }
426 if (!Normalized.empty())
427 Out.Apply.push_back(
428 [Normalized(std::move(Normalized))](const Params &, Config &C) {
429 if (C.Diagnostics.SuppressAll)
430 return;
431 for (llvm::StringRef N : Normalized)
432 C.Diagnostics.Suppress.insert(N);
433 });
434
435 if (F.UnusedIncludes) {
436 auto Val = compileEnum<Config::IncludesPolicy>("UnusedIncludes",
437 **F.UnusedIncludes)
438 .map("Strict", Config::IncludesPolicy::Strict)
439 .map("None", Config::IncludesPolicy::None)
440 .value();
441 if (!Val && **F.UnusedIncludes == "Experiment") {
442 diag(Warning,
443 "Experiment is deprecated for UnusedIncludes, use Strict instead.",
444 F.UnusedIncludes->Range);
445 Val = Config::IncludesPolicy::Strict;
446 }
447 if (Val) {
448 Out.Apply.push_back([Val](const Params &, Config &C) {
449 C.Diagnostics.UnusedIncludes = *Val;
450 });
451 }
452 }
453
454 if (F.MissingIncludes)
455 if (auto Val = compileEnum<Config::IncludesPolicy>("MissingIncludes",
456 **F.MissingIncludes)
457 .map("Strict", Config::IncludesPolicy::Strict)
458 .map("None", Config::IncludesPolicy::None)
459 .value())
460 Out.Apply.push_back([Val](const Params &, Config &C) {
461 C.Diagnostics.MissingIncludes = *Val;
462 });
463
464 compile(std::move(F.Includes));
465 compile(std::move(F.ClangTidy));
466 }
467
468 void compile(Fragment::StyleBlock &&F) {
469 if (!F.FullyQualifiedNamespaces.empty()) {
470 std::vector<std::string> FullyQualifiedNamespaces;
471 for (auto &N : F.FullyQualifiedNamespaces) {
472 // Normalize the data by dropping both leading and trailing ::
473 StringRef Namespace(*N);
474 Namespace.consume_front("::");
475 Namespace.consume_back("::");
476 FullyQualifiedNamespaces.push_back(Namespace.str());
477 }
478 Out.Apply.push_back([FullyQualifiedNamespaces(
479 std::move(FullyQualifiedNamespaces))](
480 const Params &, Config &C) {
481 C.Style.FullyQualifiedNamespaces.insert(
482 C.Style.FullyQualifiedNamespaces.begin(),
483 FullyQualifiedNamespaces.begin(), FullyQualifiedNamespaces.end());
484 });
485 }
486 }
487
488 void appendTidyCheckSpec(std::string &CurSpec,
489 const Located<std::string> &Arg, bool IsPositive) {
490 StringRef Str = StringRef(*Arg).trim();
491 // Don't support negating here, its handled if the item is in the Add or
492 // Remove list.
493 if (Str.startswith("-") || Str.contains(',')) {
494 diag(Error, "Invalid clang-tidy check name", Arg.Range);
495 return;
496 }
497 if (!Str.contains('*') && !isRegisteredTidyCheck(Str)) {
498 diag(Warning,
499 llvm::formatv("clang-tidy check '{0}' was not found", Str).str(),
500 Arg.Range);
501 return;
502 }
503 CurSpec += ',';
504 if (!IsPositive)
505 CurSpec += '-';
506 CurSpec += Str;
507 }
508
509 void compile(Fragment::DiagnosticsBlock::ClangTidyBlock &&F) {
510 std::string Checks;
511 for (auto &CheckGlob : F.Add)
512 appendTidyCheckSpec(Checks, CheckGlob, true);
513
514 for (auto &CheckGlob : F.Remove)
515 appendTidyCheckSpec(Checks, CheckGlob, false);
516
517 if (!Checks.empty())
518 Out.Apply.push_back(
519 [Checks = std::move(Checks)](const Params &, Config &C) {
520 C.Diagnostics.ClangTidy.Checks.append(
521 Checks,
522 C.Diagnostics.ClangTidy.Checks.empty() ? /*skip comma*/ 1 : 0,
523 std::string::npos);
524 });
525 if (!F.CheckOptions.empty()) {
526 std::vector<std::pair<std::string, std::string>> CheckOptions;
527 for (auto &Opt : F.CheckOptions)
528 CheckOptions.emplace_back(std::move(*Opt.first),
529 std::move(*Opt.second));
530 Out.Apply.push_back(
531 [CheckOptions = std::move(CheckOptions)](const Params &, Config &C) {
532 for (auto &StringPair : CheckOptions)
533 C.Diagnostics.ClangTidy.CheckOptions.insert_or_assign(
534 StringPair.first, StringPair.second);
535 });
536 }
537 }
538
539 void compile(Fragment::DiagnosticsBlock::IncludesBlock &&F) {
540#ifdef CLANGD_PATH_CASE_INSENSITIVE
541 static llvm::Regex::RegexFlags Flags = llvm::Regex::IgnoreCase;
542#else
543 static llvm::Regex::RegexFlags Flags = llvm::Regex::NoFlags;
544#endif
545 auto Filters = std::make_shared<std::vector<llvm::Regex>>();
546 for (auto &HeaderPattern : F.IgnoreHeader) {
547 // Anchor on the right.
548 std::string AnchoredPattern = "(" + *HeaderPattern + ")$";
549 llvm::Regex CompiledRegex(AnchoredPattern, Flags);
550 std::string RegexError;
551 if (!CompiledRegex.isValid(RegexError)) {
552 diag(Warning,
553 llvm::formatv("Invalid regular expression '{0}': {1}",
554 *HeaderPattern, RegexError)
555 .str(),
556 HeaderPattern.Range);
557 continue;
558 }
559 Filters->push_back(std::move(CompiledRegex));
560 }
561 if (Filters->empty())
562 return;
563 auto Filter = [Filters](llvm::StringRef Path) {
564 for (auto &Regex : *Filters)
565 if (Regex.match(Path))
566 return true;
567 return false;
568 };
569 Out.Apply.push_back([Filter](const Params &, Config &C) {
570 C.Diagnostics.Includes.IgnoreHeader.emplace_back(Filter);
571 });
572 }
573
574 void compile(Fragment::CompletionBlock &&F) {
575 if (F.AllScopes) {
576 Out.Apply.push_back(
577 [AllScopes(**F.AllScopes)](const Params &, Config &C) {
578 C.Completion.AllScopes = AllScopes;
579 });
580 }
581 }
582
583 void compile(Fragment::HoverBlock &&F) {
584 if (F.ShowAKA) {
585 Out.Apply.push_back([ShowAKA(**F.ShowAKA)](const Params &, Config &C) {
586 C.Hover.ShowAKA = ShowAKA;
587 });
588 }
589 }
590
591 void compile(Fragment::InlayHintsBlock &&F) {
592 if (F.Enabled)
593 Out.Apply.push_back([Value(**F.Enabled)](const Params &, Config &C) {
594 C.InlayHints.Enabled = Value;
595 });
596 if (F.ParameterNames)
597 Out.Apply.push_back(
598 [Value(**F.ParameterNames)](const Params &, Config &C) {
599 C.InlayHints.Parameters = Value;
600 });
601 if (F.DeducedTypes)
602 Out.Apply.push_back([Value(**F.DeducedTypes)](const Params &, Config &C) {
603 C.InlayHints.DeducedTypes = Value;
604 });
605 if (F.Designators)
606 Out.Apply.push_back([Value(**F.Designators)](const Params &, Config &C) {
607 C.InlayHints.Designators = Value;
608 });
609 if (F.BlockEnd)
610 Out.Apply.push_back([Value(**F.BlockEnd)](const Params &, Config &C) {
611 C.InlayHints.BlockEnd = Value;
612 });
613 if (F.TypeNameLimit)
614 Out.Apply.push_back(
615 [Value(**F.TypeNameLimit)](const Params &, Config &C) {
616 C.InlayHints.TypeNameLimit = Value;
617 });
618 }
619
620 void compile(Fragment::SemanticTokensBlock &&F) {
621 if (!F.DisabledKinds.empty()) {
622 std::vector<std::string> DisabledKinds;
623 for (auto &Kind : F.DisabledKinds)
624 DisabledKinds.push_back(std::move(*Kind));
625
626 Out.Apply.push_back(
627 [DisabledKinds(std::move(DisabledKinds))](const Params &, Config &C) {
628 for (auto &Kind : DisabledKinds) {
629 auto It = llvm::find(C.SemanticTokens.DisabledKinds, Kind);
630 if (It == C.SemanticTokens.DisabledKinds.end())
631 C.SemanticTokens.DisabledKinds.push_back(std::move(Kind));
632 }
633 });
634 }
635 if (!F.DisabledModifiers.empty()) {
636 std::vector<std::string> DisabledModifiers;
637 for (auto &Kind : F.DisabledModifiers)
638 DisabledModifiers.push_back(std::move(*Kind));
639
640 Out.Apply.push_back([DisabledModifiers(std::move(DisabledModifiers))](
641 const Params &, Config &C) {
642 for (auto &Kind : DisabledModifiers) {
643 auto It = llvm::find(C.SemanticTokens.DisabledModifiers, Kind);
644 if (It == C.SemanticTokens.DisabledModifiers.end())
645 C.SemanticTokens.DisabledModifiers.push_back(std::move(Kind));
646 }
647 });
648 }
649 }
650
651 constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
652 constexpr static llvm::SourceMgr::DiagKind Warning =
653 llvm::SourceMgr::DK_Warning;
654 void diag(llvm::SourceMgr::DiagKind Kind, llvm::StringRef Message,
655 llvm::SMRange Range) {
656 if (Range.isValid() && SourceMgr != nullptr)
657 Diagnostic(SourceMgr->GetMessage(Range.Start, Kind, Message, Range));
658 else
659 Diagnostic(llvm::SMDiagnostic("", Kind, Message));
660 }
661};
662
663} // namespace
664
665CompiledFragment Fragment::compile(DiagnosticCallback D) && {
666 llvm::StringRef ConfigFile = "<unknown>";
667 std::pair<unsigned, unsigned> LineCol = {0, 0};
668 if (auto *SM = Source.Manager.get()) {
669 unsigned BufID = SM->getMainFileID();
670 LineCol = SM->getLineAndColumn(Source.Location, BufID);
671 ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
672 }
673 trace::Span Tracer("ConfigCompile");
674 SPAN_ATTACH(Tracer, "ConfigFile", ConfigFile);
675 auto Result = std::make_shared<CompiledFragmentImpl>();
676 vlog("Config fragment: compiling {0}:{1} -> {2} (trusted={3})", ConfigFile,
677 LineCol.first, Result.get(), Source.Trusted);
678
679 FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*this));
680 // Return as cheaply-copyable wrapper.
681 return [Result(std::move(Result))](const Params &P, Config &C) {
682 return (*Result)(P, C);
683 };
684}
685
686} // namespace config
687} // namespace clangd
688} // namespace clang
689