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(elixir): span a multi-clause function's node over all of its clauses
Chad77d4beb7a8ad
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.cindex e1de896b8..e74801b47 100644--- a/internal/cbm/extract_calls.c+++ b/internal/cbm/extract_calls.c@@ -708,6 +708,59 @@ static bool elixir_call_head_in(TSNode call, const char *source, const char *concall_node_text_in(ts_node_child(call, 0), source, heads);}+/* A definition's head declares a name; it is never a call to it. The head under+ * a guard is not the def's first argument -- `def f(x) when g` parses its whole+ * head as a `when` binary_operator -- so the plain node comparison below stopped+ * recognising it, and the inner `f(x)` was recorded as a call to `f`. Under the+ * widened span that phantom lands inside the function's own node, and cbm.c+ * decides self-recursion by line containment, so an ordinary two-clause guarded+ * function reported itself recursive. `recursive` is a queryable node property+ * and seeds the cycle detection in pass_complexity, so that is a load-bearing+ * signal, not a cosmetic one. The suppression therefore travels with the fold+ * rather than following it.+ *+ * The head comes from cbm_elixir_def_head_is (helpers.c) rather than a private+ * peel here, because three files read this same node and all three have to+ * agree on which node is the head, or a node one of them is treating as a+ * definition name is recorded by another as a reference to that very name:+ * - extract_defs.c, extract_elixir_func_def, unwraps it to NAME the+ * definition. It and this file disagreeing mints the phantom above.+ * - extract_usages.c, is_elixir_def_binding, treats the whole first argument+ * as a binding occurrence rather than a reference, which is what keeps a+ * head suppressed here from re-emerging as a USAGE edge. It is deliberately+ * WIDER than the unwrap -- it covers the guard expression too -- and that+ * width is load-bearing: a suppression in one extractor of the unified walk+ * only REMOVES a phantom if the others also decline the node.+ * - extract_unified.c, compute_elixir_func_qn, resolves a def's QN for call+ * scope tracking and does NOT unwrap, so a guarded clause pushes no+ * function scope and its call edges are sourced from the File rather than+ * from the enclosing Function. Pre-existing, independent of the two above+ * (it decides edge SOURCE, not edge existence), and fixed separately.+ *+ * Suppressing the head does not only delete edges; it moves metadata on edges+ * that SURVIVE, and that is worth stating because a caller reading an edge's+ * `line` will see a different number than before. `edges` is UNIQUE on+ * (source, target, type), so where the def-head phantom happened to be the+ * dedup survivor for a pair, deleting it promotes some other row for that same+ * pair and the surviving edge takes that row's `line`.+ *+ * Measured by indexing one 970-file Elixir lib tree with the binary built from+ * this commit's parent and with this one, and diffing the `edges` rows out of+ * the two SQLite stores keyed on (source QN, target QN, type): 2,436 edges go+ * and NONE is added -- 2,104 CALLS and 332 USAGE. Of the 119,696 edges that+ * survive, 1,054 record a different `line`.+ *+ * The typespec attributes -- `@spec f(t) :: u` and the @callback / @type family+ * -- put the declared name in a `call` node too, and are deliberately NOT+ * suppressed here. A def head can be, because extract_usages.c already treats a+ * def's first argument as a binding. Nothing there treats a typespec subject as+ * one, so declining it in this walk would not remove its phantom, it would+ * RELABEL it: handle_usages reaches the bare identifier handle_calls just+ * declined and mints a USAGE onto the same function, which under+ * UNIQUE(source, target, type) is a separate row that no longer collides with+ * the real call it used to hide behind, and which pass_importance then counts.+ * A typespec phantom has to be removed before any extractor sees it, by+ * skipping the whole subtree in the unified walk. */static bool elixir_call_is_definition_role(TSNode node, const char *source) {static const char *const structural_heads[] = {"def", "defp", "defmacro", "defmodule", NULL};static const char *const function_heads[] = {"def", "defp", "defmacro", NULL};@@ -725,7 +778,7 @@ static bool elixir_call_is_definition_role(TSNode node, const char *source) {TSNode signature = ts_node_named_child_count(arguments) > 0? ts_node_named_child(arguments, 0): arguments;- return ts_node_eq(signature, node);+ return cbm_elixir_def_head_is(signature, node);}return false;}diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex cdd6b1c7f..839e482de 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -5422,8 +5422,110 @@ static TSNode elixir_call_args(TSNode node) {return args;}-// Handle Elixir def/defp/defmacro — extract function definition.-static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro) {+// Fold one more clause of an Elixir function into the def already pushed for+// the previous clause. Every clause is its own `def` call, and an Elixir QN+// carries neither module nor arity, so all of them compute one qualified name+// and collide on a single graph node. cbm_gbuf_upsert_node breaks a same-QN+// collision by keeping the LARGEST start_line, so the survivor was the last+// clause in the file and get_code_snippet returned that clause as the whole+// function: `def admin?(%__MODULE__{role: :admin}), do: true` /+// `def admin?(%__MODULE__{}), do: false` read back as a predicate that always+// returns false. One node per function is right, so widen its span instead.+//+// Two bounds keep the widened span honest, and a clause folds only if it clears+// both:+// - Same module body. `scope` is the do_block the previous clause was written+// in, so a nested `defmodule` cannot merge with its parent: the QN carries+// no module, so `Outer.run` and `Outer.Inner.run` compute the same name and+// are array-adjacent when Inner is declared just above Outer's own clause.+// - Same group. Only the immediately preceding EXTRACTED definition is a+// candidate, so a def of another name between two same-named defs ends the+// group. That is a bound this code imposes, not a guarantee Elixir gives:+// the compiler only WARNS ("clauses with the same name and arity should be+// grouped together"), it warns per name AND arity while this QN is+// arity-free, and non-contiguous clauses do compile. Split clauses+// therefore keep separate nodes, which is the pre-existing behaviour.+// What the group bound does NOT exclude is a non-definition construct+// between two clauses: `@doc` / `@spec` are unary_operators, `use` /+// `alias` / `describe` are ordinary calls, and a `defimpl` block's inner+// defs are not extracted -- none of them push a def, so anything of that+// kind written between two clauses of one function ends up inside the+// widened span, and that is not rare. Measured by indexing one 970-file+// Elixir lib tree with the binary built from this commit's parent and with+// this one and diffing the `nodes` rows out of the two SQLite stores: 6,996+// Function nodes widen their span, none is added, none is lost and none+// narrows. Reading each widened span for a module-body construct written at+// the module's own two-space indent, which no clause body reaches, 169 of+// the 6,996 cover such a line. Counting each node once per kind, 123 cover+// a typespec attribute (@spec/@type/@typep/@opaque/@callback/+// @macrocallback), 100 an @doc or @typedoc, 46 a bare comment line, 14 an+// @impl, and none a `use`, `alias`, `import`, `require` or a module-level+// `quote`. Swallowed typespecs are the largest attribute kind, not an+// absent one.+//+// The widened span also admits a phantom call that a narrower span kept out,+// which is why the head suppression in extract_calls.c travels with this fold+// rather than after it. cbm.c flags self-recursion by finding the innermost+// Function whose [start_line, end_line] contains a recorded call whose short+// name matches the function's own, so any pre-existing phantom between the+// first and the last clause becomes a self-edge as soon as the span covers it.+// That cost is real and is measured rather than asserted away. Same corpus,+// reading each flagged node's own source span for any line that names it as a+// call, a capture or a pipe target, counting a one-line clause's body after+// `, do:` as such a line: self_recursive Function nodes go 129 -> 430, and the+// ones carrying no self-call form anywhere in their span go 6 -> 48. The lines+// responsible are the typespec heads the widened span now covers -- `@spec+// f(t) :: u` puts the declared name in a `call` node, and the unified walk+// reads it as code -- a separate defect with a separate fix, not something this+// fold can close. What the fold and the head suppression together remove is+// 2,436 edges, none of them added back: see elixir_call_is_definition_role in+// extract_calls.c.+//+// A clause whose macro differs (`def` foo/1 beside `defp` foo/2) still folds,+// because the arity-free QN already puts both on one node; is_exported is then+// the OR over the folded clauses, so a name any clause exports stays exported.+// That OR changes a recorded flag, which the node diff confirms and which is+// worth stating rather than leaving to be discovered: on the same corpus+// exactly 6 Function nodes change is_exported, all false -> true and none the+// other way. Each is a `def` clause written above a `defp` clause of the same+// name, where the pre-patch last-clause-wins record reported a publicly+// callable name as unexported. Verified against the source in every one: the+// new value is the correct one.+// Returns true when the clause folded.+static bool fold_elixir_clause(CBMExtractCtx *ctx, const char *qn, TSNode scope, TSNode node,+ uint32_t end_line, bool is_exported) {+ if (!qn || ctx->result->defs.count <= 0 || ts_node_is_null(scope)) {+ return false;+ }+ TSNode here = ts_node_parent(node);+ if (ts_node_is_null(here) || !ts_node_eq(here, scope)) {+ return false;+ }+ CBMDefinition *prev = &ctx->result->defs.items[ctx->result->defs.count - 1];+ if (!prev->label || strcmp(prev->label, "Function") != 0 || !prev->qualified_name ||+ strcmp(prev->qualified_name, qn) != 0) {+ return false;+ }+ /* Only end_line moves. prev->start_line is already the minimum, so this+ * takes no start_line argument: a clause folds only into the def pushed+ * for the immediately preceding extracted clause of the same do_block, and+ * extract_elixir_call pushes a do_block's children onto its stack in+ * reverse index order so they pop in source order. A `start_line <+ * prev->start_line` guard here would be unreachable; if that traversal+ * ever stops being source-ordered, this is the line that has to change+ * with it. */+ if (end_line > prev->end_line) {+ prev->end_line = end_line;+ }+ prev->is_exported = prev->is_exported || is_exported;+ return true;+}++// Handle Elixir def/defp/defmacro — extract function definition. `scope` is the+// module body the previous clause was extracted from, and is updated to this+// def's own module body; see fold_elixir_clause for what it bounds.+static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro,+ TSNode *scope) {CBMArena *a = ctx->arena;TSNode args = elixir_call_args(node);if (ts_node_is_null(args)) {@@ -5456,15 +5558,25 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const charreturn;}+ const char *qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);+ uint32_t start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;+ uint32_t end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;+ bool is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);+ bool folded = fold_elixir_clause(ctx, qn, *scope, node, end_line, is_exported);+ *scope = ts_node_parent(node);+ if (folded) {+ return;+ }+CBMDefinition def;memset(&def, 0, sizeof(def));def.name = name;- def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);+ def.qualified_name = qn;def.label = "Function";def.file_path = ctx->rel_path;- def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;- def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;- def.is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);+ def.start_line = start_line;+ def.end_line = end_line;+ def.is_exported = is_exported;cbm_defs_push(&ctx->result->defs, a, def);}@@ -5501,6 +5613,10 @@ static TSNode emit_elixir_module_class(CBMExtractCtx *ctx, TSNode cur) {// without recursion between extract_elixir_call ↔ extract_elixir_module_def.static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {(void)spec;+ /* Module body the last extracted clause was written in; see+ * fold_elixir_clause. Null until the first def, so nothing folds into a+ * def left over from a previous top-level call node. */+ TSNode def_scope = {0};TSNodeStack stack;ts_nstack_init(&stack, ctx, CBM_SZ_64);ts_nstack_push(&stack, node);@@ -5523,7 +5639,7 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpif (strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||strcmp(macro, "defmacro") == 0) {- extract_elixir_func_def(ctx, cur, macro);+ extract_elixir_func_def(ctx, cur, macro, &def_scope);} else if (strcmp(macro, "defmodule") == 0) {TSNode do_block = emit_elixir_module_class(ctx, cur);if (!ts_node_is_null(do_block)) {diff --git a/tests/test_extraction.c b/tests/test_extraction.cindex f8b61d53c..887457cde 100644--- a/tests/test_extraction.c+++ b/tests/test_extraction.c@@ -1457,6 +1457,338 @@ TEST(extract_elixir_guarded_def_head) {PASS();}+/* Every clause of an Elixir function is its own `def` call, and an Elixir QN+ * carries neither module nor arity, so all clauses compute one qualified name+ * and collide on a single graph node. cbm_gbuf_upsert_node ranks a same-QN+ * collision by LARGEST start_line, so the survivor was the last clause in the+ * file and get_code_snippet returned that clause as the whole function: a+ * two-clause `admin?` read back as `def admin?(%__MODULE__{}), do: false` --+ * the graph asserting a predicate that always returns false. The node has to+ * span its own clauses, and ONLY its own: the fold is bounded to the+ * immediately preceding definition and to the enclosing module body, so a def+ * of another name ends the group and two same-named functions in two modules+ * stay two nodes. */+TEST(extract_elixir_clauses_fold_only_when_adjacent_in_one_module) {+ CBMFileResult *r = extract("defmodule Roles do\n" /* 1 */+ " def admin?(%{role: :admin}), do: true\n" /* 2 */+ " def admin?(%{}), do: false\n" /* 3 */+ "\n" /* 4 */+ " def promote(user), do: user\n" /* 5 */+ "\n" /* 6 */+ " def admin?(_other), do: false\n" /* 7 */+ "end\n" /* 8 */+ "\n" /* 9 */+ "defmodule Guests do\n" /* 10 */+ " def admin?(:root), do: true\n" /* 11 */+ " def admin?(_any), do: false\n" /* 12 */+ "end\n", /* 13 */+ CBM_LANG_ELIXIR, "t", "roles.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);++ /* The two adjacent Roles clauses fold; the clause past `promote` is its own+ * group; the Guests pair is a DIFFERENT function and stays its own node. */+ ASSERT_EQ(3, count_defs_named(r, "Function", "admin?"));+ ASSERT_EQ(1, count_defs_named(r, "Function", "promote"));++ const CBMDefinition *admin[3] = {NULL, NULL, NULL};+ const CBMDefinition *promote = NULL;+ int seen = 0;+ for (int i = 0; i < r->defs.count; i++) {+ if (strcmp(r->defs.items[i].label, "Function") != 0) {+ continue;+ }+ if (strcmp(r->defs.items[i].name, "admin?") == 0 && seen < 3) {+ admin[seen++] = &r->defs.items[i];+ } else if (strcmp(r->defs.items[i].name, "promote") == 0) {+ promote = &r->defs.items[i];+ }+ }+ ASSERT_NOT_NULL(admin[0]);+ ASSERT_NOT_NULL(admin[1]);+ ASSERT_NOT_NULL(admin[2]);+ ASSERT_NOT_NULL(promote);++ ASSERT_EQ(2, (int)admin[0]->start_line);+ ASSERT_EQ(3, (int)admin[0]->end_line);+ /* separated from lines 2-3 by `promote`, so it keeps its own span */+ ASSERT_EQ(7, (int)admin[1]->start_line);+ ASSERT_EQ(7, (int)admin[1]->end_line);+ /* Guests.admin? never reaches back over the module boundary */+ ASSERT_EQ(11, (int)admin[2]->start_line);+ ASSERT_EQ(12, (int)admin[2]->end_line);+ ASSERT_EQ(5, (int)promote->start_line);+ ASSERT_EQ(5, (int)promote->end_line);+ cbm_free_result(r);+ PASS();+}++/* A nested module is the one place where the "def of another name ends the+ * group" rule is not enough: an Elixir QN carries no module, so `Outer.run` and+ * `Outer.Inner.run` compute the SAME qualified name, and when Inner sits+ * directly above Outer's own clause the two are adjacent in the extracted defs.+ * The fold is bounded to the module body as well, so they stay two nodes. */+TEST(extract_elixir_nested_module_clauses_do_not_fold) {+ CBMFileResult *r = extract("defmodule Outer do\n" /* 1 */+ " defmodule Inner do\n" /* 2 */+ " def run(a), do: a\n" /* 3 */+ " end\n" /* 4 */+ "\n" /* 5 */+ " def run(b), do: b + 1\n" /* 6 */+ "end\n", /* 7 */+ CBM_LANG_ELIXIR, "t", "nested.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ ASSERT_EQ(2, count_defs_named(r, "Function", "run"));++ const CBMDefinition *runs[2] = {NULL, NULL};+ int seen = 0;+ for (int i = 0; i < r->defs.count; i++) {+ if (strcmp(r->defs.items[i].label, "Function") == 0 &&+ strcmp(r->defs.items[i].name, "run") == 0 && seen < 2) {+ runs[seen++] = &r->defs.items[i];+ }+ }+ ASSERT_NOT_NULL(runs[0]);+ ASSERT_NOT_NULL(runs[1]);+ ASSERT_EQ(3, (int)runs[0]->start_line);+ ASSERT_EQ(3, (int)runs[0]->end_line);+ ASSERT_EQ(6, (int)runs[1]->start_line);+ ASSERT_EQ(6, (int)runs[1]->end_line);+ cbm_free_result(r);+ PASS();+}++/* Folding clauses has to decide is_exported for the merged node. A `def` clause+ * and a `defp` clause of the same name AND arity are clauses of one function --+ * a private guard clause or a nil short-circuit written above the public head+ * is ordinary Elixir -- so is_exported is the OR over the folded clauses: the+ * name is callable from outside if any clause exports it, and a wholly private+ * function stays unexported. */+TEST(extract_elixir_folded_clause_is_exported_is_the_or) {+ CBMFileResult *r = extract("defmodule Acc do\n" /* 1 */+ " def build(l), do: build(l)\n" /* 2 */+ " defp build([]), do: []\n" /* 3 */+ "\n" /* 4 */+ " defp trim(nil), do: nil\n" /* 5 */+ " defp trim(s), do: s\n" /* 6 */+ "end\n", /* 7 */+ CBM_LANG_ELIXIR, "t", "acc.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ ASSERT_EQ(1, count_defs_named(r, "Function", "build"));+ ASSERT_EQ(1, count_defs_named(r, "Function", "trim"));++ const CBMDefinition *build = NULL;+ const CBMDefinition *trim = NULL;+ for (int i = 0; i < r->defs.count; i++) {+ if (strcmp(r->defs.items[i].label, "Function") != 0) {+ continue;+ }+ if (strcmp(r->defs.items[i].name, "build") == 0) {+ build = &r->defs.items[i];+ } else if (strcmp(r->defs.items[i].name, "trim") == 0) {+ trim = &r->defs.items[i];+ }+ }+ ASSERT_NOT_NULL(build);+ ASSERT_NOT_NULL(trim);+ ASSERT_EQ(2, (int)build->start_line);+ ASSERT_EQ(3, (int)build->end_line);+ ASSERT_TRUE(build->is_exported);+ ASSERT_EQ(5, (int)trim->start_line);+ ASSERT_EQ(6, (int)trim->end_line);+ ASSERT_FALSE(trim->is_exported);+ cbm_free_result(r);++ /* The other direction, and the only one the `prev->is_exported ||` disjunct+ * decides. Above, the exporting clause is the FIRST one, so a plain+ * `prev->is_exported = is_exported` assignment would also have to lose it+ * on the later defp -- but a last-clause-wins assignment happens to be+ * right whenever the def comes last. Here the defp comes first and the def+ * second, so the surviving flag can only be true if the fold ORs rather+ * than assigns. */+ CBMFileResult *o = extract("defmodule Ord do\n" /* 1 */+ " defp z(1), do: 1\n" /* 2 */+ " def z(2), do: 2\n" /* 3 */+ "end\n", /* 4 */+ CBM_LANG_ELIXIR, "t", "ord.ex");+ ASSERT_NOT_NULL(o);+ ASSERT_FALSE(o->has_error);+ ASSERT_EQ(1, count_defs_named(o, "Function", "z"));+ const CBMDefinition *z = NULL;+ for (int i = 0; i < o->defs.count; i++) {+ if (strcmp(o->defs.items[i].label, "Function") == 0 &&+ strcmp(o->defs.items[i].name, "z") == 0) {+ z = &o->defs.items[i];+ }+ }+ ASSERT_NOT_NULL(z);+ ASSERT_EQ(2, (int)z->start_line);+ ASSERT_EQ(3, (int)z->end_line);+ ASSERT_TRUE(z->is_exported);+ cbm_free_result(o);+ PASS();+}++/* First definition with this name, for the Elixir clause-fold tests below.+ * find_def() is declared much further down this file. */+static const CBMDefinition *elixir_first_def(CBMFileResult *r, const char *name) {+ for (int i = 0; i < r->defs.count; i++) {+ if (r->defs.items[i].name && strcmp(r->defs.items[i].name, name) == 0) {+ return &r->defs.items[i];+ }+ }+ return NULL;+}++/* A guarded clause head is a DECLARATION of the name, never a call to it, and+ * the call extractor has to agree with the def extractor about which node the+ * head is. It did not: it suppressed a head only when the head node IS the+ * def's first named argument, but `def f(x) when g` parses its whole head as a+ * `when` binary_operator, so the inner `f(x)` was not that argument and was+ * recorded as a call to `f`. Under the folded span that phantom lands inside+ * the function's own node, and self-recursion is decided by line containment,+ * so an ordinary two-clause guarded function reported itself recursive.+ * Recursion is a load-bearing signal: `recursive` is a queryable node property+ * and seeds the cycle detection in pass_complexity. */+TEST(extract_elixir_guarded_clause_head_is_not_a_self_call) {+ CBMFileResult *r = extract("defmodule Guarded do\n" /* 1 */+ " def f(x) when is_integer(x), do: x\n" /* 2 */+ " def f(_x), do: 0\n" /* 3 */+ "\n" /* 4 */+ " def solo(x) when is_binary(x), do: x\n" /* 5 */+ "end\n", /* 6 */+ CBM_LANG_ELIXIR, "t", "guard_call.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);++ /* No call to either name exists anywhere in this file. */+ ASSERT_EQ(0, count_calls_named(r, "f"));+ ASSERT_EQ(0, count_calls_named(r, "solo"));+ /* The guard predicate IS a real call and stays one. */+ ASSERT_EQ(1, count_calls_named(r, "is_integer"));+ ASSERT_EQ(1, count_calls_named(r, "is_binary"));++ ASSERT_EQ(1, count_defs_named(r, "Function", "f"));+ const CBMDefinition *f = elixir_first_def(r, "f");+ ASSERT_NOT_NULL(f);+ ASSERT_EQ(2, (int)f->start_line);+ ASSERT_EQ(3, (int)f->end_line);+ ASSERT_FALSE(f->is_recursive);++ /* A single guarded clause was wrong even before the span widened: the+ * phantom landed on its own one-line node. */+ const CBMDefinition *solo = elixir_first_def(r, "solo");+ ASSERT_NOT_NULL(solo);+ ASSERT_FALSE(solo->is_recursive);+ cbm_free_result(r);+ PASS();+}++/* Every reference of any kind this file records for `name`, across all four+ * extractors the unified walk runs over one subtree: a call site, a value+ * usage, a read/write occurrence, a type reference. Suppressing a node in ONE+ * of them does not remove a phantom reference, it moves it -- handle_calls+ * declining a node leaves state->callee_expr unset, and handle_usages then+ * reaches the bare identifier and mints a USAGE instead. USAGE and CALLS are+ * separate rows in the edge table, so the relabelled phantom also escapes the+ * UNIQUE(source,target,type) dedup that had been collapsing it onto a real+ * call, and pass_importance counts an inbound reference that did not exist+ * before. An assertion naming only r->calls cannot see any of that. */+static int elixir_any_ref_count(CBMFileResult *r, const char *name) {+ int count = count_calls_named(r, name);+ for (int i = 0; i < r->usages.count; i++) {+ if (r->usages.items[i].ref_name && strcmp(r->usages.items[i].ref_name, name) == 0) {+ count++;+ }+ }+ for (int i = 0; i < r->rw.count; i++) {+ if (r->rw.items[i].var_name && strcmp(r->rw.items[i].var_name, name) == 0) {+ count++;+ }+ }+ for (int i = 0; i < r->type_refs.count; i++) {+ if (r->type_refs.items[i].type_name && strcmp(r->type_refs.items[i].type_name, name) == 0) {+ count++;+ }+ }+ return count;+}++/* The head suppression has to REMOVE the phantom, not relabel it. What makes+ * that hold for a def head is not the calls walk alone: is_elixir_def_binding+ * in extract_usages.c already treats everything inside a def's first argument+ * as a binding occurrence rather than a reference, so the identifier+ * handle_calls declines is declined by handle_usages too. That agreement is the+ * invariant under test, and it is why the counts below are over every extractor+ * rather than over r->calls.+ *+ * `spec_subject` pins the boundary of this patch, and pins it as a RELATION+ * rather than a count, because the count cannot see the defect. A typespec+ * subject is the same shape as a def head and is NOT suppressed here: nothing+ * in extract_usages.c treats it as a binding, so declining it in handle_calls+ * only relabels its phantom as a USAGE. At extraction that relabel is+ * invisible -- one reference before, one after -- and it becomes a defect+ * downstream, where CALLS and USAGE are separate rows under+ * UNIQUE(source,target,type) and the relabelled phantom stops colliding with+ * the real call it used to hide behind. So the assertion is that every+ * reference recorded for a typespec subject is a CALL and none has become a+ * usage, a read/write or a type reference. */+TEST(extract_elixir_declaration_head_mints_no_reference_of_any_kind) {+ CBMFileResult *r = extract("defmodule Heads do\n" /* 1 */+ " def guarded(x) when is_integer(x), do: x\n" /* 2 */+ " def guarded(_x), do: 0\n" /* 3 */+ "\n" /* 4 */+ " defp bare(x), do: x\n" /* 5 */+ "\n" /* 6 */+ " @spec spec_subject(integer) :: integer\n" /* 7 */+ " def spec_subject(n), do: n\n" /* 8 */+ "end\n", /* 9 */+ CBM_LANG_ELIXIR, "t", "heads.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);++ /* Nothing in this file references `guarded` or `bare`. Not as a call, not+ * as a value, not as a read/write, not as a type. */+ ASSERT_EQ(0, elixir_any_ref_count(r, "guarded"));+ ASSERT_EQ(0, elixir_any_ref_count(r, "bare"));+ /* The guard predicate is a real call and survives as one. */+ ASSERT_EQ(1, count_calls_named(r, "is_integer"));+ ASSERT_EQ(1, elixir_any_ref_count(r, "is_integer"));+ /* Declared, not suppressed here; see the comment above. Every reference+ * this file records for a typespec subject is a CALL -- none of them has+ * been relabelled into a usage, a read/write or a type reference. */+ ASSERT_EQ(count_calls_named(r, "spec_subject"),+ elixir_any_ref_count(r, "spec_subject"));+ cbm_free_result(r);+ PASS();+}++/* The other direction: suppressing declaration heads must not blind the+ * detector to real recursion, and folding must not lose a self-call written in+ * a clause that is no longer the surviving one. `walk` recurses from its+ * guarded second clause; exactly one call to `walk` is recorded (the body one,+ * not the two heads) and the folded node is recursive. */+TEST(extract_elixir_real_recursion_survives_head_suppression) {+ CBMFileResult *r = extract("defmodule Rec do\n" /* 1 */+ " def walk([]), do: []\n" /* 2 */+ " def walk([h | t]) when is_integer(h), do: [h | walk(t)]\n" /* 3 */+ "end\n", /* 4 */+ CBM_LANG_ELIXIR, "t", "rec.ex");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error);+ ASSERT_EQ(1, count_calls_named(r, "walk"));+ ASSERT_EQ(1, count_defs_named(r, "Function", "walk"));+ const CBMDefinition *walk = elixir_first_def(r, "walk");+ ASSERT_NOT_NULL(walk);+ ASSERT_EQ(2, (int)walk->start_line);+ ASSERT_EQ(3, (int)walk->end_line);+ ASSERT_TRUE(walk->is_recursive);+ cbm_free_result(r);+ PASS();+}+/* tree-sitter-elixir gives a call's arguments node no field name, so the* generic `arguments` field lookup returns null and first_string_arg was never* populated for any Elixir call — Phoenix route paths, service URLs and config@@ -8700,6 +9032,12 @@ SUITE(extraction) {RUN_TEST(elixir_function);RUN_TEST(elixir_def_head_is_covers_the_head_and_every_guard_above_it);RUN_TEST(extract_elixir_guarded_def_head);+ RUN_TEST(extract_elixir_clauses_fold_only_when_adjacent_in_one_module);+ RUN_TEST(extract_elixir_nested_module_clauses_do_not_fold);+ RUN_TEST(extract_elixir_folded_clause_is_exported_is_the_or);+ RUN_TEST(extract_elixir_guarded_clause_head_is_not_a_self_call);+ RUN_TEST(extract_elixir_declaration_head_mints_no_reference_of_any_kind);+ RUN_TEST(extract_elixir_real_recursion_survives_head_suppression);RUN_TEST(elixir_call_string_argument);RUN_TEST(haskell_function);RUN_TEST(ocaml_function);