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