From 3c98f4ef024af8369feb06c96d38be95708d3675 Mon Sep 17 00:00:00 2001 From: Danny Ward Date: Fri, 13 Nov 2026 02:25:03 -0500 Subject: [PATCH 1/2] fix(csharp): attribute local function calls to methods Signed-off-by: Danny Ward --- internal/cbm/extract_unified.c | 11 ++++++ tests/test_pipeline.c | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c index 54c638af0..ce9af275f 100644 --- a/internal/cbm/extract_unified.c +++ b/internal/cbm/extract_unified.c @@ -1119,6 +1119,17 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan return configured_qn; } + /* C# local functions do not have graph nodes yet. Attribute calls in them + * to the enclosing method, whose definition span contains the call site, + * rather than to the file node (#2964). */ + if (ctx->language == CBM_LANG_CSHARP && + strcmp(ts_node_type(node), "local_function_statement") == 0) { + if (state->enclosing_func_qn && state->enclosing_func_qn != ctx->module_qn) { + return state->enclosing_func_qn; + } + return NULL; + } + (void)spec; if (ctx->language == CBM_LANG_WOLFRAM) { return compute_wolfram_func_qn(ctx, node); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index 23fabaf56..bca629c0b 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -26256,6 +26256,68 @@ TEST(pipeline_csharp_modern) { PASS(); } +/* C# local functions do not have graph nodes yet. Their calls must stay + * attributed to the enclosing method, whose definition span contains the call + * site, instead of falling back to the file node (#2964). */ +TEST(pipeline_csharp_local_function_calls_keep_enclosing_method_issue2964) { + const char *files[] = {"Repro.cs"}; + const char *contents[] = {"using System;\n" + "using System.Threading.Tasks;\n" + "namespace Repro;\n" + "public sealed class Queue {\n" + " public Task RunAsync(int n) => Task.FromResult(n);\n" + "}\n" + "public sealed class Caller {\n" + " private readonly Queue _queue = new Queue();\n" + " public async Task FromMethod() {\n" + " return await _queue.RunAsync(1);\n" + " }\n" + " public async Task WithLocalFunction() {\n" + " async Task Inner(int n) {\n" + " return await _queue.RunAsync(n);\n" + " }\n" + " return await Inner(2);\n" + " }\n" + " public async Task WithLambda() {\n" + " Func> f = async n => await _queue.RunAsync(n);\n" + " return await f(3);\n" + " }\n" + " public async Task WithNestedLocalFunction() {\n" + " async Task Outer(int n) {\n" + " async Task Deep(int m) {\n" + " return await _queue.RunAsync(m);\n" + " }\n" + " return await Deep(n);\n" + " }\n" + " return await Outer(4);\n" + " }\n" + "}\n"}; + + if (setup_lang_repo(files, contents, 1) != 0) + FAIL("tmpdir"); + char db[512]; + snprintf(db, sizeof(db), "%s/test.db", g_lang_tmpdir); + + cbm_pipeline_t *p = cbm_pipeline_new(g_lang_tmpdir, db, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + + cbm_store_t *store = cbm_store_open_path(db); + ASSERT_NOT_NULL(store); + const char *project = cbm_pipeline_project_name(p); + + ASSERT_EQ(named_edge_count(store, project, "CALLS", "FromMethod", "RunAsync"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "WithLocalFunction", "RunAsync"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "WithLambda", "RunAsync"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "WithNestedLocalFunction", "RunAsync"), + 1); + + cbm_store_close(store); + cbm_pipeline_free(p); + teardown_lang_repo(); + PASS(); +} + TEST(pipeline_bom_stripping) { /* Port of TestBOMStripping — UTF-8 BOM prefix should be handled */ snprintf(g_lang_tmpdir, sizeof(g_lang_tmpdir), "/tmp/cbm_bom_XXXXXX"); @@ -34970,6 +35032,7 @@ SUITE(pipeline) { RUN_TEST(pipeline_kotlin_project); RUN_TEST(pipeline_lua_anonymous_functions); RUN_TEST(pipeline_csharp_modern); + RUN_TEST(pipeline_csharp_local_function_calls_keep_enclosing_method_issue2964); RUN_TEST(pipeline_bom_stripping); RUN_TEST(pipeline_form_call_resolution); RUN_TEST(pipeline_python_type_inference); From 0e086c28a240eb50dd251e06290167548a414a31 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 14 Nov 2026 02:00:16 +0100 Subject: [PATCH 2/2] fix(csharp): keep top-level and extension-member local functions on their nodes Calls in a C# local function now attribute to the enclosing function (#2964). Two local_function_statement shapes are graph nodes of their own and must keep the scope QN that node carries: - a top-level local function (top-level statements) is a module-level Function node. Returning NULL for it moved its calls to the Module node, and a local function nested in it lost its caller as well. - a C# 14 extension block member parses as a local function in the body of a constructor named `extension`, which the def extractor lifts onto the type as a method (#2071). Inheriting the nodeless `extension` scope moved its calls to the File node. Both fall through to the shared resolver again, so their QNs match the def side. cbm_csharp_is_extension_block is exported from extract_defs.c so the two extractors share one predicate. Refs #2964 Signed-off-by: Martin Vogel --- internal/cbm/extract_defs.c | 4 +- internal/cbm/extract_unified.c | 39 +++++++++++--- internal/cbm/helpers.h | 7 +++ tests/test_pipeline.c | 99 ++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 10 deletions(-) diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c index 5adbf72c0..a5319518d 100644 --- a/internal/cbm/extract_defs.c +++ b/internal/cbm/extract_defs.c @@ -9605,7 +9605,7 @@ static void extract_objc_impl_methods(CBMExtractCtx *ctx, TSNode impl_node, cons * constructor_declaration named `extension` and nothing flags it: has_error * stays 0. A constructor carries its own type's name, so this shape can only be * a genuine constructor inside a type literally called `extension` (#2071). */ -static bool csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) { +bool cbm_csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) { if (strcmp(ts_node_type(node), "constructor_declaration") != 0) { return false; } @@ -9679,7 +9679,7 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c * enclosing type, rather than the block itself as a constructor named * `extension` with its members buried in the body (#2071). */ if (ctx->language == CBM_LANG_CSHARP && - csharp_is_extension_block(child, class_node, ctx->source)) { + cbm_csharp_is_extension_block(child, class_node, ctx->source)) { extract_csharp_extension_members(ctx, child, class_node, class_qn, spec); continue; } diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c index ce9af275f..00dd660bf 100644 --- a/internal/cbm/extract_unified.c +++ b/internal/cbm/extract_unified.c @@ -1109,6 +1109,26 @@ static const char *py_nearest_scope_qn(const WalkState *state) { return NULL; } +/* C# 14 extension block members parse as local functions in the body of a + * constructor named `extension`, which the def extractor lifts onto the + * enclosing type as methods (#2071). Climbs the same conditional blocks the + * def side's member iterator flattens, so #if branches count as members. */ +static bool csharp_is_extension_member(const CBMExtractCtx *ctx, TSNode node, + const WalkState *state) { + const char **conditional = cbm_conditional_block_types(ctx->language); + TSNode up = ts_node_parent(node); + while (conditional && !ts_node_is_null(up) && cbm_kind_in_set(up, conditional)) { + up = ts_node_parent(up); + } + if (ts_node_is_null(up) || strcmp(ts_node_type(up), "block") != 0 || + ts_node_is_null(state->enclosing_class_node)) { + return false; + } + TSNode owner = ts_node_parent(up); + return !ts_node_is_null(owner) && + cbm_csharp_is_extension_block(owner, state->enclosing_class_node, ctx->source); +} + // Compute function QN for scope tracking (mirrors cbm_enclosing_func_qn logic). static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, WalkState *state) { @@ -1119,15 +1139,18 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan return configured_qn; } - /* C# local functions do not have graph nodes yet. Attribute calls in them - * to the enclosing method, whose definition span contains the call site, - * rather than to the file node (#2964). */ + /* C# local functions nested in a member have no graph node. Attribute calls + * in them to the enclosing function, whose definition span contains the + * call site, rather than to the file node (#2964); the C# LSP names the + * same caller, so its resolutions still join. Two local_function_statement + * shapes ARE nodes and fall through to keep the QN that node carries: a + * top-level local function (top-level statements, module-qualified) and a + * C# 14 extension block member (lifted onto the type, class-qualified). */ if (ctx->language == CBM_LANG_CSHARP && - strcmp(ts_node_type(node), "local_function_statement") == 0) { - if (state->enclosing_func_qn && state->enclosing_func_qn != ctx->module_qn) { - return state->enclosing_func_qn; - } - return NULL; + strcmp(ts_node_type(node), "local_function_statement") == 0 && state->enclosing_func_qn && + state->enclosing_func_qn != ctx->module_qn && + !csharp_is_extension_member(ctx, node, state)) { + return state->enclosing_func_qn; } (void)spec; diff --git a/internal/cbm/helpers.h b/internal/cbm/helpers.h index 2e6b669fe..9a92ae1d2 100644 --- a/internal/cbm/helpers.h +++ b/internal/cbm/helpers.h @@ -199,6 +199,13 @@ char *cbm_cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *sou * (extract_unified.c) so the receiver-qualified method QN has ONE formula. */ char *cbm_go_receiver_type_name(CBMArena *a, TSNode recv, const char *source); +/* C#: true for a C# 14 extension block, which the grammar parses as a + * constructor_declaration named `extension` inside `class_node` (#2071). + * Defined in extract_defs.c, which lifts the block's members onto the type; + * shared so call-scope attribution (extract_unified.c) names those members + * the same way. */ +bool cbm_csharp_is_extension_block(TSNode node, TSNode class_node, const char *source); + // Find a child node by kind string. TSNode cbm_find_child_by_kind(TSNode parent, const char *kind); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index bca629c0b..f6d0f5695 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -26318,6 +26318,103 @@ TEST(pipeline_csharp_local_function_calls_keep_enclosing_method_issue2964) { PASS(); } +/* A top-level local function (C# top-level statements) is a Function node of + * its own, so calls in it stay attributed to it, and a local function nested + * in it attributes to that Function. Neither may fall back to the Module or + * File node, both named after the file (#2964). */ +TEST(pipeline_csharp_top_level_local_function_calls_keep_function_issue2964) { + const char *files[] = {"Program.cs"}; + const char *contents[] = {"using System;\n" + "var q = new Queue();\n" + "Console.WriteLine(Helper(1));\n" + "Console.WriteLine(Twice(2));\n" + "int Helper(int n) {\n" + " return q.Run(n);\n" + "}\n" + "static int Twice(int n) {\n" + " int Inner(int m) => Calc.Square(m);\n" + " return Inner(n) * 2;\n" + "}\n" + "class Queue {\n" + " public int Run(int n) => n;\n" + "}\n" + "static class Calc {\n" + " public static int Square(int x) => x * x;\n" + "}\n"}; + + if (setup_lang_repo(files, contents, 1) != 0) + FAIL("tmpdir"); + char db[512]; + snprintf(db, sizeof(db), "%s/test.db", g_lang_tmpdir); + + cbm_pipeline_t *p = cbm_pipeline_new(g_lang_tmpdir, db, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + + cbm_store_t *store = cbm_store_open_path(db); + ASSERT_NOT_NULL(store); + const char *project = cbm_pipeline_project_name(p); + + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Helper", "Run"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Twice", "Square"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Program.cs", "Run"), 0); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Program.cs", "Square"), 0); + + cbm_store_close(store); + cbm_pipeline_free(p); + teardown_lang_repo(); + PASS(); +} + +/* C# 14 extension block members parse as local functions, but the def side + * lifts them onto the type as methods (#2071): calls in them stay attributed + * to those methods, under #if too, and a local function nested in a member + * attributes to that member (#2964). */ +TEST(pipeline_csharp_extension_member_calls_keep_method_issue2964) { + const char *files[] = {"NumberExtensions.cs"}; + const char *contents[] = {"using System.Collections.Generic;\n" + "namespace App;\n" + "public static class Calc {\n" + " public static int Total(IEnumerable xs) => 0;\n" + " public static int Nothing() => 0;\n" + " public static int One() => 1;\n" + "}\n" + "public static class NumberExtensions {\n" + " extension(IEnumerable numbers) {\n" + " public int SumPositive() => Calc.Total(numbers);\n" + "#if FEATURE\n" + " public static int Zero() => Calc.Nothing();\n" + "#endif\n" + " public int Nest() { int Inner() => Calc.One(); return Inner(); }\n" + " }\n" + "}\n"}; + + if (setup_lang_repo(files, contents, 1) != 0) + FAIL("tmpdir"); + char db[512]; + snprintf(db, sizeof(db), "%s/test.db", g_lang_tmpdir); + + cbm_pipeline_t *p = cbm_pipeline_new(g_lang_tmpdir, db, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + + cbm_store_t *store = cbm_store_open_path(db); + ASSERT_NOT_NULL(store); + const char *project = cbm_pipeline_project_name(p); + + ASSERT_EQ(named_edge_count(store, project, "CALLS", "SumPositive", "Total"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Zero", "Nothing"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "Nest", "One"), 1); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "NumberExtensions.cs", "Total"), 0); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "NumberExtensions.cs", "Nothing"), 0); + ASSERT_EQ(named_edge_count(store, project, "CALLS", "NumberExtensions.cs", "One"), 0); + + cbm_store_close(store); + cbm_pipeline_free(p); + teardown_lang_repo(); + PASS(); +} + TEST(pipeline_bom_stripping) { /* Port of TestBOMStripping — UTF-8 BOM prefix should be handled */ snprintf(g_lang_tmpdir, sizeof(g_lang_tmpdir), "/tmp/cbm_bom_XXXXXX"); @@ -35033,6 +35130,8 @@ SUITE(pipeline) { RUN_TEST(pipeline_lua_anonymous_functions); RUN_TEST(pipeline_csharp_modern); RUN_TEST(pipeline_csharp_local_function_calls_keep_enclosing_method_issue2964); + RUN_TEST(pipeline_csharp_top_level_local_function_calls_keep_function_issue2964); + RUN_TEST(pipeline_csharp_extension_member_calls_keep_method_issue2964); RUN_TEST(pipeline_bom_stripping); RUN_TEST(pipeline_form_call_resolution); RUN_TEST(pipeline_python_type_inference);