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(health): walk C++ condition declaration values for reads and exclude bare receiver fields from params (#3824) (#4122)
Rishabh Mishra020205d0df9e
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.pyindex c7d650044..ba1aef1cf 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py@@ -309,6 +309,17 @@ class CppDefUseDialect(BaseDefUseDialect):self.collect_reads(arg, uses)returnif t in _DECL_KINDS:+ val = node.child_by_field_name("value")+ if val is not None:+ start = len(defs)+ decl = node.child_by_field_name("declarator")+ binder = self._binder_identifier(decl)+ if binder is not None:+ defs.append(self._occ(binder))+ self._declare(defs, start, node)+ self._process(val, defs, uses)+ return+for child in node.named_children:start = len(defs)if child.type == _INIT_DECLARATOR:diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.pyindex 9700534d3..4e1746dc7 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py@@ -75,6 +75,7 @@ from .slice_receiver import (FunctionFacts,_collect_assigned,_ends_in_exit,+ _free_names,_free_write_lines,_function_kinds,_is_receiver,@@ -302,6 +303,7 @@ class _FunctionSlicer:self.carry_defs = _rebinding_defs_dropped(analysis, self.def_lines, lmap)self.scan = _scan_for(lmap, receiver, fn_node)self.free_writes = _free_write_lines(def_use) if receiver and receiver.implicit else []+ self.free_names = _free_names(def_use) if receiver and receiver.implicit else frozenset()# Expression-oriented grammars: a block's last non-statement child is its# value, and a span ending on it would drop that value.self.tail_stmt_kinds = (@@ -425,6 +427,7 @@ class _FunctionSlicer:self.declared_first,reads=self.reads,valueless=self.valueless,+ exclude=self.free_names,)if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:return Nonediff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice_liveness.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice_liveness.pyindex bb67e403a..6ae21e62b 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice_liveness.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice_liveness.py@@ -173,6 +173,7 @@ def _infer_in_out(*,reads: dict[str, list[tuple[int, frozenset[int]]]],valueless: dict[str, frozenset[int]] | None = None,+ exclude: frozenset[str] = frozenset(),) -> tuple[tuple[str, ...], tuple[str, ...]]:"""Infer IN (parameters) and OUT (return) variables for span ``[s, e]``.@@ -195,7 +196,7 @@ def _infer_in_out("""params: list[str] = []returns: list[str] = []- for var in sorted(set(def_lines) | set(use_lines)):+ for var in sorted((set(def_lines) | set(use_lines)) - exclude):dl = def_lines.get(var, [])ul = use_lines.get(var, [])u_lo, u_hi = bisect_left(ul, s), bisect_right(ul, e)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice_receiver.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice_receiver.pyindex ccbff0d05..d0211fe88 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice_receiver.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice_receiver.py@@ -132,6 +132,14 @@ def _whole_receiver_facts(return uses, None if _UNREAD_TARGET in assigned else tuple(sorted(assigned))+def _free_names(def_use: FunctionDefUse) -> frozenset[str]:+ params = {p.name for p in def_use.params}+ declared = {d.var for d in def_use.definitions if d.declares or d.declared_at is not None}+ return frozenset(+ d.var for d in def_use.definitions if d.var not in params and d.var not in declared+ )++def _free_write_lines(def_use: FunctionDefUse) -> list[int]:"""Sorted lines writing a name the function neither declares nor takesas a parameter: in Java or C++ that is a field (or a C++ global) written@@ -156,7 +164,7 @@ def _receiver_facts(found = frozenset().union(*pre.assigns[i : j + 1])fields = None if _UNREAD_TARGET in found else tuple(sorted(found))if receiver.implicit:- return (True if named else None), (None if free_writes else fields)+ return (True if (named or free_writes) else None), (None if free_writes else fields)return named, fieldsdiff --git a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py b/packages/core/src/repowise/core/analysis/health/dataflow/stages.pyindex 36858f197..c4bdc225c 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/stages.py@@ -65,10 +65,13 @@ from .slice import (_block_prefix,_closure_state,_closure_state_crosses,+ _count_in,_declaration_escapes,_declaration_lines,_declared_before_read,_disposes_early,+ _free_names,+ _free_write_lines,_function_lines,_hoisted_bindings,_identifiers,@@ -377,6 +380,12 @@ class _Cutter:self.assigned = facts.assignedself.inner_bindings = facts.inner_bindingsself.rereads, self.import_lines = facts.rereads, facts.import_lines+ self.free_writes = (+ _free_write_lines(facts.analysis.def_use) if receiver and receiver.implicit else []+ )+ self.free_names = (+ _free_names(facts.analysis.def_use) if receiver and receiver.implicit else frozenset()+ )# In a loop body: a break / continue leaving a stage would leave the# helper instead, and a value carried to the next iteration must come# back out of the stage that writes it.@@ -446,6 +455,7 @@ class _Cutter:self.declared_first,reads=self.reads,valueless=self.valueless,+ exclude=self.free_names,)returns = self._own_bindings_dropped(span, s, e, returns)if self.carried:@@ -464,7 +474,8 @@ class _Cutter:):return Nonepre = self.pre- uses, assigns = _receiver_facts(self.receiver, pre, i, j, 0)+ free_writes = _count_in(self.free_writes, s, e)+ uses, assigns = _receiver_facts(self.receiver, pre, i, j, free_writes)return Extraction(start_line=s,end_line=e,diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.pyindex bb8ab4119..58985f31d 100644--- a/tests/unit/health/test_dataflow_langs.py+++ b/tests/unit/health/test_dataflow_langs.py@@ -3407,3 +3407,39 @@ def test_csharp_lambda_writing_outer_local_records_def():assert any(occ.name == "tryCount" for occ in fn.def_use.captured.writes)extractions = find_extractions(fn, lmap)assert not any(x.start_line <= 4 and x.end_line >= 5 for x in extractions)+++def test_cpp_condition_declaration_records_use_of_initializer() -> None:+ code = (+ b"void f() {\n"+ b" if (auto v = settings.get(1)) {\n"+ b" use(v);\n"+ b" }\n"+ b" while (auto w = helper.next()) {\n"+ b" use(w);\n"+ b" }\n"+ b"}\n"+ )+ import tree_sitter_cpp+ from tree_sitter import Language, Parser++ from repowise.core.analysis.health.complexity.languages import get_language_map+ from repowise.core.analysis.health.dataflow.dialects.cpp import CppDefUseDialect++ parser = Parser(Language(tree_sitter_cpp.language()))+ tree = parser.parse(code)+ fn = tree.root_node.children[0]+ body = fn.child_by_field_name("body")+ if_stmt = body.children[1]+ while_stmt = body.children[2]++ lmap = get_language_map("cpp")+ dialect = CppDefUseDialect()++ if_sdu = dialect.statement_def_use(if_stmt, lmap, head_only=True)+ assert any(d.name == "v" for d in if_sdu.defs)+ assert any(u.name == "settings" for u in if_sdu.uses)++ while_sdu = dialect.statement_def_use(while_stmt, lmap, head_only=True)+ assert any(d.name == "w" for d in while_sdu.defs)+ assert any(u.name == "helper" for u in while_sdu.uses)diff --git a/tests/unit/health/test_extract_method_receiver.py b/tests/unit/health/test_extract_method_receiver.pyindex 9e37fc209..afa4eda32 100644--- a/tests/unit/health/test_extract_method_receiver.py+++ b/tests/unit/health/test_extract_method_receiver.py@@ -514,3 +514,55 @@ def test_typescript_static_method_this_is_the_class():"typescript", "class A { static m() { return this; } }", frozenset({"method_definition"}))assert get_defuse_dialect("typescript").receiver(node, get_language_map("typescript")).bound is False+++def test_java_bare_field_writes_are_not_passed_as_params() -> None:+ # Issue 3824 case 2: fields written without this are receiver state, not params+ src = b"""+ class Counter {+ private int total;+ private int seen;++ void add(int[] values, int limit) {+ total = 0;+ for (int v : values) {+ if (v > limit) {+ continue;+ }+ seen++;+ }+ if (total > limit) {+ if (seen > 2) {+ total = limit;+ } else {+ total = total + seen;+ }+ }+ if (total < 0) {+ total = 0;+ }+ System.out.println(total);+ }+ }+ """+ res = analyze_file("Counter.java", "java", src, flagged_only=False)+ fn = res.functions[0]+ lmap = get_language_map("java")+ dialect = get_defuse_dialect("java")+ receiver = dialect.receiver(fn.fn_node, lmap)+ extractions = find_extractions(fn, lmap, receiver=receiver)+ assert extractions+ for x in extractions:+ assert "total" not in x.params+ assert "seen" not in x.params+ assert x.uses_receiver is True++ from repowise.core.analysis.health.dataflow.stages import find_stages++ plan = find_stages(fn, lmap, receiver=receiver)+ if plan.stages:+ for s in plan.stages:+ assert "total" not in s.params+ assert "seen" not in s.params+ assert s.uses_receiver is True+