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