diff --git a/internal/cbm/extract_channels.c b/internal/cbm/extract_channels.c index 2c20838b..441fc319 100644 --- a/internal/cbm/extract_channels.c +++ b/internal/cbm/extract_channels.c @@ -137,12 +137,12 @@ static void scan_string_consts_js(CBMExtractCtx *ctx, chan_const_table_t *tbl) { /* Python constant resolution: NAME = "value" (assignment node). */ static void scan_string_consts_python(CBMExtractCtx *ctx, chan_const_table_t *tbl) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0 && tbl->count < CHAN_CONST_CAP) { - TSNode node = stack[--top]; + while (stack.count > 0 && tbl->count < CHAN_CONST_CAP) { + TSNode node = ts_nstack_pop(&stack); const char *kind = ts_node_type(node); if (strcmp(kind, "assignment") == 0) { @@ -165,8 +165,8 @@ static void scan_string_consts_python(CBMExtractCtx *ctx, chan_const_table_t *tb } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -554,12 +554,12 @@ static void extract_channels_python(CBMExtractCtx *ctx) { chan_const_table_t consts = {0}; scan_string_consts_python(ctx, &consts); - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); const char *kind = ts_node_type(node); if (strcmp(kind, "call") == 0) { py_process_call(ctx, node, &consts); @@ -567,8 +567,8 @@ static void extract_channels_python(CBMExtractCtx *ctx) { py_process_decorator(ctx, node, &consts); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -632,18 +632,18 @@ static void go_process_call(CBMExtractCtx *ctx, TSNode call) { } static void extract_channels_go(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); if (strcmp(ts_node_type(node), "call_expression") == 0) { go_process_call(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -729,12 +729,12 @@ static void java_process_annotation(CBMExtractCtx *ctx, TSNode annotation) { } static void extract_channels_java(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); const char *kind = ts_node_type(node); if (strcmp(kind, "method_invocation") == 0) { java_process_call(ctx, node); @@ -742,8 +742,8 @@ static void extract_channels_java(CBMExtractCtx *ctx) { java_process_annotation(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -798,18 +798,18 @@ static void csharp_process_call(CBMExtractCtx *ctx, TSNode call) { } static void extract_channels_csharp(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); if (strcmp(ts_node_type(node), "invocation_expression") == 0) { csharp_process_call(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -861,18 +861,18 @@ static void ruby_process_call(CBMExtractCtx *ctx, TSNode call) { } static void extract_channels_ruby(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); if (strcmp(ts_node_type(node), "call") == 0) { ruby_process_call(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -963,12 +963,12 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) { } static void extract_channels_elixir(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); const char *kind = ts_node_type(node); if (strcmp(kind, "call") == 0) { elixir_process_call(ctx, node); @@ -976,8 +976,8 @@ static void extract_channels_elixir(CBMExtractCtx *ctx) { elixir_process_function_def(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -1039,18 +1039,18 @@ static void rust_process_call(CBMExtractCtx *ctx, TSNode call) { } static void extract_channels_rust(CBMExtractCtx *ctx) { - TSNode stack[CHAN_STACK_CAP]; - int top = 0; - stack[top++] = ctx->root; + TSNodeStack stack; + ts_nstack_init(&stack, ctx->arena, CHAN_STACK_CAP); + ts_nstack_push(&stack, ctx->arena, ctx->root); - while (top > 0) { - TSNode node = stack[--top]; + while (stack.count > 0) { + TSNode node = ts_nstack_pop(&stack); if (strcmp(ts_node_type(node), "call_expression") == 0) { rust_process_call(ctx, node); } uint32_t count = ts_node_child_count(node); - for (int i = (int)count - SKIP_ONE; i >= 0 && top < CHAN_STACK_CAP; i--) { - stack[top++] = ts_node_child(node, (uint32_t)i); + for (int i = (int)count - SKIP_ONE; i >= 0; i--) { + ts_nstack_push(&stack, ctx->arena, ts_node_child(node, (uint32_t)i)); } } } @@ -1092,3 +1092,4 @@ void cbm_extract_channels(CBMExtractCtx *ctx) { break; /* no channel detection for this language */ } } +