@@ -9714,8 +9714,119 @@ static TSNode elixir_call_args(TSNode node) {
97149714 return args;
97159715}
97169716
9717- // Handle Elixir def/defp/defmacro — extract function definition.
9718- static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro) {
9717+ // Fold one more clause of an Elixir function into the def already pushed for
9718+ // the previous clause. Every clause is its own `def` call, and an Elixir QN
9719+ // carries neither module nor arity, so all of them compute one qualified name
9720+ // and collide on a single graph node. cbm_gbuf_upsert_node breaks a same-QN
9721+ // collision by keeping the LARGEST start_line, so the survivor was the last
9722+ // clause in the file and get_code_snippet returned that clause as the whole
9723+ // function: `def admin?(%__MODULE__{role: :admin}), do: true` /
9724+ // `def admin?(%__MODULE__{}), do: false` read back as a predicate that always
9725+ // returns false. One node per function is right, so widen its span instead.
9726+ //
9727+ // Two bounds keep the widened span honest, and a clause folds only if it clears
9728+ // both:
9729+ // - Same module body. `scope` is the do_block the previous clause was written
9730+ // in, so a nested `defmodule` cannot merge with its parent: the QN carries
9731+ // no module, so `Outer.run` and `Outer.Inner.run` compute the same name and
9732+ // are array-adjacent when Inner is declared just above Outer's own clause.
9733+ // - Same group. Only the immediately preceding EXTRACTED definition is a
9734+ // candidate, so a def of another name between two same-named defs ends the
9735+ // group. That is a bound this code imposes, not a guarantee Elixir gives:
9736+ // the compiler only WARNS ("clauses with the same name and arity should be
9737+ // grouped together"), it warns per name AND arity while this QN is
9738+ // arity-free, and non-contiguous clauses do compile. Split clauses
9739+ // therefore keep separate nodes, which is the pre-existing behaviour.
9740+ // What the group bound does NOT exclude is a non-definition construct
9741+ // between two clauses: `@doc` / `@spec` are unary_operators, `use` /
9742+ // `alias` / `describe` are ordinary calls, and a `defimpl` block's inner
9743+ // defs are not extracted -- none of them push a def, so anything of that
9744+ // kind written between two clauses of one function ends up inside the
9745+ // widened span, and that is not rare. Measured by indexing one 970-file
9746+ // Elixir lib tree with the binary built from this commit's parent and with
9747+ // this one and diffing the `nodes` rows out of the two SQLite stores: 6,996
9748+ // Function nodes widen their span, none is added, none is lost and none
9749+ // narrows. Reading each widened span for a module-body construct written at
9750+ // the module's own two-space indent, which no clause body reaches, 169 of
9751+ // the 6,996 cover such a line. Counting each node once per kind, 123 cover
9752+ // a typespec attribute (@spec/@type/@typep/@opaque/@callback/
9753+ // @macrocallback), 100 an @doc or @typedoc, 46 a bare comment line, 14 an
9754+ // @impl, and none a `use`, `alias`, `import`, `require` or a module-level
9755+ // `quote`. Swallowed typespecs are the largest attribute kind, not an
9756+ // absent one.
9757+ //
9758+ // The widened span also admits a phantom call that a narrower span kept out,
9759+ // which is why the head suppression in extract_calls.c travels with this fold
9760+ // rather than after it. cbm.c flags self-recursion by finding the innermost
9761+ // Function whose [start_line, end_line] contains a recorded call whose short
9762+ // name matches the function's own, so any pre-existing phantom between the
9763+ // first and the last clause becomes a self-edge as soon as the span covers it.
9764+ // That cost is real and is measured rather than asserted away. Same corpus,
9765+ // reading each flagged node's own source span for any line that names it as a
9766+ // call, a capture or a pipe target, counting a one-line clause's body after
9767+ // `, do:` as such a line: self_recursive Function nodes go 129 -> 430, and the
9768+ // ones carrying no self-call form anywhere in their span go 6 -> 48. The lines
9769+ // responsible are the typespec heads the widened span now covers -- `@spec
9770+ // f(t) :: u` puts the declared name in a `call` node, and the unified walk
9771+ // reads it as code -- a separate defect with a separate fix, not something this
9772+ // fold can close.
9773+ //
9774+ // Skipping the typespec subtrees, later in this stack, closes that half: the
9775+ // same method then reports 389 self_recursive Function nodes and 8 with no
9776+ // self-call form, against 129 and 6 on the base. All 8 are the pre-existing
9777+ // phantom a local variable sharing the function's name mints (`defp slug(value)
9778+ // do slug = ...`), 6 of them already present on the base; the other 2 are
9779+ // guarded clauses that only have a node of their own to be flagged on because
9780+ // of this stack.
9781+ //
9782+ // What the fold and the head suppression together remove is 2,436 edges, none
9783+ // of them added back: see elixir_call_is_definition_role in extract_calls.c.
9784+ //
9785+ // A clause whose macro differs (`def` foo/1 beside `defp` foo/2) still folds,
9786+ // because the arity-free QN already puts both on one node; is_exported is then
9787+ // the OR over the folded clauses, so a name any clause exports stays exported.
9788+ // That OR changes a recorded flag, which the node diff confirms and which is
9789+ // worth stating rather than leaving to be discovered: on the same corpus
9790+ // exactly 6 Function nodes change is_exported, all false -> true and none the
9791+ // other way. Each is a `def` clause written above a `defp` clause of the same
9792+ // name, where the pre-patch last-clause-wins record reported a publicly
9793+ // callable name as unexported. Verified against the source in every one: the
9794+ // new value is the correct one.
9795+ // Returns true when the clause folded.
9796+ static bool fold_elixir_clause(CBMExtractCtx *ctx, const char *qn, TSNode scope, TSNode node,
9797+ uint32_t end_line, bool is_exported) {
9798+ if (!qn || ctx->result->defs.count <= 0 || ts_node_is_null(scope)) {
9799+ return false;
9800+ }
9801+ TSNode here = ts_node_parent(node);
9802+ if (ts_node_is_null(here) || !ts_node_eq(here, scope)) {
9803+ return false;
9804+ }
9805+ CBMDefinition *prev = &ctx->result->defs.items[ctx->result->defs.count - 1];
9806+ if (!prev->label || strcmp(prev->label, "Function") != 0 || !prev->qualified_name ||
9807+ strcmp(prev->qualified_name, qn) != 0) {
9808+ return false;
9809+ }
9810+ /* Only end_line moves. prev->start_line is already the minimum, so this
9811+ * takes no start_line argument: a clause folds only into the def pushed
9812+ * for the immediately preceding extracted clause of the same do_block, and
9813+ * extract_elixir_call pushes a do_block's children onto its stack in
9814+ * reverse index order so they pop in source order. A `start_line <
9815+ * prev->start_line` guard here would be unreachable; if that traversal
9816+ * ever stops being source-ordered, this is the line that has to change
9817+ * with it. */
9818+ if (end_line > prev->end_line) {
9819+ prev->end_line = end_line;
9820+ }
9821+ prev->is_exported = prev->is_exported || is_exported;
9822+ return true;
9823+ }
9824+
9825+ // Handle Elixir def/defp/defmacro — extract function definition. `scope` is the
9826+ // module body the previous clause was extracted from, and is updated to this
9827+ // def's own module body; see fold_elixir_clause for what it bounds.
9828+ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char *macro,
9829+ TSNode *scope) {
97199830 CBMArena *a = ctx->arena;
97209831 TSNode args = elixir_call_args(node);
97219832 if (ts_node_is_null(args)) {
@@ -9727,6 +9838,16 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
97279838 return;
97289839 }
97299840
9841+ // `def name(args) when guard` parses the whole head as a `when`
9842+ // binary_operator, so the name lives on its left operand rather than
9843+ // directly under the call. Without unwrapping it, every guarded clause is
9844+ // dropped, and a function whose clauses ALL carry guards never appears in
9845+ // the graph at all -- silently, since a missing definition is not an error.
9846+ // The unwrap lives in helpers.c because it peels only `when`: an operator
9847+ // definition (`def a + b`) is a binary_operator head too, and unwrapping
9848+ // that one would name the function after its own left parameter.
9849+ first_arg = cbm_elixir_def_head_unwrap_guard(first_arg);
9850+
97309851 const char *fk = ts_node_type(first_arg);
97319852 char *name = NULL;
97329853 if (strcmp(fk, "call") == 0 && ts_node_child_count(first_arg) > 0) {
@@ -9738,15 +9859,25 @@ static void extract_elixir_func_def(CBMExtractCtx *ctx, TSNode node, const char
97389859 return;
97399860 }
97409861
9862+ const char *qn = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);
9863+ uint32_t start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
9864+ uint32_t end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
9865+ bool is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0);
9866+ bool folded = fold_elixir_clause(ctx, qn, *scope, node, end_line, is_exported);
9867+ *scope = ts_node_parent(node);
9868+ if (folded) {
9869+ return;
9870+ }
9871+
97419872 CBMDefinition def;
97429873 memset(&def, 0, sizeof(def));
97439874 def.name = name;
9744- def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name) ;
9875+ def.qualified_name = qn ;
97459876 def.label = "Function";
97469877 def.file_path = ctx->rel_path;
9747- def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET ;
9748- def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET ;
9749- def.is_exported = (strcmp(macro, "def") == 0 || strcmp(macro, "defmacro") == 0) ;
9878+ def.start_line = start_line ;
9879+ def.end_line = end_line ;
9880+ def.is_exported = is_exported ;
97509881 cbm_defs_push(&ctx->result->defs, a, def);
97519882}
97529883
@@ -9783,6 +9914,10 @@ static TSNode emit_elixir_module_class(CBMExtractCtx *ctx, TSNode cur) {
97839914// without recursion between extract_elixir_call ↔ extract_elixir_module_def.
97849915static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
97859916 (void)spec;
9917+ /* Module body the last extracted clause was written in; see
9918+ * fold_elixir_clause. Null until the first def, so nothing folds into a
9919+ * def left over from a previous top-level call node. */
9920+ TSNode def_scope = {0};
97869921 TSNodeStack stack;
97879922 ts_nstack_init(&stack, ctx, CBM_SZ_64);
97889923 ts_nstack_push(&stack, node);
@@ -9805,12 +9940,17 @@ static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
98059940
98069941 if (strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
98079942 strcmp(macro, "defmacro") == 0) {
9808- extract_elixir_func_def(ctx, cur, macro);
9943+ extract_elixir_func_def(ctx, cur, macro, &def_scope );
98099944 } else if (strcmp(macro, "if") == 0 || strcmp(macro, "unless") == 0) {
98109945 /* A compile-time if/unless in a module body defines functions
98119946 * under a condition: its do and else branches hold variants of the
98129947 * same definitions (graph_buffer.c, "Definition variants"). The
9813- * grammar nests the else_block inside the do_block. */
9948+ * grammar nests the else_block inside the do_block.
9949+ * The clause fold composes with this: fold_elixir_clause only
9950+ * folds a clause into one written in the same block, and each
9951+ * branch is its own block, so the clauses of one branch fold into
9952+ * one definition while the branches stay separate definitions that
9953+ * the graph buffer merges as variants of one identity. */
98149954 TSNode branch = cbm_find_child_by_kind(cur, "do_block");
98159955 for (int pass = 0; pass < 2 && !ts_node_is_null(branch); pass++) {
98169956 uint32_t bc = ts_node_child_count(branch);
0 commit comments