Skip to content

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.

Merge main into fix/c-typedefs-macros-enumerators for the release batch

Martin Vogeld050fcae35e6
diff --cc internal/cbm/extract_defs.c
index 281b38359,671a81651..3bd1ecc51
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@@ -3,8 -3,9 +3,10 @@@
#include "helpers.h"
#include "lang_specs.h"
#include "foundation/constants.h"
+#include "foundation/hash_table.h"
- #include "foundation/log.h" // cbm_log_error
+ #include "foundation/log.h" // cbm_log_error
+ #include "discover/test_conventions.h"
+ #include "foundation/sha256.h"
#include "foundation/mem_core.h" // cbm_realloc/cbm_free -- walk_defs stack
#include "extract_node_stack.h"
#include "simhash/minhash.h"
diff --cc internal/cbm/lsp/c_lsp.c
index 73d1d4098,29fd12284..68c44fbe4
--- a/internal/cbm/lsp/c_lsp.c
+++ b/internal/cbm/lsp/c_lsp.c
@@@ -4956,48 -5404,60 +5426,62 @@@ static void c_process_function(CLSPCont
break;
}
- if (!func_name || !func_name[0])
+ // A keyword "name" is an error-recovery artifact (see cbm_c_reserved_func_name):
+ // the def extractor mints no node for it, so no caller QN may name it either.
+ if (!func_name || !func_name[0] || cbm_c_reserved_func_name(func_name))
return;
- // Build enclosing function QN
- const char *func_qn = c_build_qn(ctx, func_name);
- // For a method defined INLINE inside its class body, func_name is a bare
- // identifier ("compute") and enclosing_class_qn was inherited from
- // c_process_class (saved_class_qn == enclosing_class_qn). The textual
- // extractor and the registry qualify the method as module.Class.method, so
- // building func_qn as module.method here (no class) made the LSP-resolved
- // call's caller_qn disagree with the textual call's enclosing_func_qn and
- // cbm_pipeline_find_lsp_resolution never joined them — every in-method call
- // (e.g. lsp_implicit_this) silently lost its type-aware strategy. Prepend
- // the enclosing class, mirroring the Go receiver-QN fix. Out-of-line
- // definitions (Widget::compute) already carry the class in func_name (a
- // qualified_identifier), so c_build_qn produces module.Class.method and the
- // enclosing_class_qn was set HERE (saved_class_qn != enclosing_class_qn);
- // skip those, and skip names that already contain the class scope.
- if (ctx->enclosing_class_qn && saved_class_qn == ctx->enclosing_class_qn &&
- !strchr(func_qn, '.')) {
- func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->enclosing_class_qn, func_qn);
- } else if (ctx->enclosing_class_qn && saved_class_qn != ctx->enclosing_class_qn &&
- strchr(func_qn, '.')) {
- /* Out-of-line method `Class::method`: c_build_qn yields the bare
- * "Class.method" (no module) — the class scope was resolved HERE to the
- * full module-qualified class QN (saved_class_qn != enclosing_class_qn).
- * Rebuild as <class QN>.<method short name> so the caller_qn matches the
- * def walk and call-scope QN, which qualify out-of-line methods the same
- * way. Without this the caller_qn stays "Class.method", the exact-equality
- * lsp_resolve join misses, and the LSP rescue is discarded (gap #5a). */
- const char *dot = strrchr(func_qn, '.');
- func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->enclosing_class_qn, dot + 1);
- } else if (!strchr(func_qn, '.')) {
- /* A free function in a namespace is qualified by the namespace scope
- * (current_namespace is module_qn.ns), matching the def QN the extractor
- * now produces; outside any namespace this falls back to the file module
- * so non-namespaced free functions are unchanged. */
- const char *scope = ctx->current_namespace ? ctx->current_namespace : ctx->module_qn;
- if (scope) {
- func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", scope, func_qn);
+ const char *func_qn = cbm_test_definition_owner_qn(ctx->test_definition_owners, func_node);
+ if (func_qn) {
+ ctx->test_owner_seen_count++;
+ if (ctx->arena != &ctx->test_definition_owners->arena) {
+ func_qn = cbm_arena_strdup(ctx->arena, func_qn);
+ if (!func_qn) {
+ ctx->test_owner_error = true;
+ ctx->enclosing_class_qn = saved_class_qn;
+ return;
+ }
+ }
+ }
+ if (!func_qn) {
+ // Build enclosing function QN
+ func_qn = c_build_qn(ctx, func_name);
+ // For a method defined INLINE inside its class body, func_name is a bare
+ // identifier ("compute") and enclosing_class_qn was inherited from
+ // c_process_class (saved_class_qn == enclosing_class_qn). The textual
+ // extractor and the registry qualify the method as module.Class.method, so
+ // building func_qn as module.method here (no class) made the LSP-resolved
+ // call's caller_qn disagree with the textual call's enclosing_func_qn and
+ // cbm_pipeline_find_lsp_resolution never joined them — every in-method call
+ // (e.g. lsp_implicit_this) silently lost its type-aware strategy. Prepend
+ // the enclosing class, mirroring the Go receiver-QN fix. Out-of-line
+ // definitions (Widget::compute) already carry the class in func_name (a
+ // qualified_identifier), so c_build_qn produces module.Class.method and the
+ // enclosing_class_qn was set HERE (saved_class_qn != enclosing_class_qn);
+ // skip those, and skip names that already contain the class scope.
+ if (ctx->enclosing_class_qn && saved_class_qn == ctx->enclosing_class_qn &&
+ !strchr(func_qn, '.')) {
+ func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->enclosing_class_qn, func_qn);
+ } else if (ctx->enclosing_class_qn && saved_class_qn != ctx->enclosing_class_qn &&
+ strchr(func_qn, '.')) {
+ /* Out-of-line method `Class::method`: c_build_qn yields the bare
+ * "Class.method" (no module) — the class scope was resolved HERE to the
+ * full module-qualified class QN (saved_class_qn != enclosing_class_qn).
+ * Rebuild as <class QN>.<method short name> so the caller_qn matches the
+ * def walk and call-scope QN, which qualify out-of-line methods the same
+ * way. Without this the caller_qn stays "Class.method", the exact-equality
+ * lsp_resolve join misses, and the LSP rescue is discarded (gap #5a). */
+ const char *dot = strrchr(func_qn, '.');
+ func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->enclosing_class_qn, dot + 1);
+ } else if (!strchr(func_qn, '.')) {
+ /* A free function in a namespace is qualified by the namespace scope
+ * (current_namespace is module_qn.ns), matching the def QN the extractor
+ * now produces; outside any namespace this falls back to the file module
+ * so non-namespaced free functions are unchanged. */
+ const char *scope = ctx->current_namespace ? ctx->current_namespace : ctx->module_qn;
+ if (scope) {
+ func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", scope, func_qn);
+ }
}
}
ctx->enclosing_func_qn = func_qn;
diff --cc src/graph_buffer/graph_buffer.c
index 5484e1a4f,51af15e8a..7f44a2d19
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@@ -835,22 -835,164 +835,178 @@@ void cbm_gbuf_set_next_id(cbm_gbuf_t *g
gb->next_id = next_id;
}
+ /* ── Definition variants ─────────────────────────────────────────────
+ * One name defined more than once under mutually exclusive conditions —
+ * C-family #if/#else, Rust #[cfg], build-tagged files, a definition under an
+ * `if` — is ONE identity: one QN, one node, whose edges are the union of
+ * every variant's body. The collision rule below keeps one canonical winner
+ * for name, file, lines and properties; every variant's span survives in the
+ * node's `variants` property ([{"file_path","start_line","end_line"}],
+ * 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. */
+ static void *gb_yy_malloc(void *ctx, size_t size) {
+ (void)ctx;
+ return cbm_alloc(CBM_MEM_CLASS_GBUF_STRING, size);
+ }
+
+ static void *gb_yy_realloc(void *ctx, void *block, size_t old_size, size_t size) {
+ (void)ctx;
+ (void)old_size;
+ return cbm_realloc(CBM_MEM_CLASS_GBUF_STRING, block, size);
+ }
+
+ static void gb_yy_free(void *ctx, void *block) {
+ (void)ctx;
+ cbm_free(CBM_MEM_CLASS_GBUF_STRING, block);
+ }
+
+ static const yyjson_alc gb_yy_alc = {gb_yy_malloc, gb_yy_realloc, gb_yy_free, NULL};
+
+ enum { GB_VARIANTS_MAX = 64 };
+
+ typedef struct {
+ const char *file;
+ int start;
+ int end;
+ } gb_span_t;
+
+ static bool gb_variant_label(const char *label) {
+ static const char *const labels[] = {"Function", "Method", "Class", "Interface", "Enum",
+ "Struct", "Trait", "Type", "Macro"};
+ for (size_t i = 0; label && i < sizeof(labels) / sizeof(labels[0]); i++) {
+ if (strcmp(label, labels[i]) == 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ static int gb_span_cmp(const void *a, const void *b) {
+ const gb_span_t *x = a;
+ const gb_span_t *y = b;
+ int c = strcmp(x->file, y->file);
+ if (c == 0) {
+ c = (x->start > y->start) - (x->start < y->start);
+ }
+ if (c == 0) {
+ c = (x->end > y->end) - (x->end < y->end);
+ }
+ return c;
+ }
+
+ /* A definition's spans: its `variants` property, else its own span. */
+ static int gb_spans_of(yyjson_doc *doc, const char *file, int start, int end, gb_span_t *out,
+ int n) {
+ yyjson_val *list = doc ? yyjson_obj_get(yyjson_doc_get_root(doc), "variants") : NULL;
+ if (yyjson_is_arr(list) && yyjson_arr_size(list) > 0) {
+ size_t i;
+ size_t max;
+ yyjson_val *v;
+ yyjson_arr_foreach(list, i, max, v) {
+ const char *f = yyjson_get_str(yyjson_obj_get(v, "file_path"));
+ int s = (int)yyjson_get_int(yyjson_obj_get(v, "start_line"));
+ int e = (int)yyjson_get_int(yyjson_obj_get(v, "end_line"));
+ if (f && s > 0 && n < GB_VARIANTS_MAX) {
+ out[n++] = (gb_span_t){f, s, e};
+ }
+ }
+ return n;
+ }
+ if (start > 0 && n < GB_VARIANTS_MAX) {
+ out[n++] = (gb_span_t){file ? file : "", start, end};
+ }
+ return n;
+ }
+
+ /* The survivor's properties with the union of both definitions' variant
+ * spans. Returns a GBUF_STRING-owned JSON string, or NULL when there is
+ * nothing to record (fewer than two distinct spans) or on any failure — the
+ * survivor then keeps its own properties: a lost variants list costs
+ * precision, never data. */
+ static char *gb_variants_props(const char *survivor_props, const char *a_props, const char *a_file,
+ int a_start, int a_end, const char *b_props, const char *b_file,
+ int b_start, int b_end) {
+ if (a_start <= 0 || b_start <= 0) {
+ return NULL;
+ }
+ yyjson_doc *a_doc =
+ a_props ? yyjson_read_opts((char *)a_props, strlen(a_props), 0, &gb_yy_alc, NULL) : NULL;
+ yyjson_doc *b_doc =
+ b_props ? yyjson_read_opts((char *)b_props, strlen(b_props), 0, &gb_yy_alc, NULL) : NULL;
+ gb_span_t spans[GB_VARIANTS_MAX * 2];
+ int n = gb_spans_of(a_doc, a_file, a_start, a_end, spans, 0);
+ n = gb_spans_of(b_doc, b_file, b_start, b_end, spans, n);
+ qsort(spans, (size_t)n, sizeof(spans[0]), gb_span_cmp);
+ int unique = 0;
+ for (int i = 0; i < n; i++) {
+ if (unique == 0 || gb_span_cmp(&spans[unique - 1], &spans[i]) != 0) {
+ spans[unique++] = spans[i];
+ }
+ }
+ char *out = NULL;
+ if (unique >= 2) {
+ yyjson_mut_doc *mut = yyjson_mut_doc_new(&gb_yy_alc);
+ yyjson_doc *s_doc = survivor_props
+ ? yyjson_read_opts((char *)survivor_props, strlen(survivor_props),
+ 0, &gb_yy_alc, NULL)
+ : NULL;
+ yyjson_mut_val *root = s_doc && yyjson_is_obj(yyjson_doc_get_root(s_doc))
+ ? yyjson_val_mut_copy(mut, yyjson_doc_get_root(s_doc))
+ : yyjson_mut_obj(mut);
+ yyjson_mut_val *list = yyjson_mut_arr(mut);
+ for (int i = 0; root && list && i < unique; i++) {
+ yyjson_mut_val *v = yyjson_mut_arr_add_obj(mut, list);
+ yyjson_mut_obj_add_strcpy(mut, v, "file_path", spans[i].file);
+ yyjson_mut_obj_add_int(mut, v, "start_line", spans[i].start);
+ yyjson_mut_obj_add_int(mut, v, "end_line", spans[i].end);
+ }
+ if (mut && root && list) {
+ yyjson_mut_obj_remove_key(root, "variants");
+ yyjson_mut_obj_add_val(mut, root, "variants", list);
+ yyjson_mut_doc_set_root(mut, root);
+ out = yyjson_mut_write_opts(mut, 0, &gb_yy_alc, NULL, NULL);
+ }
+ yyjson_doc_free(s_doc);
+ yyjson_mut_doc_free(mut);
+ }
+ yyjson_doc_free(a_doc);
+ yyjson_doc_free(b_doc);
+ return out;
+ }
+
+ /* Record the variant spans of a same-QN collision on the surviving node. */
+ static void gb_record_variants(cbm_gbuf_node_t *survivor, const char *a_label, const char *a_props,
+ const char *a_file, int a_start, int a_end, const char *b_label,
+ const char *b_props, const char *b_file, int b_start, int b_end) {
+ if (!gb_variant_label(a_label) || !b_label || strcmp(a_label, b_label) != 0) {
+ return;
+ }
+ char *merged = gb_variants_props(survivor->properties_json, a_props, a_file, a_start, a_end,
+ b_props, b_file, b_start, b_end);
+ if (merged) {
+ cbm_free(CBM_MEM_CLASS_GBUF_STRING, survivor->properties_json);
+ survivor->properties_json = merged;
+ }
+ }
+
/* ── Node operations ─────────────────────────────────────────────── */
+/* First key of the canonical same-QN collision order: how strong a claim the
+ * entity has on a qualified name. A macro is the weakest: when a "Macro" and
+ * a definition share a QN, the definition, which carries the body, the
+ * signature and every edge, owns it whichever comes first.
+ *
+ * C-preprocessor macros no longer get here: their QN carries its own
+ * namespace (`...#macro`, CBM_MACRO_QN_SUFFIX) and cannot equal a definition's.
+ * The case left is Chialisp, whose `defmacro` / `defmac` defs are labelled
+ * "Macro" under a plain QN: a macro and a `defun` of one name in one file, or
+ * in a same-stem `.clsp` / `.clib` pair (one module QN). */
+static int gb_qn_claim_rank(const char *label) {
+ return (label && strcmp(label, "Macro") == 0) ? 0 : 1;
+}
+
int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name,
const char *qualified_name, const char *file_path, int start_line,
int end_line, const char *properties_json) {
diff --cc src/pipeline/registry.c
index d07d2ccf7,a970039e3..6860784fb
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@@ -968,11 -1204,9 +1204,11 @@@ void cbm_registry_add_lang(cbm_registry
const char *derived = simple_name(qualified_name);
const char *primary =
name && cbm_qn_callable_base_len_named(owned_qn, name) < strlen(owned_qn) ? name : derived;
- index_under_name(r, primary, owned_qn);
- /* '#' is a QN fence, and extract_defs.c's rust_cfg_qualified_name is the
- * only thing in the tree that mints one on a REGISTERED symbol today (a C
- * macro's QN ends in "#macro", but Macro is no registry label, so it never
- * gets here). A grammar that starts
+ index_under_name(r, primary, owned_qn, lang);
- /* '#' is a QN fence; no extractor mints one for a definition today (Rust
- * cfg twins are variants of one plain QN). A grammar that starts
++ /* '#' is a QN fence; no extractor mints one for a REGISTERED definition
++ * today (Rust cfg twins are variants of one plain QN; a C macro's QN ends in
++ * "#macro", but Macro is no registry label, so it never gets here). A
++ * grammar that starts
* minting a '#' opts into this second key by doing so, whatever it means by
* the fence: its symbols become reachable under the passed name as well,
* and they share that name's bucket with everything else filed under it.