clang-tools 22.0.0git
ConfigYAML.cpp
Go to the documentation of this file.
1//===--- ConfigYAML.cpp - Loading configuration fragments from YAML files -===//
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#include "ConfigFragment.h"
9#include "support/Logger.h"
10#include "llvm/ADT/SmallSet.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/MemoryBuffer.h"
14#include "llvm/Support/SourceMgr.h"
15#include "llvm/Support/YAMLParser.h"
16#include <optional>
17#include <string>
18
19namespace clang {
20namespace clangd {
21namespace config {
22namespace {
23using llvm::yaml::BlockScalarNode;
24using llvm::yaml::MappingNode;
25using llvm::yaml::Node;
26using llvm::yaml::ScalarNode;
27using llvm::yaml::SequenceNode;
28
29std::optional<llvm::StringRef>
30bestGuess(llvm::StringRef Search,
31 llvm::ArrayRef<llvm::StringRef> AllowedValues) {
32 unsigned MaxEdit = (Search.size() + 1) / 3;
33 if (!MaxEdit)
34 return std::nullopt;
35 std::optional<llvm::StringRef> Result;
36 for (const auto &AllowedValue : AllowedValues) {
37 unsigned EditDistance = Search.edit_distance(AllowedValue, true, MaxEdit);
38 // We can't do better than an edit distance of 1, so just return this and
39 // save computing other values.
40 if (EditDistance == 1U)
41 return AllowedValue;
42 if (EditDistance == MaxEdit && !Result) {
43 Result = AllowedValue;
44 } else if (EditDistance < MaxEdit) {
45 Result = AllowedValue;
46 MaxEdit = EditDistance;
47 }
48 }
49 return Result;
50}
51
52class Parser {
53 llvm::SourceMgr &SM;
54 bool HadError = false;
55
56public:
57 Parser(llvm::SourceMgr &SM) : SM(SM) {}
58
59 // Tries to parse N into F, returning false if it failed and we couldn't
60 // meaningfully recover (YAML syntax error, or hard semantic error).
61 bool parse(Fragment &F, Node &N) {
62 DictParser Dict("Config", this);
63 Dict.handle("If", [&](Node &N) { parse(F.If, N); });
64 Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
65 Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
66 Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
67 Dict.handle("Diagnostics", [&](Node &N) { parse(F.Diagnostics, N); });
68 Dict.handle("Completion", [&](Node &N) { parse(F.Completion, N); });
69 Dict.handle("Hover", [&](Node &N) { parse(F.Hover, N); });
70 Dict.handle("InlayHints", [&](Node &N) { parse(F.InlayHints, N); });
71 Dict.handle("SemanticTokens", [&](Node &N) { parse(F.SemanticTokens, N); });
72 Dict.handle("Documentation", [&](Node &N) { parse(F.Documentation, N); });
73 Dict.parse(N);
74 return !(N.failed() || HadError);
75 }
76
77private:
78 void parse(Fragment::IfBlock &F, Node &N) {
79 DictParser Dict("If", this);
80 Dict.unrecognized([&](Located<std::string>, Node &) {
81 F.HasUnrecognizedCondition = true;
82 return true; // Emit a warning for the unrecognized key.
83 });
84 Dict.handle("PathMatch", [&](Node &N) {
85 if (auto Values = scalarValues(N))
86 F.PathMatch = std::move(*Values);
87 });
88 Dict.handle("PathExclude", [&](Node &N) {
89 if (auto Values = scalarValues(N))
90 F.PathExclude = std::move(*Values);
91 });
92 Dict.parse(N);
93 }
94
95 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
96 DictParser Dict("CompileFlags", this);
97 Dict.handle("Compiler", [&](Node &N) {
98 if (auto Value = scalarValue(N, "Compiler"))
99 F.Compiler = std::move(*Value);
100 });
101 Dict.handle("Add", [&](Node &N) {
102 if (auto Values = scalarValues(N))
103 F.Add = std::move(*Values);
104 });
105 Dict.handle("Remove", [&](Node &N) {
106 if (auto Values = scalarValues(N))
107 F.Remove = std::move(*Values);
108 });
109 Dict.handle("BuiltinHeaders", [&](Node &N) {
110 if (auto BuiltinHeaders = scalarValue(N, "BuiltinHeaders"))
111 F.BuiltinHeaders = *BuiltinHeaders;
112 });
113 Dict.handle("CompilationDatabase", [&](Node &N) {
114 F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
115 });
116 Dict.parse(N);
117 }
118
119 void parse(Fragment::StyleBlock &F, Node &N) {
120 DictParser Dict("Style", this);
121 Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
122 if (auto Values = scalarValues(N))
123 F.FullyQualifiedNamespaces = std::move(*Values);
124 });
125 Dict.handle("QuotedHeaders", [&](Node &N) {
126 if (auto Values = scalarValues(N))
127 F.QuotedHeaders = std::move(*Values);
128 });
129 Dict.handle("AngledHeaders", [&](Node &N) {
130 if (auto Values = scalarValues(N))
131 F.AngledHeaders = std::move(*Values);
132 });
133 Dict.parse(N);
134 }
135
136 void parse(Fragment::DiagnosticsBlock &F, Node &N) {
137 DictParser Dict("Diagnostics", this);
138 Dict.handle("Suppress", [&](Node &N) {
139 if (auto Values = scalarValues(N))
140 F.Suppress = std::move(*Values);
141 });
142 Dict.handle("UnusedIncludes", [&](Node &N) {
143 F.UnusedIncludes = scalarValue(N, "UnusedIncludes");
144 });
145 Dict.handle("MissingIncludes", [&](Node &N) {
146 F.MissingIncludes = scalarValue(N, "MissingIncludes");
147 });
148 Dict.handle("Includes", [&](Node &N) { parse(F.Includes, N); });
149 Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
150 Dict.parse(N);
151 }
152
153 void parse(Fragment::DiagnosticsBlock::ClangTidyBlock &F, Node &N) {
154 DictParser Dict("ClangTidy", this);
155 Dict.handle("Add", [&](Node &N) {
156 if (auto Values = scalarValues(N))
157 F.Add = std::move(*Values);
158 });
159 Dict.handle("Remove", [&](Node &N) {
160 if (auto Values = scalarValues(N))
161 F.Remove = std::move(*Values);
162 });
163 Dict.handle("CheckOptions", [&](Node &N) {
164 DictParser CheckOptDict("CheckOptions", this);
165 CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
166 if (auto Value = scalarValue(Val, *Key))
167 F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
168 return false; // Don't emit a warning
169 });
170 CheckOptDict.parse(N);
171 });
172 Dict.handle("FastCheckFilter", [&](Node &N) {
173 if (auto FastCheckFilter = scalarValue(N, "FastCheckFilter"))
174 F.FastCheckFilter = *FastCheckFilter;
175 });
176 Dict.parse(N);
177 }
178
179 void parse(Fragment::DiagnosticsBlock::IncludesBlock &F, Node &N) {
180 DictParser Dict("Includes", this);
181 Dict.handle("IgnoreHeader", [&](Node &N) {
182 if (auto Values = scalarValues(N))
183 F.IgnoreHeader = std::move(*Values);
184 });
185 Dict.handle("AnalyzeAngledIncludes", [&](Node &N) {
186 if (auto Value = boolValue(N, "AnalyzeAngledIncludes"))
187 F.AnalyzeAngledIncludes = *Value;
188 });
189 Dict.parse(N);
190 }
191
192 void parse(Fragment::IndexBlock &F, Node &N) {
193 DictParser Dict("Index", this);
194 Dict.handle("Background",
195 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
196 Dict.handle("External", [&](Node &N) {
197 Fragment::IndexBlock::ExternalBlock External;
198 // External block can either be a mapping or a scalar value. Dispatch
199 // accordingly.
200 if (N.getType() == Node::NK_Mapping) {
201 parse(External, N);
202 } else if (N.getType() == Node::NK_Scalar ||
203 N.getType() == Node::NK_BlockScalar) {
204 parse(External, *scalarValue(N, "External"));
205 } else {
206 error("External must be either a scalar or a mapping.", N);
207 return;
208 }
209 F.External.emplace(std::move(External));
210 F.External->Range = N.getSourceRange();
211 });
212 Dict.handle("StandardLibrary", [&](Node &N) {
213 if (auto StandardLibrary = boolValue(N, "StandardLibrary"))
214 F.StandardLibrary = *StandardLibrary;
215 });
216 Dict.parse(N);
217 }
218
219 void parse(Fragment::IndexBlock::ExternalBlock &F,
220 Located<std::string> ExternalVal) {
221 if (!llvm::StringRef(*ExternalVal).equals_insensitive("none")) {
222 error("Only scalar value supported for External is 'None'",
223 ExternalVal.Range);
224 return;
225 }
226 F.IsNone = true;
227 F.IsNone.Range = ExternalVal.Range;
228 }
229
230 void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
231 DictParser Dict("External", this);
232 Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
233 Dict.handle("Server",
234 [&](Node &N) { F.Server = scalarValue(N, "Server"); });
235 Dict.handle("MountPoint",
236 [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
237 Dict.parse(N);
238 }
239
240 void parse(Fragment::CompletionBlock &F, Node &N) {
241 DictParser Dict("Completion", this);
242 Dict.handle("AllScopes", [&](Node &N) {
243 if (auto AllScopes = boolValue(N, "AllScopes"))
244 F.AllScopes = *AllScopes;
245 });
246 Dict.handle("ArgumentLists", [&](Node &N) {
247 if (auto ArgumentLists = scalarValue(N, "ArgumentLists"))
248 F.ArgumentLists = *ArgumentLists;
249 });
250 Dict.handle("HeaderInsertion", [&](Node &N) {
251 if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion"))
252 F.HeaderInsertion = *HeaderInsertion;
253 });
254 Dict.handle("CodePatterns", [&](Node &N) {
255 if (auto CodePatterns = scalarValue(N, "CodePatterns"))
256 F.CodePatterns = *CodePatterns;
257 });
258 Dict.handle("MacroFilter", [&](Node &N) {
259 if (auto MacroFilter = scalarValue(N, "MacroFilter"))
260 F.MacroFilter = *MacroFilter;
261 });
262 Dict.parse(N);
263 }
264
265 void parse(Fragment::HoverBlock &F, Node &N) {
266 DictParser Dict("Hover", this);
267 Dict.handle("ShowAKA", [&](Node &N) {
268 if (auto ShowAKA = boolValue(N, "ShowAKA"))
269 F.ShowAKA = *ShowAKA;
270 });
271 Dict.handle("MacroContentsLimit", [&](Node &N) {
272 if (auto MacroContentsLimit = uint32Value(N, "MacroContentsLimit"))
273 F.MacroContentsLimit = *MacroContentsLimit;
274 });
275 Dict.parse(N);
276 }
277
278 void parse(Fragment::InlayHintsBlock &F, Node &N) {
279 DictParser Dict("InlayHints", this);
280 Dict.handle("Enabled", [&](Node &N) {
281 if (auto Value = boolValue(N, "Enabled"))
282 F.Enabled = *Value;
283 });
284 Dict.handle("ParameterNames", [&](Node &N) {
285 if (auto Value = boolValue(N, "ParameterNames"))
286 F.ParameterNames = *Value;
287 });
288 Dict.handle("DeducedTypes", [&](Node &N) {
289 if (auto Value = boolValue(N, "DeducedTypes"))
290 F.DeducedTypes = *Value;
291 });
292 Dict.handle("Designators", [&](Node &N) {
293 if (auto Value = boolValue(N, "Designators"))
294 F.Designators = *Value;
295 });
296 Dict.handle("BlockEnd", [&](Node &N) {
297 if (auto Value = boolValue(N, "BlockEnd"))
298 F.BlockEnd = *Value;
299 });
300 Dict.handle("DefaultArguments", [&](Node &N) {
301 if (auto Value = boolValue(N, "DefaultArguments"))
302 F.DefaultArguments = *Value;
303 });
304 Dict.handle("TypeNameLimit", [&](Node &N) {
305 if (auto Value = uint32Value(N, "TypeNameLimit"))
306 F.TypeNameLimit = *Value;
307 });
308 Dict.parse(N);
309 }
310
311 void parse(Fragment::SemanticTokensBlock &F, Node &N) {
312 DictParser Dict("SemanticTokens", this);
313 Dict.handle("DisabledKinds", [&](Node &N) {
314 if (auto Values = scalarValues(N))
315 F.DisabledKinds = std::move(*Values);
316 });
317 Dict.handle("DisabledModifiers", [&](Node &N) {
318 if (auto Values = scalarValues(N))
319 F.DisabledModifiers = std::move(*Values);
320 });
321 Dict.parse(N);
322 }
323
324 void parse(Fragment::DocumentationBlock &F, Node &N) {
325 DictParser Dict("Documentation", this);
326 Dict.handle("CommentFormat", [&](Node &N) {
327 if (auto Value = scalarValue(N, "CommentFormat"))
328 F.CommentFormat = *Value;
329 });
330 Dict.parse(N);
331 }
332
333 // Helper for parsing mapping nodes (dictionaries).
334 // We don't use YamlIO as we want to control over unknown keys.
335 class DictParser {
336 llvm::StringRef Description;
337 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
338 std::function<bool(Located<std::string>, Node &)> UnknownHandler;
339 Parser *Outer;
340
341 public:
342 DictParser(llvm::StringRef Description, Parser *Outer)
343 : Description(Description), Outer(Outer) {}
344
345 // Parse is called when Key is encountered, and passed the associated value.
346 // It should emit diagnostics if the value is invalid (e.g. wrong type).
347 // If Key is seen twice, Parse runs only once and an error is reported.
348 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
349 for (const auto &Entry : Keys) {
350 (void)Entry;
351 assert(Entry.first != Key && "duplicate key handler");
352 }
353 Keys.emplace_back(Key, std::move(Parse));
354 }
355
356 // Handler is called when a Key is not matched by any handle().
357 // If this is unset or the Handler returns true, a warning is emitted for
358 // the unknown key.
359 void
360 unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
361 UnknownHandler = std::move(Handler);
362 }
363
364 // Process a mapping node and call handlers for each key/value pair.
365 void parse(Node &N) const {
366 if (N.getType() != Node::NK_Mapping) {
367 Outer->error(Description + " should be a dictionary", N);
368 return;
369 }
370 llvm::SmallSet<std::string, 8> Seen;
371 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
372 // We *must* consume all items, even on error, or the parser will assert.
373 for (auto &KV : llvm::cast<MappingNode>(N)) {
374 auto *K = KV.getKey();
375 if (!K) // YAMLParser emitted an error.
376 continue;
377 auto Key = Outer->scalarValue(*K, "Dictionary key");
378 if (!Key)
379 continue;
380 if (!Seen.insert(**Key).second) {
381 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
382 if (auto *Value = KV.getValue())
383 Value->skip();
384 continue;
385 }
386 auto *Value = KV.getValue();
387 if (!Value) // YAMLParser emitted an error.
388 continue;
389 bool Matched = false;
390 for (const auto &Handler : Keys) {
391 if (Handler.first == **Key) {
392 Matched = true;
393 Handler.second(*Value);
394 break;
395 }
396 }
397 if (!Matched) {
398 bool Warn = !UnknownHandler;
399 if (UnknownHandler)
400 Warn = UnknownHandler(
401 Located<std::string>(**Key, K->getSourceRange()), *Value);
402 if (Warn)
403 UnknownKeys.push_back(std::move(*Key));
404 }
405 }
406 if (!UnknownKeys.empty())
407 warnUnknownKeys(UnknownKeys, Seen);
408 }
409
410 private:
411 void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
412 const llvm::SmallSet<std::string, 8> &SeenKeys) const {
413 llvm::SmallVector<llvm::StringRef> UnseenKeys;
414 for (const auto &KeyAndHandler : Keys)
415 if (!SeenKeys.count(KeyAndHandler.first.str()))
416 UnseenKeys.push_back(KeyAndHandler.first);
417
418 for (const Located<std::string> &UnknownKey : UnknownKeys)
419 if (auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
420 Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
421 "'; did you mean '" + *BestGuess + "'?",
422 UnknownKey.Range);
423 else
424 Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
425 "'",
426 UnknownKey.Range);
427 }
428 };
429
430 // Try to parse a single scalar value from the node, warn on failure.
431 std::optional<Located<std::string>> scalarValue(Node &N,
432 llvm::StringRef Desc) {
433 llvm::SmallString<256> Buf;
434 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
435 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
436 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
437 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
438 warning(Desc + " should be scalar", N);
439 return std::nullopt;
440 }
441
442 std::optional<Located<bool>> boolValue(Node &N, llvm::StringRef Desc) {
443 if (auto Scalar = scalarValue(N, Desc)) {
444 if (auto Bool = llvm::yaml::parseBool(**Scalar))
445 return Located<bool>(*Bool, Scalar->Range);
446 warning(Desc + " should be a boolean", N);
447 }
448 return std::nullopt;
449 }
450
451 std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
452 if (auto Scalar = scalarValue(N, Desc)) {
453 unsigned long long Num;
454 if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
455 return Located<uint32_t>(Num, Scalar->Range);
456 }
457 }
458 warning(Desc + " invalid number", N);
459 return std::nullopt;
460 }
461
462 // Try to parse a list of single scalar values, or just a single value.
463 std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
464 std::vector<Located<std::string>> Result;
465 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
466 llvm::SmallString<256> Buf;
467 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
468 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
469 Result.emplace_back(S->getValue().str(), N.getSourceRange());
470 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
471 // We *must* consume all items, even on error, or the parser will assert.
472 for (auto &Child : *S) {
473 if (auto Value = scalarValue(Child, "List item"))
474 Result.push_back(std::move(*Value));
475 }
476 } else {
477 warning("Expected scalar or list of scalars", N);
478 return std::nullopt;
479 }
480 return Result;
481 }
482
483 // Report a "hard" error, reflecting a config file that can never be valid.
484 void error(const llvm::Twine &Msg, llvm::SMRange Range) {
485 HadError = true;
486 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
487 }
488 void error(const llvm::Twine &Msg, const Node &N) {
489 return error(Msg, N.getSourceRange());
490 }
491
492 // Report a "soft" error that could be caused by e.g. version skew.
493 void warning(const llvm::Twine &Msg, llvm::SMRange Range) {
494 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
495 }
496 void warning(const llvm::Twine &Msg, const Node &N) {
497 return warning(Msg, N.getSourceRange());
498 }
499};
500
501} // namespace
502
503std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
504 llvm::StringRef BufferName,
505 DiagnosticCallback Diags) {
506 // The YAML document may contain multiple conditional fragments.
507 // The SourceManager is shared for all of them.
508 log("Loading config file at {0}", BufferName);
509 auto SM = std::make_shared<llvm::SourceMgr>();
510 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
511 // Adapt DiagnosticCallback to function-pointer interface.
512 // Callback receives both errors we emit and those from the YAML parser.
513 SM->setDiagHandler(
514 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
515 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
516 },
517 &Diags);
518 std::vector<Fragment> Result;
519 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
520 if (Node *N = Doc.getRoot()) {
523 Fragment.Source.Location = N->getSourceRange().Start;
524 SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,
525 "Parsing config fragment");
526 if (Parser(*SM).parse(Fragment, *N))
527 Result.push_back(std::move(Fragment));
528 }
529 }
530 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
531 llvm::SourceMgr::DK_Note,
532 "Parsed " + llvm::Twine(Result.size()) +
533 " fragments from file");
534 // Hack: stash the buffer in the SourceMgr to keep it alive.
535 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
536 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
537 return Result;
538}
539
540} // namespace config
541} // namespace clangd
542} // namespace clang
llvm::function_ref< void(const llvm::SMDiagnostic &)> DiagnosticCallback
Used to report problems in parsing or interpreting a config.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition Logger.h:79
void log(const char *Fmt, Ts &&... Vals)
Definition Logger.h:67
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A top-level diagnostic that may have Notes and Fixes.
Definition Diagnostics.h:98
std::shared_ptr< llvm::SourceMgr > Manager
Retains a buffer of the original source this fragment was parsed from.
llvm::SMLoc Location
The start of the original source for this fragment.
A chunk of configuration obtained from a config file, LSP, or elsewhere.
static std::vector< Fragment > parseYAML(llvm::StringRef YAML, llvm::StringRef BufferName, DiagnosticCallback)
Parses fragments from a YAML file (one from each — delimited document).