blob: 0674c6030903fce97e8c75b1bd3ca784e750e17d [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 McCallf3651862020-07-09 00:13:54 +020053 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020054 }
55
Sam McCallf3651862020-07-09 00:13:54 +020056 void parse(Fragment::CompileFlagsBlock &F, Node &N) {
Sam McCalle9fb1502020-06-23 17:21:56 +020057 DictParser Dict("CompileFlags", this);
58 Dict.handle("Add", [&](Node &N) {
59 if (auto Values = scalarValues(N))
60 F.Add = std::move(*Values);
Sam McCalle9fb1502020-06-23 17:21:56 +020061 });
Sam McCallf3651862020-07-09 00:13:54 +020062 Dict.parse(N);
Sam McCalle9fb1502020-06-23 17:21:56 +020063 }
64
65 // Helper for parsing mapping nodes (dictionaries).
66 // We don't use YamlIO as we want to control over unknown keys.
67 class DictParser {
68 llvm::StringRef Description;
Sam McCallf3651862020-07-09 00:13:54 +020069 std::vector<std::pair<llvm::StringRef, std::function<void(Node &)>>> Keys;
Sam McCalle9fb1502020-06-23 17:21:56 +020070 std::function<void(llvm::StringRef)> Unknown;
71 Parser *Outer;
72
73 public:
74 DictParser(llvm::StringRef Description, Parser *Outer)
75 : Description(Description), Outer(Outer) {}
76
77 // Parse is called when Key is encountered, and passed the associated value.
78 // It should emit diagnostics if the value is invalid (e.g. wrong type).
79 // If Key is seen twice, Parse runs only once and an error is reported.
Sam McCallf3651862020-07-09 00:13:54 +020080 void handle(llvm::StringLiteral Key, std::function<void(Node &)> Parse) {
Tres Popp1a30eab2020-06-26 10:12:04 +020081 for (const auto &Entry : Keys) {
82 (void) Entry;
Sam McCalle9fb1502020-06-23 17:21:56 +020083 assert(Entry.first != Key && "duplicate key handler");
Tres Popp1a30eab2020-06-26 10:12:04 +020084 }
Sam McCalle9fb1502020-06-23 17:21:56 +020085 Keys.emplace_back(Key, std::move(Parse));
86 }
87
88 // Fallback is called when a Key is not matched by any handle().
89 // A warning is also automatically emitted.
90 void unrecognized(std::function<void(llvm::StringRef)> Fallback) {
91 Unknown = std::move(Fallback);
92 }
93
94 // Process a mapping node and call handlers for each key/value pair.
Sam McCallf3651862020-07-09 00:13:54 +020095 void parse(Node &N) const {
Sam McCalle9fb1502020-06-23 17:21:56 +020096 if (N.getType() != Node::NK_Mapping) {
97 Outer->error(Description + " should be a dictionary", N);
Sam McCallf3651862020-07-09 00:13:54 +020098 return;
Sam McCalle9fb1502020-06-23 17:21:56 +020099 }
100 llvm::SmallSet<std::string, 8> Seen;
Sam McCallf3651862020-07-09 00:13:54 +0200101 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200102 for (auto &KV : llvm::cast<MappingNode>(N)) {
103 auto *K = KV.getKey();
104 if (!K) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200105 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200106 auto Key = Outer->scalarValue(*K, "Dictionary key");
107 if (!Key)
108 continue;
109 if (!Seen.insert(**Key).second) {
110 Outer->warning("Duplicate key " + **Key + " is ignored", *K);
111 continue;
112 }
113 auto *Value = KV.getValue();
114 if (!Value) // YAMLParser emitted an error.
Sam McCallf3651862020-07-09 00:13:54 +0200115 continue;
Sam McCalle9fb1502020-06-23 17:21:56 +0200116 bool Matched = false;
117 for (const auto &Handler : Keys) {
118 if (Handler.first == **Key) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200119 Matched = true;
Sam McCallf3651862020-07-09 00:13:54 +0200120 Handler.second(*Value);
Sam McCalle9fb1502020-06-23 17:21:56 +0200121 break;
122 }
123 }
124 if (!Matched) {
125 Outer->warning("Unknown " + Description + " key " + **Key, *K);
126 if (Unknown)
127 Unknown(**Key);
128 }
129 }
Sam McCalle9fb1502020-06-23 17:21:56 +0200130 }
131 };
132
133 // Try to parse a single scalar value from the node, warn on failure.
134 llvm::Optional<Located<std::string>> scalarValue(Node &N,
135 llvm::StringRef Desc) {
136 llvm::SmallString<256> Buf;
137 if (auto *S = llvm::dyn_cast<ScalarNode>(&N))
138 return Located<std::string>(S->getValue(Buf).str(), N.getSourceRange());
139 if (auto *BS = llvm::dyn_cast<BlockScalarNode>(&N))
140 return Located<std::string>(BS->getValue().str(), N.getSourceRange());
141 warning(Desc + " should be scalar", N);
142 return llvm::None;
143 }
144
145 // Try to parse a list of single scalar values, or just a single value.
146 llvm::Optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
147 std::vector<Located<std::string>> Result;
148 if (auto *S = llvm::dyn_cast<ScalarNode>(&N)) {
149 llvm::SmallString<256> Buf;
150 Result.emplace_back(S->getValue(Buf).str(), N.getSourceRange());
151 } else if (auto *S = llvm::dyn_cast<BlockScalarNode>(&N)) {
152 Result.emplace_back(S->getValue().str(), N.getSourceRange());
153 } else if (auto *S = llvm::dyn_cast<SequenceNode>(&N)) {
Sam McCallf3651862020-07-09 00:13:54 +0200154 // We *must* consume all items, even on error, or the parser will assert.
Sam McCalle9fb1502020-06-23 17:21:56 +0200155 for (auto &Child : *S) {
156 if (auto Value = scalarValue(Child, "List item"))
157 Result.push_back(std::move(*Value));
158 }
159 } else {
160 warning("Expected scalar or list of scalars", N);
161 return llvm::None;
162 }
163 return Result;
164 }
165
166 // Report a "hard" error, reflecting a config file that can never be valid.
167 void error(const llvm::Twine &Msg, const Node &N) {
Sam McCallf3651862020-07-09 00:13:54 +0200168 HadError = true;
Sam McCalle9fb1502020-06-23 17:21:56 +0200169 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Error, Msg,
170 N.getSourceRange());
171 }
172
173 // Report a "soft" error that could be caused by e.g. version skew.
174 void warning(const llvm::Twine &Msg, const Node &N) {
175 SM.PrintMessage(N.getSourceRange().Start, llvm::SourceMgr::DK_Warning, Msg,
176 N.getSourceRange());
177 }
178};
179
180} // namespace
181
182std::vector<Fragment> Fragment::parseYAML(llvm::StringRef YAML,
183 llvm::StringRef BufferName,
184 DiagnosticCallback Diags) {
185 // The YAML document may contain multiple conditional fragments.
186 // The SourceManager is shared for all of them.
187 auto SM = std::make_shared<llvm::SourceMgr>();
188 auto Buf = llvm::MemoryBuffer::getMemBufferCopy(YAML, BufferName);
189 // Adapt DiagnosticCallback to function-pointer interface.
190 // Callback receives both errors we emit and those from the YAML parser.
191 SM->setDiagHandler(
192 [](const llvm::SMDiagnostic &Diag, void *Ctx) {
193 (*reinterpret_cast<DiagnosticCallback *>(Ctx))(Diag);
194 },
195 &Diags);
196 std::vector<Fragment> Result;
197 for (auto &Doc : llvm::yaml::Stream(*Buf, *SM)) {
Sam McCallf3651862020-07-09 00:13:54 +0200198 if (Node *N = Doc.getRoot()) {
Sam McCalle9fb1502020-06-23 17:21:56 +0200199 Fragment Fragment;
200 Fragment.Source.Manager = SM;
201 Fragment.Source.Location = N->getSourceRange().Start;
202 if (Parser(*SM).parse(Fragment, *N))
203 Result.push_back(std::move(Fragment));
204 }
205 }
206 // Hack: stash the buffer in the SourceMgr to keep it alive.
207 // SM has two entries: "main" non-owning buffer, and ignored owning buffer.
208 SM->AddNewSourceBuffer(std::move(Buf), llvm::SMLoc());
209 return Result;
210}
211
212} // namespace config
213} // namespace clangd
214} // namespace clang