fix(cpp): resolve typed member chains across headers

Preserve lexical receiver types and access operators, resolve fields by their declaring class, and unwrap supported standard smart pointers without guessing ambiguous targets. Persist class metadata for incremental resolution and invalidate older AST caches. Add regression and negative coverage for split headers, shadowing, namespace collisions, forward declarations, and cached rebuilds.
This commit is contained in:
Brenden Smith committed 2026-11-08 20:40:41 -06:00
1 parent 6c8002eca4
commit 287b78caca
5 files changed
+360 -27

No files matched your search

+11
View File
@@ -122,6 +122,17 @@ carry `INFERRED` confidence. Bare calls to package-level functions remain `EXTRA
unknown or ambiguous receivers do not bind by method name alone. AST cache schema 37
retires Go entries created before these receiver-binding rules.
C++ member chains such as `client.actions->Execute()` use lexical local/parameter
bindings and class-scoped field declarations, including fields in unchanged headers.
Raw pointers and single-type-argument `std::unique_ptr<T>` / `std::shared_ptr<T>`
receivers are supported; `.` does not unwrap a smart pointer. Resolved instance
calls are `INFERRED` static-type relationships, not proof of runtime dispatch.
Unknown/shadowed types, ambiguous class identities, unresolved aliases, custom
pointer wrappers, containers, and computed receivers are not guessed. AST cache
schema 41 retires entries without receiver chains and persistent class fields.
Rebuild existing graphs with `graphify extract . --code-only --force` to populate
unchanged headers before relying on changed-file-only updates.
## Adding a new language extractor
1. Add an `extract_<lang>(path: Path) -> dict` function following the existing pattern (tree-sitter parse → walk nodes → collect `nodes` and `edges` → call-graph second pass for INFERRED `calls` edges). New languages go in their own module under `graphify/extractors/` - see `graphify/extractors/MIGRATION.md`; `extract.py` re-exports them while the existing ones are ported out of it.
+2 -1
View File
@@ -38,7 +38,8 @@ except Exception:
_EXTRACTOR_VERSION = "unknown"
# Bump when AST cache-key semantics change independently of the package version.
_AST_CACHE_SCHEMA = 40 # Solidity members, free functions and types whose names differ only by a leading `_` (or by case) in one scope (`_grantRole(bytes32,address)` / `grantRole(bytes32,address)`) get separate node ids: the one public name keeps its id and the others are salted, as in Python and C# (#3302, #4415), so the `_` function no longer vanishes and its calls stop landing on its twin (#5027); a Solidity call on a receiver of a contract or interface type (`IERC20 token; token.transfer()`, `IERC20(a).transfer()`, `token().balanceOf()`) binds by name and arity through that type's linearization to the type's own member instead of producing no edge, gated to a single visible contract/interface and fail-closed on an unknown or ambiguous type (#5060); a Solidity call attached by a `using A for T` directive (`using SafeCast for uint256; x.toUint48()`) binds to the library or free function it attaches, matched by the receiver's type and an arity one greater than the call (the receiver), fail-closed when the type is unknown or two functions fit (#5062); a C# constructor (instance, static or primary) is its own node owned by its type through a `method` edge, its body and `: base(...)`/`: this(...)` calls credited to it, and `new X(...)`/`: base`/`: this` additively link the arity-correct overload (#5054); and a C# call inside a property/indexer accessor, expression body, operator or conversion is credited to that member (an indexer is a `this[]` node, an operator a `.operator +()` node) instead of being lost (#5056), so cached Solidity and C# extractions re-run; prior (39): A Markdown code span written as a backslash namespace (`\Foo\Bar`) or a type-qualified member (`Type::member`) is recognised as a symbol mention and emits a `ns_qualified` raw_call (#4610, #4695), so cached Markdown extractions re-run; prior (38): C# preprocessor directives and `#if` branches that would derail tree-sitter-c-sharp are blanked before parsing (byte-length preserving, so lines and columns do not move), so a directive no longer closes the enclosing class early and drops later members or calls (#5003); a Solidity library call (`Lib.f()`, including an aliased import), and an `emit`/`revert`/`require` naming a declared event or error, link to the member they name instead of producing no edge, resolved only when a single visible type and a unique name/arity member match (#5001, #5005); and a YAML `<<` merge sequence gives the earlier anchor precedence for a duplicated key, matching the merge spec and PyYAML, so the winning key carries its correct source line (#4995); so cached C#, Solidity and YAML extractions re-run; prior (37): Solidity `import "../x/Y.sol"` resolves the `..` to the real file instead of a dangling stub that kept the importer's own folder (#4985); a Python module-level singleton (`g = Service()` then `g.method()`) resolves its member calls through the instance's type, gated by an exact-spelling and lexical-shadow guard (#4972); Kotlin resolves a call to an overloaded method and attributes calls inside an `init {}` block to the class, and a bare call no longer binds to a class the caller cannot reach (#4935, #4956); a Go same-file member call resolves through the receiver's type instead of a declaration-order-dependent same-named method (#4962, #4966); a Go member call on a receiver typed from another package (`pkg.T`) binds only to the `T` declared at the path the file imports `pkg` from (#3368, #4853); and a Go interface method call additively dispatches to the interface's implicit implementers (#4934), so cached Solidity, Python, Kotlin and Go extractions re-run; prior (36): C++ extraction changes: a type-alias reference (`using Foo = Bar;` then `Foo x;`) binds by exact spelling so it no longer loses to a same-normalized function, and a salted alias keeps its own `contains` edge (#4951); a member call `x.m()` binds by the receiver's type, now typed from parameters and class fields and not just locals, with untyped receivers kept bound in-file (#3215, #4946); an out-of-line/split-header definition attaches to the unique owner whose class file is in its include set via a `_cpp_qualified` identity marker, instead of file-containing or cross-binding to an unrelated same-named function (#4916, #4920); and a local callable value (a lambda or `std::function` local) is kept out of global call resolution by byte-range binding lifetimes so it no longer mints a spurious INFERRED call to a same-named global (#4917, #4921), so cached C++ extractions re-run; prior (35): A Rust module-qualified call like `crate::b::helper()` resolves to the function it names via qualifier-verified cross-file resolution, kept inside the caller's crate and out of local names (#2135, #4944); a Rust call written inside an expression macro's arguments (`println!`, `vec!`, ...) is linked, with a parsed call winning the dedupe (#4945); and cache keys keep their written case (file I/O through os_long_path, cache identity via normpath not normcase, anLine truncated
# Schema 41 adds lexical C++ receiver chains and persistent class-scoped fields.
_AST_CACHE_SCHEMA = 41 # Solidity members, free functions and types whose names differ only by a leading `_` (or by case) in one scope (`_grantRole(bytes32,address)` / `grantRole(bytes32,address)`) get separate node ids: the one public name keeps its id and the others are salted, as in Python and C# (#3302, #4415), so the `_` function no longer vanishes and its calls stop landing on its twin (#5027); a Solidity call on a receiver of a contract or interface type (`IERC20 token; token.transfer()`, `IERC20(a).transfer()`, `token().balanceOf()`) binds by name and arity through that type's linearization to the type's own member instead of producing no edge, gated to a single visible contract/interface and fail-closed on an unknown or ambiguous type (#5060); a Solidity call attached by a `using A for T` directive (`using SafeCast for uint256; x.toUint48()`) binds to the library or free function it attaches, matched by the receiver's type and an arity one greater than the call (the receiver), fail-closed when the type is unknown or two functions fit (#5062); a C# constructor (instance, static or primary) is its own node owned by its type through a `method` edge, its body and `: base(...)`/`: this(...)` calls credited to it, and `new X(...)`/`: base`/`: this` additively link the arity-correct overload (#5054); and a C# call inside a property/indexer accessor, expression body, operator or conversion is credited to that member (an indexer is a `this[]` node, an operator a `.operator +()` node) instead of being lost (#5056), so cached Solidity and C# extractions re-run; prior (39): A Markdown code span written as a backslash namespace (`\Foo\Bar`) or a type-qualified member (`Type::member`) is recognised as a symbol mention and emits a `ns_qualified` raw_call (#4610, #4695), so cached Markdown extractions re-run; prior (38): C# preprocessor directives and `#if` branches that would derail tree-sitter-c-sharp are blanked before parsing (byte-length preserving, so lines and columns do not move), so a directive no longer closes the enclosing class early and drops later members or calls (#5003); a Solidity library call (`Lib.f()`, including an aliased import), and an `emit`/`revert`/`require` naming a declared event or error, link to the member they name instead of producing no edge, resolved only when a single visible type and a unique name/arity member match (#5001, #5005); and a YAML `<<` merge sequence gives the earlier anchor precedence for a duplicated key, matching the merge spec and PyYAML, so the winning key carries its correct source line (#4995); so cached C#, Solidity and YAML extractions re-run; prior (37): Solidity `import "../x/Y.sol"` resolves the `..` to the real file instead of a dangling stub that kept the importer's own folder (#4985); a Python module-level singleton (`g = Service()` then `g.method()`) resolves its member calls through the instance's type, gated by an exact-spelling and lexical-shadow guard (#4972); Kotlin resolves a call to an overloaded method and attributes calls inside an `init {}` block to the class, and a bare call no longer binds to a class the caller cannot reach (#4935, #4956); a Go same-file member call resolves through the receiver's type instead of a declaration-order-dependent same-named method (#4962, #4966); a Go member call on a receiver typed from another package (`pkg.T`) binds only to the `T` declared at the path the file imports `pkg` from (#3368, #4853); and a Go interface method call additively dispatches to the interface's implicit implementers (#4934), so cached Solidity, Python, Kotlin and Go extractions re-run; prior (36): C++ extraction changes: a type-alias reference (`using Foo = Bar;` then `Foo x;`) binds by exact spelling so it no longer loses to a same-normalized function, and a salted alias keeps its own `contains` edge (#4951); a member call `x.m()` binds by the receiver's type, now typed from parameters and class fields and not just locals, with untyped receivers kept bound in-file (#3215, #4946); an out-of-line/split-header definition attaches to the unique owner whose class file is in its include set via a `_cpp_qualified` identity marker, instead of file-containing or cross-binding to an unrelated same-named function (#4916, #4920); and a local callable value (a lambda or `std::function` local) is kept out of global call resolution by byte-range binding lifetimes so it no longer mints a spurious INFERRED call to a same-named global (#4917, #4921), so cached C++ extractions re-run; prior (35): A Rust module-qualified call like `crate::b::helper()` resolves to the function it names via qualifier-verified cross-file resolution, kept inside the caller's crate and out of local names (#2135, #4944); a Rust call written inside an expression macro's arguments (`println!`, `vec!`, ...) is linked, with a parsed call winning the dedupe (#4945); and cache keys keep their written case (file I/O through os_long_path, cache identity via normpath not normcase, anLine truncated
# Per-process memo: distributions() scans site-packages metadata, which is
+87 -5
View File
@@ -6159,15 +6159,16 @@ def _resolve_cpp_member_calls(
The shared cross-file pass drops every ``is_member_call`` because a bare method
name (``bar``) collides across the corpus and inflates god-nodes (#543/#1219).
The C++ extractor records each member call's receiver and a per-file
``var -> ClassName`` table (``cpp_type_table``) built from local declarations.
The C++ extractor records lexical receiver bindings, access chains and
class-scoped field types. Older scoped-call records also use a per-file
``var -> ClassName`` table (``cpp_type_table``).
This pass types the receiver, then emits an edge ONLY when that type resolves
to exactly ONE definition (the god-node guard).
Receiver typing, by precision tier:
* ``Foo::bar()`` — the scope ``Foo`` names the type explicitly -> EXTRACTED.
* ``this->bar()`` — the receiver is the caller's own enclosing class -> EXTRACTED.
* ``f.bar()`` / ``f->bar()`` — ``f`` typed via the file's local table -> INFERRED.
* ``f.bar()`` / ``f->bar()`` and field chains — declared receiver types -> INFERRED.
A receiver whose type can't be inferred locally is SKIPPED (no guess): a false
call edge is worse than a missing one. The ``_merge_decl_def_classes`` pass has
already folded each header/impl class pair into one node, so a paired class is a
@@ -6231,6 +6232,78 @@ def _resolve_cpp_member_calls(
if n.get("source_file") and label.endswith("()") and "::" in label:
qualified_method_nids.setdefault(label, []).append(n["id"])
# Receiver chains use literal, case-sensitive identities, including namespaces.
qualified_types: dict[str, list[str]] = {}
simple_types: dict[str, list[str]] = {}
exact_methods: dict[tuple[str, str], set[str]] = {}
for n in all_nodes:
identity = n.get("_cpp_qualified")
if identity and "_cpp_fields" in n and n.get("id") in contained and _is_type_like_definition(n):
qualified_types.setdefault(identity, []).append(n["id"])
simple_types.setdefault(identity.rsplit("::", 1)[-1], []).append(n["id"])
for rel in ("defines", "method"):
grouped: dict[tuple[str, str], set[str]] = {}
for e in all_edges:
target_node = node_by_id.get(e.get("target", ""), {})
label = target_node.get("label", "")
if e.get("relation") == rel and label.endswith("()"):
name = label.removesuffix("()").lstrip(".").rsplit("::", 1)[-1]
grouped.setdefault((e["source"], name), set()).add(e["target"])
exact_methods.update(grouped)
def chain_type(name: str, scope: str, operator: str) -> str | None:
name = re.sub(r"\b(const|volatile)\b", "", name).strip()
identifier = r"(?:::)?[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*"
if operator == "->":
smart = re.fullmatch(rf"std::(?:unique_ptr|shared_ptr)\s*<\s*({identifier})\s*>", name)
if smart:
name = smart[1]
elif name.endswith("*"):
name = name[:-1].strip()
else:
return None
elif operator != ".":
return None
if not re.fullmatch(identifier, name):
return None
absolute = name.startswith("::")
name = name.removeprefix("::")
if not absolute:
while scope:
candidates = qualified_types.get(f"{scope}::{name}", [])
if candidates:
return candidates[0] if len(candidates) == 1 else None
scope = scope.rsplit("::", 1)[0] if "::" in scope else ""
candidates = qualified_types.get(name, [])
if not candidates and not absolute and "::" not in name:
candidates = simple_types.get(name, [])
return candidates[0] if len(candidates) == 1 else None
def resolve_chain(rc: dict) -> str | None:
chain = rc["cpp_receiver_chain"]
if not chain:
return None
root_name = chain[0][0]
owner = enclosing_type.get(rc["caller_nid"], "")
owner_node = node_by_id.get(owner, {})
scope = rc.get("cpp_receiver_scope", owner_node.get("_cpp_qualified", ""))
if root_name == "this":
name = owner_node.get("_cpp_qualified", "") + "*"
elif "cpp_receiver_type" in rc:
name = rc["cpp_receiver_type"]
else:
name = owner_node.get("_cpp_fields", {}).get(root_name, "")
type_nid = ""
for index, (member, operator) in enumerate(chain):
if index:
owner_node = node_by_id.get(type_nid, {})
name = owner_node.get("_cpp_fields", {}).get(member, "")
scope = owner_node.get("_cpp_qualified", "")
type_nid = chain_type(name, scope, operator)
if type_nid is None:
return None
return type_nid
all_raw_calls: list[dict] = []
for result in per_file:
all_raw_calls.extend(result.get("raw_calls", []))
@@ -6252,7 +6325,12 @@ def _resolve_cpp_member_calls(
continue
# Determine the receiver's type and the resulting confidence.
qualified_fallback_label: str | None = None
if receiver == "this":
if "cpp_receiver_chain" in rc:
type_nid = resolve_chain(rc)
if type_nid is None:
continue
type_qualified = receiver == "this" and len(rc["cpp_receiver_chain"]) == 1
elif receiver == "this":
# this->bar(): receiver is the caller's own enclosing class.
type_nid = enclosing_type.get(caller)
if not type_nid:
@@ -6305,7 +6383,11 @@ def _resolve_cpp_member_calls(
continue
type_nid = type_defs[0]
type_qualified = False
method_nid = method_index.get((type_nid, _key(callee)))
if "cpp_receiver_chain" in rc:
methods = exact_methods.get((type_nid, callee), set())
method_nid = next(iter(methods)) if len(methods) == 1 else None
else:
method_nid = method_index.get((type_nid, _key(callee)))
if method_nid is None and qualified_fallback_label is not None:
candidates = qualified_method_nids.get(qualified_fallback_label, [])
if len(candidates) == 1:
+93 -21
View File
@@ -3287,9 +3287,66 @@ def _cpp_qualified_name(node, name: str, source: bytes) -> str:
return "::".join([*reversed(parents), name])
def _cpp_local_binding_ranges(body, source: bytes) -> dict[str, list[tuple[int, int]]]:
def _cpp_declared_receiver_type(node, declarator, source: bytes) -> str:
"""Keep qualification and pointer shape; unsupported declarations stay unknown."""
type_node = node.child_by_field_name("type")
if type_node is None or type_node.type not in {
"type_identifier", "qualified_identifier", "template_type",
}:
return ""
name = _read_text(type_node, source)
while declarator is not None and declarator.type not in {"identifier", "field_identifier"}:
if declarator.type == "pointer_declarator":
name += "*"
elif declarator.type not in {"reference_declarator", "init_declarator"}:
return ""
inner = declarator.child_by_field_name("declarator")
if inner is None and declarator.type == "reference_declarator":
inner = next((c for c in declarator.named_children if c.type != "type_qualifier"), None)
declarator = inner
return name if declarator is not None else ""
def _cpp_receiver_chain(node, source: bytes) -> list[list[str]]:
"""Pairs of receiver/member names and their following access operators."""
members, operators = [], []
while node is not None:
if node.type == "parenthesized_expression" and len(node.named_children) == 1:
node = node.named_children[0]
elif node.type == "field_expression":
field = node.child_by_field_name("field")
operator = node.child_by_field_name("operator")
if field is None or field.type != "field_identifier" or operator is None:
return []
members.append(_read_text(field, source))
operators.append(_read_text(operator, source))
node = node.child_by_field_name("argument")
elif node.type in {"identifier", "this"}:
members.append(_read_text(node, source))
return [list(pair) for pair in zip(reversed(members[1:]), reversed(operators))]
else:
return []
return []
def _cpp_class_fields(node, source: bytes) -> dict[str, str]:
fields: dict[str, str] = {}
body = node.child_by_field_name("body")
if body is not None:
for field in body.named_children:
if field.type != "field_declaration":
continue
for declarator in field.children_by_field_name("declarator"):
name = _cpp_declarator_name(declarator, source)
if name:
value = _cpp_declared_receiver_type(field, declarator, source)
fields[name] = value if fields.get(name, value) == value else ""
return fields
def _cpp_local_binding_ranges(body, source: bytes) -> dict[str, list[tuple[int, int, str, str]]]:
"""Lexical lifetimes of named C++ locals/parameters, not corpus callees."""
ranges: dict[str, list[tuple[int, int]]] = {}
ranges: dict[str, list[tuple[int, int, str, str]]] = {}
scopes = {"compound_statement", "for_statement", "for_range_loop", "if_statement",
"switch_statement", "catch_clause", "function_definition", "lambda_expression"}
stack = [body]
@@ -3302,15 +3359,22 @@ def _cpp_local_binding_ranges(body, source: bytes) -> dict[str, list[tuple[int,
node = stack.pop()
if node.type in {"class_specifier", "struct_specifier", "function_definition"}:
continue
if node.type in {"declaration", "parameter_declaration", "optional_parameter_declaration"}:
if node.type in {"declaration", "parameter_declaration", "optional_parameter_declaration",
"lambda_capture_initializer"}:
scope = node.parent
while scope is not None and scope.type not in scopes:
scope = scope.parent
if scope is not None:
for declarator in node.children_by_field_name("declarator"):
declarators = node.children_by_field_name(
"left" if node.type == "lambda_capture_initializer" else "declarator")
for declarator in declarators:
name = _cpp_declarator_name(declarator, source)
if name:
ranges.setdefault(name, []).append((declarator.start_byte, scope.end_byte))
ranges.setdefault(name, []).append((
declarator.start_byte, scope.end_byte,
_cpp_declared_receiver_type(node, declarator, source),
_cpp_qualified_name(node, "", source).rstrip(":"),
))
stack.extend(node.named_children)
return ranges
@@ -6996,6 +7060,9 @@ def _extract_generic(
cpp_qualified_names[class_nid] = (
qualified if cpp_qualified_names.get(class_nid, qualified) == qualified else ""
)
if node.child_by_field_name("body") is not None:
class_node = next(n for n in nodes if n["id"] == class_nid)
class_node["_cpp_fields"] = _cpp_class_fields(node, source)
# A nested class/object/trait is contained by its ENCLOSING type, not
# the file (#2040). parent_class_nid is threaded down the walk for
# every language and is always a real class-like node (never a
@@ -10212,28 +10279,29 @@ def _extract_generic(
obj.child_by_field_name("name"), source)
elif config.ts_module == "tree_sitter_cpp":
# C++: function field, then field_expression/qualified_identifier
cpp_chain = []
cpp_binding = None
func_node = node.child_by_field_name(config.call_function_field) if config.call_function_field else None
if func_node:
if func_node.type == "identifier":
callee_name = _read_text(func_node, source)
if any(start <= node.start_byte < end for start, end in
if any(start <= node.start_byte < end for start, end, *_ in
cpp_local_bindings.get(caller_nid, {}).get(callee_name, ())):
callee_name = None
elif func_node.type == "field_expression":
# `f.bar()` / `f->bar()` / `this->bar()`: receiver is the
# `argument` (object) field, callee is the `field` (#1547).
# Capture a simple-identifier (or `this`) receiver so the
# cross-file pass can resolve it through the file's type
# table; chained receivers (`a.b.method()`) are left to bail.
# Preserve the full chain for class-scoped cross-file lookup.
is_member_call = True
name = func_node.child_by_field_name("field")
if name:
callee_name = _read_text(name, source)
obj = func_node.child_by_field_name("argument")
if obj is not None and obj.type == "identifier":
member_receiver = _read_text(obj, source)
elif obj is not None and obj.type == "this":
member_receiver = "this"
cpp_chain = _cpp_receiver_chain(func_node, source)
if cpp_chain:
member_receiver = cpp_chain[0][0]
bindings = [b for b in cpp_local_bindings.get(caller_nid, {}).get(
member_receiver, ()) if b[0] <= node.start_byte < b[1]]
cpp_binding = max(bindings, key=lambda b: b[0]) if bindings else None
elif func_node.type == "qualified_identifier":
# `Foo::bar()`: the scope (`Foo`) is the receiver type named
# explicitly in source (EXTRACTED), the name is the callee.
@@ -10499,16 +10567,15 @@ def _extract_generic(
# name must not pick whichever `m` this file declares: through
# `Base* b`, `b->draw()` bound to the file's `Derived::draw`.
# Deferring sends it to _resolve_cpp_member_calls, which types x
# from the file's local/parameter/field table and bails when it
# can't. An untyped receiver and `this->m()` keep the in-file
# binding they had.
# from lexical bindings and class fields. Chains must defer even
# when their root has no known type; bare-name matching is unsafe.
_cpp_member_defer = (
config.ts_module == "tree_sitter_cpp"
and is_member_call
and not is_scoped_call
and member_receiver is not None
and member_receiver != "this"
and member_receiver in type_table
and func_node is not None and func_node.type == "field_expression"
and (len(cpp_chain) != 1 or member_receiver in type_table
or (cpp_binding is not None and bool(cpp_binding[2])))
)
if _python_defer or _java_defer or _csharp_defer or _js_member_defer or _cpp_member_defer or (_builtin_member_call and not php_builtin_self_call) or _lua_member_defer or is_scoped_call or (
is_member_call
@@ -10724,6 +10791,11 @@ def _extract_generic(
# suffix sets, so a source_file suffix alone can't separate them.
if config.ts_module == "tree_sitter_cpp":
rc_entry["lang"] = "cpp"
if func_node is not None and func_node.type == "field_expression":
rc_entry["cpp_receiver_chain"] = cpp_chain
if cpp_binding is not None:
rc_entry["cpp_receiver_type"] = cpp_binding[2]
rc_entry["cpp_receiver_scope"] = cpp_binding[3]
# PHP: separate a scoped class call (`Helper::m()`, source-
# qualified) from an instance member call (`$g->m()`), whose
# receiver is a variable the resolver must type through the
@@ -11111,7 +11183,7 @@ def _extract_generic(
# every function body so the cross-file member-call pass can type a receiver
# (#1547). File-scoped (not per-body): a later body's `Foo f;` doesn't clobber
# an earlier binding (`var not in table`), keeping resolution conservative.
cpp_local_bindings: dict[str, dict[str, list[tuple[int, int]]]] = {}
cpp_local_bindings: dict[str, dict[str, list[tuple[int, int, str, str]]]] = {}
if config.ts_module == "tree_sitter_cpp":
# Each function's table first: a local shadows a parameter there. The
# file table is shared by every function, so a name typed differently in
+167
View File
@@ -0,0 +1,167 @@
"""Declared C++ receiver chains resolve without guessing by method name."""
import json
import pytest
from graphify.extract import extract
HEADER = """
#include <memory>
namespace app {
struct Direct { void Execute() {} };
struct Smart { void Execute() {} };
struct ValueClient { Direct actions; };
struct SmartClient { std::unique_ptr<Smart> actions; };
struct SharedClient { std::shared_ptr<Smart> actions; };
struct RawClient { Smart* actions; };
struct Outer { SmartClient client; };
}
"""
def _extract(tmp_path, source, header=HEADER, reverse=False):
paths = [tmp_path / "types.hpp", tmp_path / "use.cpp"]
paths[0].write_text(header, encoding="utf-8")
paths[1].write_text('#include "types.hpp"\n' + source, encoding="utf-8")
result = extract(paths[::-1] if reverse else paths, root=tmp_path,
cache_root=tmp_path / "cache", parallel=False)
return paths, result
def _calls(result, caller="Run()"):
nodes = {n["id"]: n for n in result["nodes"]}
owners = {e["target"]: nodes[e["source"]].get("_cpp_qualified")
for e in result["edges"] if e["relation"] in {"method", "defines"}}
return [(owners.get(e["target"]), nodes[e["target"]]["label"], e["confidence"])
for e in result["edges"] if e["relation"] == "calls"
and nodes[e["source"]]["label"] == caller]
@pytest.mark.parametrize("reverse", [False, True])
@pytest.mark.parametrize("parameter,expression,owner", [
("app::Direct& x", "x.Execute()", "app::Direct"),
("std::unique_ptr<app::Smart>& x", "x->Execute()", "app::Smart"),
("app::ValueClient& x", "x.actions.Execute()", "app::Direct"),
("app::SmartClient& x", "x.actions->Execute()", "app::Smart"),
("app::SharedClient* x", "x->actions->Execute()", "app::Smart"),
("app::RawClient& x", "(x).actions->Execute()", "app::Smart"),
("app::Outer& x", "x.client.actions->Execute()", "app::Smart"),
("app::SmartClient& x", "([&] { x.actions->Execute(); })()", "app::Smart"),
])
def test_split_header_chains(tmp_path, parameter, expression, owner, reverse):
paths, result = _extract(tmp_path, f"void Run({parameter}) {{ {expression}; }}", reverse=reverse)
assert _calls(result) == [(owner, ".Execute()", "INFERRED")]
cached = extract(paths, root=tmp_path, cache_root=tmp_path / "cache", parallel=False)
assert _calls(cached) == _calls(result)
def test_scope_and_same_named_fields(tmp_path):
_, result = _extract(tmp_path, """
void Run(app::SmartClient& x) {
{ app::ValueClient x; x.actions.Execute(); }
([&](app::ValueClient& x) { x.actions.Execute(); })(*(app::ValueClient*)nullptr);
x.actions->Execute();
}
void Other(app::ValueClient& x) { x.actions.Execute(); }
""")
assert set(_calls(result)) == {("app::Direct", ".Execute()", "INFERRED"),
("app::Smart", ".Execute()", "INFERRED")}
assert _calls(result, "Other()") == [("app::Direct", ".Execute()", "INFERRED")]
@pytest.mark.parametrize("body", [
"{ auto x = unknown(); x.actions->Execute(); }",
"([&](auto& x) { x.actions->Execute(); })(unknown());",
"([x = unknown()] { x.actions->Execute(); })();",
"{ std::vector<app::SmartClient> x; x.actions->Execute(); }",
"{ UnresolvedAlias x; x.actions->Execute(); }",
"x.missing->Execute();",
"x.actions.Execute();", # dot accesses the wrapper, not its pointee
])
def test_unsupported_or_shadowed_receivers_do_not_guess(tmp_path, body):
_, result = _extract(tmp_path, f"void Run(app::SmartClient& x) {{ {body} }}")
assert _calls(result) == []
def test_namespace_ambiguity_does_not_pick_a_same_named_class(tmp_path):
header = HEADER + "\nnamespace other { struct SmartClient { app::Direct actions; }; }"
_, result = _extract(tmp_path, "void Run(SmartClient& x) { x.actions->Execute(); }", header)
assert _calls(result) == []
@pytest.mark.parametrize("body", [
"([&](app::ValueClient& x) { x.actions.Execute(); })(*(app::ValueClient*)nullptr);",
"{ app::ValueClient x; x.actions.Execute(); }",
])
def test_inner_binding_is_the_only_target(tmp_path, body):
_, result = _extract(tmp_path, f"void Run(app::SmartClient& x) {{ {body} }}")
assert _calls(result) == [("app::Direct", ".Execute()", "INFERRED")]
@pytest.mark.parametrize("expression", ["unknown().actions->Execute()", "x.missing->Execute()",
"x.actions->execute()", "x.actions.Execute()"])
def test_same_file_and_case_collisions_do_not_guess(tmp_path, expression):
_, result = _extract(tmp_path, HEADER + f"\nvoid Run(app::SmartClient& x) {{ {expression}; }}", "")
assert _calls(result) == []
def test_lowercase_smart_pointer_type_uses_the_declared_receiver(tmp_path):
_, result = _extract(tmp_path, """
struct action { void Execute() {} };
void Run(std::unique_ptr<action>& x) { x->Execute(); }
""", "")
assert _calls(result) == [("action", ".Execute()", "INFERRED")]
@pytest.mark.parametrize("expression", ["this->client.actions->Execute()", "client.actions->Execute()"])
def test_explicit_this_chain_and_implicit_field(tmp_path, expression):
header = HEADER + "\nstruct Holder { app::SmartClient client; void Run(); };"
_, result = _extract(tmp_path, f"void Holder::Run() {{ {expression}; }}", header)
assert _calls(result, "Holder::Run()") == [("app::Smart", ".Execute()", "INFERRED")]
def test_forward_declaration_is_not_a_competing_definition(tmp_path):
paths, _ = _extract(tmp_path, "void Run(app::SmartClient& x) { x.actions->Execute(); }")
forward = tmp_path / "forward.hpp"
forward.write_text("namespace app { struct Smart; }", encoding="utf-8")
result = extract([forward, *paths], root=tmp_path, cache_root=tmp_path / "cache", parallel=False)
assert _calls(result) == [("app::Smart", ".Execute()", "INFERRED")]
@pytest.mark.parametrize("wrapper", ["std::weak_ptr<app::Smart>", "CustomPtr<app::Smart>",
"std::unique_ptr<app::Smart[]>",
"std::unique_ptr<app::Smart, CustomDeleter>"])
def test_unsupported_pointer_wrappers_do_not_unwrap(tmp_path, wrapper):
_, result = _extract(tmp_path, f"void Run({wrapper}& x) {{ x->Execute(); }}")
assert _calls(result) == []
def test_prior_ast_cache_is_not_reused(tmp_path, monkeypatch):
import graphify.cache as cache
current = cache._AST_CACHE_SCHEMA
assert current > 40
with monkeypatch.context() as old:
old.setattr(cache, "_AST_CACHE_SCHEMA", 40)
paths, _ = _extract(tmp_path, "void Run(app::SmartClient& x) { x.actions->Execute(); }")
# Strip the new metadata as if these entries predated chain support.
for path in (tmp_path / "cache").rglob("*.json"):
text = path.read_text(encoding="utf-8")
path.write_text(text.replace('"_cpp_fields"', '"old_fields"')
.replace('"cpp_receiver_chain"', '"old_chain"'), encoding="utf-8")
fresh = extract(paths, root=tmp_path, cache_root=tmp_path / "cache", parallel=False)
assert _calls(fresh) == [("app::Smart", ".Execute()", "INFERRED")]
def test_incremental_caller_uses_persisted_header_fields(tmp_path):
paths, result = _extract(tmp_path, "void Run(app::SmartClient& x) { x.actions->Execute(); }")
result = json.loads(json.dumps(result))
paths[1].write_text(paths[1].read_text(encoding="utf-8") + "\n// caller edit\n", encoding="utf-8")
fresh = extract([paths[1]], root=tmp_path, cache_root=tmp_path / "cache", parallel=False,
resolution_context_nodes=[n for n in result["nodes"] if n.get("source_file") != "use.cpp"],
resolution_context_edges=[e for e in result["edges"] if e.get("source_file") != "use.cpp"])
combined = {"nodes": result["nodes"] + fresh["nodes"], "edges": [
e for e in result["edges"] if e.get("relation") in {"method", "defines"}
] + fresh["edges"]}
assert _calls(combined) == [("app::Smart", ".Execute()", "INFERRED")]
assert not any(n.get("source_file") == "types.hpp" for n in fresh["nodes"])