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;
261 void parse(Fragment::HoverBlock &F, Node &N) {
262 DictParser Dict(
"Hover",
this);
263 Dict.handle(
"ShowAKA", [&](Node &N) {
264 if (
auto ShowAKA = boolValue(N,
"ShowAKA"))
265 F.ShowAKA = *ShowAKA;
267 Dict.handle(
"MacroContentsLimit", [&](Node &N) {
268 if (
auto MacroContentsLimit = uint32Value(N,
"MacroContentsLimit"))
269 F.MacroContentsLimit = *MacroContentsLimit;
274 void parse(Fragment::InlayHintsBlock &F, Node &N) {
275 DictParser Dict(
"InlayHints",
this);
276 Dict.handle(
"Enabled", [&](Node &N) {
277 if (
auto Value = boolValue(N,
"Enabled"))
280 Dict.handle(
"ParameterNames", [&](Node &N) {
281 if (
auto Value = boolValue(N,
"ParameterNames"))
282 F.ParameterNames = *
Value;
284 Dict.handle(
"DeducedTypes", [&](Node &N) {
285 if (
auto Value = boolValue(N,
"DeducedTypes"))
286 F.DeducedTypes = *
Value;
288 Dict.handle(
"Designators", [&](Node &N) {
289 if (
auto Value = boolValue(N,
"Designators"))
290 F.Designators = *
Value;
292 Dict.handle(
"BlockEnd", [&](Node &N) {
293 if (
auto Value = boolValue(N,
"BlockEnd"))
296 Dict.handle(
"DefaultArguments", [&](Node &N) {
297 if (
auto Value = boolValue(N,
"DefaultArguments"))
298 F.DefaultArguments = *
Value;
300 Dict.handle(
"TypeNameLimit", [&](Node &N) {
301 if (
auto Value = uint32Value(N,
"TypeNameLimit"))
302 F.TypeNameLimit = *
Value;
307 void parse(Fragment::SemanticTokensBlock &F, Node &N) {
308 DictParser Dict(
"SemanticTokens",
this);
309 Dict.handle(
"DisabledKinds", [&](Node &N) {
310 if (
auto Values = scalarValues(N))
311 F.DisabledKinds = std::move(*Values);
313 Dict.handle(
"DisabledModifiers", [&](Node &N) {
314 if (
auto Values = scalarValues(N))
315 F.DisabledModifiers = std::move(*Values);
320 void parse(Fragment::DocumentationBlock &F, Node &N) {
321 DictParser Dict(
"Documentation",
this);
322 Dict.handle(
"CommentFormat", [&](Node &N) {
323 if (
auto Value = scalarValue(N,
"CommentFormat"))
324 F.CommentFormat = *
Value;
332 llvm::StringRef Description;
333 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
334 std::function<bool(Located<std::string>, Node &)> UnknownHandler;
338 DictParser(llvm::StringRef Description, Parser *Outer)
339 : Description(Description), Outer(Outer) {}
344 void handle(llvm::StringLiteral
Key, std::function<
void(Node &)> Parse) {
345 for (
const auto &Entry : Keys) {
347 assert(Entry.first !=
Key &&
"duplicate key handler");
349 Keys.emplace_back(
Key, std::move(Parse));
356 unrecognized(std::function<
bool(Located<std::string>, Node &)> Handler) {
357 UnknownHandler = std::move(Handler);
361 void parse(Node &N)
const {
362 if (N.getType() != Node::NK_Mapping) {
363 Outer->error(Description +
" should be a dictionary", N);
366 llvm::SmallSet<std::string, 8> Seen;
367 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
369 for (
auto &KV : llvm::cast<MappingNode>(N)) {
370 auto *K = KV.getKey();
373 auto Key = Outer->scalarValue(*K,
"Dictionary key");
376 if (!Seen.insert(**Key).second) {
377 Outer->warning(
"Duplicate key " + **
Key +
" is ignored", *K);
378 if (
auto *
Value = KV.getValue())
382 auto *
Value = KV.getValue();
385 bool Matched =
false;
386 for (
const auto &Handler : Keys) {
387 if (Handler.first == **
Key) {
389 Handler.second(*
Value);
394 bool Warn = !UnknownHandler;
396 Warn = UnknownHandler(
397 Located<std::string>(**
Key, K->getSourceRange()), *
Value);
399 UnknownKeys.push_back(std::move(*
Key));
402 if (!UnknownKeys.empty())
403 warnUnknownKeys(UnknownKeys, Seen);
407 void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
408 const llvm::SmallSet<std::string, 8> &SeenKeys)
const {
409 llvm::SmallVector<llvm::StringRef> UnseenKeys;
410 for (
const auto &KeyAndHandler : Keys)
411 if (!SeenKeys.count(KeyAndHandler.first.str()))
412 UnseenKeys.push_back(KeyAndHandler.first);
414 for (
const Located<std::string> &UnknownKey : UnknownKeys)
415 if (
auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
416 Outer->warning(
"Unknown " + Description +
" key '" + *UnknownKey +
417 "'; did you mean '" + *BestGuess +
"'?",
420 Outer->warning(
"Unknown " + Description +
" key '" + *UnknownKey +
427 std::optional<Located<std::string>> scalarValue(Node &N,
428 llvm::StringRef Desc) {
429 llvm::SmallString<256> Buf;
430 if (
auto *S = llvm::dyn_cast<ScalarNode>(&N))
431 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
432 if (
auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
433 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
434 warning(Desc +
" should be scalar", N);
438 std::optional<Located<bool>> boolValue(Node &N, llvm::StringRef Desc) {
439 if (
auto Scalar = scalarValue(N, Desc)) {
440 if (
auto Bool = llvm::yaml::parseBool(**Scalar))
441 return Located<bool>(*Bool, Scalar->Range);
442 warning(Desc +
" should be a boolean", N);
447 std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
448 if (
auto Scalar = scalarValue(N, Desc)) {
449 unsigned long long Num;
450 if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
451 return Located<uint32_t>(Num, Scalar->Range);
454 warning(Desc +
" invalid number", N);
459 std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
460 std::vector<Located<std::string>> Result;
461 if (
auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
462 llvm::SmallString<256> Buf;
463 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
464 }
else if (
auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
465 Result.emplace_back(S->getValue().str(), N.getSourceRange());
466 }
else if (
auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
468 for (
auto &Child : *S) {
469 if (
auto Value = scalarValue(Child,
"List item"))
470 Result.push_back(std::move(*
Value));
473 warning(
"Expected scalar or list of scalars", N);
480 void error(
const llvm::Twine &Msg, llvm::SMRange Range) {
482 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
484 void error(
const llvm::Twine &Msg,
const Node &N) {
485 return error(Msg, N.getSourceRange());
489 void warning(
const llvm::Twine &Msg, llvm::SMRange Range) {
490 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
492 void warning(
const llvm::Twine &Msg,
const Node &N) {
493 return warning(Msg, N.getSourceRange());
500 llvm::StringRef BufferName,
504 log(
"Loading config file at {0}", BufferName);
505 auto SM = std::make_shared<llvm::SourceMgr>();
506 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(
YAML, BufferName);
510 [](
const llvm::SMDiagnostic &
Diag,
void *Ctx) {
514 std::vector<Fragment> Result;
515 for (
auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
516 if (Node *N = Doc.getRoot()) {
521 "Parsing config fragment");
522 if (Parser(*SM).parse(
Fragment, *N))
523 Result.push_back(std::move(
Fragment));
526 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
527 llvm::SourceMgr::DK_Note,
528 "Parsed " + llvm::Twine(Result.size()) +
529 " fragments from file");
532 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).