Skip to content

Commit d68f491

Browse files
authored
Merge pull request DeusData#2970 from DeusData/fix/test-impact-gap-matching
fix(test-impact): a parse gap names only reached functions, variables, macros
2 parents 09bd6bb + e2257c5 commit d68f491

2 files changed

Lines changed: 133 additions & 8 deletions

File tree

‎src/mcp/test_impact_engine_reach.c‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,32 @@ static bool te_walk(te_ctx_t *c, const te_sinks_t *sinks, const int64_t *extra,
643643
/* ── Parse gaps (user decision 2026-10-04: handled per file) ────────
644644
* A partial parse leaves a few lines (index_coverage `parse_partial`, detail
645645
* "a-b,c-d") the graph has no edges from. A gap can only hide references
646-
* written in its own lines, so when a gap's text names a definition the walk
647-
* reached, the definition around the gap is reached too (its whole file when
648-
* the gap lies outside every definition) and the walk goes on, to a
649-
* fixpoint. Only C-family sources are read; tests are C. More, never fewer. */
646+
* written in its own lines, so when a gap's text names a reached function,
647+
* variable or macro, the definition around the gap is reached too, and the
648+
* walk goes on, to a fixpoint. A gap outside every definition reaches the
649+
* definitions that start on its lines, never its whole file. Only C-family
650+
* sources are read; tests are C. More, never fewer.
651+
*
652+
* User decision R155 (2026-11-11): matching every reached name (fields,
653+
* types, two-letter names) and seeding whole files fired hundreds of gaps
654+
* per change in this repository. A file-scope `__attribute__((used))` line
655+
* in compat_thread.c fired on any reached field named `used`, its whole file
656+
* included cbm_mutex_init, which the memory core calls, and so every change
657+
* reached the test runner's main. */
658+
659+
/* A hidden call or read names a function, a variable (an enum constant
660+
* included) or a macro, by a name of at least this many characters. */
661+
#define TE_GAP_NAME_MIN 3
662+
663+
static bool te_gap_name_kind(const char *label) {
664+
static const char *const kinds[] = {"Function", "Method", "Variable", "Macro"};
665+
for (size_t i = 0; label && i < sizeof(kinds) / sizeof(kinds[0]); i++) {
666+
if (strcmp(label, kinds[i]) == 0) {
667+
return true;
668+
}
669+
}
670+
return false;
671+
}
650672

651673
typedef struct {
652674
const char *file;
@@ -713,15 +735,17 @@ static bool te_load_gaps(te_ctx_t *c, te_gaps_t *gaps) {
713735
return ok;
714736
}
715737

716-
/* The short names of everything the walk reached (last QN segment). */
738+
/* The short names (last QN segment) of the reached functions, variables and
739+
* macros a gap's text can name. */
717740
static bool te_reached_names(te_ctx_t *c, cbm_impact_walk_t *walk, CBMHashTable *names) {
718741
const cbm_impact_hit_t *hits = cbm_impact_walk_hits(walk);
719742
int n = cbm_impact_walk_count(walk);
720743
for (int i = 0; i < n; i++) {
721744
const char *qn = hits[i].qualified_name;
722745
const char *dot = qn ? strrchr(qn, '.') : NULL;
723746
const char *name = dot ? dot + 1 : qn;
724-
if (!name || !*name || cbm_ht_get(names, name)) {
747+
if (!name || strlen(name) < TE_GAP_NAME_MIN || !te_gap_name_kind(hits[i].label) ||
748+
cbm_ht_get(names, name)) {
725749
continue;
726750
}
727751
const char *owned = cbm_arena_strdup(&c->arena, name);
@@ -765,7 +789,8 @@ static bool te_gap_names_reached(te_ctx_t *c, const te_gap_t *g, CBMHashTable *n
765789
return false;
766790
}
767791

768-
/* Seeds for a gap that fired: the definition around it, else its file. */
792+
/* Seeds for a gap that fired: the definition around it, else the definitions
793+
* that start on its lines (none: nothing more is reached). */
769794
static bool te_gap_seeds(te_ctx_t *c, const te_gap_t *g, te_ids_t *out) {
770795
bool ok = true;
771796
const cbm_ti_node_t *d = cbm_ti_graph_enclosing(c->graph, g->file, g->start, &ok);
@@ -775,7 +800,8 @@ static bool te_gap_seeds(te_ctx_t *c, const te_gap_t *g, te_ids_t *out) {
775800
int n = 0;
776801
const cbm_ti_node_t *nodes = ok ? cbm_ti_graph_file(c->graph, g->file, &n, &ok) : NULL;
777802
for (int i = 0; ok && i < n; i++) {
778-
ok = nodes[i].variant_span || te_ids_push(c, out, nodes[i].id);
803+
bool on_gap = nodes[i].start_line >= g->start && nodes[i].start_line <= g->end;
804+
ok = nodes[i].variant_span || !on_gap || te_ids_push(c, out, nodes[i].id);
779805
}
780806
return ok || te_fallback(c, CBM_TEST_RESULT_FALLBACK_GRAPH_UNAVAILABLE, "graph read failed");
781807
}

‎tests/test_test_impact_engine.c‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,104 @@ TEST(test_impact_engine_parse_gaps_reach_what_they_name) {
10771077
PASS();
10781078
}
10791079

1080+
/* A gap names only what its text can call or read: a reached function,
1081+
* variable or macro (user decision R155, 2026-11-11). The word of a changed
1082+
* struct field (`used`) or a very short name (`nn`) written in an unrelated
1083+
* function's gap names nothing, and a gap outside every definition reaches the
1084+
* definitions written on its own lines, never its whole file. Matching every
1085+
* reached name and seeding whole files fired hundreds of gaps per change in
1086+
* this repository and reached the runner's main through the memory core. */
1087+
static const char tie_gaps_main[] = "#include \"test_framework.h\"\n"
1088+
"int main(int argc, char **argv) {\n"
1089+
" RUN_SELECTED_SUITE(alpha);\n"
1090+
" RUN_SELECTED_SUITE(beta);\n"
1091+
" RUN_SELECTED_SUITE(gamma);\n"
1092+
" RUN_SELECTED_SUITE(delta);\n"
1093+
" RUN_SELECTED_SUITE(epsilon);\n"
1094+
" return 0;\n"
1095+
"}\n";
1096+
1097+
/* One test that calls `fn`, as the whole suite `suite`. */
1098+
static void tie_calling_suite(char *out, size_t cap, const char *suite, const char *fn) {
1099+
snprintf(out, cap,
1100+
"int %s(void);\n"
1101+
"TEST(%s_calls) {\n"
1102+
" return %s() == 0 ? 0 : 1;\n"
1103+
"}\n"
1104+
"SUITE(%s) {\n"
1105+
" RUN_TEST(%s_calls);\n"
1106+
"}\n",
1107+
fn, suite, fn, suite, suite);
1108+
}
1109+
1110+
TEST(test_impact_engine_parse_gaps_name_only_reached_functions) {
1111+
static const char lib[] = "struct counts {\n int used;\n};\n"
1112+
"int nn = 1;\n"
1113+
"int lib_value(void) {\n return 1;\n}\n"
1114+
"int lib_other(void) {\n return 2;\n}\n";
1115+
static const char changed[] = "struct counts {\n long used;\n};\n"
1116+
"int nn = 2;\n"
1117+
"int lib_value(void) {\n return 1 + 0;\n}\n"
1118+
"int lib_other(void) {\n return 2;\n}\n";
1119+
static const char gapped[] = "int lib_value(void);\n"
1120+
"int helper_named(void) {\n"
1121+
" int x = 0;\n"
1122+
" x = (lib_value() ;\n"
1123+
" return x;\n"
1124+
"}\n"
1125+
"int helper_field(void) {\n"
1126+
" int used = 0;\n"
1127+
" used = (used + ;\n"
1128+
" return used;\n"
1129+
"}\n"
1130+
"int helper_short(void) {\n"
1131+
" int nn = 0;\n"
1132+
" nn = (nn + ;\n"
1133+
" return nn;\n"
1134+
"}\n";
1135+
/* The shape of src/foundation/mem_core.c:110: a declaration the parser
1136+
* cannot read, outside every definition (no node holds its line). */
1137+
static const char table[] = "int lib_value(void);\n"
1138+
"int far_away(void) {\n"
1139+
" return 0;\n"
1140+
"}\n"
1141+
"static _Thread_local int tl_x[lib_value];\n";
1142+
char beta[512];
1143+
char gamma[512];
1144+
char delta[512];
1145+
char epsilon[512];
1146+
tie_calling_suite(beta, sizeof(beta), "beta", "helper_named");
1147+
tie_calling_suite(gamma, sizeof(gamma), "gamma", "far_away");
1148+
tie_calling_suite(delta, sizeof(delta), "delta", "helper_field");
1149+
tie_calling_suite(epsilon, sizeof(epsilon), "epsilon", "helper_short");
1150+
const char *const files[] = {
1151+
"tests/test_main.c", tie_gaps_main, "tests/test_beta.c", beta,
1152+
"tests/test_gamma.c", gamma, "tests/test_delta.c", delta,
1153+
"tests/test_epsilon.c", epsilon, "src/lib.c", lib,
1154+
"src/gapped.c", gapped, "src/table.c", table};
1155+
tie_fixture_t fx;
1156+
ASSERT_TRUE(tie_open_files(&fx, files, 8));
1157+
ASSERT_TRUE(tie_write(&fx, "src/lib.c", changed));
1158+
ASSERT_TRUE(tie_commit(&fx, "change lib_value, the field, the short global"));
1159+
char *json = tie_answer(&fx);
1160+
ASSERT_NOT_NULL(json);
1161+
bool want = tie_suite_has(json, "alpha", "STATIC") && tie_suite_has(json, "beta", "STATIC") &&
1162+
!tie_suite_has(json, "gamma", "STATIC") &&
1163+
!tie_suite_has(json, "delta", "STATIC") &&
1164+
!tie_suite_has(json, "epsilon", "STATIC");
1165+
if (!want) {
1166+
printf(" note: %s\n %.2400s\n", tie_last_note, json);
1167+
}
1168+
ASSERT_TRUE(tie_suite_has(json, "alpha", "STATIC")); /* calls lib_value */
1169+
ASSERT_TRUE(tie_suite_has(json, "beta", "STATIC")); /* its helper's gap names lib_value */
1170+
ASSERT_FALSE(tie_suite_has(json, "gamma", "STATIC")); /* a file-scope gap's file */
1171+
ASSERT_FALSE(tie_suite_has(json, "delta", "STATIC")); /* gap word = a changed field */
1172+
ASSERT_FALSE(tie_suite_has(json, "epsilon", "STATIC")); /* gap word = a 2-letter global */
1173+
free(json);
1174+
tie_close(&fx);
1175+
PASS();
1176+
}
1177+
10801178
/* The base commit's object id, read from the branch ref (never packed here). */
10811179
static bool tie_base_sha(tie_fixture_t *fx, char sha[65]) {
10821180
char path[1200];
@@ -1378,6 +1476,7 @@ SUITE(test_impact_engine) {
13781476
RUN_TEST(test_impact_engine_reached_role_handler_is_no_way_into_main);
13791477
RUN_TEST(test_impact_engine_role_started_by_a_role_selects_its_spawners);
13801478
RUN_TEST(test_impact_engine_parse_gaps_reach_what_they_name);
1479+
RUN_TEST(test_impact_engine_parse_gaps_name_only_reached_functions);
13811480
RUN_TEST(test_impact_engine_admitted_coverage_narrows_only_when_verified);
13821481
RUN_TEST(test_impact_engine_coverage_reaches_unmapped_code_through_mapped_callers);
13831482
}

0 commit comments

Comments
 (0)