@colbymchenry/codegraph-darwin-x64 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/lib/dist/bin/codegraph.js +79 -52
  2. package/lib/dist/bin/codegraph.js.map +1 -1
  3. package/lib/dist/bin/command-supervision.d.ts +12 -0
  4. package/lib/dist/bin/command-supervision.d.ts.map +1 -0
  5. package/lib/dist/bin/command-supervision.js +76 -0
  6. package/lib/dist/bin/command-supervision.js.map +1 -0
  7. package/lib/dist/db/queries.d.ts.map +1 -1
  8. package/lib/dist/db/queries.js +10 -2
  9. package/lib/dist/db/queries.js.map +1 -1
  10. package/lib/dist/directory.d.ts +32 -0
  11. package/lib/dist/directory.d.ts.map +1 -1
  12. package/lib/dist/directory.js +83 -0
  13. package/lib/dist/directory.js.map +1 -1
  14. package/lib/dist/extraction/index.d.ts +13 -1
  15. package/lib/dist/extraction/index.d.ts.map +1 -1
  16. package/lib/dist/extraction/index.js +219 -213
  17. package/lib/dist/extraction/index.js.map +1 -1
  18. package/lib/dist/extraction/parse-pool.d.ts +126 -0
  19. package/lib/dist/extraction/parse-pool.d.ts.map +1 -0
  20. package/lib/dist/extraction/parse-pool.js +319 -0
  21. package/lib/dist/extraction/parse-pool.js.map +1 -0
  22. package/lib/dist/extraction/tree-sitter.d.ts.map +1 -1
  23. package/lib/dist/extraction/tree-sitter.js +48 -19
  24. package/lib/dist/extraction/tree-sitter.js.map +1 -1
  25. package/lib/dist/mcp/daemon-paths.d.ts +30 -3
  26. package/lib/dist/mcp/daemon-paths.d.ts.map +1 -1
  27. package/lib/dist/mcp/daemon-paths.js +50 -10
  28. package/lib/dist/mcp/daemon-paths.js.map +1 -1
  29. package/lib/dist/mcp/daemon-registry.d.ts.map +1 -1
  30. package/lib/dist/mcp/daemon-registry.js +7 -3
  31. package/lib/dist/mcp/daemon-registry.js.map +1 -1
  32. package/lib/dist/mcp/daemon.d.ts +38 -0
  33. package/lib/dist/mcp/daemon.d.ts.map +1 -1
  34. package/lib/dist/mcp/daemon.js +164 -31
  35. package/lib/dist/mcp/daemon.js.map +1 -1
  36. package/lib/dist/mcp/engine.d.ts +17 -0
  37. package/lib/dist/mcp/engine.d.ts.map +1 -1
  38. package/lib/dist/mcp/engine.js +73 -1
  39. package/lib/dist/mcp/engine.js.map +1 -1
  40. package/lib/dist/mcp/index.d.ts.map +1 -1
  41. package/lib/dist/mcp/index.js +25 -43
  42. package/lib/dist/mcp/index.js.map +1 -1
  43. package/lib/dist/mcp/ppid-watchdog.d.ts +18 -0
  44. package/lib/dist/mcp/ppid-watchdog.d.ts.map +1 -1
  45. package/lib/dist/mcp/ppid-watchdog.js +37 -0
  46. package/lib/dist/mcp/ppid-watchdog.js.map +1 -1
  47. package/lib/dist/mcp/query-pool.d.ts +94 -0
  48. package/lib/dist/mcp/query-pool.d.ts.map +1 -0
  49. package/lib/dist/mcp/query-pool.js +297 -0
  50. package/lib/dist/mcp/query-pool.js.map +1 -0
  51. package/lib/dist/mcp/query-worker.d.ts +24 -0
  52. package/lib/dist/mcp/query-worker.d.ts.map +1 -0
  53. package/lib/dist/mcp/query-worker.js +87 -0
  54. package/lib/dist/mcp/query-worker.js.map +1 -0
  55. package/lib/dist/mcp/tools.d.ts +57 -0
  56. package/lib/dist/mcp/tools.d.ts.map +1 -1
  57. package/lib/dist/mcp/tools.js +147 -37
  58. package/lib/dist/mcp/tools.js.map +1 -1
  59. package/lib/dist/project-config.d.ts +20 -0
  60. package/lib/dist/project-config.d.ts.map +1 -1
  61. package/lib/dist/project-config.js +42 -2
  62. package/lib/dist/project-config.js.map +1 -1
  63. package/lib/dist/resolution/c-fnptr-synthesizer.d.ts +0 -28
  64. package/lib/dist/resolution/c-fnptr-synthesizer.d.ts.map +1 -1
  65. package/lib/dist/resolution/c-fnptr-synthesizer.js +765 -79
  66. package/lib/dist/resolution/c-fnptr-synthesizer.js.map +1 -1
  67. package/lib/dist/resolution/name-matcher.d.ts.map +1 -1
  68. package/lib/dist/resolution/name-matcher.js +44 -0
  69. package/lib/dist/resolution/name-matcher.js.map +1 -1
  70. package/lib/node_modules/.package-lock.json +1 -1
  71. package/lib/package.json +1 -1
  72. package/package.json +1 -1
@@ -1,6 +1,90 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.cFnPointerDispatchEdges = cFnPointerDispatchEdges;
37
+ /**
38
+ * C/C++ function-pointer dispatch synthesis (#932).
39
+ *
40
+ * C/C++ polymorphism is the function pointer: a struct carries a fn-pointer
41
+ * field (`int (*fn)(int)`, or a fn-pointer-typedef field `hook_func func`),
42
+ * concrete functions are *registered* into it through a table
43
+ * (`static struct cmd cmds[] = {{"add", cmd_add}, …}`, a designated
44
+ * `.fn = cmd_add`, or `x->fn = cmd_add`), and the dispatcher calls through it
45
+ * indirectly (`p->fn(argv)`). Static extraction captures neither the
46
+ * registration→field binding nor the indirect call, so the dispatcher→handler
47
+ * edge is missing and `git`'s `run_builtin` looks like it calls nothing, the
48
+ * hooks in `hook_demo.c` are unreachable, etc.
49
+ *
50
+ * This bridges it, keyed by **(struct type, fn-pointer field)**:
51
+ * • registrations — a function bound to `S.field` via a positional
52
+ * initializer (matched by field index), a designated `.field = fn`, or a
53
+ * direct `x.field = fn` / `x->field = fn` assignment;
54
+ * • dispatch — `recv->field(…)` / `recv.field(…)` where `recv` resolves to a
55
+ * value of struct type `S` (from the enclosing function's params / locals,
56
+ * or by walking a chained/array receiver `c->cmd->proc` across field types),
57
+ * falling back to the field name when it is unique to one struct;
58
+ * • field←field propagation — `a->f = b->g` merges `B.g`'s handlers into
59
+ * `A.f`, so a generic single-slot hook that is reassigned from a registry
60
+ * (the `hook_demo.c` shape: `h->func = found->fn`) still resolves.
61
+ *
62
+ * Also handles **macro-built tables** (#991) — the dominant real-world shape,
63
+ * e.g. redis' command table, sqlite's builtin functions, and vim's `:ex` /
64
+ * normal-mode commands. The fn-pointer arg lives inside a macro call
65
+ * (`MAKE_CMD(…,proc,…)` / `FUNCTION(…,xFunc)` / `EXCMD(…,fn,…)`) in a generated
66
+ * or `#include`-d file; the table's struct type may itself be an object-macro
67
+ * alias; the field may use a function-TYPE typedef; the struct may be defined
68
+ * INLINE with the array; and the whole thing may sit behind `#ifdef` switched on
69
+ * by the includer. The registration pass reads each `#include`-d file as a unit
70
+ * with the includer's effective macro env (own + headers) in scope, evaluates
71
+ * its `#ifdef`s against the includer's defined set, expands object/function
72
+ * macros, peels a brace-wrapped element, and parses an inline struct in place —
73
+ * then reads the positional/designated bindings. Dispatch additionally resolves
74
+ * an array subscript through a file-scope table (`(cmdnames[i].cmd_func)(…)`).
75
+ *
76
+ * Also bridges **bare arrays of function pointers** (no struct, no field) —
77
+ * `opcode_t *opcodes[256] = {nop,…}` dispatched `opcodes[op](…)` (SameBoy's CPU),
78
+ * `zend_rc_dtor_func_t t[] = {[IS_STRING]=(cast)fn,…}` dispatched `t[GC_TYPE(p)](…)`
79
+ * (php's Zend) — keyed by the array VARIABLE name. The element type must be a
80
+ * function typedef (the precision gate), entries are literal function names, and
81
+ * the same-file table wins on a name collision (two file-local `opcodes[256]`).
82
+ *
83
+ * Whole-graph pass after base resolution; all edges are `provenance:'heuristic'`
84
+ * (`synthesizedBy:'fn-pointer-dispatch'`). High precision via the (type, field)
85
+ * key + a real-function gate; a project with no fn-pointer dispatch is a no-op.
86
+ */
87
+ const path = __importStar(require("node:path"));
4
88
  const strip_comments_1 = require("./strip-comments");
5
89
  const C_CPP_EXT = /\.(c|h|cc|cpp|cxx|hpp|hh|hxx|cppm|ipp|inl|tcc)$/i;
6
90
  const FN_KINDS = new Set(['function', 'method']);
@@ -44,26 +128,257 @@ function splitTopLevel(body, sep) {
44
128
  out.push(body.slice(start));
45
129
  return out;
46
130
  }
47
- /** A fn-pointer field looks like `… (*name)(…)` capture `name`. */
48
- const FNPTR_DECL_RE = /\(\s*\*\s*(\w+)\s*\)\s*\(/;
49
- /** `typedef RET (*NAME)(…)` — a function-pointer typedef. */
50
- const FNPTR_TYPEDEF_RE = /\btypedef\b[^;{}]*?\(\s*\*\s*(\w+)\s*\)\s*\(/g;
131
+ /** Index of the `)` matching the `(` at `open` (which must point at a `(`). -1 if unbalanced. */
132
+ function matchParen(src, open) {
133
+ let depth = 0;
134
+ for (let i = open; i < src.length; i++) {
135
+ const c = src[i];
136
+ if (c === '(')
137
+ depth++;
138
+ else if (c === ')') {
139
+ depth--;
140
+ if (depth === 0)
141
+ return i;
142
+ }
143
+ }
144
+ return -1;
145
+ }
146
+ /**
147
+ * Collect function-like macros from (comment-stripped) source, joining
148
+ * `\`-continuations first. Only object/positional table macros matter here, so
149
+ * variadic macros are skipped. Used to expand registration tables built through
150
+ * a macro (redis' `MAKE_CMD(…)`) before reading the struct-field bindings.
151
+ */
152
+ function parseFunctionMacros(stripped) {
153
+ const out = new Map();
154
+ if (!stripped.includes('#define') && !stripped.includes('# define'))
155
+ return out;
156
+ const joined = stripped.replace(/\\\r?\n/g, ' ');
157
+ const RE = /^[ \t]*#[ \t]*define[ \t]+(\w+)\(([^)]*)\)\s+(.+)$/gm;
158
+ let m;
159
+ while ((m = RE.exec(joined))) {
160
+ const params = m[2].split(',').map((p) => p.trim()).filter(Boolean);
161
+ if (params.some((p) => p === '...' || p.endsWith('...')))
162
+ continue; // variadic — skip
163
+ out.set(m[1], { params, expansion: m[3].trim() });
164
+ }
165
+ return out;
166
+ }
167
+ /**
168
+ * Collect object-like macros `#define NAME value` (NAME not immediately followed
169
+ * by `(`). redis aliases the table's struct type this way:
170
+ * `#define COMMAND_STRUCT redisCommand`, used as `struct COMMAND_STRUCT table[]`.
171
+ */
172
+ function parseObjectMacros(stripped) {
173
+ const out = new Map();
174
+ if (!stripped.includes('#define') && !stripped.includes('# define'))
175
+ return out;
176
+ const joined = stripped.replace(/\\\r?\n/g, ' ');
177
+ const RE = /^[ \t]*#[ \t]*define[ \t]+(\w+)[ \t]+(\S[^\n]*)$/gm;
178
+ let m;
179
+ while ((m = RE.exec(joined)))
180
+ out.set(m[1], m[2].trim());
181
+ return out;
182
+ }
183
+ /** All macro names a file `#define`s (value-ful or not) — the "defined" set for #ifdef. */
184
+ function parseDefinedNames(stripped) {
185
+ const out = new Set();
186
+ if (!stripped.includes('#define') && !stripped.includes('# define'))
187
+ return out;
188
+ const RE = /^[ \t]*#[ \t]*define[ \t]+(\w+)/gm;
189
+ let m;
190
+ while ((m = RE.exec(stripped)))
191
+ out.add(m[1]);
192
+ return out;
193
+ }
194
+ /**
195
+ * Drop the inactive arms of `#ifdef`/`#ifndef`/`#if defined(X)`/`#else`/`#elif`/
196
+ * `#endif` given a set of defined macro names, keeping line offsets (inactive
197
+ * lines are blanked, not removed). A conditional whose expression we can't
198
+ * evaluate (`#if SOME_EXPR`) keeps its body — better to over-keep than to drop
199
+ * live code. This is what makes a header included with a switch macro defined
200
+ * (vim's `ex_cmds.h` under `DO_DECLARE_EXCMD`) expose only its active table.
201
+ */
202
+ function evalConditionals(text, defined) {
203
+ if (!/#\s*if/.test(text))
204
+ return text;
205
+ const lines = text.split('\n');
206
+ // stack frame: parentActive = enclosing kept?; active = this arm kept?; taken = any arm taken yet
207
+ const stack = [];
208
+ const activeNow = () => (stack.length === 0 ? true : stack[stack.length - 1].active);
209
+ const condDefined = (expr) => {
210
+ let mm = expr.match(/^defined\s*\(?\s*(\w+)\s*\)?$/);
211
+ if (mm)
212
+ return defined.has(mm[1]);
213
+ mm = expr.match(/^!\s*defined\s*\(?\s*(\w+)\s*\)?$/);
214
+ if (mm)
215
+ return !defined.has(mm[1]);
216
+ return null; // unevaluable
217
+ };
218
+ for (let i = 0; i < lines.length; i++) {
219
+ const t = lines[i].trim();
220
+ let mm;
221
+ if ((mm = t.match(/^#\s*ifdef\s+(\w+)/))) {
222
+ const pa = activeNow();
223
+ const cond = defined.has(mm[1]);
224
+ stack.push({ parentActive: pa, active: pa && cond, taken: cond });
225
+ lines[i] = '';
226
+ continue;
227
+ }
228
+ if ((mm = t.match(/^#\s*ifndef\s+(\w+)/))) {
229
+ const pa = activeNow();
230
+ const cond = !defined.has(mm[1]);
231
+ stack.push({ parentActive: pa, active: pa && cond, taken: cond });
232
+ lines[i] = '';
233
+ continue;
234
+ }
235
+ if ((mm = t.match(/^#\s*if\s+(.+)$/))) {
236
+ const pa = activeNow();
237
+ const c = condDefined(mm[1].trim());
238
+ const cond = c === null ? true : c; // unevaluable → keep
239
+ stack.push({ parentActive: pa, active: pa && cond, taken: cond });
240
+ lines[i] = '';
241
+ continue;
242
+ }
243
+ if (/^#\s*elif\b/.test(t)) {
244
+ const top = stack[stack.length - 1];
245
+ if (top) {
246
+ top.active = top.parentActive && !top.taken;
247
+ top.taken = true;
248
+ }
249
+ lines[i] = '';
250
+ continue;
251
+ }
252
+ if (/^#\s*else\b/.test(t)) {
253
+ const top = stack[stack.length - 1];
254
+ if (top) {
255
+ top.active = top.parentActive && !top.taken;
256
+ top.taken = true;
257
+ }
258
+ lines[i] = '';
259
+ continue;
260
+ }
261
+ if (/^#\s*endif\b/.test(t)) {
262
+ stack.pop();
263
+ lines[i] = '';
264
+ continue;
265
+ }
266
+ if (!activeNow())
267
+ lines[i] = ''; // blank an inactive line (keep the newline)
268
+ }
269
+ return lines.join('\n');
270
+ }
271
+ /** Resolve a type token through object-like macro aliases (transitive, capped). */
272
+ function resolveTypeName(name, objEnv) {
273
+ let n = name;
274
+ for (let i = 0; objEnv && i < 5; i++) {
275
+ const v = objEnv.get(n);
276
+ const t = v?.trim().match(/^(?:struct\s+)?(\w+)$/);
277
+ if (!t)
278
+ break;
279
+ n = t[1];
280
+ }
281
+ return n;
282
+ }
283
+ /** Substitute call args for the macro's params (whole-token) in its expansion. */
284
+ function substituteMacro(def, args) {
285
+ const map = new Map();
286
+ def.params.forEach((p, i) => map.set(p, args[i] ?? ''));
287
+ return def.expansion.replace(/\b\w+\b/g, (tok) => (map.has(tok) ? map.get(tok) : tok));
288
+ }
289
+ /**
290
+ * Expand known function-like macro calls in `text` to a fixpoint (depth-capped).
291
+ * `MAKE_CMD("get",…,getCommand,…)` → the positional value list whose slots line
292
+ * up with the struct's fields, so the existing positional registration can read
293
+ * `getCommand` straight out of the `proc` slot.
294
+ */
295
+ function expandMacroCalls(text, env) {
296
+ if (env.size === 0)
297
+ return text;
298
+ let out = text;
299
+ for (let pass = 0; pass < 6; pass++) {
300
+ let changed = false;
301
+ const RE = /\b(\w+)\s*\(/g;
302
+ let m;
303
+ while ((m = RE.exec(out))) {
304
+ const def = env.get(m[1]);
305
+ if (!def)
306
+ continue;
307
+ const open = m.index + m[0].length - 1; // index of the `(`
308
+ const close = matchParen(out, open);
309
+ if (close < 0)
310
+ continue;
311
+ const args = splitTopLevel(out.slice(open + 1, close), ',').map((a) => a.trim());
312
+ out = out.slice(0, m.index) + substituteMacro(def, args) + out.slice(close + 1);
313
+ changed = true;
314
+ break; // restart scan — offsets shifted
315
+ }
316
+ if (!changed)
317
+ break;
318
+ }
319
+ return out;
320
+ }
321
+ /** A fn-pointer field looks like `… (*name)(…)` — capture `name`. A
322
+ * calling-convention / attribute macro may precede the `*`
323
+ * (`(ZEND_FASTCALL *name)`), so allow leading word tokens. */
324
+ const FNPTR_DECL_RE = /\(\s*(?:\w+\s+)*\*\s*(\w+)\s*\)\s*\(/;
325
+ /** `typedef RET (*NAME)(…)` — a function-pointer typedef (CC/attr macro before
326
+ * the `*` allowed, as in php's `typedef void (ZEND_FASTCALL *fn_t)(…)`). */
327
+ const FNPTR_TYPEDEF_RE = /\btypedef\b[^;{}]*?\(\s*(?:\w+\s+)*\*\s*(\w+)\s*\)\s*\(/g;
328
+ /** A whole brace-free `typedef … ;` statement — capture the guts to spot the
329
+ * function-TYPE form `typedef RET NAME(params)` (no `(*name)` pointer form). */
330
+ const FNTYPE_TYPEDEF_STMT_RE = /\btypedef\b([^;{}]*);/g;
331
+ /** Return-type keywords that must never be mistaken for the typedef's name. */
332
+ const C_TYPE_KEYWORDS = new Set([
333
+ 'void', 'int', 'char', 'short', 'long', 'unsigned', 'signed', 'float', 'double',
334
+ 'const', 'struct', 'union', 'enum', 'static', 'volatile', 'register', 'inline',
335
+ ]);
336
+ /** `#include "local/header"` — captured from RAW source (string contents survive). */
337
+ const INCLUDE_RE = /#[ \t]*include[ \t]+"([^"\n]+)"/g;
338
+ /** Included files worth scanning for registration tables (e.g. a generated `.def`). */
339
+ const INCLUDABLE_EXT = /\.(def|inc|h|hh|hpp|hxx|c|cc|cpp|cxx|ipp|tcc|tbl)$/i;
51
340
  function cFnPointerDispatchEdges(queries, ctx) {
52
341
  const files = ctx.getAllFiles().filter((f) => C_CPP_EXT.test(f));
53
342
  if (files.length === 0)
54
343
  return [];
55
- // Cache stripped source per file (read once, reused across passes).
344
+ // Cache raw + stripped source per file (read once, reused across passes).
345
+ // Raw is needed for `#include "…"` directives — strip blanks string contents.
346
+ const rawCache = new Map();
347
+ const raw = (file) => {
348
+ if (rawCache.has(file))
349
+ return rawCache.get(file);
350
+ const r = ctx.readFile(file);
351
+ rawCache.set(file, r);
352
+ return r;
353
+ };
56
354
  const srcCache = new Map();
57
355
  const src = (file) => {
58
356
  if (srcCache.has(file))
59
357
  return srcCache.get(file);
60
- const raw = ctx.readFile(file);
61
- const s = raw == null ? '' : (0, strip_comments_1.stripCommentsForRegex)(raw, 'c');
358
+ const r = raw(file);
359
+ const s = r == null ? '' : (0, strip_comments_1.stripCommentsForRegex)(r, 'c');
62
360
  srcCache.set(file, s);
63
- return raw == null ? null : s;
361
+ return r == null ? null : s;
362
+ };
363
+ // Resolve a quoted include relative to the includer's directory, then the
364
+ // project root. Returns a project-root-relative path that exists on disk
365
+ // (even if it was never indexed — e.g. redis' generated `commands.def`).
366
+ const resolveInclude = (includer, inc) => {
367
+ const dir = path.posix.dirname(includer.replace(/\\/g, '/'));
368
+ const cand = path.posix.normalize(path.posix.join(dir, inc));
369
+ if (ctx.fileExists(cand))
370
+ return cand;
371
+ if (ctx.fileExists(inc))
372
+ return inc;
373
+ return null;
64
374
  };
65
- // ---- Pass A: function-pointer typedefs (cross-file) ----
375
+ // ---- Pass A: function-pointer AND function-type typedefs (cross-file) ----
376
+ // fn-pointer: typedef RET (*NAME)(…) → a field `NAME f` is a fn ptr
377
+ // fn-type: typedef RET NAME(params) → a field `NAME *f` is a fn ptr
378
+ // The fn-type form is redis' command idiom: `typedef void redisCommandProc(client*)`
379
+ // declared as `redisCommandProc *proc;`. Without this, `proc` reads as data.
66
380
  const fnPtrTypedefs = new Set();
381
+ const fnTypeTypedefs = new Set();
67
382
  for (const file of files) {
68
383
  const s = src(file);
69
384
  if (!s || !s.includes('typedef'))
@@ -72,60 +387,108 @@ function cFnPointerDispatchEdges(queries, ctx) {
72
387
  let m;
73
388
  while ((m = FNPTR_TYPEDEF_RE.exec(s)))
74
389
  fnPtrTypedefs.add(m[1]);
390
+ FNTYPE_TYPEDEF_STMT_RE.lastIndex = 0;
391
+ while ((m = FNTYPE_TYPEDEF_STMT_RE.exec(s))) {
392
+ const guts = m[1];
393
+ if (guts.includes('(*') || guts.includes('( *'))
394
+ continue; // pointer form — handled above
395
+ const fm = guts.match(/\b(\w+)\s*\(/); // last identifier before the param list
396
+ if (fm && !C_TYPE_KEYWORDS.has(fm[1]))
397
+ fnTypeTypedefs.add(fm[1]);
398
+ }
75
399
  }
76
400
  // ---- Pass B: struct field layouts ----
77
- // structLayout: struct name → ordered fields (with fn-pointer flag).
401
+ // structLayout: struct name → ordered fields, for structs with ≥1 fn-pointer
402
+ // field (drives positional registration + dispatch).
403
+ // allStructFields: EVERY struct name → ALL its field layouts (a name can be
404
+ // reused across files — e.g. redis has two unrelated `client` structs), used
405
+ // to walk a chained receiver's field types (`c->cmd->proc`: client.cmd →
406
+ // redisCommand). The walk searches every same-named layout for the field.
78
407
  // fieldToStructs: fn-pointer field name → set of struct names that declare it.
79
408
  const structLayout = new Map();
409
+ const allStructFields = new Map();
80
410
  const fieldToStructs = new Map();
81
- for (const st of ctx.getNodesByKind('struct')) {
82
- if (!C_CPP_EXT.test(st.filePath))
83
- continue;
84
- const s = srcCache.get(st.filePath) ?? src(st.filePath);
85
- if (!s)
86
- continue;
87
- const body = sliceLines(s, st.startLine, st.endLine);
88
- const open = body.indexOf('{');
89
- const close = open >= 0 ? matchBrace(body, open) : -1;
90
- if (open < 0 || close < 0)
91
- continue;
92
- const inner = body.slice(open + 1, close);
411
+ // Parse a struct body (the text between its `{` and `}`) into ordered fields.
412
+ const parseStructFields = (inner) => {
93
413
  const fields = [];
94
414
  let idx = 0;
95
415
  for (const rawDecl of splitTopLevel(inner, ';')) {
96
416
  const decl = rawDecl.trim();
97
417
  if (!decl)
98
418
  continue;
99
- let name = null;
100
- let isFnPtr = false;
101
- const ptr = decl.match(FNPTR_DECL_RE);
102
- if (ptr) {
103
- name = ptr[1];
104
- isFnPtr = true;
105
- }
106
- else {
107
- // `TYPE [*]name` fn-pointer when TYPE is a fn-pointer typedef.
108
- const fm = decl.match(/(\w+)\s+\*?\s*(\w+)\s*$/);
109
- if (fm) {
110
- name = fm[2];
111
- isFnPtr = fnPtrTypedefs.has(fm[1]);
419
+ // A field decl can declare several names sharing a leading type:
420
+ // `struct redisCommand *cmd, *lastcmd;`. Each declarator is its own
421
+ // positional slot and carries that type (so `client.cmd → redisCommand`).
422
+ const parts = splitTopLevel(decl, ',');
423
+ const firstTyped = parts[0].match(/(\w+)\s+\**\s*(\w+)\s*$/);
424
+ const sharedType = firstTyped ? firstTyped[1] : '';
425
+ for (let pi = 0; pi < parts.length; pi++) {
426
+ const p = parts[pi].trim();
427
+ let name = null;
428
+ let type = '';
429
+ let isFnPtr = false;
430
+ const ptr = p.match(FNPTR_DECL_RE);
431
+ if (ptr) {
432
+ name = ptr[1]; // `… (*name)(…)` — a function pointer
433
+ isFnPtr = true;
434
+ }
435
+ else if (pi === 0) {
436
+ if (firstTyped) {
437
+ name = firstTyped[2];
438
+ type = sharedType;
439
+ }
440
+ }
441
+ else {
442
+ // a subsequent declarator: `*name` / `**name` / `name`
443
+ const dm = p.match(/^\**\s*(\w+)/);
444
+ if (dm) {
445
+ name = dm[1];
446
+ type = sharedType;
447
+ }
112
448
  }
449
+ if (!ptr && type)
450
+ isFnPtr = fnPtrTypedefs.has(type) || fnTypeTypedefs.has(type);
451
+ // Always advance the positional index. An unparsed field (anonymous
452
+ // union, exotic declarator) still occupies one slot, and macro-expanded
453
+ // positional tables (redis' MAKE_CMD) only align if every field counts.
454
+ fields.push({ name: name ?? '', index: idx, isFnPtr: !!name && isFnPtr, type });
455
+ idx++;
113
456
  }
114
- if (!name)
115
- continue;
116
- fields.push({ name, index: idx, isFnPtr });
117
- if (isFnPtr) {
118
- if (!fieldToStructs.has(name))
119
- fieldToStructs.set(name, new Set());
120
- fieldToStructs.get(name).add(st.name);
457
+ }
458
+ return fields;
459
+ };
460
+ // Register a parsed struct under `name` into the three indexes.
461
+ const registerStructLayout = (name, fields) => {
462
+ if (!allStructFields.has(name))
463
+ allStructFields.set(name, []);
464
+ allStructFields.get(name).push(fields);
465
+ for (const f of fields) {
466
+ if (f.name && f.isFnPtr) {
467
+ if (!fieldToStructs.has(f.name))
468
+ fieldToStructs.set(f.name, new Set());
469
+ fieldToStructs.get(f.name).add(name);
121
470
  }
122
- idx++;
123
471
  }
124
472
  if (fields.some((f) => f.isFnPtr))
125
- structLayout.set(st.name, fields);
473
+ structLayout.set(name, fields);
474
+ };
475
+ for (const st of ctx.getNodesByKind('struct')) {
476
+ if (!C_CPP_EXT.test(st.filePath))
477
+ continue;
478
+ const s = srcCache.get(st.filePath) ?? src(st.filePath);
479
+ if (!s)
480
+ continue;
481
+ const body = sliceLines(s, st.startLine, st.endLine);
482
+ const open = body.indexOf('{');
483
+ const close = open >= 0 ? matchBrace(body, open) : -1;
484
+ if (open < 0 || close < 0)
485
+ continue;
486
+ registerStructLayout(st.name, parseStructFields(body.slice(open + 1, close)));
126
487
  }
127
- if (structLayout.size === 0)
128
- return [];
488
+ // NB: no early return on an empty structLayout here — an inline `struct TAG
489
+ // { … } var[]` table whose struct never became a node (vim's `cmdname`, broken
490
+ // up by `#ifdef`) is discovered later during the unit scan. The `reg.size === 0`
491
+ // guard after registration still short-circuits when nothing bridges.
129
492
  const fnPtrFieldOf = (struct, field) => !!structLayout.get(struct)?.some((f) => f.name === field && f.isFnPtr);
130
493
  // C/C++ function + method nodes, materialized once (bounded by C/C++ files).
131
494
  const cFns = [];
@@ -157,12 +520,42 @@ function cFnPointerDispatchEdges(queries, ctx) {
157
520
  reg.get(key).add(fn.id);
158
521
  idToNode.set(fn.id, fn);
159
522
  };
523
+ // Bare arrays-of-fn-pointers (no struct): array VARIABLE name → per-file sets
524
+ // of registered function ids. Multi-entry because a file-scope `static` table
525
+ // name can recur across files (SameBoy declares `static opcode_t *opcodes[256]`
526
+ // in BOTH sm83_cpu.c and sm83_disassembler.c), so dispatch resolves same-file.
527
+ const arrayReg = new Map();
528
+ const addArrayReg = (name, file, fn) => {
529
+ let entries = arrayReg.get(name);
530
+ if (!entries) {
531
+ entries = [];
532
+ arrayReg.set(name, entries);
533
+ }
534
+ let e = entries.find((x) => x.file === file);
535
+ if (!e) {
536
+ e = { file, ids: new Set() };
537
+ entries.push(e);
538
+ }
539
+ e.ids.add(fn.id);
540
+ idToNode.set(fn.id, fn);
541
+ };
160
542
  // A struct value `{ … }` (one element) — register its function entries to the
161
543
  // struct's fields, by `.field = fn` designators or by positional slot.
162
- const registerStructValue = (struct, valueBody, file) => {
544
+ const registerStructValue = (struct, valueBody, file, env) => {
163
545
  const layout = structLayout.get(struct);
164
546
  if (!layout)
165
547
  return;
548
+ if (env && env.size)
549
+ valueBody = expandMacroCalls(valueBody, env);
550
+ // A macro can expand to a whole brace-wrapped element (sqlite's
551
+ // `FUNCTION(…)` → `{nArg, …, xFunc, …}`); peel one outer layer so the
552
+ // positional slots are visible.
553
+ valueBody = valueBody.trim();
554
+ if (valueBody.startsWith('{')) {
555
+ const e = matchBrace(valueBody, 0);
556
+ if (e > 0 && valueBody.slice(e + 1).trim() === '')
557
+ valueBody = valueBody.slice(1, e);
558
+ }
166
559
  const items = splitTopLevel(valueBody, ',');
167
560
  let pos = 0;
168
561
  for (const rawItem of items) {
@@ -192,18 +585,221 @@ function cFnPointerDispatchEdges(queries, ctx) {
192
585
  pos++;
193
586
  }
194
587
  };
588
+ // Collect the literal function entries of an array-of-fn-pointers initializer
589
+ // and register them under the array's variable name. Entries may be positional
590
+ // (`fn`, `&fn`), designated by index (`[OP] = fn`), or cast-wrapped
591
+ // (`(handler_t)fn`, as in php's Zend dtor table). Non-identifier entries
592
+ // (`NULL`, `0`, a nested expression) are skipped — a miss, never a wrong edge.
593
+ // No index tracking: a runtime subscript fans the dispatch out to the whole
594
+ // set, exactly like a command table reaches every command.
595
+ const registerArrayValue = (name, body, file, env) => {
596
+ if (env && env.size)
597
+ body = expandMacroCalls(body, env);
598
+ for (const rawItem of splitTopLevel(body, ',')) {
599
+ let item = rawItem.trim();
600
+ if (!item)
601
+ continue;
602
+ const des = item.match(/^\[[^\]]*\]\s*=\s*([\s\S]*)$/); // `[IDX] = …` designator
603
+ if (des)
604
+ item = des[1].trim();
605
+ item = item.replace(/^\((?:[\w\s*]+)\)\s*/, '').replace(/^&\s*/, '').trim(); // (cast) / &
606
+ const id = item.match(/^(\w+)$/);
607
+ if (!id)
608
+ continue;
609
+ const fn = resolveFn(id[1], file);
610
+ if (fn)
611
+ addArrayReg(name, file, fn);
612
+ }
613
+ };
614
+ // Per-file macro + include parsing (any file, indexed or not), cached.
615
+ const fnMacroCache = new Map();
616
+ const fileFnMacros = (file) => {
617
+ let m = fnMacroCache.get(file);
618
+ if (!m) {
619
+ m = parseFunctionMacros(src(file) ?? '');
620
+ fnMacroCache.set(file, m);
621
+ }
622
+ return m;
623
+ };
624
+ const objMacroCache = new Map();
625
+ const fileObjMacros = (file) => {
626
+ let m = objMacroCache.get(file);
627
+ if (!m) {
628
+ m = parseObjectMacros(src(file) ?? '');
629
+ objMacroCache.set(file, m);
630
+ }
631
+ return m;
632
+ };
633
+ const definedCache = new Map();
634
+ const fileDefinedNames = (file) => {
635
+ let d = definedCache.get(file);
636
+ if (!d) {
637
+ d = parseDefinedNames(src(file) ?? '');
638
+ definedCache.set(file, d);
639
+ }
640
+ return d;
641
+ };
642
+ const includeCache = new Map();
643
+ const localIncludesOf = (file) => {
644
+ let out = includeCache.get(file);
645
+ if (out)
646
+ return out;
647
+ out = [];
648
+ const rawText = raw(file);
649
+ if (rawText && rawText.includes('include')) {
650
+ INCLUDE_RE.lastIndex = 0;
651
+ let im;
652
+ while ((im = INCLUDE_RE.exec(rawText))) {
653
+ if (!INCLUDABLE_EXT.test(im[1]))
654
+ continue;
655
+ const t = resolveInclude(file, im[1]);
656
+ if (t)
657
+ out.push(t);
658
+ }
659
+ }
660
+ includeCache.set(file, out);
661
+ return out;
662
+ };
663
+ // A file's effective macro environment = its own #defines PLUS those of the
664
+ // headers it #includes (redis' `MAKE_CMD` sits beside the table; sqlite's
665
+ // `FUNCTION` lives in `sqliteInt.h`, included by the file with the table).
666
+ // First writer wins, so the file's own defs override included ones; depth-2
667
+ // covers a macro defined in a header-of-a-header.
668
+ const buildEnv = (file, depth, seen, fn, obj, def) => {
669
+ if (depth < 0 || seen.has(file))
670
+ return;
671
+ seen.add(file);
672
+ for (const [k, v] of fileFnMacros(file))
673
+ if (!fn.has(k))
674
+ fn.set(k, v);
675
+ for (const [k, v] of fileObjMacros(file))
676
+ if (!obj.has(k))
677
+ obj.set(k, v);
678
+ for (const n of fileDefinedNames(file))
679
+ def.add(n);
680
+ for (const inc of localIncludesOf(file))
681
+ buildEnv(inc, depth - 1, seen, fn, obj, def);
682
+ };
683
+ const indexedSet = new Set(files);
684
+ const units = [];
685
+ const seenInclude = new Set();
686
+ for (const file of files) {
687
+ const env = new Map();
688
+ const objEnv = new Map();
689
+ const defined = new Set();
690
+ buildEnv(file, 2, new Set(), env, objEnv, defined);
691
+ const s = src(file);
692
+ if (s)
693
+ units.push({ text: s, file, env, objEnv });
694
+ for (const target of localIncludesOf(file)) {
695
+ if (seenInclude.has(`${file}>${target}`))
696
+ continue;
697
+ const incSrc = src(target);
698
+ if (!incSrc)
699
+ continue;
700
+ if (indexedSet.has(target)) {
701
+ // Re-scan an indexed header only when this includer unlocks guarded code.
702
+ const ownDef = fileDefinedNames(target);
703
+ const adds = [...defined].some((n) => !ownDef.has(n));
704
+ if (!adds || !/#\s*if/.test(incSrc))
705
+ continue;
706
+ }
707
+ seenInclude.add(`${file}>${target}`);
708
+ // The include is pasted into the includer — evaluate its conditionals in
709
+ // the includer's defined set (a no-op when it has none). Re-parse the
710
+ // included file's OWN macros from that resolved text so a macro it defines
711
+ // conditionally (vim's `EXCMD`, whose plain last-wins parse picks the enum
712
+ // arm) overrides with the ARM THAT IS ACTUALLY ACTIVE here.
713
+ const text = evalConditionals(incSrc, defined);
714
+ const incEnv = new Map(env);
715
+ for (const [k, v] of parseFunctionMacros(text))
716
+ incEnv.set(k, v);
717
+ const incObjEnv = new Map(objEnv);
718
+ for (const [k, v] of parseObjectMacros(text))
719
+ incObjEnv.set(k, v);
720
+ units.push({ text, file: target, env: incEnv, objEnv: incObjEnv });
721
+ }
722
+ }
723
+ // Global variable → struct type, for resolving a dispatch through a file-scope
724
+ // table by subscript (`cmdnames[i].cmd_func(…)`).
725
+ const globalVarType = new Map();
726
+ // Process a `{ … }` initializer body (array of elements or a single struct).
727
+ const processInit = (struct, body, isArray, file, env) => {
728
+ if (isArray) {
729
+ for (const el of splitTopLevel(body, ',')) {
730
+ const t = el.trim();
731
+ if (t.startsWith('{')) {
732
+ const e = matchBrace(t, 0);
733
+ if (e > 0)
734
+ registerStructValue(struct, t.slice(1, e), file, env);
735
+ }
736
+ else if (t) {
737
+ // an element built by a macro (`MAKE_CMD(…)`/`FUNCTION(…)`) or a bare value
738
+ registerStructValue(struct, t, file, env);
739
+ }
740
+ }
741
+ }
742
+ else {
743
+ registerStructValue(struct, body, file, env);
744
+ }
745
+ };
195
746
  // `(?:struct )?TYPE name[opt] = {` initializers, where TYPE is a struct that
196
747
  // has ≥1 fn-pointer field. Handles both single (`= {…}`) and array
197
- // (`[] = { {…}, {…} }`) forms.
748
+ // (`[] = { {…}, {…} }`) forms. Macro calls inside an element are expanded first.
198
749
  const INIT_RE = /(?:^|[;{}])\s*(?:(?:static|const|extern|register|volatile)\s+)*(?:struct\s+)?(\w+)\s+(\w+)\s*(\[[^\]]*\])?\s*=\s*\{/g;
199
- for (const file of files) {
200
- const s = srcCache.get(file);
201
- if (!s || !s.includes('='))
750
+ // `struct TAG { } var[opt] [= {…}]` — the struct is defined INLINE with the
751
+ // table (vim's `cmdname`/`nv_cmd`); its layout never became a node, so parse it
752
+ // here and register it before reading the entries. No leading anchor: a
753
+ // `struct TAG {` with a brace body is always a definition (it may be preceded
754
+ // by a `#define …` line ending in a digit, as in vim), and the trailing
755
+ // `var … = {` check below is what distinguishes a TABLE from a plain type.
756
+ const INLINE_STRUCT_RE = /\bstruct\s+(\w+)\s*\{/g;
757
+ // `(?:static …)* ELEMTYPE [*] name[…] = { … }` — a bare array of function
758
+ // pointers (no struct wrapper). The optional `*` covers a function-TYPE
759
+ // typedef element (`opcode_t *opcodes[]`); a function-pointer typedef element
760
+ // (`zend_rc_dtor_func_t t[]`) needs none. The typedef-set membership gate
761
+ // (below) is what separates this from a plain data/struct array.
762
+ const ARRAY_TABLE_RE = /(?:^|[;{}])\s*(?:(?:static|const|extern|register|volatile)\s+)*(\w+)\s+(\*\s*)?(\w+)\s*\[[^\]]*\]\s*=\s*\{/g;
763
+ for (const unit of units) {
764
+ const s = unit.text;
765
+ if (!s || !s.includes('{'))
766
+ continue;
767
+ INLINE_STRUCT_RE.lastIndex = 0;
768
+ let im;
769
+ while ((im = INLINE_STRUCT_RE.exec(s))) {
770
+ const tag = im[1];
771
+ const sOpen = im.index + im[0].length - 1; // the struct body's `{`
772
+ const sClose = matchBrace(s, sOpen);
773
+ if (sClose < 0)
774
+ continue;
775
+ // After `}`, expect `var [opt] [= {…}]` to be a table; else it's a plain type.
776
+ const after = s.slice(sClose + 1);
777
+ const vm = after.match(/^\s*(\w+)\s*(\[[^\]]*\])?\s*(=\s*\{)?/);
778
+ if (!vm || !vm[1])
779
+ continue;
780
+ const fields = parseStructFields(s.slice(sOpen + 1, sClose));
781
+ if (!fields.some((f) => f.isFnPtr))
782
+ continue; // only tables of fn pointers matter
783
+ if (!structLayout.has(tag))
784
+ registerStructLayout(tag, fields);
785
+ globalVarType.set(vm[1], tag);
786
+ if (vm[3]) {
787
+ const aOpen = sClose + 1 + after.indexOf('{', vm[0].length - 1);
788
+ const aClose = matchBrace(s, aOpen);
789
+ if (aClose > 0) {
790
+ processInit(tag, s.slice(aOpen + 1, aClose), !!vm[2], unit.file, unit.env);
791
+ INLINE_STRUCT_RE.lastIndex = aClose;
792
+ }
793
+ }
794
+ }
795
+ if (!s.includes('='))
202
796
  continue;
203
797
  INIT_RE.lastIndex = 0;
204
798
  let m;
205
799
  while ((m = INIT_RE.exec(s))) {
206
- const struct = m[1];
800
+ let struct = m[1];
801
+ if (!structLayout.has(struct))
802
+ struct = resolveTypeName(struct, unit.objEnv);
207
803
  if (!structLayout.has(struct))
208
804
  continue;
209
805
  const isArray = !!m[3];
@@ -211,30 +807,32 @@ function cFnPointerDispatchEdges(queries, ctx) {
211
807
  const close = matchBrace(s, open);
212
808
  if (close < 0)
213
809
  continue;
214
- const body = s.slice(open + 1, close);
215
- if (isArray) {
216
- // top-level `{ … }` element groups
217
- for (const el of splitTopLevel(body, ',')) {
218
- const t = el.trim();
219
- if (t.startsWith('{')) {
220
- const e = matchBrace(t, 0);
221
- if (e > 0)
222
- registerStructValue(struct, t.slice(1, e), file);
223
- }
224
- else if (t) {
225
- // array of bare values (rare for structs) — treat as one positional slot
226
- registerStructValue(struct, t, file);
227
- }
228
- }
229
- }
230
- else {
231
- registerStructValue(struct, body, file);
232
- }
810
+ globalVarType.set(m[2], struct);
811
+ processInit(struct, s.slice(open + 1, close), isArray, unit.file, unit.env);
233
812
  INIT_RE.lastIndex = close;
234
813
  }
814
+ // Bare arrays-of-function-pointers (no struct, no field). Gated on the
815
+ // element type being a function typedef — a fn-TYPE typedef needs the `*`
816
+ // (array of pointers to it), a fn-pointer typedef does not. A data or
817
+ // struct array's element type is never in these sets, so it never fires.
818
+ ARRAY_TABLE_RE.lastIndex = 0;
819
+ let am;
820
+ while ((am = ARRAY_TABLE_RE.exec(s))) {
821
+ const elemType = am[1];
822
+ const hasStar = !!am[2];
823
+ if (!((fnTypeTypedefs.has(elemType) && hasStar) || fnPtrTypedefs.has(elemType)))
824
+ continue;
825
+ const open = am.index + am[0].length - 1; // the `{`
826
+ const close = matchBrace(s, open);
827
+ if (close < 0)
828
+ continue;
829
+ registerArrayValue(am[3], s.slice(open + 1, close), unit.file, unit.env);
830
+ ARRAY_TABLE_RE.lastIndex = close;
831
+ }
235
832
  }
236
833
  // ---- receiver-type resolution within a function's source ----
237
- // `(?:struct )?TYPE [*]recv` declared in the params or body → TYPE (if a known struct).
834
+ // `(?:struct )?TYPE [*]recv` declared in the params or body → TYPE (if a known
835
+ // fn-pointer-bearing struct).
238
836
  const recvTypeIn = (fnSrc, recv) => {
239
837
  const re = new RegExp(`(?:struct\\s+)?(\\w+)\\s*\\*?\\s*\\b${recv}\\b\\s*(?:[,)=;]|\\[)`, 'g');
240
838
  let m;
@@ -244,6 +842,41 @@ function cFnPointerDispatchEdges(queries, ctx) {
244
842
  }
245
843
  return null;
246
844
  };
845
+ // Declared type of a local/param `v` — ANY type token, not just fn-pointer
846
+ // structs (the base of a chained receiver needn't carry a fn pointer itself).
847
+ // Falls back to a file-scope table variable (`cmdnames` in `cmdnames[i].fn()`).
848
+ const escapeRe = (x) => x.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
849
+ const varTypeIn = (fnSrc, v) => {
850
+ const re = new RegExp(`(?:struct\\s+)?(\\w+)\\s*\\*?\\s*\\b${escapeRe(v)}\\b\\s*(?:[,)=;]|\\[)`, 'g');
851
+ let m;
852
+ while ((m = re.exec(fnSrc))) {
853
+ if (!C_TYPE_KEYWORDS.has(m[1]))
854
+ return m[1];
855
+ }
856
+ return globalVarType.get(v) ?? null;
857
+ };
858
+ // Resolve a member-access chain (`c->cmd`, or just `p`) to a struct type,
859
+ // walking each segment's declared field type. `c->cmd->proc` dispatch:
860
+ // base chain `c->cmd` → client.cmd's type `redisCommand`, the proc owner.
861
+ // Array subscripts (`cmdnames[i]`) are stripped — an index yields one element.
862
+ const resolveChainType = (fnSrc, chain) => {
863
+ const segs = chain.replace(/\s*\[[^\]]*\]/g, '').split(/\s*(?:->|\.)\s*/).filter(Boolean);
864
+ if (segs.length === 0)
865
+ return null;
866
+ let t = varTypeIn(fnSrc, segs[0]);
867
+ for (let i = 1; t && i < segs.length; i++) {
868
+ let next = null;
869
+ for (const fields of allStructFields.get(t) ?? []) {
870
+ const f = fields.find((fl) => fl.name === segs[i] && fl.type);
871
+ if (f) {
872
+ next = f.type;
873
+ break;
874
+ }
875
+ }
876
+ t = next;
877
+ }
878
+ return t;
879
+ };
247
880
  // ---- Pass D: field←field propagation (`a->f = b->g`) ----
248
881
  // Collected as (targetStruct.field ← sourceStruct.field) pairs, then merged to
249
882
  // a fixpoint so a hook slot inherits a registry field's handlers.
@@ -286,11 +919,19 @@ function cFnPointerDispatchEdges(queries, ctx) {
286
919
  if (!changed)
287
920
  break;
288
921
  }
289
- if (reg.size === 0)
922
+ if (reg.size === 0 && arrayReg.size === 0)
290
923
  return [];
291
924
  // ---- Pass E: dispatch sites → edges ----
292
- // recv->field( or recv.field( where field is a known fn-pointer field.
293
- const DISPATCH_RE = /(\w+)\s*(?:->|\.)\s*(\w+)\s*\(/g;
925
+ // `base->…->field(` or `base.…field(` where `field` is a known fn-pointer field.
926
+ // The base may be a chain (`c->cmd->proc`) or carry array subscripts
927
+ // (`cmdnames[i].cmd_func`). An optional `)` before the call covers the
928
+ // parenthesized form `(cmdnames[i].cmd_func)(&ea)` vim uses.
929
+ const DISPATCH_RE = /((?:\w+(?:\s*\[[^\][]*\])?\s*(?:->|\.)\s*)+)(\w+)\s*\)?\s*\(/g;
930
+ // Bare-array dispatch: `tbl[i](…)` or the explicit-deref `(*tbl[i])(…)`. The
931
+ // subscript may itself contain a call (`tbl[GC_TYPE(p)](…)`), so the index
932
+ // class excludes only brackets. Precision comes from the `arrayReg` gate below
933
+ // — this fires only when `tbl` is a known fn-pointer array.
934
+ const ARRAY_DISPATCH_RE = /(?:\(\s*\*\s*)?\b(\w+)\s*\[[^\][]*\]\s*\)?\s*\(/g;
294
935
  const edges = [];
295
936
  const seen = new Set();
296
937
  for (const fn of cFns) {
@@ -302,14 +943,20 @@ function cFnPointerDispatchEdges(queries, ctx) {
302
943
  let m;
303
944
  let added = 0;
304
945
  while ((m = DISPATCH_RE.exec(body)) && added < FANOUT_CAP) {
305
- const recv = m[1];
946
+ const baseChain = m[1].replace(/\s*(?:->|\.)\s*$/, '').trim(); // receiver, minus the trailing arrow
306
947
  const field = m[2];
307
948
  const owners = fieldToStructs.get(field);
308
949
  if (!owners || owners.size === 0)
309
950
  continue;
310
- // Resolve the receiver's struct type; else fall back to a field name that
311
- // belongs to exactly one struct.
312
- let struct = recvTypeIn(body, recv);
951
+ // 1) resolve the receiver chain's struct type precisely (handles c->cmd->proc);
952
+ // 2) else the last segment as a simple local/param of a fn-pointer-bearing struct;
953
+ // 3) else fall back to a field name that belongs to exactly one struct.
954
+ let struct = resolveChainType(body, baseChain);
955
+ if (!struct || !owners.has(struct)) {
956
+ const lastSeg = baseChain.replace(/\s*\[[^\]]*\]/g, '').split(/\s*(?:->|\.)\s*/).pop();
957
+ const t = recvTypeIn(body, lastSeg);
958
+ struct = t && owners.has(t) ? t : null;
959
+ }
313
960
  if (!struct || !owners.has(struct))
314
961
  struct = owners.size === 1 ? [...owners][0] : null;
315
962
  if (!struct)
@@ -341,6 +988,45 @@ function cFnPointerDispatchEdges(queries, ctx) {
341
988
  break;
342
989
  }
343
990
  }
991
+ // ---- bare array-of-fn-pointers dispatch (`tbl[i](…)`) ----
992
+ if (arrayReg.size && added < FANOUT_CAP) {
993
+ ARRAY_DISPATCH_RE.lastIndex = 0;
994
+ while ((m = ARRAY_DISPATCH_RE.exec(body)) && added < FANOUT_CAP) {
995
+ const entries = arrayReg.get(m[1]);
996
+ if (!entries)
997
+ continue;
998
+ // Same-file table wins on a name collision (two file-local `opcodes`);
999
+ // a unique name resolves cross-file; otherwise ambiguous — bail.
1000
+ const ids = entries.length === 1
1001
+ ? entries[0].ids
1002
+ : (entries.find((e) => e.file === fn.filePath)?.ids ?? null);
1003
+ if (!ids)
1004
+ continue;
1005
+ const line = fn.startLine + body.slice(0, m.index).split('\n').length - 1;
1006
+ for (const tid of ids) {
1007
+ if (tid === fn.id)
1008
+ continue;
1009
+ const key = `${fn.id}>${tid}`;
1010
+ if (seen.has(key))
1011
+ continue;
1012
+ seen.add(key);
1013
+ edges.push({
1014
+ source: fn.id,
1015
+ target: tid,
1016
+ kind: 'calls',
1017
+ line,
1018
+ provenance: 'heuristic',
1019
+ metadata: {
1020
+ synthesizedBy: 'fn-pointer-dispatch',
1021
+ via: `${m[1]}[]`,
1022
+ registeredAt: `${fn.filePath}:${line}`,
1023
+ },
1024
+ });
1025
+ if (++added >= FANOUT_CAP)
1026
+ break;
1027
+ }
1028
+ }
1029
+ }
344
1030
  }
345
1031
  return edges;
346
1032
  }