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"
23using llvm::yaml::BlockScalarNode;
24using llvm::yaml::MappingNode;
25using llvm::yaml::Node;
26using llvm::yaml::ScalarNode;
27using llvm::yaml::SequenceNode;
29std::optional<llvm::StringRef>
30bestGuess(llvm::StringRef Search,
31 llvm::ArrayRef<llvm::StringRef> AllowedValues) {
32 unsigned MaxEdit = (Search.size() + 1) / 3;
35 std::optional<llvm::StringRef> Result;
36 for (
const auto &AllowedValue : AllowedValues) {
37 unsigned EditDistance = Search.edit_distance(AllowedValue,
true, MaxEdit);
40 if (EditDistance == 1U)
42 if (EditDistance == MaxEdit && !Result) {
43 Result = AllowedValue;
44 }
else if (EditDistance < MaxEdit) {
45 Result = AllowedValue;
46 MaxEdit = EditDistance;
54 bool HadError =
false;
57 Parser(llvm::SourceMgr &SM) : SM(SM) {}
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); });
74 return !(N.failed() || HadError);
78 void parse(Fragment::IfBlock &F, Node &N) {
79 DictParser Dict(
"If",
this);
80 Dict.unrecognized([&](Located<std::string>, Node &) {
81 F.HasUnrecognizedCondition =
true;
84 Dict.handle(
"PathMatch", [&](Node &N) {
85 if (
auto Values = scalarValues(N))
86 F.PathMatch = std::move(*Values);
88 Dict.handle(
"PathExclude", [&](Node &N) {
89 if (
auto Values = scalarValues(N))
90 F.PathExclude = std::move(*Values);
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);
101 Dict.handle(
"Add", [&](Node &N) {
102 if (
auto Values = scalarValues(N))
103 F.Add = std::move(*Values);
105 Dict.handle(
"Remove", [&](Node &N) {
106 if (
auto Values = scalarValues(N))
107 F.Remove = std::move(*Values);
109 Dict.handle(
"BuiltinHeaders", [&](Node &N) {
110 if (
auto BuiltinHeaders = scalarValue(N,
"BuiltinHeaders"))
111 F.BuiltinHeaders = *BuiltinHeaders;
113 Dict.handle(
"CompilationDatabase", [&](Node &N) {
114 F.CompilationDatabase = scalarValue(N,
"CompilationDatabase");
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);
125 Dict.handle(
"QuotedHeaders", [&](Node &N) {
126 if (
auto Values = scalarValues(N))
127 F.QuotedHeaders = std::move(*Values);
129 Dict.handle(
"AngledHeaders", [&](Node &N) {
130 if (
auto Values = scalarValues(N))
131 F.AngledHeaders = std::move(*Values);
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);
142 Dict.handle(
"UnusedIncludes", [&](Node &N) {
143 F.UnusedIncludes = scalarValue(N,
"UnusedIncludes");
145 Dict.handle(
"MissingIncludes", [&](Node &N) {
146 F.MissingIncludes = scalarValue(N,
"MissingIncludes");
148 Dict.handle(
"Includes", [&](Node &N) { parse(F.Includes, N); });
149 Dict.handle(
"ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
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);
159 Dict.handle(
"Remove", [&](Node &N) {
160 if (
auto Values = scalarValues(N))
161 F.Remove = std::move(*Values);
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));
170 CheckOptDict.parse(N);
172 Dict.handle(
"FastCheckFilter", [&](Node &N) {
173 if (
auto FastCheckFilter = scalarValue(N,
"FastCheckFilter"))
174 F.FastCheckFilter = *FastCheckFilter;
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);
185 Dict.handle(
"AnalyzeAngledIncludes", [&](Node &N) {
186 if (
auto Value = boolValue(N,
"AnalyzeAngledIncludes"))
187 F.AnalyzeAngledIncludes = *
Value;
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;
200 if (N.getType() == Node::NK_Mapping) {
202 }
else if (N.getType() == Node::NK_Scalar ||
203 N.getType() == Node::NK_BlockScalar) {
204 parse(External, *scalarValue(N,
"External"));
206 error(
"External must be either a scalar or a mapping.", N);
209 F.External.emplace(std::move(External));
210 F.External->Range = N.getSourceRange();
212 Dict.handle(
"StandardLibrary", [&](Node &N) {
213 if (
auto StandardLibrary = boolValue(N,
"StandardLibrary"))
214 F.StandardLibrary = *StandardLibrary;
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'",
227 F.IsNone.Range = ExternalVal.Range;
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"); });
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;
246 Dict.handle(
"ArgumentLists", [&](Node &N) {
247 if (
auto ArgumentLists = scalarValue(N,
"ArgumentLists"))
248 F.ArgumentLists = *ArgumentLists;
250 Dict.handle(
"HeaderInsertion", [&](Node &N) {
251 if (
auto HeaderInsertion = scalarValue(N,
"HeaderInsertion"))
252 F.HeaderInsertion = *HeaderInsertion;
254 Dict.handle(
"CodePatterns", [&](Node &N) {
255 if (
auto CodePatterns = scalarValue(N,
"CodePatterns"))
256 F.CodePatterns = *CodePatterns;
258 Dict.handle(
"MacroFilter", [&](Node &N) {
259 if (
auto MacroFilter = scalarValue(N,
"MacroFilter"))
260 F.MacroFilter = *MacroFilter;
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;
271 Dict.handle(
"MacroContentsLimit", [&](Node &N) {
272 if (
auto MacroContentsLimit = uint32Value(N,
"MacroContentsLimit"))
273 F.MacroContentsLimit = *MacroContentsLimit;
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"))
284 Dict.handle(
"ParameterNames", [&](Node &N) {
285 if (
auto Value = boolValue(N,
"ParameterNames"))
286 F.ParameterNames = *
Value;
288 Dict.handle(
"DeducedTypes", [&](Node &N) {
289 if (
auto Value = boolValue(N,
"DeducedTypes"))
290 F.DeducedTypes = *
Value;
292 Dict.handle(
"Designators", [&](Node &N) {
293 if (
auto Value = boolValue(N,
"Designators"))
294 F.Designators = *
Value;
296 Dict.handle(
"BlockEnd", [&](Node &N) {
297 if (
auto Value = boolValue(N,
"BlockEnd"))
300 Dict.handle(
"DefaultArguments", [&](Node &N) {
301 if (
auto Value = boolValue(N,
"DefaultArguments"))
302 F.DefaultArguments = *
Value;
304 Dict.handle(
"TypeNameLimit", [&](Node &N) {
305 if (
auto Value = uint32Value(N,
"TypeNameLimit"))
306 F.TypeNameLimit = *
Value;
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);
317 Dict.handle(
"DisabledModifiers", [&](Node &N) {
318 if (
auto Values = scalarValues(N))
319 F.DisabledModifiers = std::move(*Values);
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;
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;
342 DictParser(llvm::StringRef Description, Parser *Outer)
343 : Description(Description), Outer(Outer) {}
348 void handle(llvm::StringLiteral
Key, std::function<
void(Node &)> Parse) {
349 for (
const auto &Entry : Keys) {
351 assert(Entry.first !=
Key &&
"duplicate key handler");
353 Keys.emplace_back(
Key, std::move(Parse));
360 unrecognized(std::function<
bool(Located<std::string>, Node &)> Handler) {
361 UnknownHandler = std::move(Handler);
365 void parse(Node &N)
const {
366 if (N.getType() != Node::NK_Mapping) {
367 Outer->error(Description +
" should be a dictionary", N);
370 llvm::SmallSet<std::string, 8> Seen;
371 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
373 for (
auto &KV : llvm::cast<MappingNode>(N)) {
374 auto *K = KV.getKey();
377 auto Key = Outer->scalarValue(*K,
"Dictionary key");
380 if (!Seen.insert(**Key).second) {
381 Outer->warning(
"Duplicate key " + **
Key +
" is ignored", *K);
382 if (
auto *
Value = KV.getValue())
386 auto *
Value = KV.getValue();
389 bool Matched =
false;
390 for (
const auto &Handler : Keys) {
391 if (Handler.first == **
Key) {
393 Handler.second(*
Value);
398 bool Warn = !UnknownHandler;
400 Warn = UnknownHandler(
401 Located<std::string>(**
Key, K->getSourceRange()), *
Value);
403 UnknownKeys.push_back(std::move(*
Key));
406 if (!UnknownKeys.empty())
407 warnUnknownKeys(UnknownKeys, Seen);
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);
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 +
"'?",
424 Outer->warning(
"Unknown " + Description +
" key '" + *UnknownKey +
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);
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);
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);
458 warning(Desc +
" invalid number", N);
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)) {
472 for (
auto &Child : *S) {
473 if (
auto Value = scalarValue(Child,
"List item"))
474 Result.push_back(std::move(*
Value));
477 warning(
"Expected scalar or list of scalars", N);
484 void error(
const llvm::Twine &Msg, llvm::SMRange Range) {
486 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
488 void error(
const llvm::Twine &Msg,
const Node &N) {
489 return error(Msg, N.getSourceRange());
493 void warning(
const llvm::Twine &Msg, llvm::SMRange Range) {
494 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
496 void warning(
const llvm::Twine &Msg,
const Node &N) {
497 return warning(Msg, N.getSourceRange());
504 llvm::StringRef BufferName,
508 log(
"Loading config file at {0}", BufferName);
509 auto SM = std::make_shared<llvm::SourceMgr>();
510 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(
YAML, BufferName);
514 [](
const llvm::SMDiagnostic &
Diag,
void *Ctx) {
518 std::vector<Fragment> Result;
519 for (
auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
520 if (Node *N = Doc.getRoot()) {
525 "Parsing config fragment");
526 if (Parser(*SM).parse(
Fragment, *N))
527 Result.push_back(std::move(
Fragment));
530 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
531 llvm::SourceMgr::DK_Note,
532 "Parsed " + llvm::Twine(Result.size()) +
533 " fragments from file");
536 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
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.
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
void log(const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A top-level diagnostic that may have Notes and Fixes.
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).