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.

fix(extract): callable-source Lisp/Elixir in-body calls; correct starlark fixture

Martin Vogeld63ba9f66d13
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 676053439..1a454ba7a 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -87,6 +87,102 @@ static const char *compute_wolfram_func_qn(CBMExtractCtx *ctx, TSNode node) {
return NULL;
}
+/* True for a Lisp def-form head symbol (defn/define/...). Mirrors
+ * lisp_is_def_head() in extract_defs.c so the scope-stack walk pushes a
+ * SCOPE_FUNC only for actual definitions, never for a plain call list such as
+ * `(add x 1)` — otherwise every parenthesized form would shadow the enclosing
+ * def and the in-body call would mis-source. */
+static bool lisp_head_is_def(const char *t) {
+ if (!t) {
+ return false;
+ }
+ static const char *heads[] = {
+ "defn", "defn-", "def",
+ "defmacro", "defmulti", "defmethod",
+ "defprotocol", "defrecord", "deftype",
+ "definterface", "defonce", "define",
+ "define-syntax", "define-values", "define-syntax-rule",
+ "define-struct", "define-record-type", "define/contract",
+ "struct", NULL};
+ for (int i = 0; heads[i]; i++) {
+ if (strcmp(t, heads[i]) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* Resolve a Lisp (Clojure/Scheme/Racket) def-form's QN for scope tracking.
+ * The def node is a list/list_lit whose head names the def kind and whose
+ * second element is the name (a bare symbol) or a (name args...) nested list.
+ * Returns NULL for any non-def list (calls, vectors of args, the +/- body
+ * forms, ...), so push_boundary_scopes pushes no scope for them. Mirrors
+ * extract_lisp_def() in extract_defs.c. */
+static const char *compute_lisp_func_qn(CBMExtractCtx *ctx, TSNode node) {
+ if (ts_node_named_child_count(node) < 2) {
+ return NULL;
+ }
+ char *head = cbm_node_text(ctx->arena, ts_node_named_child(node, 0), ctx->source);
+ if (!lisp_head_is_def(head)) {
+ return NULL;
+ }
+ TSNode target = ts_node_named_child(node, 1);
+ const char *tk = ts_node_type(target);
+ TSNode name_node = target;
+ /* (define (foo args) ...) — the name is the head symbol of the nested list. */
+ if ((strcmp(tk, "list") == 0 || strcmp(tk, "list_lit") == 0) &&
+ ts_node_named_child_count(target) > 0) {
+ name_node = ts_node_named_child(target, 0);
+ }
+ if (ts_node_is_null(name_node)) {
+ return NULL;
+ }
+ char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
+ if (!name || !name[0]) {
+ return NULL;
+ }
+ return cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, name);
+}
+
+/* Resolve an Elixir def/defp/defmacro's QN for scope tracking. The def is a
+ * `call` node whose target (first child) is the def macro and whose first
+ * argument is either the function head call `name(args)` or a bare identifier
+ * (zero-arg). Returns NULL for a non-def `call` (e.g. the in-body `add(x,1)`
+ * call, whose target is not a def macro) so only defs push a scope. Mirrors
+ * extract_elixir_func_def() in extract_defs.c. */
+static const char *compute_elixir_func_qn(CBMExtractCtx *ctx, TSNode node) {
+ if (ts_node_child_count(node) == 0) {
+ return NULL;
+ }
+ char *macro = cbm_node_text(ctx->arena, ts_node_child(node, 0), ctx->source);
+ if (!macro || (strcmp(macro, "def") != 0 && strcmp(macro, "defp") != 0 &&
+ strcmp(macro, "defmacro") != 0)) {
+ return NULL;
+ }
+ TSNode args = ts_node_child_by_field_name(node, TS_FIELD("arguments"));
+ if (ts_node_is_null(args) && ts_node_child_count(node) > 1) {
+ args = ts_node_child(node, 1);
+ }
+ if (ts_node_is_null(args) || ts_node_child_count(args) == 0) {
+ return NULL;
+ }
+ TSNode first_arg = ts_node_child(args, 0);
+ if (ts_node_is_null(first_arg)) {
+ return NULL;
+ }
+ const char *fk = ts_node_type(first_arg);
+ char *name = NULL;
+ if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {
+ name = cbm_node_text(ctx->arena, ts_node_child(first_arg, 0), ctx->source);
+ } else if (strcmp(fk, "identifier") == 0) {
+ name = cbm_node_text(ctx->arena, first_arg, ctx->source);
+ }
+ if (!name || !name[0]) {
+ return NULL;
+ }
+ return cbm_fqn_compute(ctx->arena, ctx->project, ctx->rel_path, name);
+}
+
// Compute function QN for scope tracking (mirrors cbm_enclosing_func_qn logic).
static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
WalkState *state) {
@@ -95,6 +191,22 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
return compute_wolfram_func_qn(ctx, node);
}
+ /* Lisp family (Clojure/Scheme/Racket): the def node is a list/list_lit, a
+ * very general kind that also matches plain call forms. The shared resolver
+ * has no source pointer to read the head symbol, so the def-vs-call gate
+ * lives here (we have ctx->source). Non-def lists return NULL → no scope
+ * pushed → the in-body call sources to the enclosing def, not the Module. */
+ if (ctx->language == CBM_LANG_CLOJURE || ctx->language == CBM_LANG_SCHEME ||
+ ctx->language == CBM_LANG_RACKET) {
+ return compute_lisp_func_qn(ctx, node);
+ }
+
+ /* Elixir: def/defp/defmacro are `call` nodes (so is every in-body call).
+ * Gate on the def-macro target text so only definitions push a scope. */
+ if (ctx->language == CBM_LANG_ELIXIR) {
+ return compute_elixir_func_qn(ctx, node);
+ }
+
/* Resolve the function name via the single shared resolver (extract_defs) so
* call-scope attribution agrees with definition extraction across all ~130
* grammars. The old private 4-case copy returned NULL for Fortran subroutine,
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index 10b93c728..b6d50339b 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -694,6 +694,10 @@ static const char *r_env_funcs[] = {"Sys.getenv", NULL};
static const char *perl_env_funcs[] = {"$ENV", NULL};
// ==================== CLOJURE ====================
+/* Clojure def-forms (defn/def/...) are `list_lit` nodes; gating the actual
+ * def-vs-call distinction happens in cbm_resolve_func_name (returns NULL for a
+ * non-def list_lit such as a call), so non-def lists never push a SCOPE_FUNC. */
+static const char *clojure_func_types[] = {"list_lit", NULL};
static const char *clojure_module_types[] = {"source", NULL};
static const char *clojure_call_types[] = {"list_lit", NULL};
@@ -1056,6 +1060,9 @@ static const char *pascal_assign_types[] = {"assignment", NULL};
static const char *pascal_throw_types[] = {"raise", NULL};
static const char *pascal_module_types[] = {"source_file", NULL};
static const char *d_module_types[] = {"source_file", NULL};
+/* Scheme def-forms (`(define (f ..) ..)`) are `list` nodes; the def-vs-call
+ * gate is in cbm_resolve_func_name (returns NULL for a non-def list). */
+static const char *scheme_func_types[] = {"list", NULL};
static const char *scheme_call_types[] = {"list", NULL};
static const char *scheme_var_types[] = {"symbol", NULL};
static const char *scheme_module_types[] = {"program", NULL};
@@ -1125,6 +1132,9 @@ static const char *agda_branch_types[] = {"lambda", "match", "do", NULL};
static const char *agda_var_types[] = {"typed_binding", NULL};
static const char *agda_module_types[] = {"source_file", NULL};
static const char *racket_class_types[] = {"structure", NULL};
+/* Racket def-forms (`(define (f ..) ..)`) are `list` nodes; the def-vs-call
+ * gate is in cbm_resolve_func_name (returns NULL for a non-def list). */
+static const char *racket_func_types[] = {"list", NULL};
static const char *racket_call_types[] = {"list", NULL};
static const char *racket_var_types[] = {"symbol", NULL};
static const char *racket_module_types[] = {"program", NULL};
@@ -1794,7 +1804,7 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
empty_types, NULL, NULL, tree_sitter_dockerfile, NULL},
// CBM_LANG_CLOJURE
- [CBM_LANG_CLOJURE] = {CBM_LANG_CLOJURE, empty_types, empty_types, empty_types,
+ [CBM_LANG_CLOJURE] = {CBM_LANG_CLOJURE, clojure_func_types, empty_types, empty_types,
clojure_module_types, clojure_call_types, empty_types, empty_types,
empty_types, empty_types, empty_types, empty_types, NULL, empty_types,
NULL, NULL, tree_sitter_clojure, NULL},
@@ -2037,7 +2047,7 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
NULL},
// CBM_LANG_SCHEME
- [CBM_LANG_SCHEME] = {CBM_LANG_SCHEME, empty_types, empty_types, empty_types,
+ [CBM_LANG_SCHEME] = {CBM_LANG_SCHEME, scheme_func_types, empty_types, empty_types,
scheme_module_types, scheme_call_types, empty_types, empty_types,
empty_types, scheme_var_types, empty_types, empty_types, NULL, empty_types,
NULL, NULL, tree_sitter_scheme, NULL},
@@ -2085,7 +2095,7 @@ static const CBMLangSpec lang_specs[CBM_LANG_COUNT] = {
empty_types, NULL, NULL, tree_sitter_agda, NULL},
// CBM_LANG_RACKET
- [CBM_LANG_RACKET] = {CBM_LANG_RACKET, empty_types, racket_class_types, empty_types,
+ [CBM_LANG_RACKET] = {CBM_LANG_RACKET, racket_func_types, racket_class_types, empty_types,
racket_module_types, racket_call_types, empty_types, empty_types,
empty_types, racket_var_types, empty_types, empty_types, NULL, empty_types,
NULL, NULL, tree_sitter_racket, NULL},
diff --git a/tests/repro/repro_grammar_config.c b/tests/repro/repro_grammar_config.c
index f603636de..83d262f0d 100644
--- a/tests/repro/repro_grammar_config.c
+++ b/tests/repro/repro_grammar_config.c
@@ -908,17 +908,29 @@ TEST(repro_grammar_config_jsonnet) {
*/
TEST(repro_grammar_config_starlark) {
/* All calls must live INSIDE a function body for callable-sourcing (dim 7):
- * go_binary is called inside make_binary's body, so its CALLS edge sources at
+ * both calls are inside make_binary's body, so their CALLS edges source at
* the make_binary Function. The module-level statement only REFERENCES
* make_binary (a bare name assignment, not a call) so there is no
- * Module-level call site. */
+ * Module-level call site.
+ *
+ * Callable-sourcing (dim 7) counts CALLS *edges* in the graph, and pass_calls
+ * only emits a CALLS edge when the callee resolves to a node in the file
+ * (an unresolved external callee yields no edge — pass_calls.c:389). The
+ * go_binary(...) call satisfies the dim-6 calls-extracted assertion (the
+ * "go_binary" callee string is extracted), but go_binary is an external rule
+ * with no def here, so it produces no edge. _base_deps() is defined in this
+ * same file, so the in-body call to it resolves to a Function node and gives
+ * dim 7 a Function-sourced edge to attribute. */
static const char src[] =
+ "def _base_deps():\n"
+ " return [\"//internal/cbm\"]\n"
+ "\n"
"def make_binary(name, srcs, deps = []):\n"
" \"\"\"Wrapper around go_binary for internal defaults.\"\"\"\n"
" go_binary(\n"
" name = name,\n"
" srcs = srcs,\n"
- " deps = deps + [\"//internal/cbm\"],\n"
+ " deps = deps + _base_deps(),\n"
" )\n"
"\n"
"default_rule = make_binary\n";