diff --git a/__tests__/fuzzy-lexical-reach.test.ts b/__tests__/fuzzy-lexical-reach.test.ts index 964c348..b0ff1c4 100644 --- a/__tests__/fuzzy-lexical-reach.test.ts +++ b/__tests__/fuzzy-lexical-reach.test.ts @@ -138,6 +138,15 @@ describe('fuzzy reachability rejects a unique guess but never manufactures one', expect(matchFuzzy(callFrom('vite.config.js', 3), contextWith([closure, method]))).toBeNull(); }); + it('trusts no nesting in C, where a nested function is an extraction artifact', () => { + // betaflight: tree-sitter-c's recovery from `RESET_CONFIG(…, .pid = {…})` + // runs resetPidProfile to the end of pid.c, so every function after it is + // "nested" in the graph. C has no nested named functions; the call reaches it. + const cClosure = node({ ...closure, id: 'f:c', language: 'c' as Node['language'], filePath: 'pid.c' }); + const cRef = { ...callFrom('core.c', 3), language: 'c' as UnresolvedRef['language'] }; + expect(matchFuzzy(cRef, contextWith([cClosure]))?.targetNodeId).toBe('f:c'); + }); + it('resolves a lone reachable method as before', () => { expect(matchFuzzy(callFrom('vite.config.js', 3), contextWith([method]))?.targetNodeId).toBe('m:resolve'); }); diff --git a/src/resolution/name-matcher.ts b/src/resolution/name-matcher.ts index 82e567a..943b2d7 100644 --- a/src/resolution/name-matcher.ts +++ b/src/resolution/name-matcher.ts @@ -354,6 +354,9 @@ export function matchFunctionRef( return null; } +/** Languages with no nested named functions: nesting in the graph is never a scope. */ +const NO_NESTED_FUNCTIONS = new Set(['c', 'cpp']); + /** * A function nested inside another FUNCTION is only callable from within its * container — Python, JS/TS, and every closure language scope it lexically. @@ -371,6 +374,14 @@ function isLexicallyReachable( context: ResolutionContext ): boolean { if (candidate.kind !== 'function') return true; + // C and C++ have no nested named functions, so a function the graph shows + // inside another is an extraction artifact, not a scope: tree-sitter-c + // cannot parse a macro call whose arguments are designated initializers + // (betaflight's `RESET_CONFIG(pidProfile_t, pidProfile, .pid = {…})`), and + // its error recovery runs the enclosing function_definition to the end of + // the file, nesting every function after it. Trusting that nesting rejected + // 117 real calls into pid.c on that tree; the functions are reachable. + if (NO_NESTED_FUNCTIONS.has(candidate.language)) return true; const qn = candidate.qualifiedName; if (!qn || !qn.includes('::')) return true; const parentQn = qn.slice(0, qn.lastIndexOf('::'));