539 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8#include "ConfigFragment.h"9#include "support/Logger.h"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"16#include <optional>17#include <string>18 19namespace clang {20namespace clangd {21namespace config {22namespace {23using llvm::yaml::BlockScalarNode;24using llvm::yaml::MappingNode;25using llvm::yaml::Node;26using llvm::yaml::ScalarNode;27using llvm::yaml::SequenceNode;28 29std::optional<llvm::StringRef>30bestGuess(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 and39 // 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;47 }48 }49 return Result;50}51 52class Parser {53 llvm::SourceMgr &SM;54 bool HadError = false;55 56public:57 Parser(llvm::SourceMgr &SM) : SM(SM) {}58 59 // Tries to parse N into F, returning false if it failed and we couldn't60 // 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.handle("Documentation", [&](Node &N) { parse(F.Documentation, N); });73 Dict.parse(N);74 return !(N.failed() || HadError);75 }76 77private:78 void parse(Fragment::IfBlock &F, Node &N) {79 DictParser Dict("If", this);80 Dict.unrecognized([&](Located<std::string>, Node &) {81 F.HasUnrecognizedCondition = true;82 return true; // Emit a warning for the unrecognized key.83 });84 Dict.handle("PathMatch", [&](Node &N) {85 if (auto Values = scalarValues(N))86 F.PathMatch = std::move(*Values);87 });88 Dict.handle("PathExclude", [&](Node &N) {89 if (auto Values = scalarValues(N))90 F.PathExclude = std::move(*Values);91 });92 Dict.parse(N);93 }94 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);100 });101 Dict.handle("Add", [&](Node &N) {102 if (auto Values = scalarValues(N))103 F.Add = std::move(*Values);104 });105 Dict.handle("Remove", [&](Node &N) {106 if (auto Values = scalarValues(N))107 F.Remove = std::move(*Values);108 });109 Dict.handle("BuiltinHeaders", [&](Node &N) {110 if (auto BuiltinHeaders = scalarValue(N, "BuiltinHeaders"))111 F.BuiltinHeaders = *BuiltinHeaders;112 });113 Dict.handle("CompilationDatabase", [&](Node &N) {114 F.CompilationDatabase = scalarValue(N, "CompilationDatabase");115 });116 Dict.parse(N);117 }118 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);124 });125 Dict.handle("QuotedHeaders", [&](Node &N) {126 if (auto Values = scalarValues(N))127 F.QuotedHeaders = std::move(*Values);128 });129 Dict.handle("AngledHeaders", [&](Node &N) {130 if (auto Values = scalarValues(N))131 F.AngledHeaders = std::move(*Values);132 });133 Dict.parse(N);134 }135 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);141 });142 Dict.handle("UnusedIncludes", [&](Node &N) {143 F.UnusedIncludes = scalarValue(N, "UnusedIncludes");144 });145 Dict.handle("MissingIncludes", [&](Node &N) {146 F.MissingIncludes = scalarValue(N, "MissingIncludes");147 });148 Dict.handle("Includes", [&](Node &N) { parse(F.Includes, N); });149 Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });150 Dict.parse(N);151 }152 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);158 });159 Dict.handle("Remove", [&](Node &N) {160 if (auto Values = scalarValues(N))161 F.Remove = std::move(*Values);162 });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));168 return false; // Don't emit a warning169 });170 CheckOptDict.parse(N);171 });172 Dict.handle("FastCheckFilter", [&](Node &N) {173 if (auto FastCheckFilter = scalarValue(N, "FastCheckFilter"))174 F.FastCheckFilter = *FastCheckFilter;175 });176 Dict.parse(N);177 }178 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);184 });185 Dict.handle("AnalyzeAngledIncludes", [&](Node &N) {186 if (auto Value = boolValue(N, "AnalyzeAngledIncludes"))187 F.AnalyzeAngledIncludes = *Value;188 });189 Dict.parse(N);190 }191 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;198 // External block can either be a mapping or a scalar value. Dispatch199 // accordingly.200 if (N.getType() == Node::NK_Mapping) {201 parse(External, N);202 } else if (N.getType() == Node::NK_Scalar ||203 N.getType() == Node::NK_BlockScalar) {204 parse(External, *scalarValue(N, "External"));205 } else {206 error("External must be either a scalar or a mapping.", N);207 return;208 }209 F.External.emplace(std::move(External));210 F.External->Range = N.getSourceRange();211 });212 Dict.handle("StandardLibrary", [&](Node &N) {213 if (auto StandardLibrary = boolValue(N, "StandardLibrary"))214 F.StandardLibrary = *StandardLibrary;215 });216 Dict.parse(N);217 }218 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'",223 ExternalVal.Range);224 return;225 }226 F.IsNone = true;227 F.IsNone.Range = ExternalVal.Range;228 }229 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"); });237 Dict.parse(N);238 }239 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;245 });246 Dict.handle("ArgumentLists", [&](Node &N) {247 if (auto ArgumentLists = scalarValue(N, "ArgumentLists"))248 F.ArgumentLists = *ArgumentLists;249 });250 Dict.handle("HeaderInsertion", [&](Node &N) {251 if (auto HeaderInsertion = scalarValue(N, "HeaderInsertion"))252 F.HeaderInsertion = *HeaderInsertion;253 });254 Dict.handle("CodePatterns", [&](Node &N) {255 if (auto CodePatterns = scalarValue(N, "CodePatterns"))256 F.CodePatterns = *CodePatterns;257 });258 Dict.parse(N);259 }260 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;266 });267 Dict.handle("MacroContentsLimit", [&](Node &N) {268 if (auto MacroContentsLimit = uint32Value(N, "MacroContentsLimit"))269 F.MacroContentsLimit = *MacroContentsLimit;270 });271 Dict.parse(N);272 }273 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"))278 F.Enabled = *Value;279 });280 Dict.handle("ParameterNames", [&](Node &N) {281 if (auto Value = boolValue(N, "ParameterNames"))282 F.ParameterNames = *Value;283 });284 Dict.handle("DeducedTypes", [&](Node &N) {285 if (auto Value = boolValue(N, "DeducedTypes"))286 F.DeducedTypes = *Value;287 });288 Dict.handle("Designators", [&](Node &N) {289 if (auto Value = boolValue(N, "Designators"))290 F.Designators = *Value;291 });292 Dict.handle("BlockEnd", [&](Node &N) {293 if (auto Value = boolValue(N, "BlockEnd"))294 F.BlockEnd = *Value;295 });296 Dict.handle("DefaultArguments", [&](Node &N) {297 if (auto Value = boolValue(N, "DefaultArguments"))298 F.DefaultArguments = *Value;299 });300 Dict.handle("TypeNameLimit", [&](Node &N) {301 if (auto Value = uint32Value(N, "TypeNameLimit"))302 F.TypeNameLimit = *Value;303 });304 Dict.parse(N);305 }306 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);312 });313 Dict.handle("DisabledModifiers", [&](Node &N) {314 if (auto Values = scalarValues(N))315 F.DisabledModifiers = std::move(*Values);316 });317 Dict.parse(N);318 }319 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;325 });326 Dict.parse(N);327 }328 329 // Helper for parsing mapping nodes (dictionaries).330 // We don't use YamlIO as we want to control over unknown keys.331 class DictParser {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;335 Parser *Outer;336 337 public:338 DictParser(llvm::StringRef Description, Parser *Outer)339 : Description(Description), Outer(Outer) {}340 341 // Parse is called when Key is encountered, and passed the associated value.342 // It should emit diagnostics if the value is invalid (e.g. wrong type).343 // If Key is seen twice, Parse runs only once and an error is reported.344 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {345 for (const auto &Entry : Keys) {346 (void)Entry;347 assert(Entry.first != Key && "duplicate key handler");348 }349 Keys.emplace_back(Key, std::move(Parse));350 }351 352 // Handler is called when a Key is not matched by any handle().353 // If this is unset or the Handler returns true, a warning is emitted for354 // the unknown key.355 void356 unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {357 UnknownHandler = std::move(Handler);358 }359 360 // Process a mapping node and call handlers for each key/value pair.361 void parse(Node &N) const {362 if (N.getType() != Node::NK_Mapping) {363 Outer->error(Description + " should be a dictionary", N);364 return;365 }366 llvm::SmallSet<std::string, 8> Seen;367 llvm::SmallVector<Located<std::string>, 0> UnknownKeys;368 // We *must* consume all items, even on error, or the parser will assert.369 for (auto &KV : llvm::cast<MappingNode>(N)) {370 auto *K = KV.getKey();371 if (!K) // YAMLParser emitted an error.372 continue;373 auto Key = Outer->scalarValue(*K, "Dictionary key");374 if (!Key)375 continue;376 if (!Seen.insert(**Key).second) {377 Outer->warning("Duplicate key " + **Key + " is ignored", *K);378 if (auto *Value = KV.getValue())379 Value->skip();380 continue;381 }382 auto *Value = KV.getValue();383 if (!Value) // YAMLParser emitted an error.384 continue;385 bool Matched = false;386 for (const auto &Handler : Keys) {387 if (Handler.first == **Key) {388 Matched = true;389 Handler.second(*Value);390 break;391 }392 }393 if (!Matched) {394 bool Warn = !UnknownHandler;395 if (UnknownHandler)396 Warn = UnknownHandler(397 Located<std::string>(**Key, K->getSourceRange()), *Value);398 if (Warn)399 UnknownKeys.push_back(std::move(*Key));400 }401 }402 if (!UnknownKeys.empty())403 warnUnknownKeys(UnknownKeys, Seen);404 }405 406 private: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);413 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 + "'?",418 UnknownKey.Range);419 else420 Outer->warning("Unknown " + Description + " key '" + *UnknownKey +421 "'",422 UnknownKey.Range);423 }424 };425 426 // Try to parse a single scalar value from the node, warn on failure.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);435 return std::nullopt;436 }437 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);443 }444 return std::nullopt;445 }446 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);452 }453 }454 warning(Desc + " invalid number", N);455 return std::nullopt;456 }457 458 // Try to parse a list of single scalar values, or just a single value.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)) {467 // We *must* consume all items, even on error, or the parser will assert.468 for (auto &Child : *S) {469 if (auto Value = scalarValue(Child, "List item"))470 Result.push_back(std::move(*Value));471 }472 } else {473 warning("Expected scalar or list of scalars", N);474 return std::nullopt;475 }476 return Result;477 }478 479 // Report a "hard" error, reflecting a config file that can never be valid.480 void error(const llvm::Twine &Msg, llvm::SMRange Range) {481 HadError = true;482 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);483 }484 void error(const llvm::Twine &Msg, const Node &N) {485 return error(Msg, N.getSourceRange());486 }487 488 // Report a "soft" error that could be caused by e.g. version skew.489 void warning(const llvm::Twine &Msg, llvm::SMRange Range) {490 SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);491 }492 void warning(const llvm::Twine &Msg, const Node &N) {493 return warning(Msg, N.getSourceRange());494 }495};496 497} // namespace498 499std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,500 llvm::StringRef BufferName,501 DiagnosticCallback Diags) {502 // The YAML document may contain multiple conditional fragments.503 // The SourceManager is shared for all of them.504 log("Loading config file at {0}", BufferName);505 auto SM = std::make_shared<llvm::SourceMgr>();506 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);507 // Adapt DiagnosticCallback to function-pointer interface.508 // Callback receives both errors we emit and those from the YAML parser.509 SM->setDiagHandler(510 [](const llvm::SMDiagnostic &Diag, void *Ctx) {511 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);512 },513 &Diags);514 std::vector<Fragment> Result;515 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {516 if (Node *N = Doc.getRoot()) {517 Fragment Fragment;518 Fragment.Source.Manager = SM;519 Fragment.Source.Location = N->getSourceRange().Start;520 SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,521 "Parsing config fragment");522 if (Parser(*SM).parse(Fragment, *N))523 Result.push_back(std::move(Fragment));524 }525 }526 SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),527 llvm::SourceMgr::DK_Note,528 "Parsed " + llvm::Twine(Result.size()) +529 " fragments from file");530 // Hack: stash the buffer in the SourceMgr to keep it alive.531 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.532 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());533 return Result;534}535 536} // namespace config537} // namespace clangd538} // namespace clang539