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