clang-tools  14.0.0git
ConfigYAML.cpp
Go to the documentation of this file.
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/Optional.h"
10 #include "llvm/ADT/SmallSet.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 <string>
16 #include <system_error>
17 
18 namespace clang {
19 namespace clangd {
20 namespace config {
21 namespace {
22 using llvm::yaml::BlockScalarNode;
23 using llvm::yaml::MappingNode;
24 using llvm::yaml::Node;
25 using llvm::yaml::ScalarNode;
26 using llvm::yaml::SequenceNode;
27 
28 llvm::Optional<llvm::StringRef>
29 bestGuess(llvm::StringRef Search,
30  llvm::ArrayRef<llvm::StringRef> AllowedValues) {
31  unsigned MaxEdit = (Search.size() + 1) / 3;
32  if (!MaxEdit)
33  return llvm::None;
34  llvm::Optional<llvm::StringRef> Result;
35  for (const auto &AllowedValue : AllowedValues) {
36  unsigned EditDistance = Search.edit_distance(AllowedValue, true, MaxEdit);
37  // We can't do better than an edit distance of 1, so just return this and
38  // save computing other values.
39  if (EditDistance == 1U)
40  return AllowedValue;
41  if (EditDistance == MaxEdit && !Result) {
42  Result = AllowedValue;
43  } else if (EditDistance < MaxEdit) {
44  Result = AllowedValue;
45  MaxEdit = EditDistance;
46  }
47  }
48  return Result;
49 }
50 
51 class Parser {
53  bool HadError = false;
54 
55 public:
56  Parser(llvm::SourceMgr &SM) : SM(SM) {}
57 
58  // Tries to parse N into F, returning false if it failed and we couldn't
59  // meaningfully recover (YAML syntax error, or hard semantic error).
60  bool parse(Fragment &F, Node &N) {
61  DictParser Dict("Config", this);
62  Dict.handle("If", [&](Node &N) { parse(F.If, N); });
63  Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
64  Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
65  Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
66  Dict.handle("Diagnostics", [&](Node &N) { parse(F.Diagnostics, N); });
67  Dict.handle("Completion", [&](Node &N) { parse(F.Completion, N); });
68  Dict.parse(N);
69  return !(N.failed() || HadError);
70  }
71 
72 private:
73  void parse(Fragment::IfBlock &F, Node &N) {
74  DictParser Dict("If", this);
75  Dict.unrecognized([&](Located<std::string>, Node &) {
76  F.HasUnrecognizedCondition = true;
77  return true; // Emit a warning for the unrecognized key.
78  });
79  Dict.handle("PathMatch", [&](Node &N) {
80  if (auto Values = scalarValues(N))
81  F.PathMatch = std::move(*Values);
82  });
83  Dict.handle("PathExclude", [&](Node &N) {
84  if (auto Values = scalarValues(N))
85  F.PathExclude = std::move(*Values);
86  });
87  Dict.parse(N);
88  }
89 
90  void parse(Fragment::CompileFlagsBlock &F, Node &N) {
91  DictParser Dict("CompileFlags", this);
92  Dict.handle("Add", [&](Node &N) {
93  if (auto Values = scalarValues(N))
94  F.Add = std::move(*Values);
95  });
96  Dict.handle("Remove", [&](Node &N) {
97  if (auto Values = scalarValues(N))
98  F.Remove = std::move(*Values);
99  });
100  Dict.handle("CompilationDatabase", [&](Node &N) {
101  F.CompilationDatabase = scalarValue(N, "CompilationDatabase");
102  });
103  Dict.parse(N);
104  }
105 
106  void parse(Fragment::StyleBlock &F, Node &N) {
107  DictParser Dict("Style", this);
108  Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
109  if (auto Values = scalarValues(N))
110  F.FullyQualifiedNamespaces = std::move(*Values);
111  });
112  Dict.parse(N);
113  }
114 
115  void parse(Fragment::DiagnosticsBlock &F, Node &N) {
116  DictParser Dict("Diagnostics", this);
117  Dict.handle("Suppress", [&](Node &N) {
118  if (auto Values = scalarValues(N))
119  F.Suppress = std::move(*Values);
120  });
121  Dict.handle("UnusedIncludes", [&](Node &N) {
122  F.UnusedIncludes = scalarValue(N, "UnusedIncludes");
123  });
124  Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
125  Dict.parse(N);
126  }
127 
128  void parse(Fragment::DiagnosticsBlock::ClangTidyBlock &F, Node &N) {
129  DictParser Dict("ClangTidy", this);
130  Dict.handle("Add", [&](Node &N) {
131  if (auto Values = scalarValues(N))
132  F.Add = std::move(*Values);
133  });
134  Dict.handle("Remove", [&](Node &N) {
135  if (auto Values = scalarValues(N))
136  F.Remove = std::move(*Values);
137  });
138  Dict.handle("CheckOptions", [&](Node &N) {
139  DictParser CheckOptDict("CheckOptions", this);
140  CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
141  if (auto Value = scalarValue(Val, *Key))
142  F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
143  return false; // Don't emit a warning
144  });
145  CheckOptDict.parse(N);
146  });
147  Dict.parse(N);
148  }
149 
150  void parse(Fragment::IndexBlock &F, Node &N) {
151  DictParser Dict("Index", this);
152  Dict.handle("Background",
153  [&](Node &N) { F.Background = scalarValue(N, "Background"); });
154  Dict.handle("External", [&](Node &N) {
155  Fragment::IndexBlock::ExternalBlock External;
156  // External block can either be a mapping or a scalar value. Dispatch
157  // accordingly.
158  if (N.getType() == Node::NK_Mapping) {
159  parse(External, N);
160  } else if (N.getType() == Node::NK_Scalar ||
161  N.getType() == Node::NK_BlockScalar) {
162  parse(External, scalarValue(N, "External").getValue());
163  } else {
164  error("External must be either a scalar or a mapping.", N);
165  return;
166  }
167  F.External.emplace(std::move(External));
168  F.External->Range = N.getSourceRange();
169  });
170  Dict.parse(N);
171  }
172 
173  void parse(Fragment::IndexBlock::ExternalBlock &F,
174  Located<std::string> ExternalVal) {
175  if (!llvm::StringRef(*ExternalVal).equals_insensitive("none")) {
176  error("Only scalar value supported for External is 'None'",
177  ExternalVal.Range);
178  return;
179  }
180  F.IsNone = true;
181  F.IsNone.Range = ExternalVal.Range;
182  }
183 
184  void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
185  DictParser Dict("External", this);
186  Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
187  Dict.handle("Server",
188  [&](Node &N) { F.Server = scalarValue(N, "Server"); });
189  Dict.handle("MountPoint",
190  [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
191  Dict.parse(N);
192  }
193 
194  void parse(Fragment::CompletionBlock &F, Node &N) {
195  DictParser Dict("Completion", this);
196  Dict.handle("AllScopes", [&](Node &N) {
197  if (auto Value = scalarValue(N, "AllScopes")) {
198  if (auto AllScopes = llvm::yaml::parseBool(**Value))
199  F.AllScopes = *AllScopes;
200  else
201  warning("AllScopes should be a boolean", N);
202  }
203  });
204  Dict.parse(N);
205  }
206 
207  // Helper for parsing mapping nodes (dictionaries).
208  // We don't use YamlIO as we want to control over unknown keys.
209  class DictParser {
210  llvm::StringRef Description;
211  std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
212  std::function<bool(Located<std::string>, Node &)> UnknownHandler;
213  Parser *Outer;
214 
215  public:
216  DictParser(llvm::StringRef Description, Parser *Outer)
218 
219  // Parse is called when Key is encountered, and passed the associated value.
220  // It should emit diagnostics if the value is invalid (e.g. wrong type).
221  // If Key is seen twice, Parse runs only once and an error is reported.
222  void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
223  for (const auto &Entry : Keys) {
224  (void) Entry;
225  assert(Entry.first != Key && "duplicate key handler");
226  }
227  Keys.emplace_back(Key, std::move(Parse));
228  }
229 
230  // Handler is called when a Key is not matched by any handle().
231  // If this is unset or the Handler returns true, a warning is emitted for
232  // the unknown key.
233  void
234  unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
235  UnknownHandler = std::move(Handler);
236  }
237 
238  // Process a mapping node and call handlers for each key/value pair.
239  void parse(Node &N) const {
240  if (N.getType() != Node::NK_Mapping) {
241  Outer->error(Description + " should be a dictionary", N);
242  return;
243  }
244  llvm::SmallSet<std::string, 8> Seen;
245  llvm::SmallVector<Located<std::string>, 0> UnknownKeys;
246  // We *must* consume all items, even on error, or the parser will assert.
247  for (auto &KV : llvm::cast<MappingNode>(N)) {
248  auto *K = KV.getKey();
249  if (!K) // YAMLParser emitted an error.
250  continue;
251  auto Key = Outer->scalarValue(*K, "Dictionary key");
252  if (!Key)
253  continue;
254  if (!Seen.insert(**Key).second) {
255  Outer->warning("Duplicate key " + **Key + " is ignored", *K);
256  if (auto *Value = KV.getValue())
257  Value->skip();
258  continue;
259  }
260  auto *Value = KV.getValue();
261  if (!Value) // YAMLParser emitted an error.
262  continue;
263  bool Matched = false;
264  for (const auto &Handler : Keys) {
265  if (Handler.first == **Key) {
266  Matched = true;
267  Handler.second(*Value);
268  break;
269  }
270  }
271  if (!Matched) {
272  bool Warn = !UnknownHandler;
273  if (UnknownHandler)
274  Warn = UnknownHandler(
275  Located<std::string>(**Key, K->getSourceRange()), *Value);
276  if (Warn)
277  UnknownKeys.push_back(std::move(*Key));
278  }
279  }
280  if (!UnknownKeys.empty())
281  warnUnknownKeys(UnknownKeys, Seen);
282  }
283 
284  private:
285  void warnUnknownKeys(llvm::ArrayRef<Located<std::string>> UnknownKeys,
286  const llvm::SmallSet<std::string, 8> &SeenKeys) const {
287  llvm::SmallVector<llvm::StringRef> UnseenKeys;
288  for (const auto &KeyAndHandler : Keys)
289  if (!SeenKeys.count(KeyAndHandler.first.str()))
290  UnseenKeys.push_back(KeyAndHandler.first);
291 
292  for (const Located<std::string> &UnknownKey : UnknownKeys)
293  if (auto BestGuess = bestGuess(*UnknownKey, UnseenKeys))
294  Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
295  "'; did you mean '" + *BestGuess + "'?",
296  UnknownKey.Range);
297  else
298  Outer->warning("Unknown " + Description + " key '" + *UnknownKey +
299  "'",
300  UnknownKey.Range);
301  }
302  };
303 
304  // Try to parse a single scalar value from the node, warn on failure.
305  llvm::Optional<Located<std::string>> scalarValue(Node &N,
306  llvm::StringRef Desc) {
307  llvm::SmallString<256> Buf;
308  if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
309  return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
310  if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
311  return Located<std::string>(BS->getValue().str(), N.getSourceRange());
312  warning(Desc + " should be scalar", N);
313  return llvm::None;
314  }
315 
316  // Try to parse a list of single scalar values, or just a single value.
317  llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
318  std::vector<Located<std::string>> Result;
319  if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
320  llvm::SmallString<256> Buf;
321  Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
322  } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
323  Result.emplace_back(S->getValue().str(), N.getSourceRange());
324  } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
325  // We *must* consume all items, even on error, or the parser will assert.
326  for (auto &Child : *S) {
327  if (auto Value = scalarValue(Child, "List item"))
328  Result.push_back(std::move(*Value));
329  }
330  } else {
331  warning("Expected scalar or list of scalars", N);
332  return llvm::None;
333  }
334  return Result;
335  }
336 
337  // Report a "hard" error, reflecting a config file that can never be valid.
338  void error(const llvm::Twine &Msg, llvm::SMRange Range) {
339  HadError = true;
340  SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Error, Msg, Range);
341  }
342  void error(const llvm::Twine &Msg, const Node &N) {
343  return error(Msg, N.getSourceRange());
344  }
345 
346  // Report a "soft" error that could be caused by e.g. version skew.
347  void warning(const llvm::Twine &Msg, llvm::SMRange Range) {
348  SM.PrintMessage(Range.Start, llvm::SourceMgr::DK_Warning, Msg, Range);
349  }
350  void warning(const llvm::Twine &Msg, const Node &N) {
351  return warning(Msg, N.getSourceRange());
352  }
353 };
354 
355 } // namespace
356 
357 std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
358  llvm::StringRef BufferName,
360  // The YAML document may contain multiple conditional fragments.
361  // The SourceManager is shared for all of them.
362  auto SM = std::make_shared<llvm::SourceMgr>();
363  auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
364  // Adapt DiagnosticCallback to function-pointer interface.
365  // Callback receives both errors we emit and those from the YAML parser.
366  SM->setDiagHandler(
367  [](const llvm::SMDiagnostic &Diag, void *Ctx) {
368  (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
369  },
370  &Diags);
371  std::vector<Fragment> Result;
372  for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
373  if (Node *N = Doc.getRoot()) {
376  Fragment.Source.Location = N->getSourceRange().Start;
377  SM->PrintMessage(Fragment.Source.Location, llvm::SourceMgr::DK_Note,
378  "Parsing config fragment");
379  if (Parser(*SM).parse(Fragment, *N))
380  Result.push_back(std::move(Fragment));
381  }
382  }
383  SM->PrintMessage(SM->FindLocForLineAndColumn(SM->getMainFileID(), 0, 0),
384  llvm::SourceMgr::DK_Note,
385  "Parsed " + llvm::Twine(Result.size()) +
386  " fragments from file");
387  // Hack: stash the buffer in the SourceMgr to keep it alive.
388  // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
389  SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
390  return Result;
391 }
392 
393 } // namespace config
394 } // namespace clangd
395 } // namespace clang
clang::clangd::config::Fragment::SourceInfo::Location
llvm::SMLoc Location
The start of the original source for this fragment.
Definition: ConfigFragment.h:93
clang::clangd::IndexFileFormat::YAML
@ YAML
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::clangd::config::Fragment::parseYAML
static std::vector< Fragment > parseYAML(llvm::StringRef YAML, llvm::StringRef BufferName, DiagnosticCallback)
Parses fragments from a YAML file (one from each — delimited document).
Definition: ConfigYAML.cpp:357
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
Ctx
Context Ctx
Definition: TUScheduler.cpp:460
Outer
std::pair< Context, Canceler > Outer
Definition: CancellationTests.cpp:49
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:102
clang::tidy::abseil::getValue
static double getValue(const IntegerLiteral *IntLit, const FloatingLiteral *FloatLit)
Definition: DurationFactoryScaleCheck.cpp:38
clang::clangd::SymbolKind::Key
@ Key
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:97
clang::clangd::config::Fragment::Source
SourceInfo Source
Definition: ConfigFragment.h:101
Description
const char * Description
Definition: Dexp.cpp:361
Entry
Definition: Modularize.cpp:428
clang::clangd::config::Fragment::SourceInfo::Manager
std::shared_ptr< llvm::SourceMgr > Manager
Retains a buffer of the original source this fragment was parsed from.
Definition: ConfigFragment.h:90
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:38
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:140
clang::clangd::config::Fragment
A chunk of configuration obtained from a config file, LSP, or elsewhere.
Definition: ConfigFragment.h:64
ConfigFragment.h
K
Kind K
Definition: Rename.cpp:442
clang::clangd::config::DiagnosticCallback
llvm::function_ref< void(const llvm::SMDiagnostic &)> DiagnosticCallback
Used to report problems in parsing or interpreting a config.
Definition: ConfigProvider.h:51