blob: ef6003b024392257347f4840aab4dda4a58d058f [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"
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 <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); });
40 Dict.parse(N);
41 return !(N.failed() || HadError);
Sam McCalle9fb1502020-06-23 17:21:56 +020042 }
43
44private:
Sam McCallf3651862020-07-09 00:13:54 +020045 void parse(Fragment::IfBlock &F, Node &N) {
Sam McCallf12cd992020-06-26 01:49:53 +020046 DictParser Dict("If", this);
Sam McCalle9fb1502020-06-23 17:21:56 +020047 Dict.unrecognized(
48 [&](llvm::StringRef) { F.HasUnrecognizedCondition = true; });
49 Dict.handle("PathMatch", [&](Node &N) {
50 if (auto Values = scalarValues(N))
51 F.PathMatch = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020052 });
Sam McCall86f13132020-07-09 23:33:46 +020053 Dict.handle("PathExclude", [&](Node &N) {
54 if (auto Values = scalarValues(N))
55 F.PathExclude = std::move(*Values);
56 });
Sam McCallf3651862020-07-09 00:13:54 +020057 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020058 }
59
Sam McCallf3651862020-07-09 00:13:54 +020060 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
Sam McCalle9fb1502020-06-23 17:21:56 +020061 DictParser Dict("CompileFlags", this);
62 Dict.handle("Add", [&](Node &N) {
63 if (auto Values = scalarValues(N))
64 F.Add = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020065 });
Sam McCallf3651862020-07-09 00:13:54 +020066 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020067 }
68
69 // Helper for parsing mapping nodes (dictionaries).
70 // We don't use YamlIO as we want to control over unknown keys.
71 class DictParser {
72 llvm::StringRef Description;
Sam McCallf3651862020-07-09 00:13:54 +020073 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
Sam McCalle9fb1502020-06-23 17:21:56 +020074 std::function<void(llvm::StringRef)> Unknown;
75 Parser *Outer;
76
77 public:
78 DictParser(llvm::StringRef Description, Parser *Outer)
79 : Description(Description), Outer(Outer) {}
80
81 // Parse is called when Key is encountered, and passed the associated value.
82 // It should emit diagnostics if the value is invalid (e.g. wrong type).
83 // If Key is seen twice, Parse runs only once and an error is reported.
Sam McCallf3651862020-07-09 00:13:54 +020084 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
Tres Popp1a30eab2020-06-26 10:12:04 +020085 for (const auto &Entry : Keys) {
86 (void) Entry;
Sam McCalle9fb1502020-06-23 17:21:56 +020087 assert(Entry.first != Key && "duplicate key handler");
Tres Popp1a30eab2020-06-26 10:12:04 +020088 }
Sam McCalle9fb1502020-06-23 17:21:56 +020089 Keys.emplace_back(Key, std::move(Parse));
90 }
91
92 // Fallback is called when a Key is not matched by any handle().
93 // A warning is also automatically emitted.
94 void unrecognized(std::function<void(llvm::StringRef)> Fallback) {
95 Unknown = std::move(Fallback);
96 }
97
98 // Process a mapping node and call handlers for each key/value pair.
Sam McCallf3651862020-07-09 00:13:54 +020099 void parse(Node &N) const {
Sam McCalle9fb1502020-06-23 17:21:56 +0200100 if (N.getType() != Node::NK_Mapping) {
101 Outer->error(Description + " should be a dictionary", N);
Sam McCallf3651862020-07-09 00:13:54 +0200102 return;
Sam McCalle9fb1502020-06-23 17:21:56 +0200103 }
104 llvm::SmallSet<std::string, 8> Seen;
Sam McCallf3651862020-07-09 00:13:54 +0200105 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200106 for (auto &KV : llvm::cast<MappingNode>(N)) {
107 auto *K = KV.getKey();
108 if (!K) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200109 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200110 auto Key = Outer->scalarValue(*K, "Dictionary key");
111 if (!Key)
112 continue;
113 if (!Seen.insert(**Key).second) {
114 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
115 continue;
116 }
117 auto *Value = KV.getValue();
118 if (!Value) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200119 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200120 bool Matched = false;
121 for (const auto &Handler : Keys) {
122 if (Handler.first == **Key) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200123 Matched = true;
Sam McCallf3651862020-07-09 00:13:54 +0200124 Handler.second(*Value);
Sam McCalle9fb1502020-06-23 17:21:56 +0200125 break;
126 }
127 }
128 if (!Matched) {
129 Outer->warning("Unknown " + Description + " key " + **Key, *K);
130 if (Unknown)
131 Unknown(**Key);
132 }
133 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200134 }
135 };
136
137 // Try to parse a single scalar value from the node, warn on failure.
138 llvm::Optional<Located<std::string>> scalarValue(Node &N,
139 llvm::StringRef Desc) {
140 llvm::SmallString<256> Buf;
141 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
142 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
143 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
144 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
145 warning(Desc + " should be scalar", N);
146 return llvm::None;
147 }
148
149 // Try to parse a list of single scalar values, or just a single value.
150 llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
151 std::vector<Located<std::string>> Result;
152 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
153 llvm::SmallString<256> Buf;
154 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
155 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
156 Result.emplace_back(S->getValue().str(), N.getSourceRange());
157 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
Sam McCallf3651862020-07-09 00:13:54 +0200158 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200159 for (auto &Child : *S) {
160 if (auto Value = scalarValue(Child, "List item"))
161 Result.push_back(std::move(*Value));
162 }
163 } else {
164 warning("Expected scalar or list of scalars", N);
165 return llvm::None;
166 }
167 return Result;
168 }
169
170 // Report a "hard" error, reflecting a config file that can never be valid.
171 void error(const llvm::Twine &Msg, const Node &N) {
Sam McCallf3651862020-07-09 00:13:54 +0200172 HadError = true;
Sam McCalle9fb1502020-06-23 17:21:56 +0200173 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
174 N.getSourceRange());
175 }
176
177 // Report a "soft" error that could be caused by e.g. version skew.
178 void warning(const llvm::Twine &Msg, const Node &N) {
179 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
180 N.getSourceRange());
181 }
182};
183
184} // namespace
185
186std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
187 llvm::StringRef BufferName,
188 DiagnosticCallback Diags) {
189 // The YAML document may contain multiple conditional fragments.
190 // The SourceManager is shared for all of them.
191 auto SM = std::make_shared<llvm::SourceMgr>();
192 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
193 // Adapt DiagnosticCallback to function-pointer interface.
194 // Callback receives both errors we emit and those from the YAML parser.
195 SM->setDiagHandler(
196 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
197 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
198 },
199 &Diags);
200 std::vector<Fragment> Result;
201 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
Sam McCallf3651862020-07-09 00:13:54 +0200202 if (Node *N = Doc.getRoot()) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200203 Fragment Fragment;
204 Fragment.Source.Manager = SM;
205 Fragment.Source.Location = N->getSourceRange().Start;
206 if (Parser(*SM).parse(Fragment, *N))
207 Result.push_back(std::move(Fragment));
208 }
209 }
210 // Hack: stash the buffer in the SourceMgr to keep it alive.
211 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
212 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
213 return Result;
214}
215
216} // namespace config
217} // namespace clangd
218} // namespace clang