Skip to content

Commit 77d4beb

Browse files
committed
fix(elixir): span a multi-clause function's node over all of its clauses
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 breaks a same-QN collision by keeping the largest start_line, so the survivor was whichever clause was written last, 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 `def admin?(%__MODULE__{}), do: false`, so an agent asking what the function does is told the predicate always returns false. One node per function is right, so the fix widens that node's span instead of splitting each clause onto its own node. Two bounds keep the widened span honest, and a clause folds only if it clears both: it must sit in the same module body as the previous one (an Elixir QN carries no module, so `Outer.run` and `Outer.Inner.run` compute the same name), and it must be adjacent to it (only the immediately preceding EXTRACTED definition is a candidate, so a def of another name ends the group). 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` and `edges` rows out of the two SQLite stores: 4,135 functions stop being reported as one line of source, and none starts, which is the get_code_snippet defect measured on this corpus. A widened span is only honest if nothing phantom lives inside it, which is why the head suppression in extract_calls.c lands with the fold rather than as a follow-up. A guarded clause head is a declaration of the name, never a call to it, and the calls walk did not agree: it suppressed a head only when the head node IS the def's first named argument, and `def f(x) when g` parses its whole head as a `when` binary_operator, so the inner `f(x)` was not that argument. It now asks cbm_elixir_def_head_is, the same helper the defs walk reads the head through. cbm.c flags self-recursion by line containment, so under the folded span that phantom would land inside the function's own node, and `recursive` is a queryable node property that seeds the cycle detection in pass_complexity. The widened span has a real cost. Reading each flagged node's own source span for any line that names it as a call, a capture or a pipe target, and counting a one-line clause's body after `, do:` as such a line: self_recursive Function nodes 129 -> 430 ...with no self-call form in span 6 -> 48 The lines responsible are the typespec heads a widened span now covers: `@spec f(t) :: u` puts the declared name in a `call` node and the unified walk reads it as code. That is a separate defect with its own fix later in this stack, and a fold cannot close it. Two things a reader of the graph will notice: Edge metadata moves on edges that SURVIVE. `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 another row and the surviving edge takes that row's `line`. Of the 119,696 edges that survive this commit, 1,054 do. The 2,436 that go are 2,104 CALLS and 332 USAGE, measured against this commit's parent, and none is added back. is_exported becomes the OR over the folded clauses. The arity-free QN puts a public entry point and its private accumulator on one node, and a name callable from outside must stay exported. Exactly 6 Function nodes change is_exported on this corpus, 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 correct. The typespec attributes put a declared name in a `call` node too and are deliberately NOT suppressed in the calls walk. A def head can be, because is_elixir_def_binding already treats a def's first argument as a binding, so the identifier handle_calls declines is declined by handle_usages too. Nothing treats a typespec subject as a binding, so declining one here would not remove its phantom, it would RELABEL it as a USAGE: a separate row under UNIQUE(source, target, type) that no longer collides with the real call it used to hide behind. extract_elixir_declaration_head_mints_no_reference_of_any_kind pins that as a relation rather than a count, because at extraction the relabel is invisible. Depends on the shared def-head helper and guarded def-heads commits. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent 630f851 commit 77d4beb

3 files changed

Lines changed: 515 additions & 8 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,59 @@ static bool elixir_call_head_in(TSNode call, const char *source, const char *con
708708
call_node_text_in(ts_node_child(call, 0), source, heads);
709709
}
710710

711+
/* A definition's head declares a name; it is never a call to it. The head under
712+
* a guard is not the def's first argument -- `def f(x) when g` parses its whole
713+
* head as a `when` binary_operator -- so the plain node comparison below stopped
714+
* recognising it, and the inner `f(x)` was recorded as a call to `f`. Under the
715+
* widened span that phantom lands inside the function's own node, and cbm.c
716+
* decides self-recursion by line containment, so an ordinary two-clause guarded
717+
* function reported itself recursive. `recursive` is a queryable node property
718+
* and seeds the cycle detection in pass_complexity, so that is a load-bearing
719+
* signal, not a cosmetic one. The suppression therefore travels with the fold
720+
* rather than following it.
721+
*
722+
* The head comes from cbm_elixir_def_head_is (helpers.c) rather than a private
723+
* peel here, because three files read this same node and all three have to
724+
* agree on which node is the head, or a node one of them is treating as a
725+
* definition name is recorded by another as a reference to that very name:
726+
* - extract_defs.c, extract_elixir_func_def, unwraps it to NAME the
727+
* definition. It and this file disagreeing mints the phantom above.
728+
* - extract_usages.c, is_elixir_def_binding, treats the whole first argument
729+
* as a binding occurrence rather than a reference, which is what keeps a
730+
* head suppressed here from re-emerging as a USAGE edge. It is deliberately
731+
* WIDER than the unwrap -- it covers the guard expression too -- and that
732+
* width is load-bearing: a suppression in one extractor of the unified walk
733+
* only REMOVES a phantom if the others also decline the node.
734+
* - extract_unified.c, compute_elixir_func_qn, resolves a def's QN for call
735+
* scope tracking and does NOT unwrap, so a guarded clause pushes no
736+
* function scope and its call edges are sourced from the File rather than
737+
* from the enclosing Function. Pre-existing, independent of the two above
738+
* (it decides edge SOURCE, not edge existence), and fixed separately.
739+
*
740+
* Suppressing the head does not only delete edges; it moves metadata on edges
741+
* that SURVIVE, and that is worth stating because a caller reading an edge's
742+
* `line` will see a different number than before. `edges` is UNIQUE on
743+
* (source, target, type), so where the def-head phantom happened to be the
744+
* dedup survivor for a pair, deleting it promotes some other row for that same
745+
* pair and the surviving edge takes that row's `line`.
746+
*
747+
* Measured by indexing one 970-file Elixir lib tree with the binary built from
748+
* this commit's parent and with this one, and diffing the `edges` rows out of
749+
* the two SQLite stores keyed on (source QN, target QN, type): 2,436 edges go
750+
* and NONE is added -- 2,104 CALLS and 332 USAGE. Of the 119,696 edges that
751+
* survive, 1,054 record a different `line`.
752+
*
753+
* The typespec attributes -- `@spec f(t) :: u` and the @callback / @type family
754+
* -- put the declared name in a `call` node too, and are deliberately NOT
755+
* suppressed here. A def head can be, because extract_usages.c already treats a
756+
* def's first argument as a binding. Nothing there treats a typespec subject as
757+
* one, so declining it in this walk would not remove its phantom, it would
758+
* RELABEL it: handle_usages reaches the bare identifier handle_calls just
759+
* declined and mints a USAGE onto the same function, which under
760+
* UNIQUE(source, target, type) is a separate row that no longer collides with
761+
* the real call it used to hide behind, and which pass_importance then counts.
762+
* A typespec phantom has to be removed before any extractor sees it, by
763+
* skipping the whole subtree in the unified walk. */
711764
static bool elixir_call_is_definition_role(TSNode node, const char *source) {
712765
static const char *const structural_heads[] = {"def", "defp", "defmacro", "defmodule", NULL};
713766
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) {
725778
TSNode signature = ts_node_named_child_count(arguments) > 0
726779
? ts_node_named_child(arguments, 0)
727780
: arguments;
728-
return ts_node_eq(signature, node);
781+
return cbm_elixir_def_head_is(signature, node);
729782
}
730783
return false;
731784
}

‎internal/cbm/extract_defs.c‎

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5422,8 +5422,110 @@ static TSNode elixir_call_args(TSNode node) {
54225422
return args;
54235423
}
54245424

5425-
// Handle Elixir def/defp/defmacro — extract function definition.
5426-
static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro) {
5425+
// Fold one more clause of an Elixir function into the def already pushed for
5426+
// the previous clause. Every clause is its own `def` call, and an Elixir QN
5427+
// carries neither module nor arity, so all of them compute one qualified name
5428+
// and collide on a single graph node. cbm_gbuf_upsert_node breaks a same-QN
5429+
// collision by keeping the LARGEST start_line, so the survivor was the last
5430+
// clause in the file and get_code_snippet returned that clause as the whole
5431+
// function: `def admin?(%__MODULE__{role: :admin}), do: true` /
5432+
// `def admin?(%__MODULE__{}), do: false` read back as a predicate that always
5433+
// returns false. One node per function is right, so widen its span instead.
5434+
//
5435+
// Two bounds keep the widened span honest, and a clause folds only if it clears
5436+
// both:
5437+
// - Same module body. `scope` is the do_block the previous clause was written
5438+
// in, so a nested `defmodule` cannot merge with its parent: the QN carries
5439+
// no module, so `Outer.run` and `Outer.Inner.run` compute the same name and
5440+
// are array-adjacent when Inner is declared just above Outer's own clause.
5441+
// - Same group. Only the immediately preceding EXTRACTED definition is a
5442+
// candidate, so a def of another name between two same-named defs ends the
5443+
// group. That is a bound this code imposes, not a guarantee Elixir gives:
5444+
// the compiler only WARNS ("clauses with the same name and arity should be
5445+
// grouped together"), it warns per name AND arity while this QN is
5446+
// arity-free, and non-contiguous clauses do compile. Split clauses
5447+
// therefore keep separate nodes, which is the pre-existing behaviour.
5448+
// What the group bound does NOT exclude is a non-definition construct
5449+
// between two clauses: `@doc` / `@spec` are unary_operators, `use` /
5450+
// `alias` / `describe` are ordinary calls, and a `defimpl` block's inner
5451+
// defs are not extracted -- none of them push a def, so anything of that
5452+
// kind written between two clauses of one function ends up inside the
5453+
// widened span, and that is not rare. Measured by indexing one 970-file
5454+
// Elixir lib tree with the binary built from this commit's parent and with
5455+
// this one and diffing the `nodes` rows out of the two SQLite stores: 6,996
5456+
// Function nodes widen their span, none is added, none is lost and none
5457+
// narrows. Reading each widened span for a module-body construct written at
5458+
// the module's own two-space indent, which no clause body reaches, 169 of
5459+
// the 6,996 cover such a line. Counting each node once per kind, 123 cover
5460+
// a typespec attribute (@spec/@type/@typep/@opaque/@callback/
5461+
// @macrocallback), 100 an @doc or @typedoc, 46 a bare comment line, 14 an
5462+
// @impl, and none a `use`, `alias`, `import`, `require` or a module-level
5463+
// `quote`. Swallowed typespecs are the largest attribute kind, not an
5464+
// absent one.
5465+
//
5466+
// The widened span also admits a phantom call that a narrower span kept out,
5467+
// which is why the head suppression in extract_calls.c travels with this fold
5468+
// rather than after it. cbm.c flags self-recursion by finding the innermost
5469+
// Function whose [start_line, end_line] contains a recorded call whose short
5470+
// name matches the function's own, so any pre-existing phantom between the
5471+
// first and the last clause becomes a self-edge as soon as the span covers it.
5472+
// That cost is real and is measured rather than asserted away. Same corpus,
5473+
// reading each flagged node's own source span for any line that names it as a
5474+
// call, a capture or a pipe target, counting a one-line clause's body after
5475+
// `, do:` as such a line: self_recursive Function nodes go 129 -> 430, and the
5476+
// ones carrying no self-call form anywhere in their span go 6 -> 48. The lines
5477+
// responsible are the typespec heads the widened span now covers -- `@spec
5478+
// f(t) :: u` puts the declared name in a `call` node, and the unified walk
5479+
// reads it as code -- a separate defect with a separate fix, not something this
5480+
// fold can close. What the fold and the head suppression together remove is
5481+
// 2,436 edges, none of them added back: see elixir_call_is_definition_role in
5482+
// extract_calls.c.
5483+
//
5484+
// A clause whose macro differs (`def` foo/1 beside `defp` foo/2) still folds,
5485+
// because the arity-free QN already puts both on one node; is_exported is then
5486+
// the OR over the folded clauses, so a name any clause exports stays exported.
5487+
// That OR changes a recorded flag, which the node diff confirms and which is
5488+
// worth stating rather than leaving to be discovered: on the same corpus
5489+
// exactly 6 Function nodes change is_exported, all false -> true and none the
5490+
// other way. Each is a `def` clause written above a `defp` clause of the same
5491+
// name, where the pre-patch last-clause-wins record reported a publicly
5492+
// callable name as unexported. Verified against the source in every one: the
5493+
// new value is the correct one.
5494+
// Returns true when the clause folded.
5495+
static bool fold_elixir_clause(CBMExtractCtx *ctx, const char *qn, TSNode scope, TSNode node,
5496+
uint32_t end_line, bool is_exported) {
5497+
if (!qn || ctx->result->defs.count <= 0 || ts_node_is_null(scope)) {
5498+
return false;
5499+
}
5500+
TSNode here = ts_node_parent(node);
5501+
if (ts_node_is_null(here) || !ts_node_eq(here, scope)) {
5502+
return false;
5503+
}
5504+
CBMDefinition *prev = &ctx->result->defs.items[ctx->result->defs.count - 1];
5505+
if (!prev->label || strcmp(prev->label, "Function") != 0 || !prev->qualified_name ||
5506+
strcmp(prev->qualified_name, qn) != 0) {
5507+
return false;
5508+
}
5509+
/* Only end_line moves. prev->start_line is already the minimum, so this
5510+
* takes no start_line argument: a clause folds only into the def pushed
5511+
* for the immediately preceding extracted clause of the same do_block, and
5512+
* extract_elixir_call pushes a do_block's children onto its stack in
5513+
* reverse index order so they pop in source order. A `start_line <
5514+
* prev->start_line` guard here would be unreachable; if that traversal
5515+
* ever stops being source-ordered, this is the line that has to change
5516+
* with it. */
5517+
if (end_line > prev->end_line) {
5518+
prev->end_line = end_line;
5519+
}
5520+
prev->is_exported = prev->is_exported || is_exported;
5521+
return true;
5522+
}
5523+
5524+
// Handle Elixir def/defp/defmacro — extract function definition. `scope` is the
5525+
// module body the previous clause was extracted from, and is updated to this
5526+
// def's own module body; see fold_elixir_clause for what it bounds.
5527+
static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro,
5528+
TSNode *scope) {
54275529
CBMArena *a = ctx->arena;
54285530
TSNode args = elixir_call_args(node);
54295531
if (ts_node_is_null(args)) {
@@ -5456,15 +5558,25 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
54565558
return;
54575559
}
54585560

5561+
const char *qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
5562+
uint32_t start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
5563+
uint32_t end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
5564+
bool is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);
5565+
bool folded = fold_elixir_clause(ctx, qn, *scope, node, end_line, is_exported);
5566+
*scope = ts_node_parent(node);
5567+
if (folded) {
5568+
return;
5569+
}
5570+
54595571
CBMDefinition def;
54605572
memset(&def, 0, sizeof(def));
54615573
def.name = name;
5462-
def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
5574+
def.qualified_name = qn;
54635575
def.label = "Function";
54645576
def.file_path = ctx->rel_path;
5465-
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
5466-
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
5467-
def.is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);
5577+
def.start_line = start_line;
5578+
def.end_line = end_line;
5579+
def.is_exported = is_exported;
54685580
cbm_defs_push(&ctx->result->defs, a, def);
54695581
}
54705582

@@ -5501,6 +5613,10 @@ static TSNode emit_elixir_module_class(CBMExtractCtx *ctx, TSNode cur) {
55015613
// without recursion between extract_elixir_call ↔ extract_elixir_module_def.
55025614
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
55035615
(void)spec;
5616+
/* Module body the last extracted clause was written in; see
5617+
* fold_elixir_clause. Null until the first def, so nothing folds into a
5618+
* def left over from a previous top-level call node. */
5619+
TSNode def_scope = {0};
55045620
TSNodeStack stack;
55055621
ts_nstack_init(&stack, ctx, CBM_SZ_64);
55065622
ts_nstack_push(&stack, node);
@@ -5523,7 +5639,7 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
55235639

55245640
if (strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
55255641
strcmp(macro, "defmacro") == 0) {
5526-
extract_elixir_func_def(ctx, cur, macro);
5642+
extract_elixir_func_def(ctx, cur, macro, &def_scope);
55275643
} else if (strcmp(macro, "defmodule") == 0) {
55285644
TSNode do_block = emit_elixir_module_class(ctx, cur);
55295645
if (!ts_node_is_null(do_block)) {

0 commit comments

Comments
 (0)