GitNexus/gitnexus/test/integration/resolvers/cpp-qualified-receiver-typing.test.ts
Esp 369e1dd472
fix(ingestion): preserve enclosing scope in C++ and Ruby member node IDs (#4141)
* fix(ingestion): preserve enclosing scope in C++ and Ruby member node IDs

* fix(ingestion): check trailing owner segment for nested template suffix

* fix(ingestion): preserve template :: scopes and extensionless Ruby feature detection

* fix(ingestion): retain qualified C++ and Ruby member ownership

Consolidates #4141, #4159, #4161.

Co-authored-by: shanifadhil33-blip <233503992+shanifadhil33-blip@users.noreply.github.com>
Co-authored-by: Shaurya55555 <156604397+Shaurya55555@users.noreply.github.com>

* fix(cpp): own split unnamed-namespace members by the block that defines the type

Every unnamed namespace block under one parent is the same C++ namespace, so
`void Item::go() {}` may sit in a different block than `struct Item`. The
out-of-line owner path used the definition's own block ordinal and minted an
orphan `@anon2.Item.go` with no HAS_METHOD owner. It now resolves to the
sibling block that defines the qualified type, through a per-parent index
built once, so the work is linear in a file's top-level declarations.

Add the valid-C++ owner cases from review (one owner across sibling blocks,
distinct owners under different named parents), pin the scan bound, check
HAS_METHOD sources in the ownership test, and describe the ordinal scheme in
the c-cpp config comment. The compiler-negative redefinition fixtures keep
their assertions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(ingestion): index class nodes once per tree for detached owners

Every Go method with a receiver (and every R or C++ detached member) reached
findClassNodeByQualifiedName, which walked the whole file per method once this
PR let it descend into every container. A Go file with 4,000 receiver
methods took 20.3 s to index instead of 2.9 s on main. One class-node index per
tree (first match in DFS order, executable bodies skipped) turns each lookup
into a map read; the same file now takes 3.3 s. The legacy C++ declarator walk
returns to main's lines.

Also from the simplify pass: one sibling loop and one shared unnamed-namespace
predicate for C++ anonymous namespaces, the shared language lookup for the C++
analysis feature, template-suffix work only when qualified member ids are on,
and the #1919/#1978/#1991 comments the earlier edits dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cpp): mint nested-qualified out-of-line methods in the worker

`void N::Box::save() {}` produced a scope definition but no graph node, because
the worker query matched only single-level `Box::save`. Calls from such a body
lost their source, so a header-declared class lost every call its out-of-line
methods make (main attributed them to the file). Nested-qualifier patterns now
mint `Method:box.cpp:N.Box.save#0`, and the calls start there.

From review: a forward declaration no longer claims an unnamed-namespace owner,
the per-parent owner cache keys by node id (a leading #if shared byte 0 with
the root), the detached owner names callables only for languages that opt into
qualified member ids (Go closure ids are unchanged), and one helper unwraps a
qualified declarator for both the owner hook and the capture emitter. Valid C++
covers split ownership across several unnamed blocks on the worker path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ingestion): check the member-id opt-in across every provider

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(ingestion): move worker member-owner logic into a testable module

Move the out-of-line owner lookup, the opt-in detached owner and the
qualified-owner hook out of parse-worker.ts into workers/member-owner.ts,
which unit tests can import, and cover them and the C++ detached-owner
resolver with focused unit tests. Split the resolver's long functions,
group findEnclosingClassInfo's detached-owner options into one object,
and merge duplicate imports. Node ids and owner edges are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: shanifadhil33-blip <233503992+shanifadhil33-blip@users.noreply.github.com>
Co-authored-by: Shaurya55555 <156604397+Shaurya55555@users.noreply.github.com>
Co-authored-by: Abhigyan Patwari <126312502+abhigyanpatwari@users.noreply.github.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:30:01 +00:00

99 lines
3.7 KiB
TypeScript

/**
* C++ receiver typing through qualified type spellings, header prototypes and
* qualified factory callees.
*
* Every row names a caller in the `cpp-qualified-receiver-typing` fixture and
* the exact `m` targets it must reach, as `<file>:<Owner>.m`. The fixture puts
* a global `C` (other.h) beside `rp::C` (mod.h), and `a::Config` beside a
* `b::Config` declared in the caller's own namespace, so a lookup that drops
* the qualifier and lands on the nearer same-tail class fails a row instead of
* passing it.
*/
import { describe, it, expect, beforeAll } from 'vitest';
import path from 'path';
import { FIXTURES, getRelationships, runPipelineFromRepo, type PipelineResult } from './helpers.js';
const RP_C = 'mod.h:rp.C.m';
const GLOBAL_C = 'other.h:C.m';
const D = 'mod.h:D.m';
describe('C++ qualified receiver typing', () => {
let result: PipelineResult;
beforeAll(async () => {
result = await runPipelineFromRepo(
path.join(FIXTURES, 'cpp-qualified-receiver-typing'),
() => {},
);
}, 60000);
/** `<file>:<Owner>.m` for every CALLS edge from `caller` to a method `m`. */
const memberTargets = (caller: string): string[] =>
getRelationships(result, 'CALLS')
.filter((edge) => edge.source === caller && edge.target === 'm')
.map((edge) => `${edge.targetFilePath}:${edge.rel.targetId.split(':')[2]?.split('#')[0]}`)
.sort();
it.each<[string, string[]]>([
// A qualified spelling reaches the namespace member it names, never the
// global `C` that other.h makes visible under the same tail.
['declared_init', [RP_C]],
['param_ns', [RP_C]],
['param_ns_ptr', [RP_C]],
['param_template', ['mod.h:rp.Box.m']],
['bare_global', [GLOBAL_C]],
// `a::Config` written inside `namespace b`, which declares its own Config.
['param_qualified', ['a.h:a.Config.m']],
['param_local', ['callers.cpp:b.Config.m']],
])('qualified type spelling: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
it.each<[string, string[]]>([
['declared_ns', [RP_C]],
['declared_ns_ptr', [RP_C]],
])('qualified declaration without initializer: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
it.each<[string, string[]]>([
['brace_ns', [RP_C]],
['brace_qualified', ['a.h:a.Config.m']],
['field_qualified', ['a.h:a.Config.m']],
])('qualified brace-init and member field: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
it.each<[string, string[]]>([
['auto_plain', [D]],
['inline_plain', [D]],
['auto_ptr', [D]],
['global_factory', [GLOBAL_C]],
// A namespace prototype's return type is looked up inside its namespace,
// never from the file's module scope where the global Widget sits.
['using_auto', [RP_C]],
['using_same_file', ['mod.h:rp.Widget.m']],
// The block-scope `E y();` in `vexing` must not hoist a file-level `y`.
['uses_vexing_name', []],
])('header prototype return type: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
it.each<[string, string[]]>([
['using_inline', [RP_C]],
['using_inline_same_file', ['mod.h:rp.Widget.m']],
])('inline call receiver typed by definition identity: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
it.each<[string, string[]]>([
['auto_ns', [RP_C]],
['inline_ns', [RP_C]],
['ctor_ns', [RP_C]],
// An unknown qualified factory types nothing, and the inner `x` must not
// fall through to the outer `D x`.
['shadowed_auto', []],
])('qualified factory callee: %s -> %j', (caller, expected) => {
expect(memberTargets(caller)).toEqual(expected);
});
});