Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
extract: a field named like a method of its class is `<Owner>.<name>#field`
Martin Vogel11ad9ec420ff
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex 0f5f8641d..c1028f4a7 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -222,6 +222,7 @@ static void extract_variables(CBMExtractCtx *ctx, TSNode root, const CBMLangSpecstatic void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);static void extract_class_variables(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,const CBMLangSpec *spec, int fields_from);+static void fence_fields_named_like_methods(CBMExtractCtx *ctx, int methods_from, int fields_from);static void extract_rust_impl(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,const CBMLangSpec *spec);@@ -7566,11 +7567,13 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s}// Extract methods inside the class+ int methods_from = ctx->result->defs.count;extract_class_methods(ctx, node, class_qn, spec);// Extract typed struct/class fields (for cross-file LSP type resolution)int fields_from = ctx->result->defs.count;extract_class_fields(ctx, node, class_qn, spec);+ fence_fields_named_like_methods(ctx, methods_from, fields_from);// Extract class-level variables (field declarations not already Fields)extract_class_variables(ctx, node, class_qn, spec, fields_from);@@ -10616,6 +10619,51 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const chmember_iter_done(&it);}+static int cmp_cstr_ptr(const void *pa, const void *pb) {+ return strcmp(*(const char *const *)pa, *(const char *const *)pb);+}++// A field and a method of one class body may share a name (Java `count` and+// `count()`), and the graph keeps one node per qualified name, so one of them+// was lost. The field then takes the qualified name `<Owner>.<name>#field`+// (the `base#suffix` fence of `#macro` and the Rust cfg twins; the registry+// still files it under its plain name). Only a collision in the same body+// renames, so every other field keeps its name. The body's methods are the+// defs [methods_from, fields_from), its fields [fields_from, count); a sorted+// copy of the method names keeps a large class linear-logarithmic.+static void fence_fields_named_like_methods(CBMExtractCtx *ctx, int methods_from, int fields_from) {+ CBMDefinition *defs = ctx->result->defs.items;+ int fields_to = ctx->result->defs.count;+ if (fields_from >= fields_to || methods_from >= fields_from) {+ return;+ }+ const char **qns = (const char **)cbm_arena_alloc(+ ctx->arena, (size_t)(fields_from - methods_from) * sizeof(const char *));+ if (!qns) {+ return;+ }+ size_t n = 0;+ for (int i = methods_from; i < fields_from; i++) {+ if (defs[i].qualified_name && strcmp(defs[i].label, "Method") == 0) {+ qns[n++] = defs[i].qualified_name;+ }+ }+ if (n == 0) {+ return;+ }+ qsort(qns, n, sizeof(qns[0]), cmp_cstr_ptr);+ for (int i = fields_from; i < fields_to; i++) {+ const char *qn = defs[i].qualified_name;+ if (qn && strcmp(defs[i].label, "Field") == 0 &&+ bsearch(&qn, qns, n, sizeof(qns[0]), cmp_cstr_ptr)) {+ char *fenced = cbm_arena_sprintf(ctx->arena, "%s#field", qn);+ if (fenced) {+ defs[i].qualified_name = fenced;+ }+ }+ }+}+// Extract class-level variables (field declarations inside class bodies).// A declaration that extract_class_fields already made a Field (the defs from// `fields_from` on) is not minted again: Java, C# and Solidity list the samediff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex 940bef7a7..950d74573 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -3555,7 +3555,13 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,imp_keys, imp_count)) {continue;}- tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);+ /* a plain value reference names the field twin of a same-named+ * method; a callable-reference candidate keeps the method */+ bool value_ref = usage->kind == CBM_USAGE_VALUE && !usage->may_be_call_reference;+ tgt = cbm_gbuf_find_by_qn(+ rc->main_gbuf, value_ref+ ? cbm_registry_value_target(rc->registry, res.qualified_name)+ : res.qualified_name);/* #1928: the registry fallback is a bare-name guess — never let it* bind a reference across a language boundary. Mirrors the* sequential twin (pass_usages.c). */@@ -3676,7 +3682,8 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFiif (!res.qualified_name || res.qualified_name[0] == '\0') {continue;}- const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);+ const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(+ rc->main_gbuf, cbm_registry_value_target(rc->registry, res.qualified_name));if (!tgt || src->id == tgt->id) {continue;}diff --git a/src/pipeline/pass_usages.c b/src/pipeline/pass_usages.cindex f60378ff2..1ce353c02 100644--- a/src/pipeline/pass_usages.c+++ b/src/pipeline/pass_usages.c@@ -255,7 +255,12 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *resimp_keys, imp_count)) {continue;}- tgt = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);+ /* a plain value reference names the field twin of a same-named+ * method; a callable-reference candidate keeps the method */+ bool value_ref = usage->kind == CBM_USAGE_VALUE && !usage->may_be_call_reference;+ tgt = cbm_gbuf_find_by_qn(+ ctx->gbuf, value_ref ? cbm_registry_value_target(ctx->registry, res.qualified_name)+ : res.qualified_name);/* #1928: the registry fallback is a bare-name guess — never let it* bind a reference across a language boundary (the LSP-backed* semantic branch above is not affected). */@@ -381,7 +386,8 @@ static int resolve_rw_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *resultcontinue;}- const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);+ const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(+ ctx->gbuf, cbm_registry_value_target(ctx->registry, res.qualified_name));if (!tgt || src->id == tgt->id) {continue;}diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.hindex fd2233fc9..5ce3642f3 100644--- a/src/pipeline/pipeline.h+++ b/src/pipeline/pipeline.h@@ -440,6 +440,12 @@ const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *me/* Get the label of a qualified name, or NULL if not found. */const char *cbm_registry_label_of(const cbm_registry_t *r, const char *qn);+/* A read, a write or a plain value reference never denotes a call: when `qn`+ * is a Method whose class body also declares a same-named field (the extractor+ * fences that field as `<qn>#field`), the field is the target. Returns the+ * field's QN, else `qn`. Pure; unit-tested in test_registry.c. */+const char *cbm_registry_value_target(const cbm_registry_t *r, const char *qn);+/* Find all QNs with a given simple name. Sets *out and *count.* Caller does NOT free the array (owned by registry). */int cbm_registry_find_by_name(const cbm_registry_t *r, const char *name, const char ***out,diff --git a/src/pipeline/registry.c b/src/pipeline/registry.cindex 1701f4118..213554243 100644--- a/src/pipeline/registry.c+++ b/src/pipeline/registry.c@@ -1237,6 +1237,20 @@ const char *cbm_registry_label_of(const cbm_registry_t *r, const char *qn) {return cbm_ht_get(r->exact, qn);}+const char *cbm_registry_value_target(const cbm_registry_t *r, const char *qn) {+ const char *label = cbm_registry_label_of(r, qn);+ if (!label || strcmp(label, "Method") != 0) {+ return qn;+ }+ char twin[CBM_SZ_1K];+ int n = snprintf(twin, sizeof(twin), "%s#field", qn);+ if (n < 0 || (size_t)n >= sizeof(twin)) {+ return qn;+ }+ const char *owned = cbm_ht_get_key(r->exact, twin);+ return owned ? owned : qn;+}+int cbm_registry_find_by_name(const cbm_registry_t *r, const char *name, const char ***out,int *count) {if (!r || !out || !count) {diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex 5a0e94c8b..1431cefe7 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -7472,6 +7472,37 @@ static const CBMDefinition *c1_def_qn(CBMFileResult *r, const char *qn) {return NULL;}+static bool qn_ends_with(const CBMDefinition *d, const char *tail) {+ size_t n = d && d->qualified_name ? strlen(d->qualified_name) : 0;+ size_t t = strlen(tail);+ return n >= t && strcmp(d->qualified_name + n - t, tail) == 0;+}++/* A field and a same-named method of one class body would share one qualified+ * name (one node per QN): the field is fenced `<Owner>.<name>#field` and the+ * method keeps the plain name. A field without a namesake keeps its name, and+ * so does a nested class's field named like a method of the outer class. */+TEST(field_named_like_method_is_fenced) {+ CBMFileResult *r = extract("package demo;\npublic class Starter {\n"+ " private String executable;\n"+ " private String dir;\n"+ " public Starter executable(String p) { return this; }\n"+ " public void size() {}\n"+ " static class Inner { int size; }\n}\n",+ CBM_LANG_JAVA, "t", "Starter.java");+ ASSERT_NOT_NULL(r);+ const CBMDefinition *field = c1_def(r, "Field", "executable");+ const CBMDefinition *method = c1_def(r, "Method", "executable");+ ASSERT_NOT_NULL(field);+ ASSERT_NOT_NULL(method);+ ASSERT_TRUE(qn_ends_with(field, "Starter.executable#field"));+ ASSERT_TRUE(qn_ends_with(method, "Starter.executable"));+ ASSERT_TRUE(qn_ends_with(c1_def(r, "Field", "dir"), "Starter.dir"));+ ASSERT_TRUE(qn_ends_with(c1_def(r, "Field", "size"), "Inner.size"));+ cbm_free_result(r);+ PASS();+}+/* The def with this label and name that starts on `line`. */static const CBMDefinition *c1_def_at(CBMFileResult *r, const char *label, const char *name,int line) {@@ -10441,6 +10472,7 @@ SUITE(extraction) {RUN_TEST(java_class);RUN_TEST(java_method);RUN_TEST(class_field_is_one_field_not_also_a_variable);+ RUN_TEST(field_named_like_method_is_fenced);RUN_TEST(java_interface);RUN_TEST(java_interface_no_duplicate_function_issue1234);RUN_TEST(java_enum_dedup_preserves_calls_issue1234);diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex 5580fc97b..a2861d054 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -1690,6 +1690,74 @@ static int fixture_node_count(cbm_store_t *store, const char *project, const chareturn found;}+/* Edges of `edge_type` from the node named `source_name` to the node named+ * `target_name` that carries `target_label`. */+static int labeled_edge_count(cbm_store_t *s, const char *project, const char *edge_type,+ const char *source_name, const char *target_name,+ const char *target_label) {+ cbm_edge_t *edges = NULL;+ int edge_count = 0;+ if (cbm_store_find_edges_by_type(s, project, edge_type, &edges, &edge_count) != CBM_STORE_OK) {+ return -1;+ }+ int matches = 0;+ for (int i = 0; i < edge_count; i++) {+ cbm_node_t source = {0};+ cbm_node_t target = {0};+ int source_ok = cbm_store_find_node_by_id(s, edges[i].source_id, &source) == CBM_STORE_OK;+ int target_ok = cbm_store_find_node_by_id(s, edges[i].target_id, &target) == CBM_STORE_OK;+ if (source_ok && target_ok && source.name && target.name && target.label &&+ strcmp(source.name, source_name) == 0 && strcmp(target.name, target_name) == 0 &&+ strcmp(target.label, target_label) == 0) {+ matches++;+ }+ cbm_node_free_fields(&source);+ cbm_node_free_fields(&target);+ }+ if (edges) {+ cbm_store_free_edges(edges, edge_count);+ }+ return matches;+}++/* A Java field and a same-named method of one class keep their own nodes (the+ * field is fenced `<Owner>.<name>#field`), and a write or a read of the field+ * reaches the field, never the method; the method keeps its calls. The setter+ * writing its own namesake was a self-edge before, so it was dropped. */+TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_fieldfence_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ th_write_file(TH_PATH(tmp, "src/demo/Starter.java"),+ "package demo;\n\npublic class Starter {\n"+ " private String executable;\n\n"+ " public Starter executable(String path) {\n"+ " this.executable = path;\n"+ " return this;\n }\n\n"+ " public String show() {\n return executable;\n }\n\n"+ " public static void run() {\n"+ " new Starter().executable(\"x\").show();\n }\n}\n");+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/fence.db", tmp);+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);+ ASSERT_NOT_NULL(p);+ ASSERT_EQ(cbm_pipeline_run(p), 0);+ const char *project = cbm_pipeline_project_name(p);+ cbm_store_t *store = cbm_store_open_path(db_path);+ ASSERT_NOT_NULL(store);+ ASSERT_EQ(named_node_count(store, project, "executable"), 2);+ ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "executable", "executable", "Field"), 1);+ ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "executable", "executable", "Method"),+ 0);+ ASSERT_EQ(labeled_edge_count(store, project, "USAGE", "show", "executable", "Field"), 1);+ ASSERT_EQ(labeled_edge_count(store, project, "USAGE", "show", "executable", "Method"), 0);+ ASSERT_EQ(labeled_edge_count(store, project, "CALLS", "run", "executable", "Method"), 1);+ cbm_store_close(store);+ cbm_pipeline_free(p);+ th_cleanup(tmp);+ PASS();+}+typedef struct {int run_rc;bool store_opened;@@ -19314,6 +19382,7 @@ SUITE(pipeline) {RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);RUN_TEST(pipeline_hcl_block_reference_resolves_to_its_block);RUN_TEST(pipeline_incremental_preserves_cross_file_calls);+ RUN_TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes);RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_sequential);diff --git a/tests/test_registry.c b/tests/test_registry.cindex 76d9fe598..90ebf5b8a 100644--- a/tests/test_registry.c+++ b/tests/test_registry.c@@ -213,6 +213,27 @@ TEST(registry_label_of) {PASS();}+/* A read, write or value reference that resolved to a Method goes to the+ * method's fenced `#field` twin when the class has one; nothing else moves. */+TEST(registry_value_target_field_twin) {+ cbm_registry_t *r = cbm_registry_new();+ cbm_registry_add(r, "count", "p.C.count", "Method");+ cbm_registry_add(r, "count", "p.C.count#field", "Field");+ cbm_registry_add(r, "size", "p.C.size", "Method");+ cbm_registry_add(r, "name", "p.C.name", "Field");+ ASSERT_STR_EQ(cbm_registry_value_target(r, "p.C.count"), "p.C.count#field");+ ASSERT_STR_EQ(cbm_registry_value_target(r, "p.C.size"), "p.C.size");+ ASSERT_STR_EQ(cbm_registry_value_target(r, "p.C.name"), "p.C.name");+ ASSERT_STR_EQ(cbm_registry_value_target(r, "p.C.none"), "p.C.none");+ /* the fenced field is still found by its plain name */+ const char **qns = NULL;+ int n = 0;+ ASSERT_EQ(cbm_registry_find_by_name(r, "count", &qns, &n), 0);+ ASSERT_EQ(n, 2);+ cbm_registry_free(r);+ PASS();+}+TEST(registry_find_by_name) {cbm_registry_t *r = cbm_registry_new();cbm_registry_add(r, "main", "proj.cmd.main", "Function");@@ -1569,6 +1590,7 @@ SUITE(registry) {RUN_TEST(registry_free_null);RUN_TEST(registry_add_and_exists);RUN_TEST(registry_label_of);+ RUN_TEST(registry_value_target_field_twin);RUN_TEST(registry_find_by_name);RUN_TEST(registry_no_duplicates);/* Resolution */