blob: a3bb1eb29adb5200f524afe28d435d34000e998a [file] [log] [blame]
Sam McCalle9fb1502020-06-23 17:21:56 +02001//===--- 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//===----------------------------------------------------------------------===//
Sam McCalle9fb1502020-06-23 17:21:56 +02008#include "ConfigFragment.h"
Sam McCalldbf486c2020-07-14 15:17:16 +02009#include "llvm/ADT/Optional.h"
Sam McCalle9fb1502020-06-23 17:21:56 +020010#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 <system_error>
16
17namespace clang {
18namespace clangd {
19namespace config {
20namespace {
21using llvm::yaml::BlockScalarNode;
22using llvm::yaml::MappingNode;
23using llvm::yaml::Node;
24using llvm::yaml::ScalarNode;
25using llvm::yaml::SequenceNode;
26
27class Parser {
28 llvm::SourceMgr &SM;
Sam McCallf3651862020-07-09 00:13:54 +020029 bool HadError = false;
Sam McCalle9fb1502020-06-23 17:21:56 +020030
31public:
32 Parser(llvm::SourceMgr &SM) : SM(SM) {}
33
34 // Tries to parse N into F, returning false if it failed and we couldn't
Sam McCallf3651862020-07-09 00:13:54 +020035 // meaningfully recover (YAML syntax error, or hard semantic error).
Sam McCalle9fb1502020-06-23 17:21:56 +020036 bool parse(Fragment &F, Node &N) {
37 DictParser Dict("Config", this);
Sam McCallf3651862020-07-09 00:13:54 +020038 Dict.handle("If", [&](Node &N) { parse(F.If, N); });
39 Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
Adam Czachorowski7029e5d2020-09-15 20:13:00 +020040 Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
Adam Czachorowskic894bfd2020-09-15 19:47:50 +020041 Dict.handle("Style", [&](Node &N) { parse(F.Style, N); });
Nathan James20b69af2020-11-22 10:04:00 +000042 Dict.handle("ClangTidy", [&](Node &N) { parse(F.ClangTidy, N); });
Sam McCallf3651862020-07-09 00:13:54 +020043 Dict.parse(N);
44 return !(N.failed() || HadError);
Sam McCalle9fb1502020-06-23 17:21:56 +020045 }
46
47private:
Sam McCallf3651862020-07-09 00:13:54 +020048 void parse(Fragment::IfBlock &F, Node &N) {
Sam McCallf12cd992020-06-26 01:49:53 +020049 DictParser Dict("If", this);
Nathan James20b69af2020-11-22 10:04:00 +000050 Dict.unrecognized([&](Located<std::string>, Node &) {
51 F.HasUnrecognizedCondition = true;
52 return true; // Emit a warning for the unrecognized key.
53 });
Sam McCalle9fb1502020-06-23 17:21:56 +020054 Dict.handle("PathMatch", [&](Node &N) {
55 if (auto Values = scalarValues(N))
56 F.PathMatch = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020057 });
Sam McCall86f13132020-07-09 23:33:46 +020058 Dict.handle("PathExclude", [&](Node &N) {
59 if (auto Values = scalarValues(N))
60 F.PathExclude = std::move(*Values);
61 });
Sam McCallf3651862020-07-09 00:13:54 +020062 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020063 }
64
Sam McCallf3651862020-07-09 00:13:54 +020065 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
Sam McCalle9fb1502020-06-23 17:21:56 +020066 DictParser Dict("CompileFlags", this);
67 Dict.handle("Add", [&](Node &N) {
68 if (auto Values = scalarValues(N))
69 F.Add = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020070 });
Sam McCall6c16fbd2020-07-13 20:37:54 +020071 Dict.handle("Remove", [&](Node &N) {
72 if (auto Values = scalarValues(N))
73 F.Remove = std::move(*Values);
74 });
Sam McCallf3651862020-07-09 00:13:54 +020075 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020076 }
77
Adam Czachorowskic894bfd2020-09-15 19:47:50 +020078 void parse(Fragment::StyleBlock &F, Node &N) {
79 DictParser Dict("Style", this);
80 Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
81 if (auto Values = scalarValues(N))
82 F.FullyQualifiedNamespaces = std::move(*Values);
83 });
84 Dict.parse(N);
85 }
86
Nathan James20b69af2020-11-22 10:04:00 +000087 void parse(Fragment::ClangTidyBlock &F, Node &N) {
88 DictParser Dict("ClangTidy", this);
89 Dict.handle("Add", [&](Node &N) {
90 if (auto Values = scalarValues(N))
91 F.Add = std::move(*Values);
92 });
93 Dict.handle("Remove", [&](Node &N) {
94 if (auto Values = scalarValues(N))
95 F.Remove = std::move(*Values);
96 });
97 Dict.handle("CheckOptions", [&](Node &N) {
98 DictParser CheckOptDict("CheckOptions", this);
99 CheckOptDict.unrecognized([&](Located<std::string> &&Key, Node &Val) {
100 if (auto Value = scalarValue(Val, *Key))
101 F.CheckOptions.emplace_back(std::move(Key), std::move(*Value));
102 return false; // Don't emit a warning
103 });
104 CheckOptDict.parse(N);
105 });
106 Dict.parse(N);
107 }
108
Sam McCalldbf486c2020-07-14 15:17:16 +0200109 void parse(Fragment::IndexBlock &F, Node &N) {
110 DictParser Dict("Index", this);
111 Dict.handle("Background",
112 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
Kadir Cetinkaya359e2f92020-10-01 12:29:55 +0100113 Dict.handle("External", [&](Node &N) {
114 Fragment::IndexBlock::ExternalBlock External;
115 parse(External, N);
116 F.External.emplace(std::move(External));
117 F.External->Range = N.getSourceRange();
118 });
119 Dict.parse(N);
120 }
121
122 void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
123 DictParser Dict("External", this);
124 Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
125 Dict.handle("Server",
126 [&](Node &N) { F.Server = scalarValue(N, "Server"); });
127 Dict.handle("MountPoint",
128 [&](Node &N) { F.MountPoint = scalarValue(N, "MountPoint"); });
Sam McCalldbf486c2020-07-14 15:17:16 +0200129 Dict.parse(N);
130 }
131
Sam McCalle9fb1502020-06-23 17:21:56 +0200132 // Helper for parsing mapping nodes (dictionaries).
133 // We don't use YamlIO as we want to control over unknown keys.
134 class DictParser {
135 llvm::StringRef Description;
Sam McCallf3651862020-07-09 00:13:54 +0200136 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
Nathan James20b69af2020-11-22 10:04:00 +0000137 std::function<bool(Located<std::string>, Node &)> UnknownHandler;
Sam McCalle9fb1502020-06-23 17:21:56 +0200138 Parser *Outer;
139
140 public:
141 DictParser(llvm::StringRef Description, Parser *Outer)
142 : Description(Description), Outer(Outer) {}
143
144 // Parse is called when Key is encountered, and passed the associated value.
145 // It should emit diagnostics if the value is invalid (e.g. wrong type).
146 // If Key is seen twice, Parse runs only once and an error is reported.
Sam McCallf3651862020-07-09 00:13:54 +0200147 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
Tres Popp1a30eab2020-06-26 10:12:04 +0200148 for (const auto &Entry : Keys) {
149 (void) Entry;
Sam McCalle9fb1502020-06-23 17:21:56 +0200150 assert(Entry.first != Key && "duplicate key handler");
Tres Popp1a30eab2020-06-26 10:12:04 +0200151 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200152 Keys.emplace_back(Key, std::move(Parse));
153 }
154
Nathan James20b69af2020-11-22 10:04:00 +0000155 // Handler is called when a Key is not matched by any handle().
156 // If this is unset or the Handler returns true, a warning is emitted for
157 // the unknown key.
158 void
159 unrecognized(std::function<bool(Located<std::string>, Node &)> Handler) {
160 UnknownHandler = std::move(Handler);
Sam McCalle9fb1502020-06-23 17:21:56 +0200161 }
162
163 // Process a mapping node and call handlers for each key/value pair.
Sam McCallf3651862020-07-09 00:13:54 +0200164 void parse(Node &N) const {
Sam McCalle9fb1502020-06-23 17:21:56 +0200165 if (N.getType() != Node::NK_Mapping) {
166 Outer->error(Description + " should be a dictionary", N);
Sam McCallf3651862020-07-09 00:13:54 +0200167 return;
Sam McCalle9fb1502020-06-23 17:21:56 +0200168 }
169 llvm::SmallSet<std::string, 8> Seen;
Sam McCallf3651862020-07-09 00:13:54 +0200170 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200171 for (auto &KV : llvm::cast<MappingNode>(N)) {
172 auto *K = KV.getKey();
173 if (!K) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200174 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200175 auto Key = Outer->scalarValue(*K, "Dictionary key");
176 if (!Key)
177 continue;
178 if (!Seen.insert(**Key).second) {
179 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
Nathan James20b69af2020-11-22 10:04:00 +0000180 if (auto *Value = KV.getValue())
181 Value->skip();
Sam McCalle9fb1502020-06-23 17:21:56 +0200182 continue;
183 }
184 auto *Value = KV.getValue();
185 if (!Value) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200186 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200187 bool Matched = false;
188 for (const auto &Handler : Keys) {
189 if (Handler.first == **Key) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200190 Matched = true;
Sam McCallf3651862020-07-09 00:13:54 +0200191 Handler.second(*Value);
Sam McCalle9fb1502020-06-23 17:21:56 +0200192 break;
193 }
194 }
195 if (!Matched) {
Nathan James20b69af2020-11-22 10:04:00 +0000196 bool Warn = !UnknownHandler;
197 if (UnknownHandler)
198 Warn = UnknownHandler(
199 Located<std::string>(**Key, K->getSourceRange()), *Value);
200 if (Warn)
201 Outer->warning("Unknown " + Description + " key " + **Key, *K);
Sam McCalle9fb1502020-06-23 17:21:56 +0200202 }
203 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200204 }
205 };
206
207 // Try to parse a single scalar value from the node, warn on failure.
208 llvm::Optional<Located<std::string>> scalarValue(Node &N,
209 llvm::StringRef Desc) {
210 llvm::SmallString<256> Buf;
211 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
212 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
213 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
214 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
215 warning(Desc + " should be scalar", N);
216 return llvm::None;
217 }
218
219 // Try to parse a list of single scalar values, or just a single value.
220 llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
221 std::vector<Located<std::string>> Result;
222 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
223 llvm::SmallString<256> Buf;
224 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
225 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
226 Result.emplace_back(S->getValue().str(), N.getSourceRange());
227 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
Sam McCallf3651862020-07-09 00:13:54 +0200228 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200229 for (auto &Child : *S) {
230 if (auto Value = scalarValue(Child, "List item"))
231 Result.push_back(std::move(*Value));
232 }
233 } else {
234 warning("Expected scalar or list of scalars", N);
235 return llvm::None;
236 }
237 return Result;
238 }
239
240 // Report a "hard" error, reflecting a config file that can never be valid.
241 void error(const llvm::Twine &Msg, const Node &N) {
Sam McCallf3651862020-07-09 00:13:54 +0200242 HadError = true;
Sam McCalle9fb1502020-06-23 17:21:56 +0200243 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
244 N.getSourceRange());
245 }
246
247 // Report a "soft" error that could be caused by e.g. version skew.
248 void warning(const llvm::Twine &Msg, const Node &N) {
249 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
250 N.getSourceRange());
251 }
252};
253
254} // namespace
255
256std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
257 llvm::StringRef BufferName,
258 DiagnosticCallback Diags) {
259 // The YAML document may contain multiple conditional fragments.
260 // The SourceManager is shared for all of them.
261 auto SM = std::make_shared<llvm::SourceMgr>();
262 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
263 // Adapt DiagnosticCallback to function-pointer interface.
264 // Callback receives both errors we emit and those from the YAML parser.
265 SM->setDiagHandler(
266 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
267 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
268 },
269 &Diags);
270 std::vector<Fragment> Result;
271 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
Sam McCallf3651862020-07-09 00:13:54 +0200272 if (Node *N = Doc.getRoot()) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200273 Fragment Fragment;
274 Fragment.Source.Manager = SM;
275 Fragment.Source.Location = N->getSourceRange().Start;
276 if (Parser(*SM).parse(Fragment, *N))
277 Result.push_back(std::move(Fragment));
278 }
279 }
280 // Hack: stash the buffer in the SourceMgr to keep it alive.
281 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
282 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
283 return Result;
284}
285
286} // namespace config
287} // namespace clangd
288} // namespace clang