fix(lib): replace raw array accesses with array_get

(cherry picked from commit 8bd923ab9e)
This commit is contained in:
Will Lillis 2025-05-04 00:12:07 -04:00 committed by Amaan Qureshi
parent bf655c0bea
commit b890e8bea0
7 changed files with 229 additions and 227 deletions

View file

@ -16,11 +16,11 @@ typedef struct {
// CursorChildIterator
static inline bool ts_tree_cursor_is_entry_visible(const TreeCursor *self, uint32_t index) {
TreeCursorEntry *entry = &self->stack.contents[index];
TreeCursorEntry *entry = array_get(&self->stack, index);
if (index == 0 || ts_subtree_visible(*entry->subtree)) {
return true;
} else if (!ts_subtree_extra(*entry->subtree)) {
TreeCursorEntry *parent_entry = &self->stack.contents[index - 1];
TreeCursorEntry *parent_entry = array_get(&self->stack, index - 1);
return ts_language_alias_at(
self->tree->language,
parent_entry->subtree->ptr->production_id,
@ -374,7 +374,7 @@ TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self
return step;
// restore position from the parent node
const TreeCursorEntry *parent = &self->stack.contents[self->stack.size - 2];
const TreeCursorEntry *parent = array_get(&self->stack, self->stack.size - 2);
Length position = parent->position;
uint32_t child_index = array_back(&self->stack)->child_index;
const Subtree *children = ts_subtree_children((*(parent->subtree)));
@ -425,7 +425,7 @@ void ts_tree_cursor_goto_descendant(
// Ascend to the lowest ancestor that contains the goal node.
for (;;) {
uint32_t i = self->stack.size - 1;
TreeCursorEntry *entry = &self->stack.contents[i];
TreeCursorEntry *entry = array_get(&self->stack, i);
uint32_t next_descendant_index =
entry->descendant_index +
(ts_tree_cursor_is_entry_visible(self, i) ? 1 : 0) +
@ -479,7 +479,7 @@ TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) {
bool is_extra = ts_subtree_extra(*last_entry->subtree);
TSSymbol alias_symbol = is_extra ? 0 : self->root_alias_symbol;
if (self->stack.size > 1 && !is_extra) {
TreeCursorEntry *parent_entry = &self->stack.contents[self->stack.size - 2];
TreeCursorEntry *parent_entry = array_get(&self->stack, self->stack.size - 2);
alias_symbol = ts_language_alias_at(
self->tree->language,
parent_entry->subtree->ptr->production_id,
@ -516,8 +516,8 @@ void ts_tree_cursor_current_status(
// Walk up the tree, visiting the current node and its invisible ancestors,
// because fields can refer to nodes through invisible *wrapper* nodes,
for (unsigned i = self->stack.size - 1; i > 0; i--) {
TreeCursorEntry *entry = &self->stack.contents[i];
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
TreeCursorEntry *entry = array_get(&self->stack, i);
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1);
const TSSymbol *alias_sequence = ts_language_alias_sequence(
self->tree->language,
@ -630,11 +630,11 @@ uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *_self) {
TSNode ts_tree_cursor_parent_node(const TSTreeCursor *_self) {
const TreeCursor *self = (const TreeCursor *)_self;
for (int i = (int)self->stack.size - 2; i >= 0; i--) {
TreeCursorEntry *entry = &self->stack.contents[i];
TreeCursorEntry *entry = array_get(&self->stack, i);
bool is_visible = true;
TSSymbol alias_symbol = 0;
if (i > 0) {
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1);
alias_symbol = ts_language_alias_at(
self->tree->language,
parent_entry->subtree->ptr->production_id,
@ -659,8 +659,8 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) {
// Walk up the tree, visiting the current node and its invisible ancestors.
for (unsigned i = self->stack.size - 1; i > 0; i--) {
TreeCursorEntry *entry = &self->stack.contents[i];
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
TreeCursorEntry *entry = array_get(&self->stack, i);
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1);
// Stop walking up when another visible node is found.
if (