Skip to content

Commit c828105

Browse files
committed
fix(extract): write C variants in graph_buffer's schema
Main's graph_buffer.c ("Definition variants") records a definition's variant spans as [{"file_path","start_line","end_line"}], and the test-impact engine reads that schema to find every TEST(...) branch of one test. This branch's extractor wrote [{"start","end"}]. On the merge with main, graph_buffer's same-QN merge found a non-empty variants list it could not read and dropped every span, the definition's own included. The engine then no longer found the second #if branch of TEST(alpha_uses_lib), so test_impact_engine_variant_tests_are_one_test reported suite alpha UNMAPPED, in CI on macOS and Windows and locally. The extractor now writes the shared schema, with the file path JSON-escaped. The C-preprocessor-only gate and the one-carrier rule are unchanged, and the variants assertions use the shared format. test_impact_engine, extraction, pipeline, graph_buffer and conditional_variants: 857 passed. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent c4d6453 commit c828105

4 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
#include "foundation/compat.h"
2727
#include "foundation/compat_fs.h" // cbm_fopen — crash-supervisor per-file marker write
2828
#include "foundation/hash_table.h" // CBMHashTable — crash-supervisor quarantine set
29+
#include "foundation/str_util.h" // cbm_json_escape — variants file_path
2930
#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState
3031
#include "foundation/constants.h"
3132
#include "mimalloc.h" // mi_malloc/mi_calloc/mi_realloc/mi_free/mi_usable_size — bind 3rd-party allocators (#424)
3233
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
3334
#include "sqlite3.h" // sqlite3_mem_methods, sqlite3_config, SQLITE_CONFIG_MALLOC — bind sqlite to mimalloc
3435
#endif
36+
#include <limits.h> // INT_MAX
3537
#include <stdint.h> // uint32_t, uint64_t, int64_t
3638
#include <stdlib.h>
3739
#include <string.h>
@@ -2165,7 +2167,9 @@ static uint64_t cbm_variant_key(const char *qn, const char *label) {
21652167
/* refs[0..n) is one (QN, label) group in span order: give every member the
21662168
* list of the group's distinct spans. */
21672169
static void cbm_variant_assign(CBMFileResult *result, const cbm_variant_ref_t *refs, int n) {
2168-
enum { VARIANT_ENTRY_MAX = 40 }; /* ,{"start":4294967295,"end":4294967295} */
2170+
/* ,{"file_path":"","start_line":4294967295,"end_line":4294967295} + path;
2171+
* cbm_json_escape writes at most 6 bytes per source byte (\u00XX). */
2172+
enum { VARIANT_ENTRY_FIXED = 64, JSON_ESCAPE_GROWTH = 6 };
21692173
int distinct = 0;
21702174
for (int i = 0; i < n; i++) {
21712175
if (i == 0 || refs[i].start != refs[i - 1].start || refs[i].end != refs[i - 1].end) {
@@ -2175,7 +2179,17 @@ static void cbm_variant_assign(CBMFileResult *result, const cbm_variant_ref_t *r
21752179
if (distinct < 2) {
21762180
return;
21772181
}
2178-
size_t cap = (size_t)distinct * VARIANT_ENTRY_MAX + 3;
2182+
const char *path = result->defs.items[refs[0].idx].file_path;
2183+
size_t path_cap = (path ? strlen(path) : 0) * JSON_ESCAPE_GROWTH + 1;
2184+
if (path_cap > INT_MAX) {
2185+
return;
2186+
}
2187+
char *esc = (char *)cbm_arena_alloc(&result->arena, path_cap);
2188+
if (!esc) {
2189+
return;
2190+
}
2191+
(void)cbm_json_escape(esc, (int)path_cap, path ? path : "");
2192+
size_t cap = (size_t)distinct * (VARIANT_ENTRY_FIXED + strlen(esc)) + 3;
21792193
char *json = (char *)cbm_arena_alloc(&result->arena, cap);
21802194
if (!json) {
21812195
return;
@@ -2186,8 +2200,11 @@ static void cbm_variant_assign(CBMFileResult *result, const cbm_variant_ref_t *r
21862200
if (i > 0 && refs[i].start == refs[i - 1].start && refs[i].end == refs[i - 1].end) {
21872201
continue;
21882202
}
2189-
int w = snprintf(json + pos, cap - pos, "%s{\"start\":%u,\"end\":%u}", pos > 1 ? "," : "",
2190-
refs[i].start, refs[i].end);
2203+
/* graph_buffer.c's variants schema, so its same-QN merge and every
2204+
* reader of the node (the test-impact engine) see one format. */
2205+
int w = snprintf(json + pos, cap - pos,
2206+
"%s{\"file_path\":\"%s\",\"start_line\":%u,\"end_line\":%u}",
2207+
pos > 1 ? "," : "", esc, refs[i].start, refs[i].end);
21912208
if (w <= 0 || (size_t)w >= cap - pos) {
21922209
return; /* cannot happen: cap covers the longest entry */
21932210
}

‎internal/cbm/cbm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ typedef struct {
271271
* (`#if`/`#else` twins, a macro redefined per platform, overloads): the
272272
* graph keeps one node per QN, and this lists every one of those
273273
* definitions' line spans, the surviving one included, as a JSON array
274-
* sorted by start line: [{"start":10,"end":14},{"start":20,"end":26}].
274+
* sorted by start line, in graph_buffer.c's variants schema:
275+
* [{"file_path":"a.c","start_line":10,"end_line":14},
276+
* {"file_path":"a.c","start_line":20,"end_line":26}].
275277
* Carried by the definition the graph keeps for the file (the last by
276278
* start line), which makes it the node's `variants` property. NULL on
277279
* every other definition, and in every language outside

‎tests/test_extraction.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7743,15 +7743,18 @@ TEST(extract_c_variants_lists_same_file_duplicates_c1) {
77437743
ASSERT_NOT_NULL(kept);
77447744
ASSERT_NOT_NULL(other);
77457745
ASSERT_NOT_NULL(kept->variants);
7746-
ASSERT_STR_EQ(kept->variants, "[{\"start\":2,\"end\":2},{\"start\":4,\"end\":6}]");
7746+
ASSERT_STR_EQ(kept->variants, "[{\"file_path\":\"v.c\",\"start_line\":2,\"end_line\":2},"
7747+
"{\"file_path\":\"v.c\",\"start_line\":4,\"end_line\":6}]");
77477748
ASSERT_NULL(other->variants); /* one carrier per group: the node the graph keeps */
77487749

77497750
const CBMDefinition *lim_a = c1_def_at(r, "Macro", "LIMIT", 9);
77507751
const CBMDefinition *lim_b = c1_def_at(r, "Macro", "LIMIT", 11);
77517752
ASSERT_NOT_NULL(lim_a);
77527753
ASSERT_NOT_NULL(lim_b);
7753-
char want[128];
7754-
snprintf(want, sizeof(want), "[{\"start\":%u,\"end\":%u},{\"start\":%u,\"end\":%u}]",
7754+
char want[256];
7755+
snprintf(want, sizeof(want),
7756+
"[{\"file_path\":\"v.c\",\"start_line\":%u,\"end_line\":%u},"
7757+
"{\"file_path\":\"v.c\",\"start_line\":%u,\"end_line\":%u}]",
77557758
lim_a->start_line, lim_a->end_line, lim_b->start_line, lim_b->end_line);
77567759
ASSERT_NOT_NULL(lim_b->variants);
77577760
ASSERT_STR_EQ(lim_b->variants, want);

‎tests/test_pipeline.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10536,9 +10536,12 @@ static c1_variants_obs_t c1_observe_variants(const char *repo, const char *db_na
1053610536
return obs;
1053710537
}
1053810538
obs.opened = true;
10539-
obs.pick_lists_both = c1_props_contain(
10540-
s, proj, "v.pick", "\"variants\":[{\"start\":2,\"end\":2},{\"start\":4,\"end\":6}]");
10541-
obs.limit_has_list = c1_props_contain(s, proj, "v.LIMIT#macro", "\"variants\":[{\"start\":9,");
10539+
obs.pick_lists_both =
10540+
c1_props_contain(s, proj, "v.pick",
10541+
"\"variants\":[{\"file_path\":\"v.c\",\"start_line\":2,\"end_line\":2},"
10542+
"{\"file_path\":\"v.c\",\"start_line\":4,\"end_line\":6}]");
10543+
obs.limit_has_list = c1_props_contain(s, proj, "v.LIMIT#macro",
10544+
"\"variants\":[{\"file_path\":\"v.c\",\"start_line\":9,");
1054210545
obs.once_has_list = c1_props_contain(s, proj, "v.once", "\"variants\"");
1054310546
cbm_node_t *nodes = NULL;
1054410547
int count = 0;
@@ -10680,8 +10683,10 @@ TEST(pipeline_cpp_overloads_are_listed_as_variants_c1) {
1068010683
int scale_nodes = n;
1068110684
int kept_start = n > 0 ? nodes[0].start_line : -1;
1068210685
cbm_store_free_nodes(nodes, n);
10683-
bool lists_both = c1_props_contain(
10684-
s, proj, "ov.scale", "\"variants\":[{\"start\":1,\"end\":1},{\"start\":2,\"end\":4}]");
10686+
bool lists_both =
10687+
c1_props_contain(s, proj, "ov.scale",
10688+
"\"variants\":[{\"file_path\":\"ov.cpp\",\"start_line\":1,\"end_line\":1},"
10689+
"{\"file_path\":\"ov.cpp\",\"start_line\":2,\"end_line\":4}]");
1068510690
bool once_has_list = c1_props_contain(s, proj, "ov.once", "\"variants\"");
1068610691

1068710692
cbm_store_close(s);

0 commit comments

Comments
 (0)