Skip to content

Commit 1a5e0eb

Browse files
committed
[llvm][Support] Add YAMLGenerateSchema for producing YAML schemas
Introduced a way for generating schema for validating input YAML. This can be useful to see the full structure of the input YAML file for different llvm based tools that use existing YAML parser, for example clang-format, clang-tidy e.t.c. This commit also can be useful for yaml-language-server.
1 parent 2e9494f commit 1a5e0eb

File tree

8 files changed

+847
-34
lines changed

8 files changed

+847
-34
lines changed
Lines changed: 385 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
//===- llvm/Support/YAMLGenerateSchema.h ------------------------*- C++ -*-===//
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+
#ifndef LLVM_SUPPORT_YAMLGENERATE_SCHEMA_H
10+
#define LLVM_SUPPORT_YAMLGENERATE_SCHEMA_H
11+
12+
#include "llvm/Support/Casting.h"
13+
#include "llvm/Support/YAMLTraits.h"
14+
15+
namespace llvm {
16+
17+
namespace json {
18+
class Value;
19+
}
20+
21+
namespace yaml {
22+
23+
class GenerateSchema : public IO {
24+
public:
25+
GenerateSchema(raw_ostream &RO);
26+
~GenerateSchema() override = default;
27+
28+
IOKind getKind() const override;
29+
bool outputting() const override;
30+
bool mapTag(StringRef, bool) override;
31+
void beginMapping() override;
32+
void endMapping() override;
33+
bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
34+
void postflightKey(void *) override;
35+
std::vector<StringRef> keys() override;
36+
void beginFlowMapping() override;
37+
void endFlowMapping() override;
38+
unsigned beginSequence() override;
39+
void endSequence() override;
40+
bool preflightElement(unsigned, void *&) override;
41+
void postflightElement(void *) override;
42+
unsigned beginFlowSequence() override;
43+
bool preflightFlowElement(unsigned, void *&) override;
44+
void postflightFlowElement(void *) override;
45+
void endFlowSequence() override;
46+
void beginEnumScalar() override;
47+
bool matchEnumScalar(const char *, bool) override;
48+
bool matchEnumFallback() override;
49+
void endEnumScalar() override;
50+
bool beginBitSetScalar(bool &) override;
51+
bool bitSetMatch(const char *, bool) override;
52+
void endBitSetScalar() override;
53+
void scalarString(StringRef &, QuotingType) override;
54+
void blockScalarString(StringRef &) override;
55+
void scalarTag(std::string &) override;
56+
NodeKind getNodeKind() override;
57+
void setError(const Twine &message) override;
58+
std::error_code error() override;
59+
bool canElideEmptySequence() override;
60+
61+
bool preflightDocument();
62+
void postflightDocument();
63+
64+
class SchemaNode {
65+
public:
66+
virtual json::Value toJSON() const = 0;
67+
68+
virtual ~SchemaNode() = default;
69+
};
70+
71+
enum class PropertyKind : uint8_t {
72+
UserDefined,
73+
Properties,
74+
Required,
75+
Optional,
76+
Type,
77+
Enum,
78+
Items,
79+
FlowStyle,
80+
};
81+
82+
class SchemaProperty : public SchemaNode {
83+
StringRef Name;
84+
PropertyKind Kind;
85+
86+
public:
87+
SchemaProperty(StringRef Name, PropertyKind Kind)
88+
: Name(Name), Kind(Kind) {}
89+
90+
PropertyKind getKind() const { return Kind; }
91+
92+
StringRef getName() const { return Name; }
93+
};
94+
95+
class Schema;
96+
97+
class UserDefinedProperty final : public SchemaProperty {
98+
Schema *Value;
99+
100+
public:
101+
UserDefinedProperty(StringRef Name, Schema *Value)
102+
: SchemaProperty(Name, PropertyKind::UserDefined), Value(Value) {}
103+
104+
Schema *getSchema() const { return Value; }
105+
106+
json::Value toJSON() const override;
107+
108+
static bool classof(const SchemaProperty *Property) {
109+
return Property->getKind() == PropertyKind::UserDefined;
110+
}
111+
};
112+
113+
class PropertiesProperty final : public SchemaProperty,
114+
SmallVector<UserDefinedProperty *, 8> {
115+
public:
116+
using BaseVector = SmallVector<UserDefinedProperty *, 8>;
117+
118+
PropertiesProperty()
119+
: SchemaProperty("properties", PropertyKind::Properties) {}
120+
121+
using BaseVector::begin;
122+
using BaseVector::emplace_back;
123+
using BaseVector::end;
124+
using BaseVector::size;
125+
126+
json::Value toJSON() const override;
127+
128+
static bool classof(const SchemaProperty *Property) {
129+
return Property->getKind() == PropertyKind::Properties;
130+
}
131+
};
132+
133+
class RequiredProperty final : public SchemaProperty,
134+
SmallVector<StringRef, 4> {
135+
public:
136+
using BaseVector = SmallVector<StringRef, 4>;
137+
138+
RequiredProperty() : SchemaProperty("required", PropertyKind::Required) {}
139+
140+
using BaseVector::begin;
141+
using BaseVector::emplace_back;
142+
using BaseVector::end;
143+
using BaseVector::size;
144+
145+
json::Value toJSON() const override;
146+
147+
static bool classof(const SchemaProperty *Property) {
148+
return Property->getKind() == PropertyKind::Required;
149+
}
150+
};
151+
152+
class OptionalProperty final : public SchemaProperty,
153+
SmallVector<StringRef, 4> {
154+
public:
155+
using BaseVector = SmallVector<StringRef, 4>;
156+
157+
OptionalProperty() : SchemaProperty("optional", PropertyKind::Optional) {}
158+
159+
using BaseVector::begin;
160+
using BaseVector::emplace_back;
161+
using BaseVector::end;
162+
using BaseVector::size;
163+
164+
json::Value toJSON() const override;
165+
166+
static bool classof(const SchemaProperty *Property) {
167+
return Property->getKind() == PropertyKind::Optional;
168+
}
169+
};
170+
171+
class TypeProperty final : public SchemaProperty, SmallVector<StringRef, 1> {
172+
public:
173+
using BaseVector = SmallVector<StringRef, 1>;
174+
175+
using BaseVector::begin;
176+
using BaseVector::emplace_back;
177+
using BaseVector::end;
178+
using BaseVector::size;
179+
180+
TypeProperty(StringRef Value) : SchemaProperty("type", PropertyKind::Type) {
181+
emplace_back(Value);
182+
}
183+
184+
json::Value toJSON() const override;
185+
186+
static bool classof(const SchemaProperty *Property) {
187+
return Property->getKind() == PropertyKind::Type;
188+
}
189+
};
190+
191+
class EnumProperty final : public SchemaProperty, SmallVector<StringRef, 4> {
192+
public:
193+
using BaseVector = SmallVector<StringRef, 4>;
194+
195+
EnumProperty() : SchemaProperty("enum", PropertyKind::Enum) {}
196+
197+
using BaseVector::begin;
198+
using BaseVector::emplace_back;
199+
using BaseVector::end;
200+
using BaseVector::size;
201+
202+
json::Value toJSON() const override;
203+
204+
static bool classof(const SchemaProperty *Property) {
205+
return Property->getKind() == PropertyKind::Enum;
206+
}
207+
};
208+
209+
class ItemsProperty final : public SchemaProperty {
210+
Schema *Value;
211+
212+
public:
213+
ItemsProperty(Schema *Value = nullptr)
214+
: SchemaProperty("items", PropertyKind::Items), Value(Value) {}
215+
216+
Schema *getSchema() const { return Value; }
217+
218+
void setSchema(Schema *S) { Value = S; }
219+
220+
json::Value toJSON() const override;
221+
222+
static bool classof(const SchemaProperty *Property) {
223+
return Property->getKind() == PropertyKind::Items;
224+
}
225+
};
226+
227+
enum class FlowStyle : bool {
228+
Block,
229+
Flow,
230+
};
231+
232+
class FlowStyleProperty final : public SchemaProperty {
233+
FlowStyle Style;
234+
235+
public:
236+
FlowStyleProperty(FlowStyle Style = FlowStyle::Block)
237+
: SchemaProperty("flowStyle", PropertyKind::FlowStyle), Style(Style) {}
238+
239+
void setStyle(FlowStyle S) { Style = S; }
240+
241+
FlowStyle getStyle() const { return Style; }
242+
243+
json::Value toJSON() const override;
244+
245+
static bool classof(const SchemaProperty *Property) {
246+
return Property->getKind() == PropertyKind::FlowStyle;
247+
}
248+
};
249+
250+
class Schema final : public SchemaNode, SmallVector<SchemaProperty *, 8> {
251+
public:
252+
using BaseVector = SmallVector<SchemaProperty *, 8>;
253+
254+
Schema() = default;
255+
256+
using BaseVector::begin;
257+
using BaseVector::emplace_back;
258+
using BaseVector::end;
259+
using BaseVector::size;
260+
261+
json::Value toJSON() const override;
262+
};
263+
264+
private:
265+
std::vector<std::unique_ptr<SchemaNode>> SchemaNodes;
266+
SmallVector<Schema *, 8> Schemas;
267+
raw_ostream &RO;
268+
SchemaNode *Root = nullptr;
269+
270+
template <typename PropertyType, typename... PropertyArgs>
271+
PropertyType *createProperty(PropertyArgs &&...Args) {
272+
auto UPtr =
273+
std::make_unique<PropertyType>(std::forward<PropertyArgs>(Args)...);
274+
auto *Ptr = UPtr.get();
275+
SchemaNodes.emplace_back(std::move(UPtr));
276+
return Ptr;
277+
}
278+
279+
template <typename PropertyType, typename... PropertyArgs>
280+
PropertyType *getOrCreateProperty(Schema &S, PropertyArgs... Args) {
281+
auto Found = std::find_if(S.begin(), S.end(), [](SchemaProperty *Property) {
282+
return isa<PropertyType>(Property);
283+
});
284+
if (Found != S.end()) {
285+
return cast<PropertyType>(*Found);
286+
}
287+
PropertyType *Created =
288+
createProperty<PropertyType>(std::forward<PropertyArgs>(Args)...);
289+
S.emplace_back(Created);
290+
return Created;
291+
}
292+
293+
Schema *createSchema() {
294+
auto UPtr = std::make_unique<Schema>();
295+
auto *Ptr = UPtr.get();
296+
SchemaNodes.emplace_back(std::move(UPtr));
297+
return Ptr;
298+
}
299+
300+
Schema *getTopSchema() const {
301+
return Schemas.empty() ? nullptr : Schemas.back();
302+
}
303+
};
304+
305+
// Define non-member operator<< so that Output can stream out document list.
306+
template <typename T>
307+
inline std::enable_if_t<has_DocumentListTraits<T>::value, GenerateSchema &>
308+
operator<<(GenerateSchema &Gen, T &DocList) {
309+
EmptyContext Ctx;
310+
Gen.preflightDocument();
311+
yamlize(Gen, DocumentListTraits<T>::element(Gen, DocList, 0), true, Ctx);
312+
Gen.postflightDocument();
313+
return Gen;
314+
}
315+
316+
// Define non-member operator<< so that Output can stream out a map.
317+
template <typename T>
318+
inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value,
319+
GenerateSchema &>
320+
operator<<(GenerateSchema &Gen, T &Map) {
321+
EmptyContext Ctx;
322+
Gen.preflightDocument();
323+
yamlize(Gen, Map, true, Ctx);
324+
Gen.postflightDocument();
325+
return Gen;
326+
}
327+
328+
// Define non-member operator<< so that Output can stream out a sequence.
329+
template <typename T>
330+
inline std::enable_if_t<has_SequenceTraits<T>::value, GenerateSchema &>
331+
operator<<(GenerateSchema &Gen, T &Seq) {
332+
EmptyContext Ctx;
333+
Gen.preflightDocument();
334+
yamlize(Gen, Seq, true, Ctx);
335+
Gen.postflightDocument();
336+
return Gen;
337+
}
338+
339+
// Define non-member operator<< so that Output can stream out a block scalar.
340+
template <typename T>
341+
inline std::enable_if_t<has_BlockScalarTraits<T>::value, GenerateSchema &>
342+
operator<<(GenerateSchema &Gen, T &Val) {
343+
EmptyContext Ctx;
344+
Gen.preflightDocument();
345+
yamlize(Gen, Val, true, Ctx);
346+
Gen.postflightDocument();
347+
return Gen;
348+
}
349+
350+
// Define non-member operator<< so that Output can stream out a string map.
351+
template <typename T>
352+
inline std::enable_if_t<has_CustomMappingTraits<T>::value, GenerateSchema &>
353+
operator<<(GenerateSchema &Gen, T &Val) {
354+
EmptyContext Ctx;
355+
Gen.preflightDocument();
356+
yamlize(Gen, Val, true, Ctx);
357+
Gen.postflightDocument();
358+
return Gen;
359+
}
360+
361+
// Define non-member operator<< so that Output can stream out a polymorphic
362+
// type.
363+
template <typename T>
364+
inline std::enable_if_t<has_PolymorphicTraits<T>::value, GenerateSchema &>
365+
operator<<(GenerateSchema &Gen, T &Val) {
366+
EmptyContext Ctx;
367+
Gen.preflightDocument();
368+
yamlize(Gen, Val, true, Ctx);
369+
Gen.postflightDocument();
370+
return Gen;
371+
}
372+
373+
// Provide better error message about types missing a trait specialization
374+
template <typename T>
375+
inline std::enable_if_t<missingTraits<T, EmptyContext>::value, GenerateSchema &>
376+
operator<<(GenerateSchema &Gen, T &seq) {
377+
char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
378+
return Gen;
379+
}
380+
381+
} // namespace yaml
382+
383+
} // namespace llvm
384+
385+
#endif // LLVM_SUPPORT_YAMLGENERATE_SCHEMA_H

0 commit comments

Comments
 (0)