Skip to content

Commit 6b23078

Browse files
committed
fix(extract): scan every attribute_list sibling, not just the first
A C# attribute stack ([Foo][Bar][Baz]) compiles to separate sibling attribute_list nodes, one per bracket group — not one attribute_list holding several entries. find_jvm_modifiers() used ts_node_child_by_field_name(), which only ever returns the first child registered under a given field, so every attribute after the first bracket group was silently dropped and never produced a DECORATES edge (DeusData#1692). find_jvm_modifiers() now returns every matching wrapper (count + out array, capped at MAX_ATTR_WRAPPERS) instead of a single TSNode, and its two callers (extract_decorators, scan_route_annotations) iterate all of them. The multi-match child scan lives in a new helper, cbm_find_children_by_kind() (helpers.c/helpers.h), the multi-match sibling of the existing cbm_find_child_by_kind() — keeps find_jvm_modifiers() a short per-language dispatch that delegates the traversal, same shape it had before this fix. Both callers size `wrappers` at MAX_ATTR_WRAPPERS. scan_route_annotations appends the owner node after the wrappers, so its fill is capped one lower (MAX_ATTR_WRAPPERS_MINUS_1) rather than the array being declared one larger — the MAX_X / MAX_X_MINUS_1 pair this file already uses for MAX_BASES, MAX_PARAMS and MAX_RETURN_TYPES. No-op for the other languages on this switch: Java/Kotlin/Swift use the `modifiers` wrapper, a single node that never repeats, and PHP groups a `#[A] #[B] #[C]` stack under ONE attribute_list holding three attribute_group children, which the previous lookup already returned whole. Signed-off-by: Jumaga2015 <jumaga2015@gmail.com>
1 parent 46ae198 commit 6b23078

3 files changed

Lines changed: 51 additions & 29 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#define MAX_PARAMS_MINUS_1 31
2424
#define MAX_RETURN_TYPES 16
2525
#define MAX_RETURN_TYPES_MINUS_1 15
26+
#define MAX_ATTR_WRAPPERS 16 // stacked C#/PHP attribute_list siblings per declaration (#1692)
27+
#define MAX_ATTR_WRAPPERS_MINUS_1 15
2628

2729
// Tree traversal limits.
2830
enum {
@@ -1287,7 +1289,7 @@ static const char *extract_docstring(CBMArena *a, TSNode node, const char *sourc
12871289
return NULL;
12881290
}
12891291

1290-
static TSNode find_jvm_modifiers(TSNode node, CBMLanguage lang);
1292+
static int find_jvm_modifiers(TSNode node, CBMLanguage lang, TSNode *out, int max);
12911293

12921294
/* HTTP method names recognized in decorator calls (e.g., @router.post → "POST") */
12931295
static const char *decorator_method_name(const char *attr_text) {
@@ -1673,12 +1675,9 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
16731675
*out_method = NULL;
16741676
*out_jax_path = NULL;
16751677

1676-
TSNode wrappers[2];
1677-
int wn = 0;
1678-
TSNode modifiers = find_jvm_modifiers(owner, spec->language);
1679-
if (!ts_node_is_null(modifiers)) {
1680-
wrappers[wn++] = modifiers;
1681-
}
1678+
/* MINUS_1: the owner node itself is appended below, after the wrappers. */
1679+
TSNode wrappers[MAX_ATTR_WRAPPERS];
1680+
int wn = find_jvm_modifiers(owner, spec->language, wrappers, MAX_ATTR_WRAPPERS_MINUS_1);
16821681
/* Direct-child annotations (some grammars attach the annotation as a child
16831682
* of the method node rather than under `modifiers`). */
16841683
wrappers[wn++] = owner;
@@ -1832,13 +1831,23 @@ static int count_modifier_annotations(TSNode modifiers, const CBMLangSpec *spec)
18321831
return count;
18331832
}
18341833

1835-
// Find the wrapper child that holds annotations/attributes for languages where
1836-
// they are nested under an intermediate node rather than being a prev-sibling:
1837-
// Java/Kotlin/C#/Swift → `modifiers` (contains annotation/attribute)
1838-
// PHP 8 → `attribute_list` (contains attribute_group)
1839-
// Returns a null node when the language has no such wrapper.
1840-
static TSNode find_jvm_modifiers(TSNode node, CBMLanguage lang) {
1841-
TSNode null_node = {0};
1834+
// Find every wrapper child that holds annotations/attributes for languages
1835+
// where they are nested under an intermediate node rather than being a
1836+
// prev-sibling:
1837+
// Java/Kotlin/Swift → `modifiers` (one node, contains every annotation)
1838+
// C#/PHP 8 → `attribute_list` (contains attribute/attribute_group)
1839+
//
1840+
// C#/PHP attribute stacks are NOT a single wrapper: each bracketed group
1841+
// (`[Foo]`, `[Bar]`, ...) compiles to its own `attribute_list` node, so
1842+
// `[A] [B] [C]` above a declaration produces three separate `attribute_list`
1843+
// siblings among that declaration's children — not one `attribute_list`
1844+
// holding three entries. A field-name lookup (`ts_node_child_by_field_name`)
1845+
// only ever returns the first child registered under a given field, so using
1846+
// it here silently dropped every attribute after the first bracket group
1847+
// (#1692). Scanning all children by kind fixes that for C#/PHP and is a
1848+
// no-op change for Java/Kotlin/Swift, where `modifiers` never repeats.
1849+
// Writes up to `max` wrapper nodes into `out`; returns how many were found.
1850+
static int find_jvm_modifiers(TSNode node, CBMLanguage lang, TSNode *out, int max) {
18421851
const char *wrapper = NULL;
18431852
switch (lang) {
18441853
case CBM_LANG_JAVA:
@@ -1848,19 +1857,12 @@ static TSNode find_jvm_modifiers(TSNode node, CBMLanguage lang) {
18481857
break;
18491858
case CBM_LANG_CSHARP:
18501859
case CBM_LANG_PHP:
1851-
/* C# attributes live in an `attribute_list` child (modifiers like
1852-
* `public` are separate `modifier` nodes); PHP 8 likewise nests
1853-
* `attribute_group` under `attribute_list`. */
18541860
wrapper = "attribute_list";
18551861
break;
18561862
default:
1857-
return null_node;
1858-
}
1859-
TSNode w = ts_node_child_by_field_name(node, wrapper, (uint32_t)strlen(wrapper));
1860-
if (ts_node_is_null(w)) {
1861-
w = cbm_find_child_by_kind(node, wrapper);
1863+
return 0;
18621864
}
1863-
return w;
1865+
return cbm_find_children_by_kind(node, wrapper, out, max);
18641866
}
18651867

18661868
// Count direct children of `node` that are decorator/annotation nodes (used by
@@ -1938,13 +1940,14 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
19381940
prev = ts_node_prev_sibling(prev);
19391941
}
19401942

1941-
TSNode modifiers = {0};
1943+
TSNode wrappers[MAX_ATTR_WRAPPERS];
1944+
int wn = 0;
19421945
int mod_count = 0;
19431946
int child_count = 0;
19441947
if (count == 0) {
1945-
modifiers = find_jvm_modifiers(node, lang);
1946-
if (!ts_node_is_null(modifiers)) {
1947-
mod_count = count_modifier_annotations(modifiers, spec);
1948+
wn = find_jvm_modifiers(node, lang, wrappers, MAX_ATTR_WRAPPERS);
1949+
for (int w = 0; w < wn; w++) {
1950+
mod_count += count_modifier_annotations(wrappers[w], spec);
19481951
}
19491952
/* Languages like Scala attach the annotation directly as a child of the
19501953
* definition node (no wrapper, no prev-sibling). */
@@ -1974,8 +1977,8 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
19741977
}
19751978
prev = ts_node_prev_sibling(prev);
19761979
}
1977-
if (!ts_node_is_null(modifiers)) {
1978-
idx = collect_modifier_decorators(a, modifiers, source, spec, result, idx, total);
1980+
for (int w = 0; w < wn && mod_count > 0; w++) {
1981+
idx = collect_modifier_decorators(a, wrappers[w], source, spec, result, idx, total);
19791982
}
19801983
if (child_count > 0) {
19811984
idx = collect_child_decorators(a, node, source, spec, result, idx, total);

‎internal/cbm/helpers.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ TSNode cbm_find_child_by_kind(TSNode parent, const char *kind) {
364364
return null_node;
365365
}
366366

367+
int cbm_find_children_by_kind(TSNode parent, const char *kind, TSNode *out, int max) {
368+
int n = 0;
369+
uint32_t count = ts_node_child_count(parent);
370+
for (uint32_t i = 0; i < count && n < max; i++) {
371+
TSNode child = ts_node_child(parent, i);
372+
if (strcmp(ts_node_type(child), kind) == 0) {
373+
out[n++] = child;
374+
}
375+
}
376+
return n;
377+
}
378+
367379
/* ── Node-type classification: TSSymbol bitset acceleration ───────────────
368380
* cbm_kind_in_set is called for nearly every AST node (function/class/call/
369381
* import/branching sets), so a linear strcmp over the type-name array is a hot

‎internal/cbm/helpers.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ char *cbm_cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *sou
122122
// Find a child node by kind string.
123123
TSNode cbm_find_child_by_kind(TSNode parent, const char *kind);
124124

125+
// Find every child node matching `kind` (unlike cbm_find_child_by_kind,
126+
// which stops at the first match). Writes up to `max` nodes into `out`;
127+
// returns how many were found. Repeated sibling nodes of the same kind are
128+
// common in some grammars (e.g. C#/PHP stack each `[Attr]` as its own
129+
// attribute_list child) — see #1692.
130+
int cbm_find_children_by_kind(TSNode parent, const char *kind, TSNode *out, int max);
131+
125132
// Check if node kind matches a set of types (NULL-terminated array of strings).
126133
bool cbm_kind_in_set(TSNode node, const char **types);
127134

0 commit comments

Comments
 (0)