Skip to content

Commit 8d3e6ff

Browse files
committed
Swift: add label iteration
1 parent 450a4a0 commit 8d3e6ff

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

swift/codegen/templates/cpp_classes_h.mustache

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace codeql {
1717
{{#classes}}
1818

1919
struct {{name}}{{#has_bases}} : {{#bases}}{{^first}}, {{/first}}{{ref.name}}{{/bases}}{{/has_bases}} {
20+
static constexpr const char* NAME = "{{name}}";
21+
2022
{{#final}}
2123
explicit {{name}}(TrapLabel<{{name}}Tag> id) : id{id} {}
2224

@@ -33,6 +35,41 @@ struct {{name}}{{#has_bases}} : {{#bases}}{{^first}}, {{/first}}{{ref.name}}{{/b
3335
}
3436
{{/final}}
3537

38+
{{^final}}
39+
protected:
40+
{{/final}}
41+
template <typename F>
42+
void forEachLabel(F f) {
43+
{{#final}}
44+
f("id", -1, id);
45+
{{/final}}
46+
{{#bases}}
47+
{{ref.name}}::forEachLabel(f);
48+
{{/bases}}
49+
{{#fields}}
50+
{{#is_label}}
51+
{{#is_repeated}}
52+
for (auto i = 0u; i < {{field_name}}.size(); ++i) {
53+
{{#is_optional}}
54+
if ({{field_name}}[i]) f("{{field_name}}", i, *{{field_name}}[i]);
55+
{{/is_optional}}
56+
{{^is_optional}}
57+
f("{{field_name}}", i, {{field_name}}[i]);
58+
{{/is_optional}}
59+
}
60+
{{/is_repeated}}
61+
{{^is_repeated}}
62+
{{#is_optional}}
63+
if ({{field_name}}) f("{{field_name}}", -1, *{{field_name}});
64+
{{/is_optional}}
65+
{{^is_optional}}
66+
f("{{field_name}}", -1, {{field_name}});
67+
{{/is_optional}}
68+
{{/is_repeated}}
69+
{{/is_label}}
70+
{{/fields}}
71+
}
72+
3673
protected:
3774
void emit({{^final}}TrapLabel<{{name}}Tag> id, {{/final}}std::ostream& out) const;
3875
};

swift/codegen/templates/trap_traps_h.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@ namespace codeql {
1414

1515
// {{table_name}}
1616
struct {{name}}Trap {
17-
{{#fields}}
17+
static constexpr const char* NAME = "{{name}}Trap";
18+
19+
{{#fields}}
1820
{{type}} {{field_name}}{};
19-
{{/fields}}
21+
{{/fields}}
22+
23+
template <typename F>
24+
void forEachLabel(F f) {
25+
{{#fields}}
26+
{{#is_label}}
27+
f("{{field_name}}", -1, {{field_name}});
28+
{{/is_label}}
29+
{{/fields}}
30+
}
2031
};
2132

2233
std::ostream &operator<<(std::ostream &out, const {{name}}Trap &e);
34+
2335
{{#id}}
2436

2537
namespace detail {

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,29 @@ class SwiftDispatcher {
6060
}
6161

6262
template <typename Entry>
63-
void emit(const Entry& entry) {
63+
void emit(Entry&& entry) {
64+
entry.forEachLabel([&entry](const char* field, int index, auto& label) {
65+
if (!label.valid()) {
66+
std::cerr << entry.NAME << " has undefined " << field;
67+
if (index >= 0) {
68+
std::cerr << '[' << index << ']';
69+
}
70+
std::cerr << '\n';
71+
}
72+
});
6473
trap.emit(entry);
6574
}
6675

6776
template <typename Entry>
68-
void emit(const std::optional<Entry>& entry) {
77+
void emit(std::optional<Entry>&& entry) {
6978
if (entry) {
70-
emit(*entry);
79+
emit(std::move(*entry));
7180
}
7281
}
7382

7483
template <typename... Cases>
75-
void emit(const std::variant<Cases...>& entry) {
76-
std::visit([this](const auto& e) { this->emit(e); }, entry);
84+
void emit(std::variant<Cases...>&& entry) {
85+
std::visit([this](auto&& e) { this->emit(std::move(e)); }, std::move(entry));
7786
}
7887

7988
// This is a helper method to emit TRAP entries for AST nodes that we don't fully support yet.

0 commit comments

Comments
 (0)