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"
17#include <system_error>
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); });
73 return !(N.failed() || HadError);
77 void parse(Fragment::IfBlock &F, Node &N) {
78 DictParser Dict(
"If",
this);
79 Dict.unrecognized([&](Located<std::string>, Node &) {
80 F.HasUnrecognizedCondition =
true;
83 Dict.handle(
"PathMatch", [&](Node &N) {
84 if (
auto Values = scalarValues(N))
85 F.PathMatch = std::move(*Values);
87 Dict.handle(
"PathExclude", [&](Node &N) {
88 if (
auto Values = scalarValues(N))
89 F.PathExclude = std::move(*Values);
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);
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");
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);
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); });
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));
157 CheckOptDict.parse(N);
159 Dict.handle(
"FastCheckFilter", [&](Node &N) {
160 if (
auto FastCheckFilter = scalarValue(N,
"FastCheckFilter"))
161 F.FastCheckFilter = *FastCheckFilter;
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.handle(
"AnalyzeAngledIncludes", [&](Node &N) {
173 if (
auto Value = boolValue(N,
"AnalyzeAngledIncludes"))
174 F.AnalyzeAngledIncludes = *
Value;
179 void parse(Fragment::IndexBlock &F, Node &N) {
180 DictParser Dict(
"Index",
this);
181 Dict.handle(
"Background",
182 [&](Node &N) { F.Background = scalarValue(N,
"Background"); });
183 Dict.handle(
"External", [&](Node &N) {
184 Fragment::IndexBlock::ExternalBlock External;
187 if (N.getType() == Node::NK_Mapping) {
189 }
else if (N.getType() == Node::NK_Scalar ||
190 N.getType() == Node::NK_BlockScalar) {
191 parse(External, *scalarValue(N,
"External"));
193 error(
"External must be either a scalar or a mapping.", N);
196 F.External.emplace(std::move(External));
197 F.External->Range = N.getSourceRange();
199 Dict.handle(
"StandardLibrary", [&](Node &N) {
200 if (
auto StandardLibrary = boolValue(N,
"StandardLibrary"))
201 F.StandardLibrary = *StandardLibrary;
206 void parse(Fragment::IndexBlock::ExternalBlock &F,
207 Located<std::string> ExternalVal) {
208 if (!llvm::StringRef(*ExternalVal).equals_insensitive(
"none")) {
209 error(
"Only scalar value supported for External is 'None'",
214 F.IsNone.Range = ExternalVal.Range;
217 void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
218 DictParser Dict(
"External",
this);
219 Dict.handle(
"File", [&](Node &N) { F.File = scalarValue(N,
"File"); });
220 Dict.handle(
"Server",
221 [&](Node &N) { F.Server = scalarValue(N,
"Server"); });
222 Dict.handle(
"MountPoint",
223 [&](Node &N) { F.MountPoint = scalarValue(N,
"MountPoint"); });
227 void parse(Fragment::CompletionBlock &F, Node &N) {
228 DictParser Dict(
"Completion",
this);
229 Dict.handle(
"AllScopes", [&](Node &N) {
230 if (
auto AllScopes = boolValue(N,
"AllScopes"))
231 F.AllScopes = *AllScopes;
236 void parse(Fragment::HoverBlock &F, Node &N) {
237 DictParser Dict(
"Hover",
this);
238 Dict.handle(
"ShowAKA", [&](Node &N) {
239 if (
auto ShowAKA = boolValue(N,
"ShowAKA"))
240 F.ShowAKA = *ShowAKA;
245 void parse(Fragment::InlayHintsBlock &F, Node &N) {
246 DictParser Dict(
"InlayHints",
this);
247 Dict.handle(
"Enabled", [&](Node &N) {
248 if (
auto Value = boolValue(N,
"Enabled"))
251 Dict.handle(
"ParameterNames", [&](Node &N) {
252 if (
auto Value = boolValue(N,
"ParameterNames"))
253 F.ParameterNames = *
Value;
255 Dict.handle(
"DeducedTypes", [&](Node &N) {
256 if (
auto Value = boolValue(N,
"DeducedTypes"))
257 F.DeducedTypes = *
Value;
259 Dict.handle(
"Designators", [&](Node &N) {
260 if (
auto Value = boolValue(N,
"Designators"))
261 F.Designators = *
Value;
263 Dict.handle(
"BlockEnd", [&](Node &N) {
264 if (
auto Value = boolValue(N,
"BlockEnd"))
267 Dict.handle(
"TypeNameLimit", [&](Node &N) {
268 if (
auto Value = uint32Value(N,
"TypeNameLimit"))
269 F.TypeNameLimit = *
Value;
274 void parse(Fragment::SemanticTokensBlock &F, Node &N) {
275 DictParser Dict(
"SemanticTokens",
this);
276 Dict.handle(
"DisabledKinds", [&](Node &N) {
277 if (
auto Values = scalarValues(N))
278 F.DisabledKinds = std::move(*Values);
280 Dict.handle(
"DisabledModifiers", [&](Node &N) {
281 if (
auto Values = scalarValues(N))
282 F.DisabledModifiers = std::move(*Values);
290 llvm::StringRef Description;
291 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
292 std::function<bool(Located<std::string>, Node &)> UnknownHandler;
296 DictParser(llvm::StringRef Description, Parser *Outer)
297 : Description(Description), Outer(Outer) {}
302 void handle(llvm::StringLiteral Key, std::function<
void(Node &)> Parse) {
303 for (
const auto &
Entry : Keys) {
305 assert(
Entry.first != Key &&
"duplicate key handler");
307 Keys.emplace_back(Key, std::move(Parse));
314 unrecognized(std::function<
bool(Located<std::string>, Node &)> Handler) {
315 UnknownHandler = std::move(Handler);
319 void parse(Node &N)
const {
320 if (N.getType() != Node::NK_Mapping) {
321 Outer->error(Description +
" should be a dictionary", N);
324 llvm::SmallSet<std::string, 8> Seen;
325 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
327 for (
auto &KV : llvm::cast<MappingNode>(N)) {
328 auto *
K = KV.getKey();
331 auto Key = Outer->scalarValue(*
K,
"Dictionary key");
334 if (!Seen.insert(**Key).second) {
335 Outer->warning(
"Duplicate key " + **Key +
" is ignored", *
K);
336 if (
auto *
Value = KV.getValue())
340 auto *
Value = KV.getValue();
343 bool Matched =
false;
344 for (
const auto &Handler : Keys) {
345 if (Handler.first == **Key) {
347 Handler.second(*
Value);
352 bool Warn = !UnknownHandler;
354 Warn = UnknownHandler(
355 Located<std::string>(**Key,
K->getSourceRange()), *
Value);
357 UnknownKeys.push_back(std::move(*Key));
360 if (!UnknownKeys.empty())
361 warnUnknownKeys(UnknownKeys, Seen);
365 void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
366 const llvm::SmallSet<std::string, 8> &SeenKeys)
const {
367 llvm::SmallVector<llvm::StringRef> UnseenKeys;
368 for (
const auto &KeyAndHandler : Keys)
369 if (!SeenKeys.count(KeyAndHandler.first.str()))
370 UnseenKeys.push_back(KeyAndHandler.first);
372 for (
const Located<std::string> &UnknownKey : UnknownKeys)
373 if (
auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
374 Outer->warning(
"Unknown " + Description +
" key '" + *UnknownKey +
375 "'; did you mean '" + *BestGuess +
"'?",
378 Outer->warning(
"Unknown " + Description +
" key '" + *UnknownKey +
385 std::optional<Located<std::string>> scalarValue(Node &N,
386 llvm::StringRef Desc) {
387 llvm::SmallString<256> Buf;
388 if (
auto *S = llvm::dyn_cast<ScalarNode>(&N))
389 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
390 if (
auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
391 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
392 warning(Desc +
" should be scalar", N);
396 std::optional<Located<bool>> boolValue(Node &N, llvm::StringRef Desc) {
397 if (
auto Scalar = scalarValue(N, Desc)) {
398 if (
auto Bool = llvm::yaml::parseBool(**Scalar))
399 return Located<bool>(*Bool, Scalar->Range);
400 warning(Desc +
" should be a boolean", N);
405 std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
406 if (
auto Scalar = scalarValue(N, Desc)) {
407 unsigned long long Num;
408 if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
409 return Located<uint32_t>(Num, Scalar->Range);
412 warning(Desc +
" invalid number", N);
417 std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
418 std::vector<Located<std::string>> Result;
419 if (
auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
420 llvm::SmallString<256> Buf;
421 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
422 }
else if (
auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
423 Result.emplace_back(S->getValue().str(), N.getSourceRange());
424 }
else if (
auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
426 for (
auto &Child : *S) {
427 if (
auto Value = scalarValue(Child,
"List item"))
428 Result.push_back(std::move(*
Value));
431 warning(
"Expected scalar or list of scalars", N);
438 void error(
const llvm::Twine &Msg, llvm::SMRange Range) {
440 SM.PrintMessage(
Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
442 void error(
const llvm::Twine &Msg,
const Node &N) {
443 return error(Msg, N.getSourceRange());
447 void warning(
const llvm::Twine &Msg, llvm::SMRange Range) {
448 SM.PrintMessage(
Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
450 void warning(
const llvm::Twine &Msg,
const Node &N) {
451 return warning(Msg, N.getSourceRange());
458 llvm::StringRef BufferName,
462 auto SM = std::make_shared<llvm::SourceMgr>();
463 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(
YAML, BufferName);
467 [](
const llvm::SMDiagnostic &
Diag,
void *Ctx) {
471 std::vector<Fragment> Result;
472 for (
auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
473 if (
Node *N = Doc.getRoot()) {
478 "Parsing config fragment");
479 if (Parser(*SM).parse(
Fragment, *N))
480 Result.push_back(std::move(
Fragment));
483 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
484 llvm::SourceMgr::DK_Note,
485 "Parsed " + llvm::Twine(Result.size()) +
486 " fragments from file");
489 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
CharSourceRange Range
SourceRange for the file name.
::clang::DynTypedNode Node
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)
===– 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).