mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Swift: add label iteration
This commit is contained in:
@@ -17,6 +17,8 @@ namespace codeql {
|
||||
{{#classes}}
|
||||
|
||||
struct {{name}}{{#has_bases}} : {{#bases}}{{^first}}, {{/first}}{{ref.name}}{{/bases}}{{/has_bases}} {
|
||||
static constexpr const char* NAME = "{{name}}";
|
||||
|
||||
{{#final}}
|
||||
explicit {{name}}(TrapLabel<{{name}}Tag> id) : id{id} {}
|
||||
|
||||
@@ -33,6 +35,41 @@ struct {{name}}{{#has_bases}} : {{#bases}}{{^first}}, {{/first}}{{ref.name}}{{/b
|
||||
}
|
||||
{{/final}}
|
||||
|
||||
{{^final}}
|
||||
protected:
|
||||
{{/final}}
|
||||
template <typename F>
|
||||
void forEachLabel(F f) {
|
||||
{{#final}}
|
||||
f("id", -1, id);
|
||||
{{/final}}
|
||||
{{#bases}}
|
||||
{{ref.name}}::forEachLabel(f);
|
||||
{{/bases}}
|
||||
{{#fields}}
|
||||
{{#is_label}}
|
||||
{{#is_repeated}}
|
||||
for (auto i = 0u; i < {{field_name}}.size(); ++i) {
|
||||
{{#is_optional}}
|
||||
if ({{field_name}}[i]) f("{{field_name}}", i, *{{field_name}}[i]);
|
||||
{{/is_optional}}
|
||||
{{^is_optional}}
|
||||
f("{{field_name}}", i, {{field_name}}[i]);
|
||||
{{/is_optional}}
|
||||
}
|
||||
{{/is_repeated}}
|
||||
{{^is_repeated}}
|
||||
{{#is_optional}}
|
||||
if ({{field_name}}) f("{{field_name}}", -1, *{{field_name}});
|
||||
{{/is_optional}}
|
||||
{{^is_optional}}
|
||||
f("{{field_name}}", -1, {{field_name}});
|
||||
{{/is_optional}}
|
||||
{{/is_repeated}}
|
||||
{{/is_label}}
|
||||
{{/fields}}
|
||||
}
|
||||
|
||||
protected:
|
||||
void emit({{^final}}TrapLabel<{{name}}Tag> id, {{/final}}std::ostream& out) const;
|
||||
};
|
||||
|
||||
@@ -14,12 +14,24 @@ namespace codeql {
|
||||
|
||||
// {{table_name}}
|
||||
struct {{name}}Trap {
|
||||
{{#fields}}
|
||||
static constexpr const char* NAME = "{{name}}Trap";
|
||||
|
||||
{{#fields}}
|
||||
{{type}} {{field_name}}{};
|
||||
{{/fields}}
|
||||
{{/fields}}
|
||||
|
||||
template <typename F>
|
||||
void forEachLabel(F f) {
|
||||
{{#fields}}
|
||||
{{#is_label}}
|
||||
f("{{field_name}}", -1, {{field_name}});
|
||||
{{/is_label}}
|
||||
{{/fields}}
|
||||
}
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const {{name}}Trap &e);
|
||||
|
||||
{{#id}}
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -60,20 +60,29 @@ class SwiftDispatcher {
|
||||
}
|
||||
|
||||
template <typename Entry>
|
||||
void emit(const Entry& entry) {
|
||||
void emit(Entry&& entry) {
|
||||
entry.forEachLabel([&entry](const char* field, int index, auto& label) {
|
||||
if (!label.valid()) {
|
||||
std::cerr << entry.NAME << " has undefined " << field;
|
||||
if (index >= 0) {
|
||||
std::cerr << '[' << index << ']';
|
||||
}
|
||||
std::cerr << '\n';
|
||||
}
|
||||
});
|
||||
trap.emit(entry);
|
||||
}
|
||||
|
||||
template <typename Entry>
|
||||
void emit(const std::optional<Entry>& entry) {
|
||||
void emit(std::optional<Entry>&& entry) {
|
||||
if (entry) {
|
||||
emit(*entry);
|
||||
emit(std::move(*entry));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Cases>
|
||||
void emit(const std::variant<Cases...>& entry) {
|
||||
std::visit([this](const auto& e) { this->emit(e); }, entry);
|
||||
void emit(std::variant<Cases...>&& entry) {
|
||||
std::visit([this](auto&& e) { this->emit(std::move(e)); }, std::move(entry));
|
||||
}
|
||||
|
||||
// This is a helper method to emit TRAP entries for AST nodes that we don't fully support yet.
|
||||
|
||||
Reference in New Issue
Block a user