blob: bc7d4fdfe85bdadcb6da09ef4593499f4fa5513d [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//===----------------------------------------------------------------------===//
8
9#include "ConfigFragment.h"
Sam McCalldbf486c2020-07-14 15:17:16 +020010#include "llvm/ADT/Optional.h"
Sam McCalle9fb1502020-06-23 17:21:56 +020011#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
18namespace clang {
19namespace clangd {
20namespace config {
21namespace {
22using llvm::yaml::BlockScalarNode;
23using llvm::yaml::MappingNode;
24using llvm::yaml::Node;
25using llvm::yaml::ScalarNode;
26using llvm::yaml::SequenceNode;
27
28class Parser {
29 llvm::SourceMgr &SM;
Sam McCallf3651862020-07-09 00:13:54 +020030 bool HadError = false;
Sam McCalle9fb1502020-06-23 17:21:56 +020031
32public:
33 Parser(llvm::SourceMgr &SM) : SM(SM) {}
34
35 // Tries to parse N into F, returning false if it failed and we couldn't
Sam McCallf3651862020-07-09 00:13:54 +020036 // meaningfully recover (YAML syntax error, or hard semantic error).
Sam McCalle9fb1502020-06-23 17:21:56 +020037 bool parse(Fragment &F, Node &N) {
38 DictParser Dict("Config", this);
Sam McCallf3651862020-07-09 00:13:54 +020039 Dict.handle("If", [&](Node &N) { parse(F.If, N); });
40 Dict.handle("CompileFlags", [&](Node &N) { parse(F.CompileFlags, N); });
Adam Czachorowski7029e5d2020-09-15 20:13:00 +020041 Dict.handle("Index", [&](Node &N) { parse(F.Index, N); });
Adam Czachorowskic894bfd2020-09-15 19:47:50 +020042 Dict.handle("Style", [&](Node &N) { parse(F.Style, 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);
Sam McCalle9fb1502020-06-23 17:21:56 +020050 Dict.unrecognized(
51 [&](llvm::StringRef) { F.HasUnrecognizedCondition = true; });
52 Dict.handle("PathMatch", [&](Node &N) {
53 if (auto Values = scalarValues(N))
54 F.PathMatch = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020055 });
Sam McCall86f13132020-07-09 23:33:46 +020056 Dict.handle("PathExclude", [&](Node &N) {
57 if (auto Values = scalarValues(N))
58 F.PathExclude = std::move(*Values);
59 });
Sam McCallf3651862020-07-09 00:13:54 +020060 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020061 }
62
Sam McCallf3651862020-07-09 00:13:54 +020063 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
Sam McCalle9fb1502020-06-23 17:21:56 +020064 DictParser Dict("CompileFlags", this);
65 Dict.handle("Add", [&](Node &N) {
66 if (auto Values = scalarValues(N))
67 F.Add = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020068 });
Sam McCall6c16fbd2020-07-13 20:37:54 +020069 Dict.handle("Remove", [&](Node &N) {
70 if (auto Values = scalarValues(N))
71 F.Remove = std::move(*Values);
72 });
Sam McCallf3651862020-07-09 00:13:54 +020073 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020074 }
75
Adam Czachorowskic894bfd2020-09-15 19:47:50 +020076 void parse(Fragment::StyleBlock &F, Node &N) {
77 DictParser Dict("Style", this);
78 Dict.handle("FullyQualifiedNamespaces", [&](Node &N) {
79 if (auto Values = scalarValues(N))
80 F.FullyQualifiedNamespaces = std::move(*Values);
81 });
82 Dict.parse(N);
83 }
84
Sam McCalldbf486c2020-07-14 15:17:16 +020085 void parse(Fragment::IndexBlock &F, Node &N) {
86 DictParser Dict("Index", this);
87 Dict.handle("Background",
88 [&](Node &N) { F.Background = scalarValue(N, "Background"); });
89 Dict.parse(N);
90 }
91
Sam McCalle9fb1502020-06-23 17:21:56 +020092 // Helper for parsing mapping nodes (dictionaries).
93 // We don't use YamlIO as we want to control over unknown keys.
94 class DictParser {
95 llvm::StringRef Description;
Sam McCallf3651862020-07-09 00:13:54 +020096 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
Sam McCalle9fb1502020-06-23 17:21:56 +020097 std::function<void(llvm::StringRef)> Unknown;
98 Parser *Outer;
99
100 public:
101 DictParser(llvm::StringRef Description, Parser *Outer)
102 : Description(Description), Outer(Outer) {}
103
104 // Parse is called when Key is encountered, and passed the associated value.
105 // It should emit diagnostics if the value is invalid (e.g. wrong type).
106 // If Key is seen twice, Parse runs only once and an error is reported.
Sam McCallf3651862020-07-09 00:13:54 +0200107 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
Tres Popp1a30eab2020-06-26 10:12:04 +0200108 for (const auto &Entry : Keys) {
109 (void) Entry;
Sam McCalle9fb1502020-06-23 17:21:56 +0200110 assert(Entry.first != Key && "duplicate key handler");
Tres Popp1a30eab2020-06-26 10:12:04 +0200111 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200112 Keys.emplace_back(Key, std::move(Parse));
113 }
114
115 // Fallback is called when a Key is not matched by any handle().
116 // A warning is also automatically emitted.
117 void unrecognized(std::function<void(llvm::StringRef)> Fallback) {
118 Unknown = std::move(Fallback);
119 }
120
121 // Process a mapping node and call handlers for each key/value pair.
Sam McCallf3651862020-07-09 00:13:54 +0200122 void parse(Node &N) const {
Sam McCalle9fb1502020-06-23 17:21:56 +0200123 if (N.getType() != Node::NK_Mapping) {
124 Outer->error(Description + " should be a dictionary", N);
Sam McCallf3651862020-07-09 00:13:54 +0200125 return;
Sam McCalle9fb1502020-06-23 17:21:56 +0200126 }
127 llvm::SmallSet<std::string, 8> Seen;
Sam McCallf3651862020-07-09 00:13:54 +0200128 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200129 for (auto &KV : llvm::cast<MappingNode>(N)) {
130 auto *K = KV.getKey();
131 if (!K) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200132 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200133 auto Key = Outer->scalarValue(*K, "Dictionary key");
134 if (!Key)
135 continue;
136 if (!Seen.insert(**Key).second) {
137 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
138 continue;
139 }
140 auto *Value = KV.getValue();
141 if (!Value) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200142 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200143 bool Matched = false;
144 for (const auto &Handler : Keys) {
145 if (Handler.first == **Key) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200146 Matched = true;
Sam McCallf3651862020-07-09 00:13:54 +0200147 Handler.second(*Value);
Sam McCalle9fb1502020-06-23 17:21:56 +0200148 break;
149 }
150 }
151 if (!Matched) {
152 Outer->warning("Unknown " + Description + " key " + **Key, *K);
153 if (Unknown)
154 Unknown(**Key);
155 }
156 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200157 }
158 };
159
160 // Try to parse a single scalar value from the node, warn on failure.
161 llvm::Optional<Located<std::string>> scalarValue(Node &N,
162 llvm::StringRef Desc) {
163 llvm::SmallString<256> Buf;
164 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
165 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
166 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
167 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
168 warning(Desc + " should be scalar", N);
169 return llvm::None;
170 }
171
172 // Try to parse a list of single scalar values, or just a single value.
173 llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
174 std::vector<Located<std::string>> Result;
175 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
176 llvm::SmallString<256> Buf;
177 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
178 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
179 Result.emplace_back(S->getValue().str(), N.getSourceRange());
180 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
Sam McCallf3651862020-07-09 00:13:54 +0200181 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200182 for (auto &Child : *S) {
183 if (auto Value = scalarValue(Child, "List item"))
184 Result.push_back(std::move(*Value));
185 }
186 } else {
187 warning("Expected scalar or list of scalars", N);
188 return llvm::None;
189 }
190 return Result;
191 }
192
193 // Report a "hard" error, reflecting a config file that can never be valid.
194 void error(const llvm::Twine &Msg, const Node &N) {
Sam McCallf3651862020-07-09 00:13:54 +0200195 HadError = true;
Sam McCalle9fb1502020-06-23 17:21:56 +0200196 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
197 N.getSourceRange());
198 }
199
200 // Report a "soft" error that could be caused by e.g. version skew.
201 void warning(const llvm::Twine &Msg, const Node &N) {
202 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
203 N.getSourceRange());
204 }
205};
206
207} // namespace
208
209std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
210 llvm::StringRef BufferName,
211 DiagnosticCallback Diags) {
212 // The YAML document may contain multiple conditional fragments.
213 // The SourceManager is shared for all of them.
214 auto SM = std::make_shared<llvm::SourceMgr>();
215 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
216 // Adapt DiagnosticCallback to function-pointer interface.
217 // Callback receives both errors we emit and those from the YAML parser.
218 SM->setDiagHandler(
219 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
220 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
221 },
222 &Diags);
223 std::vector<Fragment> Result;
224 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
Sam McCallf3651862020-07-09 00:13:54 +0200225 if (Node *N = Doc.getRoot()) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200226 Fragment Fragment;
227 Fragment.Source.Manager = SM;
228 Fragment.Source.Location = N->getSourceRange().Start;
229 if (Parser(*SM).parse(Fragment, *N))
230 Result.push_back(std::move(Fragment));
231 }
232 }
233 // Hack: stash the buffer in the SourceMgr to keep it alive.
234 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
235 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
236 return Result;
237}
238
239} // namespace config
240} // namespace clangd
241} // namespace clang