mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-10-04 02:31:36 +00:00
* Initial plan * feat: add C scope resolution files for language migration (RFC #909) Add 11 C language scope resolution files following the Go pattern: - query.ts: tree-sitter-c query and parser for C constructs - captures.ts: emit scope captures with arity enrichment - import-decomposer.ts: decompose #include into structured captures - arity-metadata.ts: C function declaration/call arity computation - interpret.ts: interpret C imports and type bindings - import-target.ts: resolve #include paths via suffix matching - arity.ts: C arity compatibility (variadic detection) - merge-bindings.ts: first-wins binding merge by tier - simple-hooks.ts: null hooks (no receivers/methods in C) - index.ts: barrel re-exports - scope-resolver.ts: ScopeResolver implementation for C Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * feat: migrate C to scope-based resolution (RFC #909 Ring 3) Add C ScopeResolver with: - tree-sitter-c scope query (structs, unions, enums, functions, macros, variables, includes) - emitCScopeCaptures with arity enrichment and typedef-struct dedup - interpretCImport for #include directives (system headers filtered) - resolveCImportTarget with suffix matching - cArityCompatibility with variadic detection - cMergeBindings (first-wins by tier) - Header file scanning for cross-language #include resolution - Register in SCOPE_RESOLVERS and MIGRATED_LANGUAGES - Integration test with 4 passing test cases Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/ddcbc075-2999-492c-a0ac-47cddd401a4b Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: update registry-primary-flag test and add C legacy parity expected failures Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/ddcbc075-2999-492c-a0ac-47cddd401a4b Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * refactor: improve arity-metadata readability per review feedback Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/ddcbc075-2999-492c-a0ac-47cddd401a4b Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: address CI failures — unused import, Dirent types, null comparison, lint, formatting Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/f4b6e20d-8d56-4834-8296-db82af27f8e1 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: replace loose comparisons with strict equality, remove optional chaining from childForFieldName Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/20628629-9dc7-45ec-8ae4-f3a14ad29d93 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * Potential fix for pull request finding 'CodeQL / Comparison between inconvertible types' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix: remove unnecessary optional chaining on non-null decl in findFuncDeclarator Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/9b012c35-7494-4180-8c6a-b83da8d8abb9 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: address 5 production readiness review findings Finding 1: Enforce static functions as file-local via expandsWildcardTo hook - Add static-linkage.ts tracking module with markStaticName/isStaticName/expandCWildcardNames - Update captures.ts to detect storage_class_specifier static on functions - Wire expandsWildcardTo in scope-resolver.ts Finding 2: Expand test coverage to ≥30 cases (74 unit tests added) - c-captures.test.ts: 55 tests (scopes, structs, unions, enums, functions, typedef, field, variable, macro, imports, references, type bindings, arity, static) - c-imports.test.ts: 12 tests (decomposition, interpretation, target resolution, determinism, edge cases) - c-arity.test.ts: 18 tests (declaration arity, call arity, compatibility) Finding 3: Deterministic #include resolution on depth ties - Add lexicographic tiebreak in import-target.ts when candidates tie on path depth Finding 4: Revert unexplained package-lock.json change - Restored to pre-PR state (node >=20.0.0) Finding 5: Planning artifact commit acknowledged (squash on merge) Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/22ee780c-2b44-4e69-b9c4-8843ad6ec1ee Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: address code review feedback — Set-based dedup, SyntaxNode type alias Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/22ee780c-2b44-4e69-b9c4-8843ad6ec1ee Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * chore(autofix): apply prettier + eslint fixes via /autofix command * fix: address second review findings 1-4 — isFileLocalDef hook, singleton docs, fn-ptr typedef, static isolation test Finding 1: Added `isFileLocalDef` hook to ScopeResolver contract + implementation in free-call-fallback.ts to filter C static functions from global free-call fallback. Threads caller filePath through pickUniqueGlobalCallable so static defs in other files are excluded. Finding 2: Documented single-invocation assumption on staticNames Map. Added clearStaticNames() call in loadResolutionConfig to prevent cross-repo contamination in server-mode scenarios. Finding 3: Added tree-sitter query pattern for function pointer typedef aliases (typedef void (*callback)(int, int)) in query.ts. Added unit test. Finding 4: Added c-static-isolation integration fixture (a.c with static helper, b.c with non-static helper, caller.c) and test asserting no CALLS edge from caller to a.c's static helper. Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/5b948327-9f59-4ca2-9d8c-8c8087feb510 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * chore(autofix): apply prettier + eslint fixes via /autofix command * fix: skip static isolation integration test in legacy parity mode The `caller.c calls b:helper via include, NOT a:static helper` test requires scope-based wildcard import binding + isFileLocalDef filtering which is only available in the registry-primary path. The legacy DAG path does not resolve cross-file calls through #include → prototype chains. Added to LEGACY_RESOLVER_PARITY_EXPECTED_FAILURES. Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/3be48249-b375-4446-973b-657400f530fb Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: address 3 review findings — static leakage in Phase 2, build-dir skip list, same-directory preference Finding 1: Apply isFileLocalDef filtering in Phase 2 of pickUniqueGlobalCallable so cross-file static defs cannot leak through the SemanticModel fallback path. Finding 2: Expand scanHeaderFiles skip list with dist, build, out, target, _build, .next, cmake-build-* to avoid generated headers shadowing source ones. Finding 3: Implement same-directory sibling preference in resolveCImportTarget, matching C compiler #include "…" relative-lookup semantics. Sibling check now runs before exact match and suffix fallback. Tests: 11 new header-scan tests, 4 new import-target tests (96 total C tests). Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/5236f2b8-72a0-476d-bf39-cca041781014 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * refactor: skip redundant sibling path computation when targetRaw has no backslashes Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/5236f2b8-72a0-476d-bf39-cca041781014 Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * chore(autofix): apply prettier + eslint fixes via /autofix command * fix: normalize header-scan paths to forward slashes for Windows compatibility On Windows, path.relative() produces backslash-separated paths (e.g. src\foo.h) which caused test failures when comparing against forward-slash expectations. Normalize output with .replace(/\\/g, '/') matching the convention used throughout the scope-resolution pipeline. Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/88420759-8a8c-4e6d-affa-ecb5458a235a Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> * fix: address 4 findings — K&R arity, function-pointer docs, prototype docs, _filePath rename Finding 1: K&R empty parameter list `int foo()` now returns unknown arity `{}` instead of `{parameterCount:0}`. Distinguishes from explicit `int foo(void)`. 3 unit tests cover K&R definition, prototype, and void comparison. Finding 2: Added code comment documenting function-pointer-variable call capture as known architectural trade-off (same as Go resolver). Finding 3: Added code comment documenting prototype/definition duplication as graph-quality concern (no false CALLS edges). Finding 4: Renamed `_filePath` → `filePath` in captures.ts since it is actively used in markStaticName(). Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/ab2164f7-972f-4ea8-81fa-a14ce20d7cce Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: magyargergo <11230420+magyargergo@users.noreply.github.com> Co-authored-by: Gergő Magyar <gergomagyar@icloud.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
212 lines
7.5 KiB
TypeScript
212 lines
7.5 KiB
TypeScript
/**
|
|
* Unit tests for C arity computation and compatibility.
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import { getCParser } from '../../../../src/core/ingestion/languages/c/query.js';
|
|
import {
|
|
computeCDeclarationArity,
|
|
computeCCallArity,
|
|
} from '../../../../src/core/ingestion/languages/c/arity-metadata.js';
|
|
import { cArityCompatibility } from '../../../../src/core/ingestion/languages/c/arity.js';
|
|
import type { SyntaxNode } from '../../../../src/core/ingestion/utils/ast-helpers.js';
|
|
import type { Callsite, SymbolDefinition } from 'gitnexus-shared';
|
|
|
|
function parseFunctionNode(src: string): SyntaxNode | null {
|
|
const tree = getCParser().parse(src);
|
|
for (let i = 0; i < tree.rootNode.namedChildCount; i++) {
|
|
const child = tree.rootNode.namedChild(i);
|
|
if (child?.type === 'function_definition' || child?.type === 'declaration') {
|
|
return child as SyntaxNode;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function parseCallNode(src: string): SyntaxNode | null {
|
|
const tree = getCParser().parse(src);
|
|
// Walk deeper to find call_expression
|
|
function findCall(node: SyntaxNode): SyntaxNode | null {
|
|
if (node.type === 'call_expression') return node;
|
|
for (let i = 0; i < node.namedChildCount; i++) {
|
|
const found = findCall(node.namedChild(i) as SyntaxNode);
|
|
if (found !== null) return found;
|
|
}
|
|
return null;
|
|
}
|
|
return findCall(tree.rootNode as SyntaxNode);
|
|
}
|
|
|
|
describe('computeCDeclarationArity', () => {
|
|
it('returns count for simple parameters', () => {
|
|
const node = parseFunctionNode('int add(int a, int b) { return a + b; }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBe(2);
|
|
expect(arity.requiredParameterCount).toBe(2);
|
|
});
|
|
|
|
it('returns zero for (void) parameter list', () => {
|
|
const node = parseFunctionNode('void f(void) { }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBe(0);
|
|
expect(arity.requiredParameterCount).toBe(0);
|
|
expect(arity.parameterTypes).toEqual([]);
|
|
});
|
|
|
|
it('handles variadic functions — parameterCount is undefined', () => {
|
|
const node = parseFunctionNode('int printf(const char *fmt, ...) { return 0; }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBeUndefined();
|
|
expect(arity.requiredParameterCount).toBe(1);
|
|
expect(arity.parameterTypes).toContain('...');
|
|
});
|
|
|
|
it('extracts parameter types', () => {
|
|
const node = parseFunctionNode('void f(int a, float b, char *c) { }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterTypes).toEqual(['int', 'float', 'char']);
|
|
});
|
|
|
|
it('handles pointer-return function', () => {
|
|
const node = parseFunctionNode('int *create(int size) { return 0; }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBe(1);
|
|
});
|
|
|
|
it('handles function prototype (no body)', () => {
|
|
const node = parseFunctionNode('int add(int a, int b);');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBe(2);
|
|
});
|
|
|
|
it('returns empty for non-function node', () => {
|
|
const node = parseFunctionNode('int x = 5;');
|
|
// This might be a declaration node, but without function_declarator
|
|
if (node !== null) {
|
|
const arity = computeCDeclarationArity(node);
|
|
expect(arity.parameterCount).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it('handles single parameter', () => {
|
|
const node = parseFunctionNode('void f(int x) { }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBe(1);
|
|
expect(arity.requiredParameterCount).toBe(1);
|
|
});
|
|
|
|
it('returns unknown arity for K&R empty parameter list int foo()', () => {
|
|
const node = parseFunctionNode('int foo() { return 0; }');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
// K&R old-style: unspecified parameters, NOT zero parameters
|
|
expect(arity.parameterCount).toBeUndefined();
|
|
expect(arity.requiredParameterCount).toBeUndefined();
|
|
expect(arity.parameterTypes).toBeUndefined();
|
|
});
|
|
|
|
it('distinguishes K&R int foo() from explicit int foo(void)', () => {
|
|
const knrNode = parseFunctionNode('int foo() { return 0; }');
|
|
const voidNode = parseFunctionNode('int foo(void) { return 0; }');
|
|
expect(knrNode).not.toBeNull();
|
|
expect(voidNode).not.toBeNull();
|
|
|
|
const knrArity = computeCDeclarationArity(knrNode!);
|
|
const voidArity = computeCDeclarationArity(voidNode!);
|
|
|
|
// K&R: unknown arity
|
|
expect(knrArity.parameterCount).toBeUndefined();
|
|
// Explicit void: zero params
|
|
expect(voidArity.parameterCount).toBe(0);
|
|
expect(voidArity.requiredParameterCount).toBe(0);
|
|
});
|
|
|
|
it('returns unknown arity for K&R prototype int foo();', () => {
|
|
const node = parseFunctionNode('int foo();');
|
|
expect(node).not.toBeNull();
|
|
const arity = computeCDeclarationArity(node!);
|
|
expect(arity.parameterCount).toBeUndefined();
|
|
expect(arity.requiredParameterCount).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('computeCCallArity', () => {
|
|
it('counts zero arguments', () => {
|
|
const node = parseCallNode('void f(void) { init(); }');
|
|
expect(node).not.toBeNull();
|
|
expect(computeCCallArity(node!)).toBe(0);
|
|
});
|
|
|
|
it('counts two arguments', () => {
|
|
const node = parseCallNode('void f(void) { add(1, 2); }');
|
|
expect(node).not.toBeNull();
|
|
expect(computeCCallArity(node!)).toBe(2);
|
|
});
|
|
|
|
it('counts three arguments', () => {
|
|
const node = parseCallNode('void f(void) { func(a, b, c); }');
|
|
expect(node).not.toBeNull();
|
|
expect(computeCCallArity(node!)).toBe(3);
|
|
});
|
|
|
|
it('counts string literal arguments', () => {
|
|
const node = parseCallNode('void f(void) { printf("hello %s", name); }');
|
|
expect(node).not.toBeNull();
|
|
expect(computeCCallArity(node!)).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('cArityCompatibility', () => {
|
|
function makeDef(params: Partial<SymbolDefinition>): SymbolDefinition {
|
|
return {
|
|
nodeId: 'test',
|
|
filePath: 'test.c',
|
|
type: 'Function',
|
|
...params,
|
|
};
|
|
}
|
|
|
|
function makeCallsite(arity: number): Callsite {
|
|
return { arity } as Callsite;
|
|
}
|
|
|
|
it('returns compatible for exact match', () => {
|
|
const def = makeDef({ parameterCount: 2, requiredParameterCount: 2 });
|
|
expect(cArityCompatibility(def, makeCallsite(2))).toBe('compatible');
|
|
});
|
|
|
|
it('returns incompatible for too few args', () => {
|
|
const def = makeDef({ parameterCount: 3, requiredParameterCount: 3 });
|
|
expect(cArityCompatibility(def, makeCallsite(1))).toBe('incompatible');
|
|
});
|
|
|
|
it('returns incompatible for too many args (non-variadic)', () => {
|
|
const def = makeDef({ parameterCount: 2, requiredParameterCount: 2 });
|
|
expect(cArityCompatibility(def, makeCallsite(5))).toBe('incompatible');
|
|
});
|
|
|
|
it('returns compatible for variadic with enough args', () => {
|
|
const def = makeDef({
|
|
requiredParameterCount: 1,
|
|
parameterTypes: ['const char *', '...'],
|
|
});
|
|
expect(cArityCompatibility(def, makeCallsite(3))).toBe('compatible');
|
|
});
|
|
|
|
it('returns unknown when no arity info on def', () => {
|
|
const def = makeDef({});
|
|
expect(cArityCompatibility(def, makeCallsite(2))).toBe('unknown');
|
|
});
|
|
|
|
it('returns unknown for negative callsite arity', () => {
|
|
const def = makeDef({ parameterCount: 2, requiredParameterCount: 2 });
|
|
expect(cArityCompatibility(def, makeCallsite(-1))).toBe('unknown');
|
|
});
|
|
});
|