fix(cpp): resolve callers for typed pointer method calls (#445)
Resolves typed member-pointer method calls like `m_cpAlg->Processing()` so `codegraph callers CDetect::Processing` returns the expected callers.
- Extract C/C++ `field_expression` member calls as receiver-qualified references, so `ptr->method()` is preserved as a receiver-aware reference.
- Surface out-of-line C++ method definitions (`int CDetect::Processing() {...}` in `.cpp` with class in `.hpp`) as proper method nodes with the correct qualified identity.
- C++ receiver-type inference: declarator regex requires a terminator after the receiver (rules out matching `return m_cpAlg->...`), handles `Type*x`/`Type *x`/`Type* x` uniformly, and rejects C++ keywords as a final guard.
- `resolveMethodOnType` matches by `Class::method` qualified-name suffix, so out-of-line definitions across files resolve (typical `.hpp`/`.cpp` split).
Validated on bitcoin-core (1306 .cpp files): 38,180 → 40,503 cpp method incoming-call edges (+6.1%), deterministic across re-indexes. Regression test added for the ambiguous-name + `return ptr->m()` / `Type x = ptr->m()` patterns.
Closes #445
Co-authored-by: chenyuxuan <458254969@qq.com>
Co-authored-by: Colby McHenry <me@colbymchenry.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
chenyuxuan
Colby McHenry
Claude Opus 4.7
parent
72c08c2bef
commit
c0cf9c1e7d
@@ -161,6 +161,111 @@ describe('C++ end-to-end — virtual override synthesis', () => {
|
||||
tmpDir = undefined;
|
||||
});
|
||||
|
||||
it('resolves callers through typed object pointers', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
|
||||
let cg: CodeGraph | undefined;
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'detect.hpp'),
|
||||
'class CDetect {\n' +
|
||||
' public:\n' +
|
||||
' int Processing();\n' +
|
||||
'};\n' +
|
||||
'class CDetector {\n' +
|
||||
' private:\n' +
|
||||
' CDetect* m_cpAlg = nullptr;\n' +
|
||||
' public:\n' +
|
||||
' int Run();\n' +
|
||||
' int Flush();\n' +
|
||||
'};\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'detect.cpp'),
|
||||
'#include "detect.hpp"\n' +
|
||||
'int CDetector::Run() { return m_cpAlg->Processing(); }\n' +
|
||||
'int CDetector::Flush() { return m_cpAlg->Processing(); }\n' +
|
||||
'int CDetect::Processing() { return 0; }\n'
|
||||
);
|
||||
|
||||
cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
const processing = cg
|
||||
.getNodesByKind('method')
|
||||
.find((n) => n.qualifiedName.endsWith('CDetect::Processing'));
|
||||
expect(processing).toBeDefined();
|
||||
|
||||
const callers = cg.getCallers(processing!.id).map((c) => c.node.qualifiedName);
|
||||
expect(callers).toContain('CDetector::Run');
|
||||
expect(callers).toContain('CDetector::Flush');
|
||||
|
||||
const runMethod = cg
|
||||
.getNodesByKind('method')
|
||||
.find((n) => n.qualifiedName.endsWith('CDetector::Run'));
|
||||
expect(runMethod).toBeDefined();
|
||||
const callees = cg.getCallees(runMethod!.id).map((c) => c.node.qualifiedName);
|
||||
expect(callees).toContain('CDetect::Processing');
|
||||
} finally {
|
||||
cg?.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves typed pointer callers when the method name is ambiguous and the call sits inside a return/declaration', async () => {
|
||||
// Regression: an earlier version of the C++ receiver-type inference matched
|
||||
// the call line itself (`return m_cpAlg->Processing()`) and treated `return`
|
||||
// as the type, OR grabbed `int r =` as a type from the prefix. With Strategy
|
||||
// 3's "unique method name" fallback, the original issue example resolved
|
||||
// anyway — but as soon as two classes share a method name (very common in
|
||||
// real C++), both calls go unresolved.
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
|
||||
let cg: CodeGraph | undefined;
|
||||
try {
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'detect.hpp'),
|
||||
'class CDetect { public: int Processing(); };\n' +
|
||||
'class CWidget { public: int Processing(); };\n' +
|
||||
'class CDetector {\n' +
|
||||
' private:\n' +
|
||||
' CDetect* m_cpAlg = nullptr;\n' +
|
||||
' public:\n' +
|
||||
' int RunReturn();\n' +
|
||||
' int RunAssign();\n' +
|
||||
'};\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'detect.cpp'),
|
||||
'#include "detect.hpp"\n' +
|
||||
'int CDetector::RunReturn() { return m_cpAlg->Processing(); }\n' +
|
||||
'int CDetector::RunAssign() { int r = m_cpAlg->Processing(); return r; }\n' +
|
||||
'int CDetect::Processing() { return 0; }\n' +
|
||||
'int CWidget::Processing() { return 0; }\n'
|
||||
);
|
||||
|
||||
cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
const detectProc = cg
|
||||
.getNodesByKind('method')
|
||||
.find((n) => n.qualifiedName === 'CDetect::Processing');
|
||||
const widgetProc = cg
|
||||
.getNodesByKind('method')
|
||||
.find((n) => n.qualifiedName === 'CWidget::Processing');
|
||||
expect(detectProc).toBeDefined();
|
||||
expect(widgetProc).toBeDefined();
|
||||
|
||||
const detectCallers = cg.getCallers(detectProc!.id).map((c) => c.node.qualifiedName);
|
||||
expect(detectCallers).toContain('CDetector::RunReturn');
|
||||
expect(detectCallers).toContain('CDetector::RunAssign');
|
||||
|
||||
// CWidget::Processing is never called — calls must NOT misroute here.
|
||||
const widgetCallers = cg.getCallers(widgetProc!.id).map((c) => c.node.qualifiedName);
|
||||
expect(widgetCallers).not.toContain('CDetector::RunReturn');
|
||||
expect(widgetCallers).not.toContain('CDetector::RunAssign');
|
||||
} finally {
|
||||
cg?.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('bridges a base virtual method to the subclass override', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-cpp-'));
|
||||
fs.writeFileSync(
|
||||
|
||||
Reference in New Issue
Block a user