Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): give C++ calls through this a call site (#4828)

Swati Ahuja8000da6adf57
diff --git a/packages/core/src/repowise/core/ingestion/queries/cpp.scm b/packages/core/src/repowise/core/ingestion/queries/cpp.scm
index 4c1aba361..58d01fe65 100644
--- a/packages/core/src/repowise/core/ingestion/queries/cpp.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/cpp.scm
@@ -368,10 +368,23 @@
arguments: (argument_list) @call.arguments
) @call.site
-; Method call: obj.method(args) or obj->method(args)
+; Method call: obj.method(args) or obj->method(args), and the two ``this``
+; spellings. ``this->m()`` keeps ``this`` as the receiver, which the resolver
+; answers from the caller's own class. ``this->f_->m()`` captures ``f_`` alone,
+; as java.scm does for ``this.f.m()``: the field is the caller's own, so it is
+; typed exactly as a bare ``f_->m()`` is (ceiling: a same-named parameter
+; then shadows the field, as in Java). Any other nested receiver
+; (``a->b->m()``) stays uncaptured, since ``b`` is not the caller's field.
+; One alternation, so tree-sitter runs no extra match pass per call.
(call_expression
function: (field_expression
- argument: (identifier) @call.receiver
+ argument: [
+ (identifier) @call.receiver
+ (this) @call.receiver
+ (field_expression
+ argument: (this)
+ field: (field_identifier) @call.receiver)
+ ]
field: (field_identifier) @call.target
)
arguments: (argument_list) @call.arguments
diff --git a/tests/unit/ingestion/test_cpp_this_calls.py b/tests/unit/ingestion/test_cpp_this_calls.py
new file mode 100644
index 000000000..306404a8a
--- /dev/null
+++ b/tests/unit/ingestion/test_cpp_this_calls.py
@@ -0,0 +1,100 @@
+"""A C++ call written through ``this`` must produce a call site.
+
+The member-call pattern captured an ``identifier`` receiver only, and the
+grammar spells ``this`` as its own node, so ``this->m()`` and
+``this->f_->m()`` produced no call site and no tier could resolve them.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
+
+
+def _write(root: Path, body: str) -> None:
+ (root / "lib").mkdir(parents=True, exist_ok=True)
+ (root / "lib" / "s.cc").write_text(
+ "struct Peer { virtual void Run(int) = 0; };\n"
+ "class S {\n"
+ " public:\n"
+ " Peer* s_;\n"
+ " int other(int n) { return n; }\n"
+ " int step(int n) {\n"
+ f" {body}\n"
+ " }\n"
+ "};\n"
+ )
+
+
+def _parse_and_build(root: Path):
+ parser = ASTParser()
+ builder = GraphBuilder(repo_path=root)
+ parsed = []
+ for fi in FileTraverser(root).traverse():
+ pf = parser.parse_file(fi, Path(fi.abs_path).read_bytes())
+ parsed.append(pf)
+ builder.add_file(pf)
+ calls = [c for pf in parsed for c in pf.calls]
+ return calls, builder.build()
+
+
+def _sites(calls) -> set[tuple[str | None, str]]:
+ return {(c.receiver_name, c.target_name) for c in calls}
+
+
+class TestThisMethodCall:
+ def test_it_has_this_as_its_receiver(self, tmp_path: Path) -> None:
+ _write(tmp_path, "return this->other(n);")
+ calls, _ = _parse_and_build(tmp_path)
+ assert ("this", "other") in _sites(calls)
+
+ def test_it_resolves_to_the_callers_own_method(self, tmp_path: Path) -> None:
+ _write(tmp_path, "return this->other(n);")
+ _, graph = _parse_and_build(tmp_path)
+ data = graph.get_edge_data("lib/s.cc::S::step", "lib/s.cc::S::other")
+ assert data is not None
+ assert data["resolution_origin"] == "self_scope"
+
+
+class TestThisFieldCall:
+ def test_an_arrow_field_keeps_the_field_as_receiver(self, tmp_path: Path) -> None:
+ _write(tmp_path, "this->s_->Run(1); return 0;")
+ calls, _ = _parse_and_build(tmp_path)
+ assert ("s_", "Run") in _sites(calls)
+
+ def test_a_dot_field_keeps_the_field_as_receiver(self, tmp_path: Path) -> None:
+ _write(tmp_path, "return this->m_buf.size();")
+ calls, _ = _parse_and_build(tmp_path)
+ assert ("m_buf", "size") in _sites(calls)
+
+ def test_it_matches_the_call_written_without_this(self, tmp_path: Path) -> None:
+ """``this->s_->Run(1)`` is the same call as ``s_->Run(1)``."""
+ _write(tmp_path / "a", "this->s_->Run(1); return 0;")
+ _write(tmp_path / "b", "s_->Run(1); return 0;")
+ with_this, _ = _parse_and_build(tmp_path / "a")
+ without, _ = _parse_and_build(tmp_path / "b")
+ assert with_this == without
+
+
+class TestOtherNestedReceiversUnchanged:
+ def test_a_field_of_another_object_is_not_captured(self, tmp_path: Path) -> None:
+ """``b`` in ``a->b->Run()`` belongs to ``a``'s type, not the caller's class."""
+ _write(tmp_path, "a->b->Run(2); return 0;")
+ calls, _ = _parse_and_build(tmp_path)
+ assert not any(c.target_name == "Run" for c in calls)
+
+ def test_a_field_of_a_this_field_is_not_captured(self, tmp_path: Path) -> None:
+ _write(tmp_path, "this->a->b->Run(2); return 0;")
+ calls, _ = _parse_and_build(tmp_path)
+ assert not any(c.target_name == "Run" for c in calls)
+
+ def test_a_this_call_to_the_caller_itself_draws_no_self_edge(self, tmp_path: Path) -> None:
+ _write(tmp_path, "return this->step(n - 1);")
+ _, graph = _parse_and_build(tmp_path)
+ assert not graph.has_edge("lib/s.cc::S::step", "lib/s.cc::S::step")
+
+ def test_a_plain_receiver_is_unchanged(self, tmp_path: Path) -> None:
+ _write(tmp_path, "s_->Run(1); return 0;")
+ calls, _ = _parse_and_build(tmp_path)
+ assert ("s_", "Run") in _sites(calls)