Skip to content

Commit 13c3429

Browse files
fix(extract): attribute C/C++ CALLS edges to the enclosing function
A CALLS edge whose caller is a C/C++/CUDA/GLSL function was sourced to the file's Module node instead of the calling Function. "Find callers of X" returned a file path, outbound trace_path returned empty, and (:Function)-[:CALLS]->(:Function) queries missed for these languages. Root cause: the enclosing-function resolvers read only tree-sitter's `name` field, but a `function_definition` node has none — the name lives in the declarator chain (pointer/function/parenthesized/array declarators). So func_node_name() (internal/cbm/helpers.c) and resolve_func_name_node() (internal/cbm/extract_unified.c) returned NULL, the enclosing scope fell back to the module QN, and the edge was attributed to the Module node. This is the C counterpart to DeusData#220, which fixed the definition-naming path but not the enclosing-call path. Fix: descend the declarator chain to the innermost name node (mirroring resolve_c_declarator_name in extract_defs.c, including qualified and operator names) when a function_definition lacks a `name` field. Adds the regression test c_caller_attribution asserting a C call's enclosing_func_qn is the function, not the module. Fixes DeusData#438 Signed-off-by: Kris Kersey <kris@kerseyfabrications.com>
1 parent 34efbc0 commit 13c3429

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,44 @@ 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+
90128
// Resolve the name node for a function, handling arrow functions.
91129
static TSNode resolve_func_name_node(TSNode node) {
92130
TSNode name_node = ts_node_child_by_field_name(node, TS_FIELD("name"));
@@ -101,6 +139,10 @@ static TSNode resolve_func_name_node(TSNode node) {
101139
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_declaration") == 0) {
102140
name_node = cbm_find_child_by_kind(node, "simple_identifier");
103141
}
142+
/* C/C++/CUDA/GLSL: function_definition name lives in the declarator chain. */
143+
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_definition") == 0) {
144+
name_node = resolve_c_declarator_name_node(node);
145+
}
104146
return name_node;
105147
}
106148

‎internal/cbm/helpers.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,46 @@ TSNode cbm_find_enclosing_func(TSNode node, CBMLanguage lang) {
718718
}
719719

720720
// 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) {
731+
TSNode decl = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
732+
for (int depth = 0; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
733+
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) {
737+
return decl;
738+
}
739+
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;
747+
}
748+
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
749+
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
750+
inner = ts_node_named_child(decl, 0);
751+
}
752+
if (ts_node_is_null(inner)) {
753+
break;
754+
}
755+
decl = inner;
756+
}
757+
TSNode null_node = {0};
758+
return null_node;
759+
}
760+
721761
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
722762
CBMLanguage lang) {
723763
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)
@@ -752,6 +792,13 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
752792
}
753793
}
754794
}
795+
// C/C++/CUDA/GLSL: function_definition carries its name in the declarator chain.
796+
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
797+
TSNode dn = c_declarator_name_node(func_node);
798+
if (!ts_node_is_null(dn)) {
799+
return cbm_node_text(a, dn, source);
800+
}
801+
}
755802
return NULL;
756803
}
757804

‎tests/test_extraction.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,31 @@ TEST(wolfram_caller_attribution) {
17871787
PASS();
17881788
}
17891789

1790+
/* Issue #438: a C function_definition has no `name` field — the name lives in the
1791+
* declarator chain. Calls inside a C function must be attributed to the enclosing
1792+
* function, not the module. Pre-fix, enclosing_func_qn fell back to the module QN. */
1793+
TEST(c_caller_attribution) {
1794+
CBMFileResult *r = extract("int helper(int x) { return x; }\n"
1795+
"int caller(void) { return helper(1); }\n",
1796+
CBM_LANG_C, "t", "main.c");
1797+
ASSERT_NOT_NULL(r);
1798+
ASSERT_FALSE(r->has_error);
1799+
ASSERT_GT(r->calls.count, 0);
1800+
int saw_helper = 0;
1801+
for (int i = 0; i < r->calls.count; i++) {
1802+
if (strcmp(r->calls.items[i].callee_name, "helper") == 0) {
1803+
saw_helper = 1;
1804+
/* enclosing_func_qn must be the function, NOT empty and NOT the module QN. */
1805+
ASSERT_NOT_NULL(r->calls.items[i].enclosing_func_qn);
1806+
ASSERT_FALSE(strcmp(r->calls.items[i].enclosing_func_qn, "") == 0);
1807+
ASSERT_FALSE(strcmp(r->calls.items[i].enclosing_func_qn, "t.main") == 0);
1808+
}
1809+
}
1810+
ASSERT(saw_helper);
1811+
cbm_free_result(r);
1812+
PASS();
1813+
}
1814+
17901815
/* --- Wolfram parse (simple assignment) --- */
17911816
TEST(wolfram_parse) {
17921817
CBMFileResult *r = extract("x = 42;\ny = x + 1;\n", CBM_LANG_WOLFRAM, "t", "simple.wl");
@@ -3091,6 +3116,7 @@ SUITE(extraction) {
30913116
RUN_TEST(wolfram_function_extended);
30923117
RUN_TEST(wolfram_call);
30933118
RUN_TEST(wolfram_caller_attribution);
3119+
RUN_TEST(c_caller_attribution);
30943120
RUN_TEST(wolfram_parse);
30953121
RUN_TEST(wolfram_import);
30963122
RUN_TEST(wolfram_nested_def);

0 commit comments

Comments
 (0)