Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(graph): platform variants keep their path QNs and are related by variant_key
Martin Vogel7aa384b40137
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.hindex 57ffb253d..9fac8cf55 100644--- a/internal/cbm/cbm.h+++ b/internal/cbm/cbm.h@@ -304,6 +304,15 @@ typedef struct {* values are escaped where the properties are written. Tail field:* zero-init stays valid. */const char **extra_props;+ /* C-family platform variants (helpers.h cbm_platform_neutral_qn): the+ * platform-neutral QN of a non-static file-scope function whose path+ * carries a platform token (foo_win.c, src/unix/fs.c). The definition+ * keeps its own path QN; this becomes the node's `variant_key` property,+ * and the nodes of a project sharing one key are the platform variants of+ * one function (the test-impact walk treats a change in any of them as a+ * change of all, store_impact.h follow_variant_keys). NULL on every other+ * definition. Tail field: zero-init stays valid. */+ const char *variant_key;} CBMDefinition;/* Argument captured from a call expression */diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex e78b2cafe..a017a5518 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -6609,14 +6609,6 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec* proj.myapp.db.Func, not proj.myapp.db.conn.Func). Other langs unchanged. */def.qualified_name =cbm_fqn_compute_source_lang(a, ctx->project, ctx->rel_path, qn_name, ctx->language);- /* C-family platform variants (foo_win.c / foo_posix.c, src/unix/ vs- * src/win/): an external-linkage file-scope function takes its- * platform-neutral QN, so its variants are one definition. The call-scope- * twin in extract_unified.c applies the same rule. */- if (!ctx->enclosing_class_qn) {- def.qualified_name = cbm_platform_variant_qn(a, ctx->language, ctx->project, ctx->rel_path,- qn_name, def.qualified_name, node);- }/* A free function declared inside a namespace (C++/C#/PHP) is qualified by* the namespace scope the def walk carries (enclosing_class_qn was extended* by is_namespace_scope_kind), so `ns::serialize` is `proj.file.ns.serialize`@@ -6715,6 +6707,16 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec}}+ /* C-family platform variants (foo_win.c / foo_posix.c, src/unix/ vs+ * src/win/): an external-linkage file-scope function keeps its path QN and+ * carries its platform-neutral QN as the key that relates its variants+ * (the node's `variant_key`). NULL for anything scoped, static, or in a+ * path without a platform token. */+ if (!ctx->enclosing_class_qn) {+ def.variant_key = cbm_platform_neutral_qn(a, ctx->language, ctx->project, ctx->rel_path,+ qn_name, def.qualified_name, node);+ }+// Pony: fun/be/new (method/constructor/ffi_method) live in pony_func_types,// so the main def-walk extracts them here as "Function"; but one declared// inside a class/actor/struct/trait/interface/primitive IS a method. Detectdiff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.cindex f0590de5f..f8e4a228f 100644--- a/internal/cbm/extract_unified.c+++ b/internal/cbm/extract_unified.c@@ -1111,13 +1111,9 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLanreturn cbm_arena_sprintf(ctx->arena, "%s.%s", state->enclosing_class_qn, qn_name);}/* Java/Go: directory-based module so this enclosing-func QN matches the def- * QN and the LSP caller_qn (the lsp_resolve join keys on exact equality).- * C-family platform variants share their platform-neutral QN — the same- * rule extract_func_def applies, so in-body calls source to the def. */- const char *qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, qn_name,- ctx->language);- return cbm_platform_variant_qn(ctx->arena, ctx->language, ctx->project, ctx->rel_path, qn_name,- qn, node);+ * QN and the LSP caller_qn (the lsp_resolve join keys on exact equality). */+ return cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path, qn_name,+ ctx->language);}// Compute class QN for scope tracking.diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.cindex b793524b3..f08d047e7 100644--- a/internal/cbm/helpers.c+++ b/internal/cbm/helpers.c@@ -1946,16 +1946,16 @@ static bool func_is_static(TSNode func_node) {return false;}-const char *cbm_platform_variant_qn(CBMArena *a, CBMLanguage lang, const char *project,+const char *cbm_platform_neutral_qn(CBMArena *a, CBMLanguage lang, const char *project,const char *rel_path, const char *name, const char *qn,TSNode func_node) {if (!c_family_variant_lang(lang) || !rel_path || !name || !name[0] || !qn ||ts_node_is_null(func_node) || func_is_static(func_node)) {- return qn;+ return NULL;}char *plain = cbm_fqn_compute(a, project, rel_path, name);if (!plain || strcmp(plain, qn) != 0) {- return qn; /* namespaced or otherwise scoped: not a file-scope function */+ return NULL; /* namespaced or otherwise scoped: not a file-scope function */}/* Rebuild the path without platform tokens: a directory segment that is* one is dropped; a filename stem loses its platform suffix, and a stem@@ -1963,7 +1963,7 @@ const char *cbm_platform_variant_qn(CBMArena *a, CBMLanguage lang, const char *psize_t len = strlen(rel_path);char *neutral = (char *)cbm_arena_alloc(a, len + SKIP_ONE);if (!neutral) {- return qn;+ return NULL;}size_t out = 0;bool changed = false;@@ -2010,16 +2010,15 @@ const char *cbm_platform_variant_qn(CBMArena *a, CBMLanguage lang, const char *p}neutral[out] = '\0';if (!changed) {- return qn;+ return NULL;}if (dir_level) {/* No file stem left: the folder form keeps every directory segment* (cbm_fqn_compute would strip a dotted segment as an extension). */char *folder = cbm_fqn_folder(a, project ? project : "", neutral);- return folder ? cbm_arena_sprintf(a, "%s.%s", folder, name) : qn;+ return folder ? cbm_arena_sprintf(a, "%s.%s", folder, name) : NULL;}- char *variant = cbm_fqn_compute(a, project, neutral, name);- return variant ? variant : qn;+ return cbm_fqn_compute(a, project, neutral, name);}char *cbm_fqn_folder(CBMArena *a, const char *project, const char *rel_dir) {diff --git a/internal/cbm/helpers.h b/internal/cbm/helpers.hindex f96f46709..724466679 100644--- a/internal/cbm/helpers.h+++ b/internal/cbm/helpers.h@@ -295,12 +295,17 @@ char *cbm_fqn_compute_source_lang(CBMArena *a, const char *project, const char *// C-family platform variants: the same external-linkage function defined in// files that differ only by a platform token (foo_win.c / foo_posix.c,-// src/unix/fs.c / src/win/fs.c) is ONE function with platform variants — two+// src/unix/fs.c / src/win/fs.c) is one function with platform variants — two// external definitions of one name cannot link into one program, so they are-// alternatives by construction. Returns the platform-neutral QN (the path with-// its platform tokens removed) for a non-static file-scope function of-// C/C++/CUDA/ObjC whose `qn` is the plain file-stem form; otherwise `qn`.-const char *cbm_platform_variant_qn(CBMArena *a, CBMLanguage lang, const char *project,+// alternatives by construction. Each definition keeps its own path QN (it is+// found by its file); this returns the KEY that relates the variants: the+// platform-neutral QN (the path with its platform tokens removed), for a+// non-static file-scope function of C/C++/CUDA/ObjC whose `qn` is the plain+// file-stem form and whose path carries a platform token. NULL otherwise. A+// pure function of the definition's own path, name and linkage: no other file+// takes part, so a full index and an incremental repair write the same key.+// It becomes the node's `variant_key` property (CBMDefinition.variant_key).+const char *cbm_platform_neutral_qn(CBMArena *a, CBMLanguage lang, const char *project,const char *rel_path, const char *name, const char *qn,TSNode func_node);diff --git a/internal/cbm/result_compact.c b/internal/cbm/result_compact.cindex 6b41a5170..76606ac51 100644--- a/internal/cbm/result_compact.c+++ b/internal/cbm/result_compact.c@@ -278,6 +278,7 @@ static void cr_walk_def(cr_ctx_t *c, CBMDefinition *d) {cr_str(c, &d->variants);cr_list(c, &d->extra_props);cr_str(c, &d->decl_namespace);+ cr_str(c, &d->variant_key);}static void cr_walk_call(cr_ctx_t *c, CBMCall *call) {diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.cindex 13f0db3d7..7065814b5 100644--- a/src/graph_buffer/graph_buffer.c+++ b/src/graph_buffer/graph_buffer.c@@ -889,7 +889,11 @@ int cbm_gbuf_duplicate_edge_ids(const cbm_gbuf_t *gb) {* sorted, unique), so a change in any variant maps to the node. Recorded only* between definitions of the same label with real spans: a different kind* sharing the QN (C: struct vs function) is no variant, a span on line 0 is- * no location (delta-route proxies), and a full tie is the same entity. */+ * no location (delta-route proxies), and a full tie is the same entity.+ * C-family platform files (foo_win.c / foo_posix.c, src/unix/ vs src/win/)+ * do not collide here: each definition keeps its path QN, and they share a+ * `variant_key` property instead (helpers.h cbm_platform_neutral_qn), which+ * the test-impact walk follows (store_impact.h follow_variant_keys). */static void *gb_yy_malloc(void *ctx, size_t size) {(void)ctx;return cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, size);diff --git a/src/mcp/test_impact_engine_reach.c b/src/mcp/test_impact_engine_reach.cindex 49a1142cc..e54724aca 100644--- a/src/mcp/test_impact_engine_reach.c+++ b/src/mcp/test_impact_engine_reach.c@@ -290,11 +290,15 @@ static bool te_collect_sinks(te_ctx_t *c, te_sinks_t *s) {static bool te_walk(te_ctx_t *c, const te_sinks_t *sinks, const int64_t *extra, int extra_count,cbm_impact_walk_t **out) {+ /* A change in one platform variant of a function (foo_win.c / foo_posix.c)+ * reaches whoever calls any of its variants: a caller binds to one of+ * them, and the build of another platform runs another. */cbm_impact_policy_t policy = {.project = "test-impact",.edge_types = te_walk_types,.edge_type_count =(int)(sizeof(te_walk_types) / sizeof(te_walk_types[0])),- .follow_routes = true};+ .follow_routes = true,+ .follow_variant_keys = true};*out = NULL;if (cbm_impact_walk_open_scoped(c->scope, &policy, out) != CBM_STORE_OK) {return te_fallback(c, CBM_TEST_RESULT_FALLBACK_ENGINE_SATURATED,diff --git a/src/mcp/test_impact_seed.c b/src/mcp/test_impact_seed.cindex 113c1ab7e..6026e0d44 100644--- a/src/mcp/test_impact_seed.c+++ b/src/mcp/test_impact_seed.c@@ -168,11 +168,13 @@ static bool ts_has_span(const cbm_ti_node_t *nodes, int count, const cbm_node_t}/* The file's definitions, each with every span it has in the file: a- * definition written once per #if branch or platform file is ONE node+ * definition written once per #if branch or Go build-tagged file is ONE node* (graph_buffer.c "Definition variants") whose own span is one variant, so* its other variants here (other branches, or a node whose file_path names- * another platform file) join as further spans of the same node. A change- * in any variant then seeds that definition. */+ * another file of the same QN) join as further spans of the same node. A+ * change in any variant then seeds that definition. C-family platform files+ * are separate nodes sharing a `variant_key`; the walk relates them+ * (store_impact.h follow_variant_keys). */static ts_file_t *ts_load_file(cbm_ti_graph_t *g, const char *file) {cbm_node_t *rows = NULL;int count = 0;diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.cindex b97a60769..eb414a649 100644--- a/src/pipeline/pass_definitions.c+++ b/src/pipeline/pass_definitions.c@@ -309,9 +309,10 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)}size_t pos = (size_t)n;append_json_string(buf, bufsize, &pos, "docstring", def->docstring);- /* Right after the docstring: the buffer is sized for exactly these two- * uncapped fields (pd_props_buf), so neither can be squeezed out. */+ /* Right after the docstring: the buffer is sized for exactly these three+ * uncapped fields (pd_props_buf), so none can be squeezed out. */append_json_raw(buf, bufsize, &pos, "variants", def->variants);+ append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);append_json_string(buf, bufsize, &pos, "signature", def->signature);append_json_string(buf, bufsize, &pos, "return_type", def->return_type);append_json_string(buf, bufsize, &pos, "parent_class", def->parent_class);@@ -353,10 +354,10 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)}/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole- * serialized docstring and variants fields, which have no length cap (a field- * that does not fit is dropped whole). Returns `stack` for a def with neither,- * or when the larger buffer cannot be allocated. Twin of pass_parallel.c --- * keep both in sync. */+ * serialized docstring, variants and variant_key fields, which have no length+ * cap (a field that does not fit is dropped whole). Returns `stack` for a def+ * with none of them, or when the larger buffer cannot be allocated. Twin of+ * pass_parallel.c -- keep both in sync. */static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {size_t need = *size;if (def->docstring && def->docstring[0]) {@@ -365,6 +366,10 @@ static char *pd_props_buf(const CBMDefinition *def, char *stack, size_t *size) {if (def->variants && def->variants[0]) {need += strlen("variants") + strlen(def->variants) + PD_JSON_FIELD_OVERHEAD;}+ if (def->variant_key && def->variant_key[0]) {+ need +=+ strlen("variant_key") + def_json_escaped_len(def->variant_key) + PD_JSON_FIELD_OVERHEAD;+ }if (need == *size) {return stack;}diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex efc407934..cc16e443b 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -539,9 +539,10 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)}size_t pos = (size_t)n;append_json_string(buf, bufsize, &pos, "docstring", def->docstring);- /* Right after the docstring: the buffer is sized for exactly these two- * uncapped fields (pp_props_buf), so neither can be squeezed out. */+ /* Right after the docstring: the buffer is sized for exactly these three+ * uncapped fields (pp_props_buf), so none can be squeezed out. */append_json_raw(buf, bufsize, &pos, "variants", def->variants);+ append_json_string(buf, bufsize, &pos, "variant_key", def->variant_key);append_json_string(buf, bufsize, &pos, "signature", def->signature);append_json_string(buf, bufsize, &pos, "return_type", def->return_type);append_json_string(buf, bufsize, &pos, "parent_class", def->parent_class);@@ -731,10 +732,10 @@ typedef struct {enum { PP_OVERSIZED_WARN_MAX = 32 };/* A def's properties buffer: CBM_SZ_2K for every other field plus the whole- * serialized docstring and variants fields, which have no length cap (a field- * that does not fit is dropped whole). Returns `stack` for a def with neither,- * or when the larger buffer cannot be allocated. Twin of pass_definitions.c --- * keep both in sync. */+ * serialized docstring, variants and variant_key fields, which have no length+ * cap (a field that does not fit is dropped whole). Returns `stack` for a def+ * with none of them, or when the larger buffer cannot be allocated. Twin of+ * pass_definitions.c -- keep both in sync. */static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {size_t need = *size;if (def->docstring && def->docstring[0]) {@@ -743,6 +744,10 @@ static char *pp_props_buf(const CBMDefinition *def, char *stack, size_t *size) {if (def->variants && def->variants[0]) {need += strlen("variants") + strlen(def->variants) + PP_JSON_FIELD_OVERHEAD;}+ if (def->variant_key && def->variant_key[0]) {+ need +=+ strlen("variant_key") + pp_json_escaped_len(def->variant_key) + PP_JSON_FIELD_OVERHEAD;+ }if (need == *size) {return stack;}diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.cindex 9b5134f58..b660c9595 100644--- a/src/pipeline/pipeline.c+++ b/src/pipeline/pipeline.c@@ -2045,7 +2045,8 @@ static void log_result_census(const char *tag, CBMFileResult **cache, int file_cstr_def_fp += def->fingerprint ? (size_t)def->fingerprint_k * sizeof(uint32_t) : 0;str_def_misc += census_len(def->route_path) + census_len(def->route_method) +census_len(def->impl_trait) + census_len(def->http_base_url) +- census_list(def->extra_props) + census_len(def->decl_namespace);+ census_list(def->extra_props) + census_len(def->decl_namespace) ++ census_len(def->variant_key);}for (int c = 0; c < r->calls.count; c++) {const CBMCall *call = &r->calls.items[c];diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.cindex 1ee95389a..319289d39 100644--- a/src/pipeline/pipeline_incremental.c+++ b/src/pipeline/pipeline_incremental.c@@ -1400,7 +1400,7 @@ enum {* the honest signal that a rebuild costs less than a repair. */CLOSURE_BUDGET_FLOOR_FILES = 8,/* Variant partners of partners: real chains are one step (a header and- * its source, a set of platform files); a longer one declines. */+ * its source, a set of Go build-tagged files); a longer one declines. */CLOSURE_VARIANT_ROUNDS_MAX = 8,};@@ -2348,16 +2348,21 @@ static bool closure_path_listed(char **paths, int count, const char *path) {}/* Variant partners (graph_buffer.c "Definition variants"): a definition- * written in several files (platform files, a struct's header declaration- * and its source definition, Go build-tagged files) is ONE node whose- * file_path names one of them, with a DEFINES edge from every file that- * writes a variant. Re-parsing one of those files alone rebuilds the node- * without the others' spans and outbound edges, or leaves a changed- * partner's stale span behind; so every file of such a node joins the- * closure, to a fixpoint. `paths` is the delta (changed, then deleted from- * index n_changed on); `dependents` already joined. A deleted partner is in- * the delta already; any other partner outside the discovery declines, as a- * dependent would. Returns the decline reason, or NULL. */+ * written in several files (a struct's header declaration and its source+ * definition, Go build-tagged files) is ONE node whose file_path names one of+ * them, with a DEFINES edge from every file that writes a variant.+ * Re-parsing one of those files alone rebuilds the node without the others'+ * spans and outbound edges, or leaves a changed partner's stale span behind;+ * so every file of such a node joins the closure, to a fixpoint. `paths` is+ * the delta (changed, then deleted from index n_changed on); `dependents`+ * already joined. A deleted partner is in the delta already; any other+ * partner outside the discovery declines, as a dependent would. Returns the+ * decline reason, or NULL.+ * C-family platform files (foo_win.c / foo_posix.c) are no partners: each+ * definition is its own node with its own path QN, and the `variant_key` that+ * relates them is a function of the definition's own path, name and linkage,+ * so re-parsing the changed file alone writes what a full index writes; the+ * group is formed where it is read (store_impact.c), never stored. */static const char *closure_add_variant_partners(cbm_store_t *store, const char *project,CBMHashTable *closure_set,CBMHashTable *files_by_path, char **paths,diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.hindex c3891f9a2..99c9d4226 100644--- a/src/pipeline/pipeline_internal.h+++ b/src/pipeline/pipeline_internal.h@@ -1391,7 +1391,10 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr* diagrams are Diagram/DiagramElement nodes with DIAGRAM_ARROW edges;* OpenAPI operations are Routes; a Markdown section's text keeps a* diagram fence's labels instead of its syntax, and its candidates- * carry the p of its diagrams' kind family. */+ * carry the p of its diagrams' kind family; a non-static file-scope+ * C/C++/CUDA/ObjC function in a platform file or directory (foo_win.c,+ * src/unix/) keeps its path QN and carries a `variant_key` property+ * (its platform-neutral QN) that relates its platform variants. */enum { CBM_SEMANTIC_INDEX_VERSION = 7 };typedef struct {diff --git a/src/store/store_impact.c b/src/store/store_impact.cindex 40c3f75a9..c0f1bf06c 100644--- a/src/store/store_impact.c+++ b/src/store/store_impact.c@@ -29,6 +29,9 @@ enum {};static const char IMPACT_HANDLES[] = "HANDLES";+/* The via_edge of a node reached as a platform variant (follow_variant_keys):+ * the property that relates the two, since no edge does. */+static const char IMPACT_VARIANT_KEY[] = "variant_key";static const char IMPACT_ROUTE_LABEL[] = "Route";static const char *const IMPACT_ROUTE_IN[IMPACT_ROUTE_IN_COUNT] = {"HTTP_CALLS", "ASYNC_CALLS","GRPC_CALLS"};@@ -41,12 +44,22 @@ struct cbm_impact_walk {char **edge_types; /* owned copies: a hit's via_edge points into these */int edge_type_count;bool follow_routes;+ bool follow_variant_keys;int max_hops; /* 0 = fixpoint */int64_t max_id; /* largest node id when the walk opened */int32_t *slot; /* [max_id + 1]: index into hits + 1; 0 = not reached */uint8_t *sink; /* [max_id + 1]: reached, never expanded */+ /* Platform variant groups (follow_variant_keys), read once at open: the+ * nodes sharing one `variant_key`, groups of two or more only. The+ * members of group g are variant_members[variant_start[g] ..+ * variant_start[g + 1]), by id. variant_group is NULL when no group+ * exists. */+ int32_t *variant_group; /* [max_id + 1]: group index + 1; 0 = in none */+ int64_t *variant_members;+ int32_t *variant_start; /* [group count + 1] */+cbm_impact_hit_t *hits;int count;int cap;@@ -346,25 +359,32 @@ static bool impact_expand(cbm_impact_walk_t *w, sqlite3_stmt *stmt, int from, coreturn rc == SQLITE_DONE && sqlite3_reset(stmt) == SQLITE_OK && impact_check(w);}-static bool impact_expand_level(cbm_impact_walk_t *w, const impact_frontier_list_t *frontier,- impact_frontier_list_t *next) {- for (int t = 0; t < w->edge_type_count; t++) {- for (int f = 0; f < frontier->count; f++) {- if (!impact_check(w)) {- return false;- }- int hit = frontier->items[f].hit;- if (w->sink[w->hits[hit].id]) {- continue;- }- if (!impact_expand(w, w->stmt_in, hit, w->edge_types[t], next)) {- return false;- }- }+/* Reach the other platform variants of `from` (a frontier hit): the members+ * of its variant group not reached yet. */+static bool impact_expand_variants(cbm_impact_walk_t *w, int from, impact_frontier_list_t *next) {+ if (!impact_check(w) || w->hits[from].hop == INT_MAX) {+ return false;}- if (!w->follow_routes) {+ int64_t from_id = w->hits[from].id;+ int32_t group = w->variant_group[from_id];+ if (group == 0) {return true;}+ int hop = w->hits[from].hop + SKIP_ONE;+ for (int32_t m = w->variant_start[group - SKIP_ONE]; m < w->variant_start[group]; m++) {+ int64_t id = w->variant_members[m];+ if (w->slot[id] != 0) {+ continue;+ }+ if (impact_add_hit(w, next, id, hop, from_id, IMPACT_VARIANT_KEY) == IMPACT_ADD_ERROR) {+ return false;+ }+ }+ return impact_check(w);+}++static bool impact_expand_routes(cbm_impact_walk_t *w, const impact_frontier_list_t *frontier,+ impact_frontier_list_t *next) {for (int f = 0; f < frontier->count; f++) {if (!impact_check(w)) {return false;@@ -392,6 +412,38 @@ static bool impact_expand_level(cbm_impact_walk_t *w, const impact_frontier_listreturn true;}+static bool impact_expand_level(cbm_impact_walk_t *w, const impact_frontier_list_t *frontier,+ impact_frontier_list_t *next) {+ for (int t = 0; t < w->edge_type_count; t++) {+ for (int f = 0; f < frontier->count; f++) {+ if (!impact_check(w)) {+ return false;+ }+ int hit = frontier->items[f].hit;+ if (w->sink[w->hits[hit].id]) {+ continue;+ }+ if (!impact_expand(w, w->stmt_in, hit, w->edge_types[t], next)) {+ return false;+ }+ }+ }+ if (w->follow_routes && !impact_expand_routes(w, frontier, next)) {+ return false;+ }+ /* Platform variants rank last: an edge from the same level wins. */+ for (int f = 0; w->variant_group && f < frontier->count; f++) {+ if (!impact_check(w)) {+ return false;+ }+ int hit = frontier->items[f].hit;+ if (!w->sink[w->hits[hit].id] && !impact_expand_variants(w, hit, next)) {+ return false;+ }+ }+ return true;+}+static void impact_free_strings(cbm_impact_walk_t *w) {for (int i = 0; i < w->count; i++) {cbm_free(CBM_MEM_CLASS_STORE, (char *)w->hits[i].qualified_name);@@ -410,6 +462,9 @@ void cbm_impact_walk_close(cbm_impact_walk_t *w) {cbm_free(CBM_MEM_CLASS_STORE, w->hits);cbm_free(CBM_MEM_CLASS_STORE, w->slot);cbm_free(CBM_MEM_CLASS_STORE, w->sink);+ cbm_free(CBM_MEM_CLASS_STORE, w->variant_group);+ cbm_free(CBM_MEM_CLASS_STORE, w->variant_members);+ cbm_free(CBM_MEM_CLASS_STORE, w->variant_start);for (int i = 0; i < w->edge_type_count; i++) {cbm_free(CBM_MEM_CLASS_STORE, w->edge_types[i]);}@@ -439,6 +494,7 @@ static bool impact_copy_policy(cbm_impact_walk_t *w, const cbm_impact_policy_t *w->edge_type_count = i + SKIP_ONE;}w->follow_routes = policy->follow_routes;+ w->follow_variant_keys = policy->follow_variant_keys;w->max_hops = policy->max_hops > 0 ? policy->max_hops : 0;return true;}@@ -484,6 +540,111 @@ static bool impact_prepare(cbm_impact_walk_t *w) {impact_check(w);}+static bool impact_grow(void **items, int *cap, int count, size_t size) {+ if (count < *cap) {+ return true;+ }+ if (*cap > INT_MAX / IMPACT_GROWTH) {+ return false;+ }+ int next = *cap ? *cap * IMPACT_GROWTH : IMPACT_INIT_CAP;+ void *grown = cbm_realloc(CBM_MEM_CLASS_STORE, *items, (size_t)next * size);+ if (!grown) {+ return false;+ }+ *items = grown;+ *cap = next;+ return true;+}++/* The ids read so far end the run of one key: it is a group when two or more+ * nodes carry the key; a key on one node relates nothing and is dropped. */+static bool impact_close_variant_run(cbm_impact_walk_t *w, int run, int *count, int *groups,+ int *group_cap) {+ if (*count - run < PAIR_LEN) {+ *count = run;+ return true;+ }+ if (!impact_grow((void **)&w->variant_start, group_cap, *groups, sizeof(int32_t))) {+ return false;+ }+ w->variant_start[(*groups)++] = run;+ return true;+}++/* Platform variant groups (follow_variant_keys): every node of the project+ * that carries a `variant_key`, in one read ordered by (key, id), so the+ * groups are a pure function of the graph. The instr() guard keeps the JSON+ * parse to the nodes that have the property. */+static bool impact_load_variants(cbm_impact_walk_t *w) {+ if (!w->follow_variant_keys) {+ return true;+ }+ sqlite3_stmt *stmt = NULL;+ if (!impact_check(w) ||+ sqlite3_prepare_v2(w->db,+ "SELECT k, id FROM (SELECT id, CASE WHEN json_valid(properties)"+ " THEN json_extract(properties, '$.variant_key') END AS k FROM nodes"+ " WHERE project = ?1 AND instr(properties, '\"variant_key\"') > 0)"+ " WHERE typeof(k) = 'text' ORDER BY k, id",+ CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK ||+ sqlite3_bind_text(stmt, IMPACT_BIND_ID, w->project, CBM_NOT_FOUND, SQLITE_STATIC) !=+ SQLITE_OK) {+ sqlite3_finalize(stmt);+ return false;+ }+ int count = 0;+ int cap = 0;+ int groups = 0;+ int group_cap = 0;+ int run = 0; /* where the current key's ids start in variant_members */+ char *key = NULL; /* the current key */+ bool ok = true;+ int rc = SQLITE_ROW;+ while (ok && (rc = sqlite3_step(stmt)) == SQLITE_ROW) {+ const char *k = (const char *)sqlite3_column_text(stmt, 0);+ int64_t id = sqlite3_column_int64(stmt, SKIP_ONE);+ ok = impact_check(w);+ if (!ok || !k || !impact_known_id(w, id)) {+ continue;+ }+ if (!key || strcmp(key, k) != 0) {+ ok = impact_close_variant_run(w, run, &count, &groups, &group_cap);+ run = count;+ cbm_free(CBM_MEM_CLASS_STORE, key);+ key = cbm_mem_strdup(CBM_MEM_CLASS_STORE, k);+ ok = ok && key;+ }+ ok = ok &&+ impact_grow((void **)&w->variant_members, &cap, count, sizeof(*w->variant_members));+ if (ok) {+ w->variant_members[count++] = id;+ }+ }+ cbm_free(CBM_MEM_CLASS_STORE, key);+ ok = ok && rc == SQLITE_DONE && impact_close_variant_run(w, run, &count, &groups, &group_cap);+ ok = sqlite3_finalize(stmt) == SQLITE_OK && ok && impact_check(w);+ if (!ok || groups == 0) {+ return ok;+ }+ /* The end of the last group. */+ if (!impact_grow((void **)&w->variant_start, &group_cap, groups, sizeof(int32_t))) {+ return false;+ }+ w->variant_start[groups] = count;+ w->variant_group = (int32_t *)cbm_calloc(CBM_MEM_CLASS_STORE,+ ((size_t)w->max_id + SKIP_ONE) * sizeof(int32_t));+ if (!w->variant_group) {+ return false;+ }+ for (int g = 0; g < groups; g++) {+ for (int32_t m = w->variant_start[g]; m < w->variant_start[g + SKIP_ONE]; m++) {+ w->variant_group[w->variant_members[m]] = g + SKIP_ONE;+ }+ }+ return impact_check(w);+}+static int impact_open(cbm_store_t *s, cbm_store_read_scope_t *scope,const cbm_impact_policy_t *policy, cbm_impact_walk_t **out) {if (out) {@@ -506,7 +667,8 @@ static int impact_open(cbm_store_t *s, cbm_store_read_scope_t *scope,}w->db = db;w->scope = scope;- if (!impact_copy_policy(w, policy) || !impact_prepare(w) || !impact_check(w)) {+ if (!impact_copy_policy(w, policy) || !impact_prepare(w) || !impact_load_variants(w) ||+ !impact_check(w)) {int status = impact_fail(w, CBM_STORE_ERR);cbm_impact_walk_close(w);return status;diff --git a/src/store/store_impact.h b/src/store/store_impact.hindex a32f8aa32..b8d691046 100644--- a/src/store/store_impact.h+++ b/src/store/store_impact.h@@ -38,6 +38,13 @@ typedef struct {* HANDLES, and a Route reaches whoever calls it over HTTP_CALLS,* ASYNC_CALLS or GRPC_CALLS. These rank after every requested type. */bool follow_routes;+ /* Also cross platform variants: a node with a `variant_key` property+ * reaches every other node of the project with the same key (C-family+ * platform files, helpers.h cbm_platform_neutral_qn), one hop later, as+ * if through an edge that ranks after every other step. Its via_edge is+ * "variant_key". The key -> nodes index is read once, when the walk+ * opens; a key only one node carries relates nothing. */+ bool follow_variant_keys;/* 0 walks to the fixpoint. A positive value stops expanding nodes that are* that many hops from the seeds of their run: the bounded neighbourhood,* with the same ordering and parents. */diff --git a/tests/test_conditional_variants.c b/tests/test_conditional_variants.cindex 6c05c373c..5d5aa1716 100644--- a/tests/test_conditional_variants.c+++ b/tests/test_conditional_variants.c@@ -6,7 +6,9 @@* run ever sees: C-family #if/#else, C#/F# #if, Rust #[cfg], Swift #if,* Go build-tagged files, D version blocks, Erlang -ifdef, Haskell CPP,* Fortran .F90 preprocessing, Verilog `ifdef, Elixir/Python/Ruby/PHP- * definitions under an `if`, and C platform files (foo_win.c / foo_posix.c).+ * definitions under an `if`. C platform files (foo_win.c / foo_posix.c) are+ * the exception: each keeps its own node and path QN, so it is found by its+ * file, and they share a `variant_key` (cv_platform_check below).** Every fixture defines `pick` twice — variant A calls `alpha`, variant B* calls `beta` — plus a `caller` that calls `pick`. The graph must hold:@@ -28,6 +30,7 @@#include <pipeline/pipeline_internal.h>#include <store/store.h>#include <sqlite3.h>+#include "helpers.h" /* cbm_fqn_compute: the path-derived QN of a definition */#include <stdio.h>#include <stdlib.h>@@ -417,15 +420,122 @@ TEST(cv_verilog_ifdef) {PASS();}-/* C platform files: the same function in foo_win.c and foo_posix.c is one- * definition with two variants, and a caller in a shared file binds to it. */+/* One text value from SQL with text parameters (as cv_count); false when the+ * query fails or returns no row or NULL. */+static bool cv_text(cv_proj_t *p, const char *sql, const char *a, const char *b, char *out,+ size_t n) {+ sqlite3 *db = cbm_store_get_db(p->store);+ sqlite3_stmt *stmt = NULL;+ out[0] = '\0';+ if (!db || sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {+ sqlite3_finalize(stmt);+ return false;+ }+ int params = sqlite3_bind_parameter_count(stmt);+ const char *values[] = {p->project, a, b};+ for (int i = 0; i < params && i < 3; i++) {+ sqlite3_bind_text(stmt, i + 1, values[i], -1, SQLITE_TRANSIENT);+ }+ const char *text =+ sqlite3_step(stmt) == SQLITE_ROW ? (const char *)sqlite3_column_text(stmt, 0) : NULL;+ if (text) {+ snprintf(out, n, "%s", text);+ }+ sqlite3_finalize(stmt);+ return text != NULL;+}++/* One platform variant of `pick`: its file, the function its body calls, and+ * the other variant's callee, which must not be reached from it. */+typedef struct {+ const char *file;+ const char *callee;+ const char *other_callee;+} cv_platform_t;++/* C platform variants (foo_win.c / foo_posix.c, src/unix/ vs src/win/): every+ * definition keeps its own node under its path-derived QN, the QN its file+ * and name compute (findability: a lookup by that QN finds it); it calls what+ * its own body calls; all of them carry the platform-neutral QN+ * `<project>.<neutral>.pick` as variant_key, which relates them, and no+ * `variants` list; a caller in a shared file binds to one of them. */+static int cv_platform_check(const char *lang, const cv_file_t *files, int count,+ const cv_platform_t *variants, int variant_count,+ const char *neutral) {+ cv_proj_t p;+ if (!cv_index(&p, files, count)) {+ fprintf(stderr, " [%s] fixture could not be indexed\n", lang);+ cv_cleanup(&p);+ return 0;+ }+ CBMArena arena;+ cbm_arena_init(&arena);+ char want_key[512];+ snprintf(want_key, sizeof(want_key), "%s.%s.pick", p.project, neutral);+ int ok = 1;+ int nodes = cv_count(&p,+ "SELECT COUNT(DISTINCT qualified_name) FROM nodes WHERE project=?1 "+ "AND name='pick' AND label='Function'",+ NULL, NULL);+ if (nodes != variant_count) {+ fprintf(stderr, " [%s] pick nodes=%d, want %d\n", lang, nodes, variant_count);+ ok = 0;+ }+ const char *calls = "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id "+ "JOIN nodes t ON t.id=e.target_id WHERE e.project=?1 AND "+ "e.type='CALLS' AND s.qualified_name=?2 AND t.name=?3";+ for (int i = 0; i < variant_count; i++) {+ const cv_platform_t *v = &variants[i];+ const char *want_qn = cbm_fqn_compute(&arena, p.project, v->file, "pick");+ char qn[512];+ char key[512];+ bool found = cv_text(&p,+ "SELECT qualified_name FROM nodes WHERE project=?1 AND name='pick' "+ "AND label='Function' AND file_path=?2",+ v->file, NULL, qn, sizeof(qn));+ bool keyed = found && cv_text(&p,+ "SELECT json_extract(properties,'$.variant_key') FROM nodes "+ "WHERE project=?1 AND qualified_name=?2",+ qn, NULL, key, sizeof(key));+ int own = found ? cv_count(&p, calls, qn, v->callee) : -1;+ int other = found ? cv_count(&p, calls, qn, v->other_callee) : -1;+ if (!found || !want_qn || strcmp(qn, want_qn) != 0 || !keyed ||+ strcmp(key, want_key) != 0 || own != 1 || other != 0) {+ fprintf(stderr, " [%s] %s: qn=%s (want %s) variant_key=%s (want %s) ->%s=%d ->%s=%d\n",+ lang, v->file, found ? qn : "-", want_qn ? want_qn : "?", keyed ? key : "-",+ want_key, v->callee, own, v->other_callee, other);+ ok = 0;+ }+ }+ int listed = cv_count(&p,+ "SELECT COUNT(*) FROM nodes WHERE project=?1 AND name='pick' "+ "AND json_extract(properties,'$.variants') IS NOT NULL",+ NULL, NULL);+ int from_caller = cv_count(&p,+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id "+ "JOIN nodes t ON t.id=e.target_id WHERE e.project=?1 AND "+ "e.type='CALLS' AND s.name='caller' AND t.name='pick'",+ NULL, NULL);+ if (listed != 0 || from_caller < 1) {+ fprintf(stderr, " [%s] pick nodes with variants=%d caller->pick=%d\n", lang, listed,+ from_caller);+ ok = 0;+ }+ cbm_arena_destroy(&arena);+ cv_cleanup(&p);+ return ok;+}++/* C platform files: the same function in foo_win.c and foo_posix.c. */TEST(cv_c_platform_files) {static const cv_file_t files[] = {{"src/plat_win.c", "void alpha(void);\nvoid pick(void) { alpha(); }\n"},{"src/plat_posix.c", "void beta(void);\nvoid pick(void) { beta(); }\n"},{"src/plat.c", "void alpha(void) {}\nvoid beta(void) {}\nvoid pick(void);\n""void caller(void) { pick(); }\n"}};- ASSERT_TRUE(cv_run("c_platform_files", files, 3, "pick", "alpha", "beta", "caller"));+ static const cv_platform_t variants[] = {{"src/plat_win.c", "alpha", "beta"},+ {"src/plat_posix.c", "beta", "alpha"}};+ ASSERT_TRUE(cv_platform_check("c_platform_files", files, 3, variants, 2, "src.plat"));PASS();}@@ -437,13 +547,16 @@ TEST(cv_c_platform_directories) {{"src/win/fs.c", "void beta(void);\nvoid pick(void) { beta(); }\n"},{"src/core.c", "void alpha(void) {}\nvoid beta(void) {}\nvoid pick(void);\n""void caller(void) { pick(); }\n"}};- ASSERT_TRUE(cv_run("c_platform_dirs", files, 3, "pick", "alpha", "beta", "caller"));+ static const cv_platform_t variants[] = {{"src/unix/fs.c", "alpha", "beta"},+ {"src/win/fs.c", "beta", "alpha"}};+ ASSERT_TRUE(cv_platform_check("c_platform_dirs", files, 3, variants, 2, "src.fs"));PASS();}/* Not variants: static functions are file-local even in platform files, and* same-named external functions in files without a platform token may belong- * to different programs. Each keeps its own node. */+ * to different programs. Each keeps its own node, and none carries a+ * variant_key. */TEST(cv_c_non_variants_keep_their_nodes) {static const cv_file_t files[] = {{"src/plat_win.c", "static void pick(void) {}\n"},{"src/plat_posix.c", "static void pick(void) {}\n"},@@ -459,9 +572,14 @@ TEST(cv_c_non_variants_keep_their_nodes) {"SELECT COUNT(DISTINCT qualified_name) FROM nodes WHERE project=?1 ""AND name='helper' AND label='Function'",NULL, NULL);+ int keyed = cv_count(&p,+ "SELECT COUNT(*) FROM nodes WHERE project=?1 "+ "AND json_extract(properties,'$.variant_key') IS NOT NULL",+ NULL, NULL);cv_cleanup(&p);ASSERT_EQ(picks, 2);ASSERT_EQ(helpers, 2);+ ASSERT_EQ(keyed, 0);PASS();}@@ -499,11 +617,13 @@ TEST(cv_no_cross_language_name_binding) {/* ── Incremental: a definition written in several files ───────────────* Closure repair re-parses only the changed files and their recorded- * dependents. `pick` written in two platform files is ONE node; editing- * either file must re-parse the other too, or the repair drops the other- * variant's span and calls (when the edited file holds the node) or keeps- * the edited file's stale ones. After each edit the repaired graph must be- * the graph a full index of the same tree builds. */+ * dependents. `Pick` written in two Go build-tagged files is ONE node;+ * editing either file must re-parse the other too, or the repair drops the+ * other variant's span and calls (when the edited file holds the node) or+ * keeps the edited file's stale ones. C platform files are separate nodes+ * whose shared variant_key comes from each file alone, so the changed file+ * is all a repair re-parses. After each edit the repaired graph must be the+ * graph a full index of the same tree builds. */static bool cv_pipeline(const char *repo, const char *db, cbm_incremental_route_t *route) {cbm_pipeline_t *p = cbm_pipeline_new(repo, db, CBM_MODE_FULL);@@ -520,7 +640,8 @@ static bool cv_pipeline(const char *repo, const char *db, cbm_incremental_route_return ok;}-/* CALLS edges by name plus pick's variant spans, one line each. */+/* CALLS edges by qualified name, every node's variant spans and every node's+ * variant_key, one line each. */static bool cv_signature(const char *db_path, char *out, size_t n) {sqlite3 *db = NULL;if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {@@ -528,10 +649,15 @@ static bool cv_signature(const char *db_path, char *out, size_t n) {return false;}const char *sql =- "SELECT line FROM (SELECT s.name || '>' || t.name AS line FROM edges e "- "JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id WHERE e.type='CALLS' "- "UNION ALL SELECT 'variants ' || json_extract(properties,'$.variants') FROM nodes "- "WHERE name='pick' AND label='Function') ORDER BY line";+ "SELECT line FROM (SELECT s.qualified_name || '>' || t.qualified_name AS line "+ "FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON t.id=e.target_id "+ "WHERE e.type='CALLS' "+ "UNION ALL SELECT 'variants ' || qualified_name || ' ' || "+ "json_extract(properties,'$.variants') FROM nodes "+ "WHERE json_extract(properties,'$.variants') IS NOT NULL "+ "UNION ALL SELECT 'key ' || qualified_name || ' ' || "+ "json_extract(properties,'$.variant_key') FROM nodes "+ "WHERE json_extract(properties,'$.variant_key') IS NOT NULL) ORDER BY line";sqlite3_stmt *stmt = NULL;bool ok = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK;out[0] = '\0';@@ -571,33 +697,79 @@ static bool cv_edit_matches_full(const char *repo, const char *dbdir, const charreturn ok;}+/* Write `files` into a fresh repository and index it once (the base the+ * edits repair). */+static bool cv_incremental_base(char *repo, size_t repo_n, char *dbdir, size_t dbdir_n,+ const cv_file_t *files, int count) {+ snprintf(repo, repo_n, "%s", th_mktempdir("cbm-cvinc"));+ snprintf(dbdir, dbdir_n, "%s", th_mktempdir("cbm-cvdb"));+ bool ok = repo[0] && dbdir[0];+ for (int i = 0; ok && i < count; i++) {+ ok = cv_write(repo, &files[i]);+ }+ char inc_db[600];+ snprintf(inc_db, sizeof(inc_db), "%s/inc.db", dbdir);+ return ok && cv_pipeline(repo, inc_db, NULL);+}+TEST(cv_incremental_variant_partners) {char repo[300];char dbdir[300];- snprintf(repo, sizeof(repo), "%s", th_mktempdir("cbm-cvinc"));- snprintf(dbdir, sizeof(dbdir), "%s", th_mktempdir("cbm-cvdb"));- ASSERT_TRUE(repo[0] && dbdir[0]);+ static const cv_file_t files[] = {+ {"go.mod", "module example.com/cvinc\n\ngo 1.22\n"},+ {"pick/util.go", "package pick\n\nfunc alpha() {}\nfunc beta() {}\nfunc gammaFn() {}\n"},+ {"pick/pick_linux.go", "//go:build linux\npackage pick\n\nfunc Pick() { alpha() }\n"},+ {"pick/pick_windows.go", "//go:build windows\npackage pick\n\nfunc Pick() { beta() }\n"},+ {"pick/caller.go", "package pick\n\nfunc Caller() { Pick() }\n"},+ };+ ASSERT_TRUE(cv_incremental_base(repo, sizeof(repo), dbdir, sizeof(dbdir), files,+ (int)(sizeof(files) / sizeof(files[0]))));+ static char sig[8192];+ /* Edit each variant file in turn (one of them holds the node). */+ ASSERT_TRUE(cv_edit_matches_full(+ repo, dbdir, "pick/pick_linux.go",+ "//go:build linux\npackage pick\n\nfunc Pick() { alpha(); gammaFn() }\n", sig,+ sizeof(sig)));+ ASSERT_TRUE(strstr(sig, "cvinc.pick.Pick>cvinc.pick.beta\n") &&+ strstr(sig, "cvinc.pick.Pick>cvinc.pick.gammaFn\n"));+ ASSERT_TRUE(cv_edit_matches_full(repo, dbdir, "pick/pick_windows.go",+ "//go:build windows\npackage pick\n\nfunc Pick() {\n"+ "\tbeta()\n}\n",+ sig, sizeof(sig)));+ ASSERT_TRUE(strstr(sig, "cvinc.pick.Pick>cvinc.pick.alpha\n") &&+ strstr(sig, "cvinc.pick.Pick>cvinc.pick.beta\n"));+ th_rmtree(repo);+ th_rmtree(dbdir);+ PASS();+}++/* C platform files: each variant keeps its own node, calls and variant_key+ * through a repair of either file. */+TEST(cv_incremental_platform_files) {+ char repo[300];+ char dbdir[300];static const cv_file_t files[] = {{"util.c", "void alpha(void) {}\nvoid beta(void) {}\nvoid gamma_fn(void) {}\n"},{"pick_linux.c", "void pick(void) { alpha(); }\n"},{"pick_win.c", "void pick(void) { beta(); }\n"},{"caller.c", "void caller(void) { pick(); }\n"},};- for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {- ASSERT_TRUE(cv_write(repo, &files[i]));- }- char inc_db[600];- snprintf(inc_db, sizeof(inc_db), "%s/inc.db", dbdir);- ASSERT_TRUE(cv_pipeline(repo, inc_db, NULL));+ ASSERT_TRUE(cv_incremental_base(repo, sizeof(repo), dbdir, sizeof(dbdir), files,+ (int)(sizeof(files) / sizeof(files[0]))));static char sig[8192];- /* Edit each variant file in turn (one of them holds the node). */ASSERT_TRUE(cv_edit_matches_full(repo, dbdir, "pick_linux.c","void pick(void) { alpha(); gamma_fn(); }\n", sig,sizeof(sig)));- ASSERT_TRUE(strstr(sig, "pick>beta\n") && strstr(sig, "pick>gamma_fn\n"));+ ASSERT_TRUE(strstr(sig, "cvinc.pick_linux.pick>cvinc.util.gamma_fn\n") &&+ strstr(sig, "cvinc.pick_win.pick>cvinc.util.beta\n") &&+ strstr(sig, "key cvinc.pick_linux.pick cvinc.pick.pick\n") &&+ strstr(sig, "key cvinc.pick_win.pick cvinc.pick.pick\n"));ASSERT_TRUE(cv_edit_matches_full(repo, dbdir, "pick_win.c", "void pick(void) {\n beta();\n}\n",sig, sizeof(sig)));- ASSERT_TRUE(strstr(sig, "pick>alpha\n") && strstr(sig, "pick>beta\n"));+ ASSERT_TRUE(strstr(sig, "cvinc.pick_linux.pick>cvinc.util.alpha\n") &&+ strstr(sig, "cvinc.pick_win.pick>cvinc.util.beta\n") &&+ strstr(sig, "key cvinc.pick_linux.pick cvinc.pick.pick\n") &&+ strstr(sig, "key cvinc.pick_win.pick cvinc.pick.pick\n"));th_rmtree(repo);th_rmtree(dbdir);PASS();@@ -632,4 +804,5 @@ SUITE(conditional_variants) {RUN_TEST(cv_c_non_variants_keep_their_nodes);RUN_TEST(cv_no_cross_language_name_binding);RUN_TEST(cv_incremental_variant_partners);+ RUN_TEST(cv_incremental_platform_files);}diff --git a/tests/test_store_impact.c b/tests/test_store_impact.cindex 0c1224fc6..2fa306859 100644--- a/tests/test_store_impact.c+++ b/tests/test_store_impact.c@@ -321,6 +321,63 @@ TEST(impact_walk_crosses_a_route_only_when_asked) {PASS();}+static int64_t add_keyed(cbm_store_t *s, const char *name, const char *key) {+ char qn[64];+ char props[128];+ snprintf(qn, sizeof(qn), "test.%s", name);+ snprintf(props, sizeof(props), "{\"variant_key\":\"%s\"}", key);+ cbm_node_t node = {.project = "test",+ .label = "Function",+ .name = name,+ .qualified_name = qn,+ .properties_json = props};+ return cbm_store_upsert_node(s, &node);+}++TEST(impact_walk_crosses_platform_variants_only_when_asked) {+ /* pick_win and pick_posix are the platform variants of one function (one+ * variant_key); caller binds to pick_posix alone. A change to pick_win+ * concerns caller. A key on one node relates nothing. */+ cbm_store_t *s = new_store();+ int64_t win = add_keyed(s, "pick_win", "test.pick");+ int64_t posix = add_keyed(s, "pick_posix", "test.pick");+ int64_t lone = add_keyed(s, "lone_win", "test.lone");+ int64_t caller = add_node(s, "Function", "caller");+ int64_t lone_caller = add_node(s, "Function", "lone_caller");+ add_edge(s, caller, posix, "CALLS");+ add_edge(s, lone_caller, lone, "CALLS");+ cbm_impact_walk_t *w = open_walk(s, CALLS_ONLY, 1, false);+ ASSERT_EQ(cbm_impact_walk_run(w, &win, 1), CBM_STORE_OK);+ ASSERT_EQ(cbm_impact_walk_count(w), 1);+ cbm_impact_walk_close(w);++ cbm_impact_policy_t variants = {.project = "test",+ .edge_types = CALLS_ONLY,+ .edge_type_count = 1,+ .follow_variant_keys = true};+ ASSERT_EQ(cbm_impact_walk_open(s, &variants, &w), CBM_STORE_OK);+ ASSERT_EQ(cbm_impact_walk_run(w, &win, 1), CBM_STORE_OK);+ ASSERT_EQ(cbm_impact_walk_count(w), 3);+ ASSERT_EQ(hit_of(w, posix)->hop, 1);+ ASSERT_EQ(hit_of(w, posix)->via_id, win);+ ASSERT_STR_EQ(hit_of(w, posix)->via_edge, "variant_key");+ ASSERT_EQ(hit_of(w, caller)->hop, 2);+ ASSERT_STR_EQ(hit_of(w, caller)->via_edge, "CALLS");+ cbm_impact_walk_close(w);++ /* The other direction, and a singleton key. */+ ASSERT_EQ(cbm_impact_walk_open(s, &variants, &w), CBM_STORE_OK);+ int64_t seeds[] = {posix, lone};+ ASSERT_EQ(cbm_impact_walk_run(w, seeds, 2), CBM_STORE_OK);+ ASSERT_EQ(cbm_impact_walk_count(w), 5);+ ASSERT_EQ(hit_of(w, win)->hop, 1);+ ASSERT_EQ(hit_of(w, caller)->hop, 1);+ ASSERT_EQ(hit_of(w, lone_caller)->hop, 1);+ cbm_impact_walk_close(w);+ cbm_store_close(s);+ PASS();+}+TEST(impact_walk_rejects_what_it_cannot_answer) {cbm_store_t *s = new_store();int64_t seed = add_node(s, "Function", "seed");@@ -359,5 +416,6 @@ SUITE(store_impact) {RUN_TEST(impact_walk_reaches_a_sink_and_stops_there);RUN_TEST(impact_walk_second_run_continues_from_the_first);RUN_TEST(impact_walk_crosses_a_route_only_when_asked);+ RUN_TEST(impact_walk_crosses_platform_variants_only_when_asked);RUN_TEST(impact_walk_rejects_what_it_cannot_answer);}diff --git a/tests/test_test_impact_engine.c b/tests/test_test_impact_engine.cindex 535b4aa20..ea50101da 100644--- a/tests/test_test_impact_engine.c+++ b/tests/test_test_impact_engine.c@@ -523,6 +523,51 @@ TEST(test_impact_engine_variant_tests_are_one_test) {PASS();}+/* Platform variants of one function (pick_posix.c / pick_win.c) are separate+ * graph nodes sharing a variant_key, and the test's call binds to one of+ * them. A change confined to EITHER variant reaches the test, whichever+ * variant the call bound to: the other platform's build runs the other. */+static const char tie_pick_posix[] = "int pick(void) {\n"+ " return 1;\n"+ "}\n";+static const char tie_pick_win[] = "int pick(void) {\n"+ " return 2;\n"+ "}\n";+static const char tie_pick_beta[] = "int pick(void);\n"+ "TEST(beta_picks) {\n"+ " return pick() > 0 ? 0 : 1;\n"+ "}\n"+ "SUITE(beta) {\n"+ " RUN_TEST(beta_picks);\n"+ "}\n";++TEST(test_impact_engine_platform_variants_reach_each_others_callers) {+ const char *const extra[] = {"src/pick_posix.c", tie_pick_posix, /* variant A */+ "src/pick_win.c", tie_pick_win, /* variant B */+ "tests/test_beta.c", tie_pick_beta};+ static const char *const changes[][2] = {+ {"src/pick_win.c", "int pick(void) {\n return 2 + 0;\n}\n"},+ {"src/pick_posix.c", "int pick(void) {\n return 1 + 0;\n}\n"}};+ const char *reset[] = {"reset", "--quiet", "--hard", "main", NULL};+ tie_fixture_t fx;+ ASSERT_TRUE(tie_open_files(&fx, extra, 3));+ for (int i = 0; i < 2; i++) {+ ASSERT_TRUE(tie_git(&fx, reset));+ ASSERT_TRUE(tie_write(&fx, changes[i][0], changes[i][1]));+ ASSERT_TRUE(tie_commit(&fx, "change one platform variant"));+ char *json = tie_answer(&fx);+ ASSERT_NOT_NULL(json);+ if (!tie_suite_has(json, "beta", "STATIC")) {+ printf(" %s changed: %.900s\n", changes[i][0], json);+ }+ ASSERT_TRUE(tie_suite_has(json, "beta", "STATIC"));+ ASSERT_FALSE(tie_suite_has(json, "alpha", "STATIC"));+ free(json);+ }+ tie_close(&fx);+ PASS();+}+/* A test that starts a program runs code no CALLS edge leads to; the program* runs main, and main reaches everything. A change that reaches the product's* main reaches the tests that spawn; one that does not, does not. */@@ -572,6 +617,7 @@ TEST(test_impact_engine_spawning_tests_follow_main) {SUITE(test_impact_engine) {RUN_TEST(test_impact_engine_variant_tests_are_one_test);+ RUN_TEST(test_impact_engine_platform_variants_reach_each_others_callers);RUN_TEST(test_impact_engine_spawning_tests_follow_main);RUN_TEST(test_impact_engine_reached_suites_carry_static);RUN_TEST(test_impact_engine_macro_registered_tests_select_their_suite);diff --git a/tests/test_test_impact_seed.c b/tests/test_test_impact_seed.cindex 9ac19dc29..b22f6d3cf 100644--- a/tests/test_test_impact_seed.c+++ b/tests/test_test_impact_seed.c@@ -344,7 +344,8 @@ TEST(test_impact_seed_unclassified_header_lines_escalate_the_file) {/* One definition with variants (graph_buffer.c "Definition variants") is one* node holding one variant's span; the others are in its `variants`* property, in the same file (#if branches) or in another file that DEFINES- * it too (platform files). A hunk in ANY variant seeds that node. */+ * it too (Go build-tagged files; the cross-file node is built by hand here).+ * A hunk in ANY variant seeds that node. */TEST(test_impact_seed_variant_spans_seed_their_definition) {static const char pick_src[] = "#ifdef _WIN32\nvoid pick(void) {\n beta();\n}\n#else\n""void pick(void) {\n alpha();\n}\n#endif\n";