Skip to content

Commit 442989e

Browse files
authored
Merge pull request DeusData#2754 from gaurav0107/fix/2666-c-extraction-a-macro-argument-identifier
fix(pipeline): a bare C reference never binds a struct Field
2 parents dca2a44 + 1aeaf6f commit 442989e

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
@@ -3789,10 +3789,9 @@ static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
37893789
if (tgt && cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
37903790
continue;
37913791
}
3792-
/* #1942/#1962: a bare Go reference can never denote a struct
3793-
* field; the member half of a selector may. */
3794-
if (tgt && cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, usage->is_member_access,
3795-
tgt->label)) {
3792+
/* #1942/#1962/#2666: a bare Go or C reference can never denote
3793+
* a struct field; the member half of a selector may. */
3794+
if (tgt && cbm_suppress_bare_field_ref(lang, usage->is_member_access, tgt->label)) {
37963795
continue;
37973796
}
37983797
if (usage->semantic_reference_blocked && (usage->semantic_reference_local_shadow ||
@@ -3918,9 +3917,9 @@ static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFi
39183917
if (cbm_suppress_cross_language_ref(lang, tgt->file_path)) {
39193918
continue;
39203919
}
3921-
/* #1942/#1962: a bare Go reference can never denote a struct field;
3922-
* a selector-LHS write (`t.err = x`) may bind it. */
3923-
if (cbm_go_suppress_bare_field_ref(lang == CBM_LANG_GO, rw->is_member_access, tgt->label)) {
3920+
/* #1942/#1962/#2666: a bare Go or C reference can never denote a
3921+
* struct field; a selector-LHS write (`t.err = x`) may bind it. */
3922+
if (cbm_suppress_bare_field_ref(lang, rw->is_member_access, tgt->label)) {
39243923
continue;
39253924
}
39263925
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
@@ -467,13 +467,14 @@ bool cbm_suppress_cross_language_suffix_match(CBMLanguage caller_lang, const cha
467467
* unit-tested in test_registry.c. */
468468
bool cbm_suppress_cross_language_ref(CBMLanguage caller_lang, const char *target_file_path);
469469

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

478479
/* What to do with a call whose registry resolution landed on a Field node.
479480
* 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
@@ -1276,18 +1276,29 @@ bool cbm_suppress_cross_language_ref(CBMLanguage caller_lang, const char *target
12761276
return true;
12771277
}
12781278

1279-
bool cbm_go_suppress_bare_field_ref(bool is_go, bool is_member_access, const char *target_label) {
1279+
bool cbm_suppress_bare_field_ref(CBMLanguage lang, bool is_member_access,
1280+
const char *target_label) {
12801281
/* #1942/#1962: a bare Go identifier can never denote a struct field —
12811282
* field access is always a selector expression (x.f). The extractor
12821283
* strips the receiver before the resolver runs (resolve_lhs_write_name
12831284
* records the trailing field name; is_reference_node records the inner
12841285
* field_identifier), so the reference TEXT is always dot-less and cannot
12851286
* carry the distinction — the recorded is_member_access shape can. Only a
12861287
* reference that was never the member half of a selector is refused a
1287-
* Field bind. Go-gated: a C#/Java/C++/Python method body legitimately
1288+
* Field bind.
1289+
*
1290+
* #2666: C has the same rule. A struct member is named only as the member
1291+
* half of `a.b`, `a->b` or a designator `.b`, all recorded as
1292+
* field_identifier; a bare C identifier (a macro argument such as
1293+
* RUN_SELECTED_SUITE(mcp), a stray name in an unexpanded macro body) is
1294+
* never one. The call path already drops a bare C call onto a Field
1295+
* (cbm_call_onto_field_policy).
1296+
*
1297+
* Go- and C-gated: a C#/Java/C++/Python method body legitimately
12881298
* references its own members bare (cp_reads_writes_cs_static_field pins
1289-
* that shape as required), so a global veto would break those languages. */
1290-
if (!is_go || is_member_access || !target_label) {
1299+
* that shape as required), so a global veto would break those languages.
1300+
* A `.h` is parsed as C++ and keeps that behavior. */
1301+
if ((lang != CBM_LANG_GO && lang != CBM_LANG_C) || is_member_access || !target_label) {
12911302
return false;
12921303
}
12931304
return strcmp(target_label, "Field") == 0;

‎tests/test_pipeline.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -28686,6 +28773,8 @@ SUITE(pipeline) {
2868628773
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2868728774
RUN_TEST(pipeline_go_bare_ref_never_binds_field);
2868828775
RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
28776+
RUN_TEST(pipeline_c_bare_ref_never_binds_field);
28777+
RUN_TEST(pipeline_c_bare_ref_never_binds_field_parallel);
2868928778
RUN_TEST(pipeline_c_call_reaches_header_function_beside_field);
2869028779
RUN_TEST(pipeline_c_call_reaches_header_function_beside_field_parallel);
2869128780
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

@@ -2571,6 +2590,7 @@ SUITE(registry) {
25712590
RUN_TEST(registry_tie_prefers_csharp_implementation_over_ref_stub);
25722591
RUN_TEST(cross_language_ref_drops_go_vs_c);
25732592
RUN_TEST(go_bare_ref_never_binds_field);
2593+
RUN_TEST(c_bare_ref_never_binds_field);
25742594
RUN_TEST(call_onto_field_follows_language_and_shape);
25752595
RUN_TEST(c_bare_call_does_not_weigh_field_namesakes);
25762596
RUN_TEST(c_arrow_member_call_is_c_family_and_arrow_only);

0 commit comments

Comments
 (0)