Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(graph): keep std:: and free-function C++ calls off same-named repo members (#4826)
Swati Ahuja58d6b2351ef0
diff --git a/packages/core/src/repowise/core/ingestion/call_language_strategies.py b/packages/core/src/repowise/core/ingestion/call_language_strategies.pyindex 78beb3a15..3bec2bb51 100644--- a/packages/core/src/repowise/core/ingestion/call_language_strategies.py+++ b/packages/core/src/repowise/core/ingestion/call_language_strategies.py@@ -76,6 +76,29 @@ _TYPED_RECEIVER = ("_resolve_typed_receiver",)_EXPECT = "expect"+_FRIEND = re.compile(r"\bfriend\b")+++def _is_member_function(symbol: Symbol | None) -> bool:+ """A method other than a constructor, which a bare call may still name."""+ return (+ symbol is not None+ and symbol.kind == "method"+ and bool(symbol.parent_name)+ and symbol.name != bare_type_name(symbol.parent_name or "")+ )+++def _is_free_function(symbol: Symbol | None) -> bool:+ """A function outside every class, not a macro body (``TEST_F``) that may sit in one."""+ return (+ symbol is not None+ and symbol.kind == "function"+ and not symbol.parent_name+ and not symbol.name.isupper()+ )++def _gradle_script_receiver(path: str) -> str | None:"""The ``this`` a Gradle Kotlin script's own code runs with."""if not path.endswith(".gradle.kts"):@@ -365,6 +388,55 @@ class LanguageStrategiesMixin:return Nonereturn ResolvedCall(caller_id, sym_id, 0.93, call.line, "scoped_name")+ def _cpp_names_no_repo_member(self, file_path: str, call: CallSite, callee_id: str) -> bool:+ """Can a receiver-less C/C++ call not mean *callee_id*, whatever its name?++ ``std::find(..)`` is the standard library's, never a repo symbol that+ shares the name. A bare ``count(..)`` in a function outside every class+ cannot reach a member function either: with no ``this`` and no+ qualifier, only a free function or a ``using namespace`` import answers.+ A constructor is still reached (``Slice(s)``). A caller named in+ capitals is a macro body (``TEST_F``), which may sit inside a class,+ and a caller that is not a function may be a class whose member bodies+ the parse folded in, so both are left alone.+ """+ if self._language_of(file_path) not in ("c", "cpp"):+ return False+ scope = call.scope_name or ""+ if scope == "std" or scope.startswith("std::"):+ return True+ if call.receiver_call is not None or scope:+ return False+ callee = self._symbols_by_id.get(callee_id)+ caller = self._symbols_by_id.get(call.caller_symbol_id or "")+ return _is_free_function(caller) and self._is_cpp_member_function(callee)++ def _is_cpp_member_function(self, symbol: Symbol | None) -> bool:+ """Is *symbol* a member function a bare call from outside its class cannot reach?++ Not when its parent names no repo type: ``int util::clamp()`` is a+ namespace's free function. Nor a ``friend`` defined in the class body,+ which lives at namespace scope and is found by argument lookup. And+ only a body inside its class, or one whose own line writes+ ``Parent::name``: a macro before a C return type+ (``INLINE rtree_t\\nrtree_read(..)``) can parse as a member of that type.+ """+ if not _is_member_function(symbol):+ return False+ assert symbol is not None+ parent = bare_type_name(symbol.parent_name or "")+ if not self._declared_types_named(parent):+ return False+ path = self._symbol_paths_by_id.get(symbol.id, "")+ lines = self._text_of(path).splitlines()+ head = lines[symbol.start_line - 1] if 0 < symbol.start_line <= len(lines) else ""+ if _FRIEND.search(head):+ return False+ span = self._spans_for(path).get(self._only_type_in(path, parent) or "")+ return f"::{symbol.name}" in head or (+ span is not None and span[0] <= symbol.start_line <= span[1]+ )+def _resolve_cpp_same_target(self,file_path: str,diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.pyindex 0dfae9f5e..dd642ca45 100644--- a/packages/core/src/repowise/core/ingestion/call_resolver.py+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py@@ -958,6 +958,8 @@ class CallResolver(if invoked is not None:return invokedhit = self._resolve_free_call(file_path, call, caller_id)+ if hit is not None and self._cpp_names_no_repo_member(file_path, call, hit.callee_id):+ return Nonealias = self._symbols_by_id.get(hit.callee_id) if hit is not None else Noneif _is_kotlin_alias(alias):return self._through_kotlin_alias(alias, call, caller_id)diff --git a/tests/unit/ingestion/test_cpp_std_name_leaks.py b/tests/unit/ingestion/test_cpp_std_name_leaks.pynew file mode 100644index 000000000..94aaa03df--- /dev/null+++ b/tests/unit/ingestion/test_cpp_std_name_leaks.py@@ -0,0 +1,113 @@+"""A receiver-less C++ call that cannot mean a repo member must not bind to one by name.++``std::find(a, b, c)`` is the standard algorithm, and a bare ``count(..)`` in a+free function under ``using namespace std`` is too: neither can reach+``HashMap::find`` or ``HashMap::count``, which only a receiver or an enclosing+class's ``this`` can.+"""++from __future__ import annotations++from pathlib import Path++from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder+++def _callees(root: Path, caller: str) -> set[str]:+ traverser = FileTraverser(root)+ parser = ASTParser()+ builder = GraphBuilder(repo_path=root)+ for fi in traverser.traverse():+ builder.add_file(parser.parse_file(fi, Path(fi.abs_path).read_bytes()))+ graph = builder.build()+ return {+ t for s, t, d in graph.edges(data=True) if d.get("edge_type") == "calls" and s == caller+ }+++_MAP_H = (+ "#pragma once\nstruct HashMap {\n int find(int k);\n int count(int k);\n"+ " int Lookup(int k) { return find(k); }\n};\n"+ "inline int HashMap::find(int k) { return k; }\ninline int HashMap::count(int k) { return k; }\n"+)+++def _repo(root: Path, body: str) -> None:+ (root / "src").mkdir(parents=True, exist_ok=True)+ (root / "src" / "map.h").write_text(_MAP_H)+ (root / "src" / "use.cc").write_text(+ '#include <algorithm>\n#include "map.h"\nusing namespace std;\n' + body+ )+++class TestStdNameLeaks:+ def test_a_std_qualified_call_binds_no_repo_member(self, tmp_path: Path) -> None:+ _repo(tmp_path, "int Free(int* a, int* b) { return *std::find(a, b, 1); }\n")+ assert not _callees(tmp_path, "src/use.cc::Free")++ def test_a_bare_call_in_a_free_function_binds_no_member(self, tmp_path: Path) -> None:+ _repo(tmp_path, "int Free(int* a, int* b) { return count(a, b, 2); }\n")+ assert not _callees(tmp_path, "src/use.cc::Free")++ def test_a_member_still_reaches_its_own_class_method(self, tmp_path: Path) -> None:+ """The control: inside ``HashMap``, a bare ``find`` is ``this->find``."""+ _repo(tmp_path, "int Free() { return 0; }\n")+ assert "src/map.h::HashMap::find" in _callees(tmp_path, "src/map.h::HashMap::Lookup")++ def test_a_free_function_of_the_name_still_binds(self, tmp_path: Path) -> None:+ _repo(+ tmp_path,+ "int count(int* a, int* b, int v) { return 0; }\n"+ "int Free(int* a, int* b) { return count(a, b, 2); }\n",+ )+ assert "src/use.cc::count" in _callees(tmp_path, "src/use.cc::Free")++ def test_a_namespace_qualified_definition_is_a_free_function(self, tmp_path: Path) -> None:+ """``int util::clamp()`` names a namespace, not a class: the bare call stands."""+ _repo(tmp_path, "")+ (tmp_path / "src" / "util.cc").write_text("int util::clamp(int x) { return x; }\n")+ (tmp_path / "src" / "use.cc").write_text(+ "using namespace util;\nint Free() { return clamp(1); }\n"+ )+ assert "src/util.cc::util::clamp" in _callees(tmp_path, "src/use.cc::Free")++ def test_a_friend_defined_in_the_class_still_binds(self, tmp_path: Path) -> None:+ _repo(tmp_path, "")+ (tmp_path / "src" / "foo.h").write_text(+ "#pragma once\nstruct Foo {\n int v;\n"+ " friend void Exchange(Foo& a, Foo& b) { a.v = b.v; }\n};\n"+ )+ (tmp_path / "src" / "use.cc").write_text(+ '#include "foo.h"\nvoid Free(Foo a, Foo b) { Exchange(a, b); }\n'+ )+ assert any(t.endswith("Exchange") for t in _callees(tmp_path, "src/use.cc::Free"))++ def test_a_constructor_still_binds(self, tmp_path: Path) -> None:+ _repo(tmp_path, "")+ (tmp_path / "src" / "slice.h").write_text(+ "#pragma once\nstruct Slice {\n Slice(const char* s);\n const char* d;\n};\n"+ "inline Slice::Slice(const char* s) : d(s) {}\n"+ )+ (tmp_path / "src" / "use.cc").write_text(+ '#include "slice.h"\nint Free() { Slice s = Slice("x"); return 0; }\n'+ )+ assert any("Slice::Slice" in t for t in _callees(tmp_path, "src/use.cc::Free"))++ def test_a_macro_misparsed_free_function_still_binds(self, tmp_path: Path) -> None:+ """An unexpanded ``INLINE box_contents_t`` parses ``box_decode`` as its member.++ The spelling is jemalloc's ``rtree.h`` cut down; the parse is sensitive to it.+ """+ _repo(tmp_path, "")+ (tmp_path / "src" / "types.h").write_text(+ "typedef struct box_s { int x; } box_contents_t;\n"+ )+ (tmp_path / "src" / "box.h").write_text(+ "LIB_ALWAYS_INLINE uintptr_t\nbox_encode(box_contents_t contents) {\n\treturn 0;\n}\n\n"+ "LIB_ALWAYS_INLINE box_contents_t\nbox_decode(uintptr_t bits) {\n"+ "\tbox_contents_t contents;\n\treturn contents;\n}\n"+ )+ (tmp_path / "src" / "use.cc").write_text(+ '#include "box.h"\nint Free() { return box_decode(1).x; }\n'+ )+ assert any(t.endswith("box_decode") for t in _callees(tmp_path, "src/use.cc::Free"))