@@ -11516,6 +11516,93 @@ TEST(pipeline_go_bare_ref_never_binds_field_parallel) {
1151611516 PASS();
1151711517}
1151811518
11519+ /* #2666: tests/test_main.c runs every suite through a macro,
11520+ * RUN_SELECTED_SUITE(mcp); the argument is a bare C identifier that names no
11521+ * declaration, and the registry fallback bound it to whichever struct member
11522+ * was called `mcp`. toggle() writes a bare name the extractor cannot see
11523+ * declared (a macro-made global, say) that a struct member also carries.
11524+ * state.c holds the genuine member uses that must keep their edges.
11525+ * pad_files pushes the run past MIN_FILES_FOR_PARALLEL for the parallel twin. */
11526+ static void write_c_bare_field_fixture(const char *tmp, int pad_files) {
11527+ write_temp_file(tmp, "include/suite.h",
11528+ "typedef struct suite_state {\n"
11529+ " char mcp[64];\n"
11530+ " int count;\n"
11531+ " int enabled;\n"
11532+ "} suite_state_t;\n");
11533+ write_temp_file(tmp, "tests/main.c",
11534+ "#include \"suite.h\"\n"
11535+ "#define RUN_SELECTED_SUITE(name) run_suite(#name)\n"
11536+ "int run_suite(const char *name);\n"
11537+ "int main(void) {\n"
11538+ " RUN_SELECTED_SUITE(mcp);\n"
11539+ " RUN_SELECTED_SUITE(count);\n"
11540+ " return 0;\n"
11541+ "}\n"
11542+ "void toggle(void) {\n"
11543+ " enabled = 1;\n"
11544+ "}\n");
11545+ write_temp_file(tmp, "src/state.c",
11546+ "#include \"suite.h\"\n"
11547+ "int state_count(const suite_state_t *s) { return s->count; }\n"
11548+ "void state_reset(suite_state_t *s) { s->enabled = 0; }\n");
11549+ for (int i = 0; i < pad_files; i++) {
11550+ char name[64];
11551+ char body[128];
11552+ snprintf(name, sizeof(name), "pad/filler%d.c", i);
11553+ snprintf(body, sizeof(body), "int filler%d(void) { return %d; }\n", i, i);
11554+ write_temp_file(tmp, name, body);
11555+ }
11556+ }
11557+
11558+ static int run_c_bare_field_case(const char *tmpl, int pad_files) {
11559+ char tmp[256];
11560+ snprintf(tmp, sizeof(tmp), "%s", tmpl);
11561+ if (!cbm_mkdtemp(tmp)) {
11562+ FAIL("tmpdir");
11563+ }
11564+ write_c_bare_field_fixture(tmp, pad_files);
11565+
11566+ char db_path[512];
11567+ snprintf(db_path, sizeof(db_path), "%s/c_bare.db", tmp);
11568+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
11569+ ASSERT_NOT_NULL(p);
11570+ ASSERT_EQ(cbm_pipeline_run(p), 0);
11571+ const char *project = cbm_pipeline_project_name(p);
11572+
11573+ cbm_store_t *s = cbm_store_open_path(db_path);
11574+ ASSERT_NOT_NULL(s);
11575+
11576+ /* The fields must exist for the probes to mean anything. */
11577+ ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "mcp", "Field"), 1);
11578+ ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "count", "Field"), 1);
11579+ ASSERT_GTE(fixture_node_count(s, project, "include/suite.h", "enabled", "Field"), 1);
11580+ /* Reproduce-first: RED before the fix — the macro arguments bind the
11581+ * same-named struct members, and the bare write binds `enabled`. */
11582+ ASSERT_FALSE(cross_file_edge_exists(s, project, "main", "mcp", "USAGE"));
11583+ ASSERT_FALSE(cross_file_edge_exists(s, project, "main", "count", "USAGE"));
11584+ ASSERT_FALSE(cross_file_edge_exists(s, project, "toggle", "enabled", "WRITES"));
11585+ /* The member half of `s->count` / `s->enabled = 0` still binds the field. */
11586+ ASSERT_TRUE(cross_file_edge_exists(s, project, "state_count", "count", "USAGE"));
11587+ ASSERT_TRUE(cross_file_edge_exists(s, project, "state_reset", "enabled", "WRITES"));
11588+
11589+ cbm_store_close(s);
11590+ cbm_pipeline_free(p);
11591+ th_rmtree(tmp);
11592+ PASS();
11593+ }
11594+
11595+ TEST(pipeline_c_bare_ref_never_binds_field) {
11596+ /* Sequential path: resolve_usage_edges / resolve_rw_edges. */
11597+ return run_c_bare_field_case("/tmp/cbm_c_bare_XXXXXX", 0);
11598+ }
11599+
11600+ TEST(pipeline_c_bare_ref_never_binds_field_parallel) {
11601+ /* Parallel twin: resolve_file_usages / resolve_file_rw must consult the
11602+ * same predicate (#1928's lesson). */
11603+ return run_c_bare_field_case("/tmp/cbm_c_barep_XXXXXX", 52);
11604+ }
11605+
1151911606/* A C caller of a header function that shares its name with a struct member
1152011607 * elsewhere (kernel: include/linux/mm.h put_page beside the arm64
1152111608 * kvm_pgtable_mm_ops.put_page member). pad_files pushes the run past
@@ -28888,6 +28975,8 @@ SUITE(pipeline) {
2888828975 RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2888928976 RUN_TEST(pipeline_go_bare_ref_never_binds_field);
2889028977 RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
28978+ RUN_TEST(pipeline_c_bare_ref_never_binds_field);
28979+ RUN_TEST(pipeline_c_bare_ref_never_binds_field_parallel);
2889128980 RUN_TEST(pipeline_c_call_reaches_header_function_beside_field);
2889228981 RUN_TEST(pipeline_c_call_reaches_header_function_beside_field_parallel);
2889328982 RUN_TEST(pipeline_c_member_access_binds_field_by_object_type);
0 commit comments