Skip to content

Commit c933d0b

Browse files
committed
fix(pipeline): a bare C reference never binds a struct Field
A bare C identifier that names no visible declaration reached the short-name registry fallback in the USAGE and READS/WRITES resolvers, which bound it to whichever struct member carried the same name. tests/test_main.c's RUN_SELECTED_SUITE(mcp) gave `main` a USAGE edge to cbm_daemon_application_session.mcp, and every bare local write (`offset = 0`, `trait_qn = head`) became a WRITES edge to an unrelated struct's field. In C a member is only ever named as the member half of `a.b`, `a->b` or a designator `.b`, all recorded as is_member_access and bound through the C LSP owner rows. Widen the Go rule (DeusData#1942/DeusData#1962) to C: cbm_go_suppress_bare_field_ref becomes cbm_suppress_bare_field_ref and takes the caller language. C++ (and every .h, parsed as C++) keeps its bare member references, as a method body names its own members without a selector. Sequential and parallel resolvers both consult it. Fixes DeusData#2666 Signed-off-by: Gaurav Dubey <gauravdubey0107@gmail.com>
1 parent 3457b21 commit c933d0b

6 files changed

Lines changed: 150 additions & 31 deletions

File tree

‎src/pipeline/pass_parallel.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,10 +3746,9 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
37463746
if (tgt && cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
37473747
continue;
37483748
}
3749-
/* #1942/#1962: a bare Go reference can never denote a struct
3750-
* field; the member half of a selector may. */
3751-
if (tgt && cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, usage->is_member_access,
3752-
tgt->label)) {
3749+
/* #1942/#1962/#2666: a bare Go or C reference can never denote
3750+
* a struct field; the member half of a selector may. */
3751+
if (tgt && cbm_suppress_bare_field_ref(lang, usage->is_member_access, tgt->label)) {
37533752
continue;
37543753
}
37553754
if (usage->semantic_reference_blocked && (usage->semantic_reference_local_shadow ||
@@ -3875,9 +3874,9 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFi
38753874
if (cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
38763875
continue;
38773876
}
3878-
/* #1942/#1962: a bare Go reference can never denote a struct field;
3879-
* a selector-LHS write (`t.err = x`) may bind it. */
3880-
if (cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, rw->is_member_access, tgt->label)) {
3877+
/* #1942/#1962/#2666: a bare Go or C reference can never denote a
3878+
* struct field; a selector-LHS write (`t.err = x`) may bind it. */
3879+
if (cbm_suppress_bare_field_ref(lang, rw->is_member_access, tgt->label)) {
38813880
continue;
38823881
}
38833882
const char *etype = rw->is_write ? "WRITES" : "READS";

‎src/pipeline/pass_usages.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ static int resolve_usage_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *res
283283
if (tgt && cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
284284
continue;
285285
}
286-
/* #1942/#1962: a bare Go reference can never denote a struct
287-
* field; the member half of a selector may. */
288-
if (tgt && cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, usage->is_member_access,
289-
tgt->label)) {
286+
/* #1942/#1962/#2666: a bare Go or C reference can never denote
287+
* a struct field; the member half of a selector may. */
288+
if (tgt && cbm_suppress_bare_field_ref(lang, usage->is_member_access, tgt->label)) {
290289
continue;
291290
}
292291
if (usage->semantic_reference_blocked && (usage->semantic_reference_local_shadow ||
@@ -416,9 +415,9 @@ static int resolve_rw_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result
416415
if (cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
417416
continue;
418417
}
419-
/* #1942/#1962: a bare Go reference can never denote a struct field;
420-
* a selector-LHS write (`t.err = x`) may bind it. */
421-
if (cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, rw->is_member_access, tgt->label)) {
418+
/* #1942/#1962/#2666: a bare Go or C reference can never denote a
419+
* struct field; a selector-LHS write (`t.err = x`) may bind it. */
420+
if (cbm_suppress_bare_field_ref(lang, rw->is_member_access, tgt->label)) {
422421
continue;
423422
}
424423

‎src/pipeline/pipeline.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,14 @@ bool cbm_suppress_cross_language_suffix_match(CBMLanguage caller_lang, const cha
447447
* unit-tested in test_registry.c. */
448448
bool cbm_suppress_cross_language_ref(CBMLanguage caller_lang, const char *target_file_path);
449449

450-
/* #1942: a bare (dot-less) Go reference can never denote a struct field —
451-
* field access is always a selector expression, and selector references
452-
* resolve on the LSP path. Drops a READS/WRITES/USAGE bind whose target is a
453-
* Field when the reference text carries no '.'. Go only: other OO languages
454-
* legitimately reference their own members bare inside method bodies. Pure;
455-
* unit-tested in test_registry.c. */
456-
bool cbm_go_suppress_bare_field_ref(bool is_go, bool is_member_access, const char *target_label);
450+
/* #1942/#2666: a bare Go or C reference can never denote a struct field —
451+
* field access is always a selector expression (or, in C, a designator), and
452+
* the member half of one is recorded as is_member_access. Drops a
453+
* READS/WRITES/USAGE bind whose target is a Field when the reference was not
454+
* the member half of a selector. Go and C only: other OO languages (and C++,
455+
* which every `.h` is parsed as) legitimately reference their own members
456+
* bare inside method bodies. Pure; unit-tested in test_registry.c. */
457+
bool cbm_suppress_bare_field_ref(CBMLanguage lang, bool is_member_access, const char *target_label);
457458

458459
/* What to do with a call whose registry resolution landed on a Field node.
459460
* Pure; unit-tested in test_registry.c. */

‎src/pipeline/registry.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,18 +1040,29 @@ bool cbm_suppress_cross_language_ref(CBMLanguage caller_lang, const char *target
10401040
return true;
10411041
}
10421042

1043-
bool cbm_go_suppress_bare_field_ref(bool is_go, bool is_member_access, const char *target_label) {
1043+
bool cbm_suppress_bare_field_ref(CBMLanguage lang, bool is_member_access,
1044+
const char *target_label) {
10441045
/* #1942/#1962: a bare Go identifier can never denote a struct field —
10451046
* field access is always a selector expression (x.f). The extractor
10461047
* strips the receiver before the resolver runs (resolve_lhs_write_name
10471048
* records the trailing field name; is_reference_node records the inner
10481049
* field_identifier), so the reference TEXT is always dot-less and cannot
10491050
* carry the distinction — the recorded is_member_access shape can. Only a
10501051
* reference that was never the member half of a selector is refused a
1051-
* Field bind. Go-gated: a C#/Java/C++/Python method body legitimately
1052+
* Field bind.
1053+
*
1054+
* #2666: C has the same rule. A struct member is named only as the member
1055+
* half of `a.b`, `a->b` or a designator `.b`, all recorded as
1056+
* field_identifier; a bare C identifier (a macro argument such as
1057+
* RUN_SELECTED_SUITE(mcp), a stray name in an unexpanded macro body) is
1058+
* never one. The call path already drops a bare C call onto a Field
1059+
* (cbm_call_onto_field_policy).
1060+
*
1061+
* Go- and C-gated: a C#/Java/C++/Python method body legitimately
10521062
* references its own members bare (cp_reads_writes_cs_static_field pins
1053-
* that shape as required), so a global veto would break those languages. */
1054-
if (!is_go || is_member_access || !target_label) {
1063+
* that shape as required), so a global veto would break those languages.
1064+
* A `.h` is parsed as C++ and keeps that behavior. */
1065+
if ((lang != CBM_LANG_GO && lang != CBM_LANG_C) || is_member_access || !target_label) {
10551066
return false;
10561067
}
10571068
return strcmp(target_label, "Field") == 0;

‎tests/test_pipeline.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11055,6 +11055,93 @@ TEST(pipeline_go_bare_ref_never_binds_field_parallel) {
1105511055
PASS();
1105611056
}
1105711057

11058+
/* #2666: tests/test_main.c runs every suite through a macro,
11059+
* RUN_SELECTED_SUITE(mcp); the argument is a bare C identifier that names no
11060+
* declaration, and the registry fallback bound it to whichever struct member
11061+
* was called `mcp`. toggle() writes a bare name the extractor cannot see
11062+
* declared (a macro-made global, say) that a struct member also carries.
11063+
* state.c holds the genuine member uses that must keep their edges.
11064+
* pad_files pushes the run past MIN_FILES_FOR_PARALLEL for the parallel twin. */
11065+
static void write_c_bare_field_fixture(const char *tmp, int pad_files) {
11066+
write_temp_file(tmp, "include/suite.h",
11067+
"typedef struct suite_state {\n"
11068+
" char mcp[64];\n"
11069+
" int count;\n"
11070+
" int enabled;\n"
11071+
"} suite_state_t;\n");
11072+
write_temp_file(tmp, "tests/main.c",
11073+
"#include \"suite.h\"\n"
11074+
"#define RUN_SELECTED_SUITE(name) run_suite(#name)\n"
11075+
"int run_suite(const char *name);\n"
11076+
"int main(void) {\n"
11077+
" RUN_SELECTED_SUITE(mcp);\n"
11078+
" RUN_SELECTED_SUITE(count);\n"
11079+
" return 0;\n"
11080+
"}\n"
11081+
"void toggle(void) {\n"
11082+
" enabled = 1;\n"
11083+
"}\n");
11084+
write_temp_file(tmp, "src/state.c",
11085+
"#include \"suite.h\"\n"
11086+
"int state_count(const suite_state_t *s) { return s->count; }\n"
11087+
"void state_reset(suite_state_t *s) { s->enabled = 0; }\n");
11088+
for (int i = 0; i < pad_files; i++) {
11089+
char name[64];
11090+
char body[128];
11091+
snprintf(name, sizeof(name), "pad/filler%d.c", i);
11092+
snprintf(body, sizeof(body), "int filler%d(void) { return %d; }\n", i, i);
11093+
write_temp_file(tmp, name, body);
11094+
}
11095+
}
11096+
11097+
static int run_c_bare_field_case(const char *tmpl, int pad_files) {
11098+
char tmp[256];
11099+
snprintf(tmp, sizeof(tmp), "%s", tmpl);
11100+
if (!cbm_mkdtemp(tmp)) {
11101+
FAIL("tmpdir");
11102+
}
11103+
write_c_bare_field_fixture(tmp, pad_files);
11104+
11105+
char db_path[512];
11106+
snprintf(db_path, sizeof(db_path), "%s/c_bare.db", tmp);
11107+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11108+
ASSERT_NOT_NULL(p);
11109+
ASSERT_EQ(cbm_pipeline_run(p), 0);
11110+
const char *project = cbm_pipeline_project_name(p);
11111+
11112+
cbm_store_t *s = cbm_store_open_path(db_path);
11113+
ASSERT_NOT_NULL(s);
11114+
11115+
/* The fields must exist for the probes to mean anything. */
11116+
ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "mcp", "Field"), 1);
11117+
ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "count", "Field"), 1);
11118+
ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "enabled", "Field"), 1);
11119+
/* Reproduce-first: RED before the fix — the macro arguments bind the
11120+
* same-named struct members, and the bare write binds `enabled`. */
11121+
ASSERT_FALSE(cross_file_edge_exists(s, project, "main", "mcp", "USAGE"));
11122+
ASSERT_FALSE(cross_file_edge_exists(s, project, "main", "count", "USAGE"));
11123+
ASSERT_FALSE(cross_file_edge_exists(s, project, "toggle", "enabled", "WRITES"));
11124+
/* The member half of `s->count` / `s->enabled = 0` still binds the field. */
11125+
ASSERT_TRUE(cross_file_edge_exists(s, project, "state_count", "count", "USAGE"));
11126+
ASSERT_TRUE(cross_file_edge_exists(s, project, "state_reset", "enabled", "WRITES"));
11127+
11128+
cbm_store_close(s);
11129+
cbm_pipeline_free(p);
11130+
th_rmtree(tmp);
11131+
PASS();
11132+
}
11133+
11134+
TEST(pipeline_c_bare_ref_never_binds_field) {
11135+
/* Sequential path: resolve_usage_edges / resolve_rw_edges. */
11136+
return run_c_bare_field_case("/tmp/cbm_c_bare_XXXXXX", 0);
11137+
}
11138+
11139+
TEST(pipeline_c_bare_ref_never_binds_field_parallel) {
11140+
/* Parallel twin: resolve_file_usages / resolve_file_rw must consult the
11141+
* same predicate (#1928's lesson). */
11142+
return run_c_bare_field_case("/tmp/cbm_c_barep_XXXXXX", 52);
11143+
}
11144+
1105811145
/* A C caller of a header function that shares its name with a struct member
1105911146
* elsewhere (kernel: include/linux/mm.h put_page beside the arm64
1106011147
* kvm_pgtable_mm_ops.put_page member). pad_files pushes the run past
@@ -27700,6 +27787,8 @@ SUITE(pipeline) {
2770027787
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2770127788
RUN_TEST(pipeline_go_bare_ref_never_binds_field);
2770227789
RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
27790+
RUN_TEST(pipeline_c_bare_ref_never_binds_field);
27791+
RUN_TEST(pipeline_c_bare_ref_never_binds_field_parallel);
2770327792
RUN_TEST(pipeline_c_call_reaches_header_function_beside_field);
2770427793
RUN_TEST(pipeline_c_call_reaches_header_function_beside_field_parallel);
2770527794
RUN_TEST(pipeline_c_member_access_binds_field_by_object_type);

‎tests/test_registry.c‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,17 +1245,36 @@ TEST(go_bare_ref_never_binds_field) {
12451245
* trailing name; is_reference_node records the inner field_identifier),
12461246
* so the selector-vs-bare distinction arrives as the recorded
12471247
* is_member_access signal, never as a dot in the reference text. */
1248-
ASSERT_TRUE(cbm_go_suppress_bare_field_ref(true, false, "Field"));
1248+
ASSERT_TRUE(cbm_suppress_bare_field_ref(CBM_LANG_GO, false, "Field"));
12491249
/* The member half of a selector may bind a field. */
1250-
ASSERT_FALSE(cbm_go_suppress_bare_field_ref(true, true, "Field"));
1250+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_GO, true, "Field"));
12511251
/* Bare references to non-fields are untouched. */
1252-
ASSERT_FALSE(cbm_go_suppress_bare_field_ref(true, false, "Variable"));
1253-
ASSERT_FALSE(cbm_go_suppress_bare_field_ref(true, false, "Function"));
1252+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_GO, false, "Variable"));
1253+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_GO, false, "Function"));
12541254
/* Other languages reference their own members bare inside methods —
12551255
* never suppressed (cp_reads_writes_cs_static_field pins the C# shape). */
1256-
ASSERT_FALSE(cbm_go_suppress_bare_field_ref(false, false, "Field"));
1256+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_CSHARP, false, "Field"));
1257+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_JAVA, false, "Field"));
12571258
/* Degenerate input → nothing to judge. */
1258-
ASSERT_FALSE(cbm_go_suppress_bare_field_ref(true, false, NULL));
1259+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_GO, false, NULL));
1260+
PASS();
1261+
}
1262+
1263+
TEST(c_bare_ref_never_binds_field) {
1264+
/* #2666: a C struct member is named only as the member half of `a.b`,
1265+
* `a->b` or a designator `.b` (field_identifier → is_member_access). A
1266+
* bare C identifier — a macro argument like RUN_SELECTED_SUITE(mcp) — is
1267+
* never one, so it must not bind a same-named Field. */
1268+
ASSERT_TRUE(cbm_suppress_bare_field_ref(CBM_LANG_C, false, "Field"));
1269+
/* The member half still binds (through the C owner rows upstream). */
1270+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_C, true, "Field"));
1271+
/* A bare C name keeps binding globals, functions and types. */
1272+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_C, false, "Variable"));
1273+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_C, false, "Function"));
1274+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_C, false, "Type"));
1275+
/* C++ (and every `.h`, parsed as C++): a method body names its own
1276+
* members bare, so the bind is kept. */
1277+
ASSERT_FALSE(cbm_suppress_bare_field_ref(CBM_LANG_CPP, false, "Field"));
12591278
PASS();
12601279
}
12611280

@@ -2270,6 +2289,7 @@ SUITE(registry) {
22702289
RUN_TEST(registry_tie_prefers_csharp_implementation_over_ref_stub);
22712290
RUN_TEST(cross_language_ref_drops_go_vs_c);
22722291
RUN_TEST(go_bare_ref_never_binds_field);
2292+
RUN_TEST(c_bare_ref_never_binds_field);
22732293
RUN_TEST(call_onto_field_follows_language_and_shape);
22742294
RUN_TEST(c_bare_call_does_not_weigh_field_namesakes);
22752295
RUN_TEST(c_arrow_member_call_is_c_family_and_arrow_only);

0 commit comments

Comments
 (0)