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
fix(graph): an untyped C member access still binds an unambiguous member name
Martin Vogel193b0a4d066f
diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.cindex 1d90bbffa..aad2c1190 100644--- a/internal/cbm/lsp/c_lsp.c+++ b/internal/cbm/lsp/c_lsp.c@@ -4041,6 +4041,15 @@ static void c_emit_field_reference(CLSPContext *ctx, TSNode node) {return;const CBMRegisteredType *owner = c_lookup_field_owner(ctx, type_qn, field_name, 0);+ if (!owner && ctx->cpp_mode) {+ /* C++: an object of a KNOWN type that declares no such field+ * (`text.size()` on a std::string) is evidence that no project Field+ * is meant. Publish the type anyway: the row names no node, so the+ * resolvers bind nothing instead of falling back to the member name.+ * C keeps the fallback, since a C aggregate's nested anonymous members+ * are not all registered on it. */+ owner = c_resolve_field_owner_type(ctx, type_qn);+ }if (!owner || !owner->qualified_name)return;diff --git a/src/pipeline/lsp_resolve.h b/src/pipeline/lsp_resolve.hindex 235f3f523..e52448dac 100644--- a/src/pipeline/lsp_resolve.h+++ b/src/pipeline/lsp_resolve.h@@ -1022,19 +1022,23 @@ static inline const cbm_gbuf_node_t *cbm_pipeline_lsp_field_node(const cbm_gbuf_return node && node->label && strcmp(node->label, "Field") == 0 ? node : NULL;}-/* The Field a C member call (`cb.close(ctx)`) lands on, decided by the type of- * its object: among the owner rows of (enclosing function, member name) that- * name a project Field, the one with the smallest QN, so the sequential scan- * and the sorted index agree. NULL when no owner is known. */+/* The Field a C/C++ member call (`cb.close(ctx)`) lands on, decided by the+ * type of its object: among the owner rows of (enclosing function, member+ * name) that name a project Field, the one with the smallest QN, so the+ * sequential scan and the sorted index agree. *typed says whether the LSP+ * typed the object at all; NULL with *typed set means "typed, and no project+ * Field is meant", NULL without it means the caller may try the member name. */static inline const cbm_gbuf_node_t *cbm_pipeline_c_member_call_field(const CBMResolvedCallArray *arr, const cbm_pipeline_lsp_field_index_t *index,const cbm_gbuf_t *gbuf, const char *project_name, const char *enclosing_func_qn,- const char *callee_text) {+ const char *callee_text, bool *typed) {cbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(arr, index, enclosing_func_qn, cbm_lsp_bare_segment(callee_text));const cbm_gbuf_node_t *best = NULL;const CBMResolvedCall *owner;+ *typed = false;while ((owner = cbm_pipeline_lsp_field_next(&owners)) != NULL) {+ *typed = true;const cbm_gbuf_node_t *field = cbm_pipeline_lsp_field_node(gbuf, project_name, owner);if (field && (!best || strcmp(field->qualified_name, best->qualified_name) < 0)) {best = field;diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.cindex d5e87ea53..0c324f466 100644--- a/src/pipeline/pass_calls.c+++ b/src/pipeline/pass_calls.c@@ -880,16 +880,25 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBMtarget_node->file_path)) {case CBM_FIELD_CALL_DROP:return 0;- case CBM_FIELD_CALL_BY_OWNER:+ case CBM_FIELD_CALL_BY_OWNER: {+ bool typed = false;target_node =cbm_pipeline_c_member_call_field(lsp_calls, NULL, ctx->gbuf, ctx->project_name,- call->enclosing_func_qn, call->callee_name);+ call->enclosing_func_qn, call->callee_name, &typed);+ if (!target_node && !typed) {+ /* Untyped object: only a member name the project holds exactly+ * once may still bind. */+ const char *only = cbm_registry_unique_field_qn(+ ctx->registry, cbm_lsp_bare_segment(call->callee_name));+ target_node = only ? cbm_gbuf_find_by_qn(ctx->gbuf, only) : NULL;+ }if (!target_node || source_node->id == target_node->id) {return 0;}res.qualified_name = target_node->qualified_name;- res.strategy = "lsp_field_access";+ res.strategy = typed ? "lsp_field_access" : "unique_name";break;+ }case CBM_FIELD_CALL_KEEP:break;}diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex 86e725242..14644cd51 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -3156,14 +3156,22 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBcbm_pipeline_lsp_field_index_build(&result->resolved_calls, &field_index);field_index_built = true;}+ bool typed = false;target_node = cbm_pipeline_c_member_call_field(&result->resolved_calls, field_index_ready ? &field_index : NULL, rc->main_gbuf,- rc->project_name, call->enclosing_func_qn, call->callee_name);+ rc->project_name, call->enclosing_func_qn, call->callee_name, &typed);+ if (!target_node && !typed) {+ /* Untyped object: only a member name the project holds+ * exactly once may still bind. */+ const char *only = cbm_registry_unique_field_qn(+ rc->registry, cbm_lsp_bare_segment(call->callee_name));+ target_node = only ? cbm_gbuf_find_by_qn(rc->main_gbuf, only) : NULL;+ }if (!target_node || source_node->id == target_node->id) {continue;}res.qualified_name = target_node->qualified_name;- res.strategy = "lsp_field_access";+ res.strategy = typed ? "lsp_field_access" : "unique_name";}}if (target_node && source_node->id != target_node->id &&@@ -3241,20 +3249,35 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,cbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(&result->resolved_calls, field_index_ready ? &field_index : NULL,usage->enclosing_func_qn, usage->ref_name);+ char member_esc[CBM_SZ_256];+ cbm_json_escape(member_esc, sizeof(member_esc), usage->ref_name);+ char member_props[CBM_SZ_512];+ snprintf(member_props, sizeof(member_props), "{\"callee\":\"%s\"}", member_esc);const CBMResolvedCall *owner;+ bool typed = false;while ((owner = cbm_pipeline_lsp_field_next(&owners)) != NULL) {+ typed = true;const cbm_gbuf_node_t *field =cbm_pipeline_lsp_field_node(rc->main_gbuf, rc->project_name, owner);if (!field || field->id == src->id) {continue;}- char member_esc[CBM_SZ_256];- cbm_json_escape(member_esc, sizeof(member_esc), usage->ref_name);- char member_props[CBM_SZ_512];- snprintf(member_props, sizeof(member_props), "{\"callee\":\"%s\"}", member_esc);cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, field->id, "USAGE", member_props);ws->usages_resolved++;}+ if (!typed) {+ /* The LSP could not type the object: only a member name the+ * project holds exactly once may still bind. */+ const char *only = cbm_registry_unique_field_qn(rc->registry, usage->ref_name);+ const cbm_gbuf_node_t *field =+ only ? cbm_gbuf_find_by_qn(rc->main_gbuf, only) : NULL;+ if (field && field->id != src->id &&+ !cbm_suppress_cross_language_ref(lang, field->file_path)) {+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, field->id, "USAGE",+ member_props);+ ws->usages_resolved++;+ }+ }continue;}const cbm_gbuf_node_t *tgt = NULL;@@ -3392,15 +3415,26 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFicbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(&result->resolved_calls, field_index_ready ? &field_index : NULL,rw->enclosing_func_qn, rw->var_name);+ const char *member_edge = rw->is_write ? "WRITES" : "READS";const CBMResolvedCall *owner;+ bool typed = false;while ((owner = cbm_pipeline_lsp_field_next(&owners)) != NULL) {+ typed = true;const cbm_gbuf_node_t *field =cbm_pipeline_lsp_field_node(rc->main_gbuf, rc->project_name, owner);if (!field || field->id == src->id) {continue;}- cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, field->id,- rw->is_write ? "WRITES" : "READS", "{}");+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, field->id, member_edge, "{}");+ }+ if (!typed) {+ const char *only = cbm_registry_unique_field_qn(rc->registry, rw->var_name);+ const cbm_gbuf_node_t *field =+ only ? cbm_gbuf_find_by_qn(rc->main_gbuf, only) : NULL;+ if (field && field->id != src->id &&+ !cbm_suppress_cross_language_ref(lang, field->file_path)) {+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, field->id, member_edge, "{}");+ }}continue;}diff --git a/src/pipeline/pass_usages.c b/src/pipeline/pass_usages.cindex 9944952b9..31f3b015d 100644--- a/src/pipeline/pass_usages.c+++ b/src/pipeline/pass_usages.c@@ -172,20 +172,33 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *rescbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(&result->resolved_calls, field_index_ready ? &field_index : NULL,usage->enclosing_func_qn, usage->ref_name);+ char member_esc[CBM_SZ_256];+ cbm_json_escape(member_esc, sizeof(member_esc), usage->ref_name);+ char member_props[CBM_SZ_512];+ snprintf(member_props, sizeof(member_props), "{\"callee\":\"%s\"}", member_esc);const CBMResolvedCall *owner;+ bool typed = false;while ((owner = cbm_pipeline_lsp_field_next(&owners)) != NULL) {+ typed = true;const cbm_gbuf_node_t *field =cbm_pipeline_lsp_field_node(ctx->gbuf, ctx->project_name, owner);if (!field || field->id == src->id) {continue;}- char member_esc[CBM_SZ_256];- cbm_json_escape(member_esc, sizeof(member_esc), usage->ref_name);- char member_props[CBM_SZ_512];- snprintf(member_props, sizeof(member_props), "{\"callee\":\"%s\"}", member_esc);cbm_gbuf_insert_edge(ctx->gbuf, src->id, field->id, "USAGE", member_props);resolved++;}+ if (!typed) {+ /* The LSP could not type the object: only a member name the+ * project holds exactly once may still bind. */+ const char *only = cbm_registry_unique_field_qn(ctx->registry, usage->ref_name);+ const cbm_gbuf_node_t *field = only ? cbm_gbuf_find_by_qn(ctx->gbuf, only) : NULL;+ if (field && field->id != src->id &&+ !cbm_suppress_cross_language_ref(lang, field->file_path)) {+ cbm_gbuf_insert_edge(ctx->gbuf, src->id, field->id, "USAGE", member_props);+ resolved++;+ }+ }continue;}@@ -332,17 +345,28 @@ static int resolve_rw_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *resultcbm_pipeline_lsp_field_cursor_t owners = cbm_pipeline_lsp_field_cursor(&result->resolved_calls, field_index_ready ? &field_index : NULL,rw->enclosing_func_qn, rw->var_name);+ const char *member_edge = rw->is_write ? "WRITES" : "READS";const CBMResolvedCall *owner;+ bool typed = false;while ((owner = cbm_pipeline_lsp_field_next(&owners)) != NULL) {+ typed = true;const cbm_gbuf_node_t *field =cbm_pipeline_lsp_field_node(ctx->gbuf, ctx->project_name, owner);if (!field || field->id == src->id) {continue;}- cbm_gbuf_insert_edge(ctx->gbuf, src->id, field->id,- rw->is_write ? "WRITES" : "READS", "{}");+ cbm_gbuf_insert_edge(ctx->gbuf, src->id, field->id, member_edge, "{}");resolved++;}+ if (!typed) {+ const char *only = cbm_registry_unique_field_qn(ctx->registry, rw->var_name);+ const cbm_gbuf_node_t *field = only ? cbm_gbuf_find_by_qn(ctx->gbuf, only) : NULL;+ if (field && field->id != src->id &&+ !cbm_suppress_cross_language_ref(lang, field->file_path)) {+ cbm_gbuf_insert_edge(ctx->gbuf, src->id, field->id, member_edge, "{}");+ resolved++;+ }+ }continue;}diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.hindex 4d0421ad9..e27750303 100644--- a/src/pipeline/pipeline.h+++ b/src/pipeline/pipeline.h@@ -370,11 +370,16 @@ cbm_field_call_policy_t cbm_call_onto_field_policy(CBMLanguage caller_lang, consconst char *target_file_path);/* True when a reference is the member half of a C `a.b` / `a->b`. Such a- * reference binds only through the C LSP's field-owner rows (the type of `a`),- * never through a bare-name lookup of `b`. C only. Pure; unit-tested in- * test_registry.c. */+ * reference binds through the C LSP's field-owner rows (the type of `a`); with+ * no typed owner it binds only cbm_registry_unique_field_qn(). C only. Pure;+ * unit-tested in test_registry.c. */bool cbm_c_member_binds_by_owner(bool is_c, bool is_member_access);+/* The QN of the Field `member_name` names when exactly one Field in the+ * registry carries that name (symbols of other labels are not candidates for+ * a member); NULL when none or several do. */+const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *member_name);+/* 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);diff --git a/src/pipeline/registry.c b/src/pipeline/registry.cindex 96f84f94b..1fc8fceb1 100644--- a/src/pipeline/registry.c+++ b/src/pipeline/registry.c@@ -849,7 +849,9 @@ bool cbm_c_member_binds_by_owner(bool is_c, bool is_member_access) {* registry bound whichever Field (or function, or variable) was called* `b` -- on this repository 9,150 USAGE and 2,810 WRITES edges onto* Fields, 3,976 of the USAGE edges on a name several structs share. Such a- * reference binds only through the C LSP's field-owner rows.+ * reference binds through the C LSP's field-owner rows; an object the LSP+ * could not type binds only a member name that is unique in the project+ * (cbm_registry_unique_field_qn), never one of several.** C-gated like the Go rule above: a C++ or Objective-C method body* reaches its own members without a selector, and their member accesses@@ -1034,6 +1036,33 @@ int cbm_registry_find_by_name(const cbm_registry_t *r, const char *name, const creturn 0;}+const char *cbm_registry_unique_field_qn(const cbm_registry_t *r, const char *member_name) {+ /* A member name that exactly one Field in the whole project carries names+ * that Field without any choice being made. This is what an untyped C+ * member access may still bind: the defect in bare-name member resolution+ * was the arbitrary pick among SEVERAL same-named members (kernel: 359,236+ * of the edges a typed-only rule dropped were onto such a unique name).+ * Only Fields are candidates -- a function or variable of the same name+ * can never be what `a.b` means -- and two of them bind nothing. */+ const char **qns = NULL;+ int count = 0;+ if (!member_name || cbm_registry_find_by_name(r, member_name, &qns, &count) != 0 || !qns) {+ return NULL;+ }+ const char *only = NULL;+ for (int i = 0; i < count; i++) {+ const char *label = qns[i] ? cbm_registry_label_of(r, qns[i]) : NULL;+ if (!label || strcmp(label, "Field") != 0) {+ continue;+ }+ if (only) {+ return NULL;+ }+ only = qns[i];+ }+ return only;+}+int cbm_registry_size(const cbm_registry_t *r) {return r ? (int)cbm_ht_count(r->exact) : 0;}diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex 3e0abb38f..91862bfc8 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -6824,7 +6824,9 @@ static int field_inbound_edge_count(cbm_store_t *s, const char *project, const c* `count`, and each function touches exactly one of them. A bare-name lookup* of `count` can only pick one Field for both functions; only the type of the* object expression says which struct is meant. `use_opaque` reaches `count`- * through a type the project never defines, so no Field may be bound at all.+ * through a type the project never defines, so no Field may be bound at all;+ * `beta_only` through that same untyped object is the one Field of that name+ * in the project, so nothing has to be picked and it binds.** Calls follow the same rule. The structs carry a function-pointer member* `notify` (declared through a typedef: a member spelled `void (*notify)(int)`@@ -6890,7 +6892,7 @@ static void write_c_member_field_fixture(const char *tmp, int pad_files) {"struct Opaque;\n""\n""int use_opaque(struct Opaque *o) {\n"- " return o->count;\n"+ " return o->count + o->beta_only;\n""}\n");for (int i = 0; i < pad_files; i++) {char name[64];@@ -6919,9 +6921,12 @@ static int assert_c_member_fields_bind_by_type(cbm_store_t *s, const char *proje/* A field whose name is unique keeps its edge. */ASSERT_TRUE(field_edge_exists(s, project, "use_alpha", "Alpha", "alpha_only", "USAGE"));ASSERT_TRUE(field_edge_exists(s, project, "use_beta", "Beta", "beta_only", "USAGE"));- /* An object whose type the project never defines binds no Field. */+ /* An object whose type the project never defines binds no Field by a+ * name several structs share... */ASSERT_FALSE(field_edge_exists(s, project, "use_opaque", "Alpha", "count", "USAGE"));ASSERT_FALSE(field_edge_exists(s, project, "use_opaque", "Beta", "count", "USAGE"));+ /* ...and still binds a member name the project holds exactly once. */+ ASSERT_TRUE(field_edge_exists(s, project, "use_opaque", "Beta", "beta_only", "USAGE"));/* A call through a function-pointer member lands on the member of its* object's type, not on the same-named member its own file declares. */ASSERT_GTE(fixture_node_count(s, project, "mix.c", "notify", "Field"), 1);diff --git a/tests/test_registry.c b/tests/test_registry.cindex 53895a29a..4f9aba696 100644--- a/tests/test_registry.c+++ b/tests/test_registry.c@@ -1070,6 +1070,39 @@ TEST(call_onto_field_follows_language_and_shape) {PASS();}+TEST(unique_field_qn_needs_exactly_one_field_of_that_name) {+ /* What an untyped C member access may still bind: a name exactly one+ * Field carries. Two Fields would be a choice; a function or variable of+ * the same name is not a candidate for a member at all. */+ cbm_registry_t *r = cbm_registry_new();+ ASSERT_NOT_NULL(r);+ cbm_registry_add(r, "only_here", "proj.a.Alpha.only_here", "Field");+ cbm_registry_add(r, "count", "proj.a.Alpha.count", "Field");+ cbm_registry_add(r, "count", "proj.b.Beta.count", "Field");+ cbm_registry_add(r, "close", "proj.cb.Callbacks.close", "Field");+ cbm_registry_add(r, "close", "proj.io.close", "Function");+ cbm_registry_add(r, "lonely_fn", "proj.io.lonely_fn", "Function");++ const char *only = cbm_registry_unique_field_qn(r, "only_here");+ ASSERT_NOT_NULL(only);+ ASSERT_STR_EQ(only, "proj.a.Alpha.only_here");+ /* Two fields share the name: no pick. */+ ASSERT_NULL(cbm_registry_unique_field_qn(r, "count"));+ /* One field and one function share the name: the function is not a+ * candidate for a member, so the field is still the only one. */+ const char *member = cbm_registry_unique_field_qn(r, "close");+ ASSERT_NOT_NULL(member);+ ASSERT_STR_EQ(member, "proj.cb.Callbacks.close");+ /* Unique, but not a field. */+ ASSERT_NULL(cbm_registry_unique_field_qn(r, "lonely_fn"));+ /* Unknown name and degenerate input. */+ ASSERT_NULL(cbm_registry_unique_field_qn(r, "absent"));+ ASSERT_NULL(cbm_registry_unique_field_qn(r, NULL));+ ASSERT_NULL(cbm_registry_unique_field_qn(NULL, "count"));+ cbm_registry_free(r);+ PASS();+}+TEST(c_member_access_binds_by_owner_only) {/* The member half of a C `a.b` / `a->b` means nothing without the type of* `a`: it binds through the C LSP's field-owner rows and never through a@@ -1344,6 +1377,7 @@ SUITE(registry) {RUN_TEST(cross_language_ref_drops_go_vs_c);RUN_TEST(go_bare_ref_never_binds_field);RUN_TEST(call_onto_field_follows_language_and_shape);+ RUN_TEST(unique_field_qn_needs_exactly_one_field_of_that_name);RUN_TEST(c_member_access_binds_by_owner_only);RUN_TEST(dynamic_suppress_drops_weak_method_matches);RUN_TEST(dynamic_suppress_keeps_high_confidence_and_non_methods);