Skip to content

Commit 1c3c557

Browse files
authored
Merge pull request DeusData#463 from KerseyFabrications/fix/c-calls-function-source
fix(extract): attribute C/C++ CALLS edges to the enclosing function
2 parents a76e97d + adc8304 commit 1c3c557

5 files changed

Lines changed: 100 additions & 48 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static TSNode resolve_func_name_node(TSNode node) {
101101
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_declaration") == 0) {
102102
name_node = cbm_find_child_by_kind(node, "simple_identifier");
103103
}
104+
/* C/C++/CUDA/GLSL: function_definition name lives in the declarator chain. */
105+
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_definition") == 0) {
106+
name_node = cbm_resolve_c_declarator_name_node(node);
107+
}
104108
return name_node;
105109
}
106110

‎internal/cbm/helpers.c‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,52 @@ 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)
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) {
744+
TSNode decl = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
745+
for (int depth = 0; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
746+
const char *dk = ts_node_type(decl);
747+
if (is_c_terminal_name(dk)) {
748+
return decl;
749+
}
750+
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
751+
return resolve_qualified_name(decl);
752+
}
753+
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
754+
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
755+
inner = ts_node_named_child(decl, 0);
756+
}
757+
if (ts_node_is_null(inner)) {
758+
break;
759+
}
760+
decl = inner;
761+
}
762+
TSNode null_node = {0};
763+
return null_node;
764+
}
765+
721766
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
722767
CBMLanguage lang) {
723768
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)
@@ -752,6 +797,13 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
752797
}
753798
}
754799
}
800+
// C/C++/CUDA/GLSL: function_definition carries its name in the declarator chain.
801+
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
802+
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
803+
if (!ts_node_is_null(dn)) {
804+
return cbm_node_text(a, dn, source);
805+
}
806+
}
755807
return NULL;
756808
}
757809

‎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

‎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)