Prevent accidents with uninitialized nodes (#1139)

This comes from #1092 where I wrote bad code and I felt it should have been caught more clearly.
This commit is contained in:
Jon Meow
2022-03-16 16:03:50 -07:00
committed by GitHub
parent 48d5b18026
commit 50d9fc5189
3 changed files with 25 additions and 1 deletions
+6
View File
@@ -37,6 +37,7 @@ auto ParseTree::postorder() const -> llvm::iterator_range<PostorderIterator> {
auto ParseTree::postorder(Node n) const
-> llvm::iterator_range<PostorderIterator> {
CHECK(n.is_valid());
// The postorder ends after this node, the root, and begins at the start of
// its subtree.
int end_index = n.index_ + 1;
@@ -47,6 +48,7 @@ auto ParseTree::postorder(Node n) const
auto ParseTree::children(Node n) const
-> llvm::iterator_range<SiblingIterator> {
CHECK(n.is_valid());
int end_index = n.index_ - node_impls_[n.index_].subtree_size;
return {SiblingIterator(*this, Node(n.index_ - 1)),
SiblingIterator(*this, Node(end_index))};
@@ -59,18 +61,22 @@ auto ParseTree::roots() const -> llvm::iterator_range<SiblingIterator> {
}
auto ParseTree::node_has_error(Node n) const -> bool {
CHECK(n.is_valid());
return node_impls_[n.index_].has_error;
}
auto ParseTree::node_kind(Node n) const -> ParseNodeKind {
CHECK(n.is_valid());
return node_impls_[n.index_].kind;
}
auto ParseTree::node_token(Node n) const -> TokenizedBuffer::Token {
CHECK(n.is_valid());
return node_impls_[n.index_].token;
}
auto ParseTree::GetNodeText(Node n) const -> llvm::StringRef {
CHECK(n.is_valid());
return tokens_->GetTokenText(node_impls_[n.index_].token);
}