Skip to content

Commit 06bce44

Browse files
committed
[libSyntax] Add validate method to all syntax nodes
For syntax nodes that previously didn’t have a `validate` method, the newly added `validate` method is a no-op. This will make validation easier in upcoming generic code.
1 parent c5fd7eb commit 06bce44

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

include/swift/Syntax/SyntaxCollection.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ class SyntaxCollection : public Syntax {
6868
}
6969

7070
public:
71-
SyntaxCollection(const RC<const SyntaxData> &Data) : Syntax(Data) {}
71+
SyntaxCollection(const RC<const SyntaxData> &Data) : Syntax(Data) {
72+
validate();
73+
}
7274

7375
SyntaxCollection(std::initializer_list<Element> list):
7476
SyntaxCollection(SyntaxCollection::makeData(list)) {}
7577

78+
void validate() {}
79+
7680
/// Returns true if the collection is empty.
7781
bool empty() const {
7882
return size() == 0;

include/swift/Syntax/SyntaxNodes.h.gyb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ class ${node.name} ${qualifier} : public ${node.base_type} {
5656
% if node.is_buildable():
5757
friend class ${node.name}Builder;
5858
% end
59-
% if node.requires_validation():
6059
void validate() const;
61-
% end
6260

6361
public:
6462
% if node.children:
@@ -70,10 +68,8 @@ public:
7068
% end
7169

7270
${node.name}(const RC<const SyntaxData> &Data) : ${node.base_type}(Data) {
73-
% if node.requires_validation():
74-
this->validate();
75-
% end
76-
}
71+
this->validate();
72+
}
7773

7874
% for child in node.children:
7975
% for line in dedented_lines(child.description):

include/swift/Syntax/TokenSyntax.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class TokenSyntax final : public Syntax {
3333
assert(getRaw()->isToken());
3434
}
3535
public:
36-
TokenSyntax(const RC<const SyntaxData> &Data) : Syntax(Data) {}
36+
TokenSyntax(const RC<const SyntaxData> &Data) : Syntax(Data) {
37+
validate();
38+
}
3739

3840
static TokenSyntax missingToken(const tok Kind, StringRef Text,
3941
const RC<SyntaxArena> &Arena) {

lib/Syntax/SyntaxNodes.cpp.gyb

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,39 @@ using namespace swift;
2525
using namespace swift::syntax;
2626

2727
% for node in SYNTAX_NODES:
28-
% if node.requires_validation():
29-
void ${node.name}::validate() const {
28+
% if not node.is_syntax_collection():
29+
void ${node.name}Ref::validate() const {
30+
% if node.requires_validation():
3031
#ifndef NDEBUG
31-
auto raw = getData()->getRaw();
32+
auto raw = getDataRef()->getRaw();
3233
if (isMissing()) return;
3334
assert(raw->getLayout().size() == ${len(node.children)});
34-
% for child in node.children:
35-
% if child.token_choices:
36-
% choices = ", ".join("tok::" + choice.kind
37-
% for choice in child.token_choices)
35+
% for child in node.children:
36+
% if child.token_choices:
37+
% choices = ", ".join("tok::" + choice.kind
38+
% for choice in child.token_choices)
3839
syntax_assert_child_token(raw, ${child.name}, ${choices});
39-
% end
40-
% if child.main_token() and child.text_choices:
41-
% token_kind = child.main_token().kind
42-
% choices = ", ".join("\"%s\"" % choice
43-
% for choice in child.text_choices)
40+
% end
41+
% if child.main_token() and child.text_choices:
42+
% token_kind = child.main_token().kind
43+
% choices = ", ".join("\"%s\"" % choice
44+
% for choice in child.text_choices)
4445
syntax_assert_child_token_text(raw, ${child.name},
4546
tok::${token_kind}, ${choices});
46-
% end
47-
% if child.node_choices:
47+
% end
48+
% if child.node_choices:
4849
if (auto __Child = raw->getChild(Cursor::${child.name}))
4950
assert(${check_child_condition_raw(child)}(__Child));
51+
% end
5052
% end
51-
% end
5253
#endif
54+
% end
5355
}
56+
57+
void ${node.name}::validate() const {
58+
${node.name}Ref(*this).validate();
59+
}
60+
5461
% end
5562

5663
% for child in node.children:

0 commit comments

Comments
 (0)