Skip to content

Commit adc8304

Browse files
refactor(extract): dedup C declarator-name walker into one shared helper
Addresses DeusData#463 review: the declarator-chain name resolver was copied into helpers.c, extract_unified.c, and extract_defs.c, and CBM_DECLARATOR_DEPTH_LIMIT was #defined twice -- the same triplication drift that caused DeusData#438. - Add cbm_resolve_c_declarator_name_node() to helpers.{c,h} as the single source of truth, carrying is_c_terminal_name/resolve_qualified_name with it. - Route the defs, calls, and unified extractors through it. - Hoist CBM_DECLARATOR_DEPTH_LIMIT into helpers.h; extract_defs.c's DECLARATOR_DEPTH_LIMIT now derives from it. Canonicalizes on the original extract_defs.c logic (operator/destructor aware) so defs behavior is unchanged and calls/unified now agree with it. Test: full suite green except an unrelated ASan RSS-budget check; clang-format clean. Signed-off-by: Kris Kersey <kris@kerseyfabrications.com>
1 parent 13c3429 commit adc8304

4 files changed

Lines changed: 45 additions & 108 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
// Tree traversal limits.
2424
enum {
2525
TEMPLATE_DEPTH_LIMIT = 4,
26-
DECLARATOR_DEPTH_LIMIT = 8,
26+
DECLARATOR_DEPTH_LIMIT = CBM_DECLARATOR_DEPTH_LIMIT, // shared define in helpers.h
27+
2728
EXPORT_ANCESTOR_DEPTH = 4,
2829
DECORATOR_SCAN_LIMIT = 3,
2930
C_RETURN_WALK_DEPTH = 5,
@@ -457,27 +458,6 @@ static TSNode resolve_func_name_fp(TSNode node, CBMLanguage lang, const char *ki
457458
return null_node;
458459
}
459460

460-
// Check if a node type is a terminal C declarator name.
461-
static bool is_c_terminal_name(const char *dk) {
462-
return strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
463-
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0 ||
464-
strcmp(dk, "destructor_name") == 0;
465-
}
466-
467-
// Resolve name from a C++ qualified_identifier/scoped_identifier.
468-
static TSNode resolve_qualified_name(TSNode decl) {
469-
static const char *name_kinds[] = {"operator_name", "operator_cast", "destructor_name",
470-
"identifier", "field_identifier", NULL};
471-
for (const char **k = name_kinds; *k; k++) {
472-
TSNode found = cbm_find_child_by_kind(decl, *k);
473-
if (!ts_node_is_null(found)) {
474-
return found;
475-
}
476-
}
477-
TSNode null_node = {0};
478-
return null_node;
479-
}
480-
481461
// C++/CUDA: out-of-line method definitions name the function with a qualified
482462
// declarator (`Foo::bar`, or `ns::Foo::bar`). Return the immediate enclosing
483463
// class name (the scope segment directly left of the function name, e.g. "Foo"),
@@ -528,30 +508,6 @@ static char *cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *
528508
return (text && text[0]) ? text : NULL;
529509
}
530510

531-
// Resolve function name from C/C++/CUDA/GLSL declarator chain.
532-
static TSNode resolve_c_declarator_name(TSNode node) {
533-
TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));
534-
for (int depth = 0; depth < DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
535-
const char *dk = ts_node_type(decl);
536-
if (is_c_terminal_name(dk)) {
537-
return decl;
538-
}
539-
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
540-
return resolve_qualified_name(decl);
541-
}
542-
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
543-
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
544-
inner = ts_node_named_child(decl, 0);
545-
}
546-
if (ts_node_is_null(inner)) {
547-
break;
548-
}
549-
decl = inner;
550-
}
551-
TSNode null_node = {0};
552-
return null_node;
553-
}
554-
555511
// R: resolve function_definition name from parent binary_operator lhs.
556512
static TSNode resolve_r_func_name(TSNode node) {
557513
TSNode parent = ts_node_parent(node);
@@ -675,7 +631,7 @@ static TSNode resolve_func_name_c_family(TSNode *node_ptr, CBMLanguage lang, con
675631
lang == CBM_LANG_GLSL || lang == CBM_LANG_HLSL || lang == CBM_LANG_ISPC ||
676632
lang == CBM_LANG_SLANG) &&
677633
strcmp(kind, "function_definition") == 0) {
678-
return resolve_c_declarator_name(*node_ptr);
634+
return cbm_resolve_c_declarator_name_node(*node_ptr);
679635
}
680636
TSNode null_node = {0};
681637
return null_node;

‎internal/cbm/extract_unified.c‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -87,44 +87,6 @@ static const char *compute_wolfram_func_qn(CBMExtractCtx *ctx, TSNode node) {
8787
return NULL;
8888
}
8989

90-
/* C/C++/CUDA/GLSL: function_definition has no `name` field — the name is nested
91-
* in the declarator chain. Descend the `declarator` field to the innermost name
92-
* node. Without this, the enclosing-function scope for calls made inside a C
93-
* function resolves to NULL and the call is attributed to the module rather than
94-
* the function (issue #438). Mirrors resolve_c_declarator_name() in extract_defs.c. */
95-
#ifndef CBM_DECLARATOR_DEPTH_LIMIT
96-
#define CBM_DECLARATOR_DEPTH_LIMIT 8 /* matches DECLARATOR_DEPTH_LIMIT in extract_defs.c */
97-
#endif
98-
static TSNode resolve_c_declarator_name_node(TSNode node) {
99-
TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));
100-
for (int depth = 0; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
101-
const char *dk = ts_node_type(decl);
102-
if (strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
103-
strcmp(dk, "type_identifier") == 0 || strcmp(dk, "destructor_name") == 0 ||
104-
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0) {
105-
return decl;
106-
}
107-
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
108-
TSNode nm = ts_node_child_by_field_name(decl, TS_FIELD("name"));
109-
if (!ts_node_is_null(nm)) {
110-
decl = nm;
111-
continue;
112-
}
113-
return decl;
114-
}
115-
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
116-
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
117-
inner = ts_node_named_child(decl, 0);
118-
}
119-
if (ts_node_is_null(inner)) {
120-
break;
121-
}
122-
decl = inner;
123-
}
124-
TSNode null_node = {0};
125-
return null_node;
126-
}
127-
12890
// Resolve the name node for a function, handling arrow functions.
12991
static TSNode resolve_func_name_node(TSNode node) {
13092
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
@@ -141,7 +103,7 @@ static TSNode resolve_func_name_node(TSNode node) {
141103
}
142104
/* C/C++/CUDA/GLSL: function_definition name lives in the declarator chain. */
143105
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_definition") == 0) {
144-
name_node = resolve_c_declarator_name_node(node);
106+
name_node = cbm_resolve_c_declarator_name_node(node);
145107
}
146108
return name_node;
147109
}

‎internal/cbm/helpers.c‎

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -717,33 +717,38 @@ TSNode cbm_find_enclosing_func(TSNode node, CBMLanguage lang) {
717717
return null_node;
718718
}
719719

720-
// Get the name of a function node (basic: try "name" field)
721-
// C/C++/CUDA/GLSL: function_definition has no "name" field — the function name is
722-
// nested in the declarator chain (pointer/function/parenthesized/array
723-
// declarators wrap it). Descend the `declarator` field to the innermost name
724-
// node. Without this, calls made inside C functions are attributed to the module
725-
// rather than the enclosing function (issue #438). Mirrors resolve_c_declarator_name()
726-
// in extract_defs.c.
727-
#ifndef CBM_DECLARATOR_DEPTH_LIMIT
728-
#define CBM_DECLARATOR_DEPTH_LIMIT 8 /* matches DECLARATOR_DEPTH_LIMIT in extract_defs.c */
729-
#endif
730-
static TSNode c_declarator_name_node(TSNode func_node) {
720+
// Check if a node type is a terminal C declarator name.
721+
static bool is_c_terminal_name(const char *dk) {
722+
return strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
723+
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0 ||
724+
strcmp(dk, "destructor_name") == 0;
725+
}
726+
727+
// Resolve name from a C++ qualified_identifier/scoped_identifier.
728+
static TSNode resolve_qualified_name(TSNode decl) {
729+
static const char *name_kinds[] = {"operator_name", "operator_cast", "destructor_name",
730+
"identifier", "field_identifier", NULL};
731+
for (const char **k = name_kinds; *k; k++) {
732+
TSNode found = cbm_find_child_by_kind(decl, *k);
733+
if (!ts_node_is_null(found)) {
734+
return found;
735+
}
736+
}
737+
TSNode null_node = {0};
738+
return null_node;
739+
}
740+
741+
// Resolve function name from C/C++/CUDA/GLSL declarator chain. Shared canonical
742+
// implementation — see the header for the full rationale (#438).
743+
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
731744
TSNode decl = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
732745
for (int depth = 0; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
733746
const char *dk = ts_node_type(decl);
734-
if (strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
735-
strcmp(dk, "type_identifier") == 0 || strcmp(dk, "destructor_name") == 0 ||
736-
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0) {
747+
if (is_c_terminal_name(dk)) {
737748
return decl;
738749
}
739750
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
740-
// out-of-line method def (Foo::bar): take the rightmost name segment
741-
TSNode nm = ts_node_child_by_field_name(decl, TS_FIELD("name"));
742-
if (!ts_node_is_null(nm)) {
743-
decl = nm;
744-
continue;
745-
}
746-
return decl;
751+
return resolve_qualified_name(decl);
747752
}
748753
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
749754
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
@@ -794,7 +799,7 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
794799
}
795800
// C/C++/CUDA/GLSL: function_definition carries its name in the declarator chain.
796801
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
797-
TSNode dn = c_declarator_name_node(func_node);
802+
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
798803
if (!ts_node_is_null(dn)) {
799804
return cbm_node_text(a, dn, source);
800805
}

‎internal/cbm/helpers.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ const char *cbm_enclosing_func_qn(CBMArena *a, TSNode node, CBMLanguage lang, co
3636
// Cached version: uses ctx->ef_cache to avoid repeated parent-chain walks.
3737
const char *cbm_enclosing_func_qn_cached(CBMExtractCtx *ctx, TSNode node);
3838

39+
// Max declarator-chain descent depth for C/C++/CUDA/GLSL function-name
40+
// resolution. Single source of truth — extract_defs.c's DECLARATOR_DEPTH_LIMIT
41+
// is derived from this so the three extractors cannot drift.
42+
#define CBM_DECLARATOR_DEPTH_LIMIT 8
43+
44+
// Resolve the function-name node for a C/C++/CUDA/GLSL `function_definition`.
45+
// Such nodes have no `name` field — the name is nested in the declarator chain
46+
// (pointer/function/parenthesized/array declarators wrap it; out-of-line method
47+
// definitions name it with a qualified_identifier). Descends the `declarator`
48+
// field to the innermost name node and returns it, or a null node if none is
49+
// found. Shared by the defs, calls, and unified extractors so all three agree on
50+
// enclosing-function attribution — drift between private copies caused #438.
51+
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node);
52+
3953
// Find a child node by kind string.
4054
TSNode cbm_find_child_by_kind(TSNode parent, const char *kind);
4155

0 commit comments

Comments
 (0)