clang-tools  11.0.0
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 
9 #include "ConfigFragment.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/SmallSet.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 <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 class Parser {
29  llvm::SourceMgr &SM;
30  bool HadError = false;
31 
32 public:
33  Parser(llvm::SourceMgr &SM) : SM(SM) {}
34 
35  // Tries to parse N into F, returning false if it failed and we couldn't
36  // meaningfully recover (YAML syntax error, or hard semantic error).
37  bool parse(Fragment &F, Node &N) {
38  DictParser Dict("Config", this);
39  Dict.handle("If", [&](Node &N) { parse(F.If, N); });
40  Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
41  Dict.parse(N);
42  return !(N.failed() || HadError);
43  }
44 
45 private:
46  void parse(Fragment::IfBlock &F, Node &N) {
47  DictParser Dict("If", this);
48  Dict.unrecognized(
49  [&](llvm::StringRef) { F.HasUnrecognizedCondition = true; });
50  Dict.handle("PathMatch", [&](Node &N) {
51  if (auto Values = scalarValues(N))
52  F.PathMatch = std::move(*Values);
53  });
54  Dict.handle("PathExclude", [&](Node &N) {
55  if (auto Values = scalarValues(N))
56  F.PathExclude = std::move(*Values);
57  });
58  Dict.parse(N);
59  }
60 
61  void parse(Fragment::CompileFlagsBlock &F, Node &N) {
62  DictParser Dict("CompileFlags", this);
63  Dict.handle("Add", [&](Node &N) {
64  if (auto Values = scalarValues(N))
65  F.Add = std::move(*Values);
66  });
67  Dict.handle("Remove", [&](Node &N) {
68  if (auto Values = scalarValues(N))
69  F.Remove = std::move(*Values);
70  });
71  Dict.parse(N);
72  }
73 
74  void parse(Fragment::IndexBlock &F, Node &N) {
75  DictParser Dict("Index", this);
76  Dict.handle("Background",
77  [&](Node &N) { F.Background = scalarValue(N, "Background"); });
78  Dict.parse(N);
79  }
80 
81  // Helper for parsing mapping nodes (dictionaries).
82  // We don't use YamlIO as we want to control over unknown keys.
83  class DictParser {
84  llvm::StringRef Description;
85  std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
86  std::function<void(llvm::StringRef)> Unknown;
87  Parser *Outer;
88 
89  public:
90  DictParser(llvm::StringRef Description, Parser *Outer)
92 
93  // Parse is called when Key is encountered, and passed the associated value.
94  // It should emit diagnostics if the value is invalid (e.g. wrong type).
95  // If Key is seen twice, Parse runs only once and an error is reported.
96  void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
97  for (const auto &Entry : Keys) {
98  (void) Entry;
99  assert(Entry.first != Key && "duplicate key handler");
100  }
101  Keys.emplace_back(Key, std::move(Parse));
102  }
103 
104  // Fallback is called when a Key is not matched by any handle().
105  // A warning is also automatically emitted.
106  void unrecognized(std::function<void(llvm::StringRef)> Fallback) {
107  Unknown = std::move(Fallback);
108  }
109 
110  // Process a mapping node and call handlers for each key/value pair.
111  void parse(Node &N) const {
112  if (N.getType() != Node::NK_Mapping) {
113  Outer->error(Description + " should be a dictionary", N);
114  return;
115  }
116  llvm::SmallSet<std::string, 8> Seen;
117  // We *must* consume all items, even on error, or the parser will assert.
118  for (auto &KV : llvm::cast<MappingNode>(N)) {
119  auto *K = KV.getKey();
120  if (!K) // YAMLParser emitted an error.
121  continue;
122  auto Key = Outer->scalarValue(*K, "Dictionary key");
123  if (!Key)
124  continue;
125  if (!Seen.insert(**Key).second) {
126  Outer->warning("Duplicate key " + **Key + " is ignored", *K);
127  continue;
128  }
129  auto *Value = KV.getValue();
130  if (!Value) // YAMLParser emitted an error.
131  continue;
132  bool Matched = false;
133  for (const auto &Handler : Keys) {
134  if (Handler.first == **Key) {
135  Matched = true;
136  Handler.second(*Value);
137  break;
138  }
139  }
140  if (!Matched) {
141  Outer->warning("Unknown " + Description + " key " + **Key, *K);
142  if (Unknown)
143  Unknown(**Key);
144  }
145  }
146  }
147  };
148 
149  // Try to parse a single scalar value from the node, warn on failure.
150  llvm::Optional<Located<std::string>> scalarValue(Node &N,
151  llvm::StringRef Desc) {
152  llvm::SmallString<256> Buf;
153  if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
154  return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
155  if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
156  return Located<std::string>(BS->getValue().str(), N.getSourceRange());
157  warning(Desc + " should be scalar", N);
158  return llvm::None;
159  }
160 
161  // Try to parse a list of single scalar values, or just a single value.
162  llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
163  std::vector<Located<std::string>> Result;
164  if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
165  llvm::SmallString<256> Buf;
166  Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
167  } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
168  Result.emplace_back(S->getValue().str(), N.getSourceRange());
169  } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
170  // We *must* consume all items, even on error, or the parser will assert.
171  for (auto &Child : *S) {
172  if (auto Value = scalarValue(Child, "List item"))
173  Result.push_back(std::move(*Value));
174  }
175  } else {
176  warning("Expected scalar or list of scalars", N);
177  return llvm::None;
178  }
179  return Result;
180  }
181 
182  // Report a "hard" error, reflecting a config file that can never be valid.
183  void error(const llvm::Twine &Msg, const Node &N) {
184  HadError = true;
185  SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
186  N.getSourceRange());
187  }
188 
189  // Report a "soft" error that could be caused by e.g. version skew.
190  void warning(const llvm::Twine &Msg, const Node &N) {
191  SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
192  N.getSourceRange());
193  }
194 };
195 
196 } // namespace
197 
198 std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
199  llvm::StringRef BufferName,
201  // The YAML document may contain multiple conditional fragments.
202  // The SourceManager is shared for all of them.
203  auto SM = std::make_shared<llvm::SourceMgr>();
204  auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
205  // Adapt DiagnosticCallback to function-pointer interface.
206  // Callback receives both errors we emit and those from the YAML parser.
207  SM->setDiagHandler(
208  [](const llvm::SMDiagnostic &Diag, void *Ctx) {
209  (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
210  },
211  &Diags);
212  std::vector<Fragment> Result;
213  for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
214  if (Node *N = Doc.getRoot()) {
216  Fragment.Source.Manager = SM;
217  Fragment.Source.Location = N->getSourceRange().Start;
218  if (Parser(*SM).parse(Fragment, *N))
219  Result.push_back(std::move(Fragment));
220  }
221  }
222  // Hack: stash the buffer in the SourceMgr to keep it alive.
223  // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
224  SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
225  return Result;
226 }
227 
228 } // namespace config
229 } // namespace clangd
230 } // 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
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:198
Ctx
Context Ctx
Definition: TUScheduler.cpp:324
Outer
std::pair< Context, Canceler > Outer
Definition: CancellationTests.cpp:49
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:72
clang::clangd::SymbolKind::Key
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:86
clang::clangd::config::Fragment::Source
SourceInfo Source
Definition: ConfigFragment.h:95
Description
const char * Description
Definition: Dexp.cpp:320
Entry
Definition: Modularize.cpp:429
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
clang::pp_trace::error
static LLVM_ATTRIBUTE_NORETURN void error(Twine Message)
Definition: PPTrace.cpp:72
Diags
CapturedDiags Diags
Definition: ConfigCompileTests.cpp:26
clang::clangd::config::Fragment
A chunk of configuration obtained from a config file, LSP, or elsewhere.
Definition: ConfigFragment.h:64
ConfigFragment.h
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:47