[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clangd / ConfigYAML.cpp
blobce09af819247aecea89cb14a6ab67bf0f4d45c3a
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 #include <system_error>
19 namespace clang {
20 namespace clangd {
21 namespace config {
22 namespace {
23 using llvm::yaml::BlockScalarNode;
24 using llvm::yaml::MappingNode;
25 using llvm::yaml::Node;
26 using llvm::yaml::ScalarNode;
27 using llvm::yaml::SequenceNode;
29 std::optional<llvm::StringRef>
30 bestGuess(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;
49 return Result;
52 class Parser {
53 llvm::SourceMgr &SM;
54 bool HadError = false;
56 public:
57 Parser(llvm::SourceMgr &SM) : SM(SM) {}
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.parse(N);
73 return !(N.failed() || HadError);
76 private:
77 void parse(Fragment::IfBlock &F, Node &N) {
78 DictParser Dict("If", this);
79 Dict.unrecognized([&](Located<std::string>, Node &) {
80 F.HasUnrecognizedCondition = true;
81 return true; // Emit a warning for the unrecognized key.
82 });
83 Dict.handle("PathMatch", [&](Node &N) {
84 if (auto Values = scalarValues(N))
85 F.PathMatch = std::move(*Values);
86 });
87 Dict.handle("PathExclude", [&](Node &N) {
88 if (auto Values = scalarValues(N))
89 F.PathExclude = std::move(*Values);
90 });
91 Dict.parse(N);
94 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
95 DictParser Dict("CompileFlags", this);
96 Dict.handle("Compiler", [&](Node &N) {
97 if (auto Value = scalarValue(N, "Compiler"))
98 F.Compiler = std::move(*Value);
99 });
100 Dict.handle("Add", [&](Node &N) {
101 if (auto Values = scalarValues(N))
102 F.Add = std::move(*Values);
104 Dict.handle("Remove", [&](Node &N) {
105 if (auto Values = scalarValues(N))
106 F.Remove = std::move(*Values);
108 Dict.handle("CompilationDatabase", [&](Node &N) {
109 F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
111 Dict.parse(N);
114 void parse(Fragment::StyleBlock &F, Node &N) {
115 DictParser Dict("Style", this);
116 Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
117 if (auto Values = scalarValues(N))
118 F.FullyQualifiedNamespaces = std::move(*Values);
120 Dict.parse(N);
123 void parse(Fragment::DiagnosticsBlock &F, Node &N) {
124 DictParser Dict("Diagnostics", this);
125 Dict.handle("Suppress", [&](Node &N) {
126 if (auto Values = scalarValues(N))
127 F.Suppress = std::move(*Values);
129 Dict.handle("UnusedIncludes", [&](Node &N) {
130 F.UnusedIncludes = scalarValue(N, "UnusedIncludes");
132 Dict.handle("MissingIncludes", [&](Node &N) {
133 F.MissingIncludes = scalarValue(N, "MissingIncludes");
135 Dict.handle("Includes", [&](Node &N) { parse(F.Includes, N); });
136 Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
137 Dict.parse(N);
140 void parse(Fragment::DiagnosticsBlock::ClangTidyBlock &F, Node &N) {
141 DictParser Dict("ClangTidy", this);
142 Dict.handle("Add", [&](Node &N) {
143 if (auto Values = scalarValues(N))
144 F.Add = std::move(*Values);
146 Dict.handle("Remove", [&](Node &N) {
147 if (auto Values = scalarValues(N))
148 F.Remove = std::move(*Values);
150 Dict.handle("CheckOptions", [&](Node &N) {
151 DictParser CheckOptDict("CheckOptions", this);
152 CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
153 if (auto Value = scalarValue(Val, *Key))
154 F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
155 return false; // Don't emit a warning
157 CheckOptDict.parse(N);
159 Dict.handle("FastCheckFilter", [&](Node &N) {
160 if (auto FastCheckFilter = scalarValue(N, "FastCheckFilter"))
161 F.FastCheckFilter = *FastCheckFilter;
163 Dict.parse(N);
166 void parse(Fragment::DiagnosticsBlock::IncludesBlock &F, Node &N) {
167 DictParser Dict("Includes", this);
168 Dict.handle("IgnoreHeader", [&](Node &N) {
169 if (auto Values = scalarValues(N))
170 F.IgnoreHeader = std::move(*Values);
172 Dict.parse(N);
175 void parse(Fragment::IndexBlock &F, Node &N) {
176 DictParser Dict("Index", this);
177 Dict.handle("Background",
178 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
179 Dict.handle("External", [&](Node &N) {
180 Fragment::IndexBlock::ExternalBlock External;
181 // External block can either be a mapping or a scalar value. Dispatch
182 // accordingly.
183 if (N.getType() == Node::NK_Mapping) {
184 parse(External, N);
185 } else if (N.getType() == Node::NK_Scalar ||
186 N.getType() == Node::NK_BlockScalar) {
187 parse(External, *scalarValue(N, "External"));
188 } else {
189 error("External must be either a scalar or a mapping.", N);
190 return;
192 F.External.emplace(std::move(External));
193 F.External->Range = N.getSourceRange();
195 Dict.handle("StandardLibrary", [&](Node &N) {
196 if (auto StandardLibrary = boolValue(N, "StandardLibrary"))
197 F.StandardLibrary = *StandardLibrary;
199 Dict.parse(N);
202 void parse(Fragment::IndexBlock::ExternalBlock &F,
203 Located<std::string> ExternalVal) {
204 if (!llvm::StringRef(*ExternalVal).equals_insensitive("none")) {
205 error("Only scalar value supported for External is 'None'",
206 ExternalVal.Range);
207 return;
209 F.IsNone = true;
210 F.IsNone.Range = ExternalVal.Range;
213 void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
214 DictParser Dict("External", this);
215 Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
216 Dict.handle("Server",
217 [&](Node &N) { F.Server = scalarValue(N, "Server"); });
218 Dict.handle("MountPoint",
219 [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
220 Dict.parse(N);
223 void parse(Fragment::CompletionBlock &F, Node &N) {
224 DictParser Dict("Completion", this);
225 Dict.handle("AllScopes", [&](Node &N) {
226 if (auto AllScopes = boolValue(N, "AllScopes"))
227 F.AllScopes = *AllScopes;
229 Dict.parse(N);
232 void parse(Fragment::HoverBlock &F, Node &N) {
233 DictParser Dict("Hover", this);
234 Dict.handle("ShowAKA", [&](Node &N) {
235 if (auto ShowAKA = boolValue(N, "ShowAKA"))
236 F.ShowAKA = *ShowAKA;
238 Dict.parse(N);
241 void parse(Fragment::InlayHintsBlock &F, Node &N) {
242 DictParser Dict("InlayHints", this);
243 Dict.handle("Enabled", [&](Node &N) {
244 if (auto Value = boolValue(N, "Enabled"))
245 F.Enabled = *Value;
247 Dict.handle("ParameterNames", [&](Node &N) {
248 if (auto Value = boolValue(N, "ParameterNames"))
249 F.ParameterNames = *Value;
251 Dict.handle("DeducedTypes", [&](Node &N) {
252 if (auto Value = boolValue(N, "DeducedTypes"))
253 F.DeducedTypes = *Value;
255 Dict.handle("Designators", [&](Node &N) {
256 if (auto Value = boolValue(N, "Designators"))
257 F.Designators = *Value;
259 Dict.handle("BlockEnd", [&](Node &N) {
260 if (auto Value = boolValue(N, "BlockEnd"))
261 F.BlockEnd = *Value;
263 Dict.handle("TypeNameLimit", [&](Node &N) {
264 if (auto Value = uint32Value(N, "TypeNameLimit"))
265 F.TypeNameLimit = *Value;
267 Dict.parse(N);
270 void parse(Fragment::SemanticTokensBlock &F, Node &N) {
271 DictParser Dict("SemanticTokens", this);
272 Dict.handle("DisabledKinds", [&](Node &N) {
273 if (auto Values = scalarValues(N))
274 F.DisabledKinds = std::move(*Values);
276 Dict.handle("DisabledModifiers", [&](Node &N) {
277 if (auto Values = scalarValues(N))
278 F.DisabledModifiers = std::move(*Values);
280 Dict.parse(N);
283 // Helper for parsing mapping nodes (dictionaries).
284 // We don't use YamlIO as we want to control over unknown keys.
285 class DictParser {
286 llvm::StringRef Description;
287 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
288 std::function<bool(Located<std::string>, Node &)> UnknownHandler;
289 Parser *Outer;
291 public:
292 DictParser(llvm::StringRef Description, Parser *Outer)
293 : Description(Description), Outer(Outer) {}
295 // Parse is called when Key is encountered, and passed the associated value.
296 // It should emit diagnostics if the value is invalid (e.g. wrong type).
297 // If Key is seen twice, Parse runs only once and an error is reported.
298 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
299 for (const auto &Entry : Keys) {
300 (void)Entry;
301 assert(Entry.first != Key && "duplicate key handler");
303 Keys.emplace_back(Key, std::move(Parse));
306 // Handler is called when a Key is not matched by any handle().
307 // If this is unset or the Handler returns true, a warning is emitted for
308 // the unknown key.
309 void
310 unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
311 UnknownHandler = std::move(Handler);
314 // Process a mapping node and call handlers for each key/value pair.
315 void parse(Node &N) const {
316 if (N.getType() != Node::NK_Mapping) {
317 Outer->error(Description + " should be a dictionary", N);
318 return;
320 llvm::SmallSet<std::string, 8> Seen;
321 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
322 // We *must* consume all items, even on error, or the parser will assert.
323 for (auto &KV : llvm::cast<MappingNode>(N)) {
324 auto *K = KV.getKey();
325 if (!K) // YAMLParser emitted an error.
326 continue;
327 auto Key = Outer->scalarValue(*K, "Dictionary key");
328 if (!Key)
329 continue;
330 if (!Seen.insert(**Key).second) {
331 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
332 if (auto *Value = KV.getValue())
333 Value->skip();
334 continue;
336 auto *Value = KV.getValue();
337 if (!Value) // YAMLParser emitted an error.
338 continue;
339 bool Matched = false;
340 for (const auto &Handler : Keys) {
341 if (Handler.first == **Key) {
342 Matched = true;
343 Handler.second(*Value);
344 break;
347 if (!Matched) {
348 bool Warn = !UnknownHandler;
349 if (UnknownHandler)
350 Warn = UnknownHandler(
351 Located<std::string>(**Key, K->getSourceRange()), *Value);
352 if (Warn)
353 UnknownKeys.push_back(std::move(*Key));
356 if (!UnknownKeys.empty())
357 warnUnknownKeys(UnknownKeys, Seen);
360 private:
361 void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
362 const llvm::SmallSet<std::string, 8> &SeenKeys) const {
363 llvm::SmallVector<llvm::StringRef> UnseenKeys;
364 for (const auto &KeyAndHandler : Keys)
365 if (!SeenKeys.count(KeyAndHandler.first.str()))
366 UnseenKeys.push_back(KeyAndHandler.first);
368 for (const Located<std::string> &UnknownKey : UnknownKeys)
369 if (auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
370 Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
371 "'; did you mean '" + *BestGuess + "'?",
372 UnknownKey.Range);
373 else
374 Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
375 "'",
376 UnknownKey.Range);
380 // Try to parse a single scalar value from the node, warn on failure.
381 std::optional<Located<std::string>> scalarValue(Node &N,
382 llvm::StringRef Desc) {
383 llvm::SmallString<256> Buf;
384 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
385 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
386 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
387 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
388 warning(Desc + " should be scalar", N);
389 return std::nullopt;
392 std::optional<Located<bool>> boolValue(Node &N, llvm::StringRef Desc) {
393 if (auto Scalar = scalarValue(N, Desc)) {
394 if (auto Bool = llvm::yaml::parseBool(**Scalar))
395 return Located<bool>(*Bool, Scalar->Range);
396 warning(Desc + " should be a boolean", N);
398 return std::nullopt;
401 std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
402 if (auto Scalar = scalarValue(N, Desc)) {
403 unsigned long long Num;
404 if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
405 return Located<uint32_t>(Num, Scalar->Range);
408 warning(Desc + " invalid number", N);
409 return std::nullopt;
412 // Try to parse a list of single scalar values, or just a single value.
413 std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
414 std::vector<Located<std::string>> Result;
415 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
416 llvm::SmallString<256> Buf;
417 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
418 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
419 Result.emplace_back(S->getValue().str(), N.getSourceRange());
420 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
421 // We *must* consume all items, even on error, or the parser will assert.
422 for (auto &Child : *S) {
423 if (auto Value = scalarValue(Child, "List item"))
424 Result.push_back(std::move(*Value));
426 } else {
427 warning("Expected scalar or list of scalars", N);
428 return std::nullopt;
430 return Result;
433 // Report a "hard" error, reflecting a config file that can never be valid.
434 void error(const llvm::Twine &Msg, llvm::SMRange Range) {
435 HadError = true;
436 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
438 void error(const llvm::Twine &Msg, const Node &N) {
439 return error(Msg, N.getSourceRange());
442 // Report a "soft" error that could be caused by e.g. version skew.
443 void warning(const llvm::Twine &Msg, llvm::SMRange Range) {
444 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
446 void warning(const llvm::Twine &Msg, const Node &N) {
447 return warning(Msg, N.getSourceRange());
451 } // namespace
453 std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
454 llvm::StringRef BufferName,
455 DiagnosticCallback Diags) {
456 // The YAML document may contain multiple conditional fragments.
457 // The SourceManager is shared for all of them.
458 auto SM = std::make_shared<llvm::SourceMgr>();
459 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
460 // Adapt DiagnosticCallback to function-pointer interface.
461 // Callback receives both errors we emit and those from the YAML parser.
462 SM->setDiagHandler(
463 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
464 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
466 &Diags);
467 std::vector<Fragment> Result;
468 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
469 if (Node *N = Doc.getRoot()) {
470 Fragment Fragment;
471 Fragment.Source.Manager = SM;
472 Fragment.Source.Location = N->getSourceRange().Start;
473 SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,
474 "Parsing config fragment");
475 if (Parser(*SM).parse(Fragment, *N))
476 Result.push_back(std::move(Fragment));
479 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
480 llvm::SourceMgr::DK_Note,
481 "Parsed " + llvm::Twine(Result.size()) +
482 " fragments from file");
483 // Hack: stash the buffer in the SourceMgr to keep it alive.
484 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
485 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
486 return Result;
489 } // namespace config
490 } // namespace clangd
491 } // namespace clang