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(dead-code): rescue C/C++ files listed in CMake, .vcxproj or .rc (#3820) (#4121)

Rishabh Mishrabd819e7c34ac
diff --git a/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
index 6072be801..bd8e80d35 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/cpp_reachability.py
@@ -178,6 +178,11 @@ def is_cpp_file_reachable(
if has_dependency_importer(graph, node):
return True
+ # Build-listed rescue: the file is compiled by a CMake target, MSBuild
+ # .vcxproj, or included by an .rc resource script.
+ if graph.nodes.get(node, {}).get("build_listed"):
+ return True
+
# ``int main`` / ``WinMain`` / fuzzer entry — the file IS the binary
# entry point.
if _file_defines_entry_function(graph, node):
diff --git a/packages/core/src/repowise/core/ingestion/graph_warmups.py b/packages/core/src/repowise/core/ingestion/graph_warmups.py
index 9bd1d3d21..043e66dcb 100644
--- a/packages/core/src/repowise/core/ingestion/graph_warmups.py
+++ b/packages/core/src/repowise/core/ingestion/graph_warmups.py
@@ -316,6 +316,7 @@ def _warmup_cpp(ctx: ResolverContext) -> None:
break
_mark_cpp_entry_point_files(parsed_files, graph, getattr(ctx, "source_map", None))
+ _stamp_cpp_build_listed(ctx, graph, index)
# Tokens whose presence means the surrounding TU wires itself into a
@@ -793,3 +794,59 @@ def run_warmups(
done = getattr(progress, "on_phase_done", None)
if callable(done):
done(phase_name)
+
+
+def _stamp_cpp_build_listed(ctx: ResolverContext, graph: Any, index: Any) -> None:
+ """Stamp file nodes compiled by CMake, .vcxproj, or included by .rc as build_listed."""
+ import re
+
+ from repowise.core.fs_walk import glob_via
+
+ repo_path = ctx.repo_path
+ if repo_path is None:
+ return
+ root = repo_path.resolve()
+
+ # 1. CMake targets from cpp_workspace index (sources only; headers are not compiled TUs)
+ for target in getattr(index, "targets", {}).values():
+ for src in getattr(target, "sources", ()):
+ if src in graph.nodes:
+ graph.nodes[src]["build_listed"] = True
+
+ # 2. MSBuild .vcxproj files (ClCompile only; ClInclude headers are not compiled TUs)
+ item_re = re.compile(r"""<ClCompile\s+Include=["']([^"']+)["']""", re.IGNORECASE)
+ for vcxproj in glob_via(ctx.walk_snapshot, root, "*.vcxproj", prune_nested_git=ctx.prune_nested_git):
+ try:
+ content = vcxproj.read_text(encoding="utf-8", errors="ignore")
+ except OSError:
+ continue
+ vdir = vcxproj.parent
+ for m in item_re.finditer(content):
+ inc = m.group(1).strip()
+ if "$(" in inc or "*" in inc or "?" in inc:
+ continue
+ resolved = (vdir / inc.replace("\\", "/")).resolve()
+ try:
+ rel = str(resolved.relative_to(root)).replace("\\", "/")
+ if rel in graph.nodes:
+ graph.nodes[rel]["build_listed"] = True
+ except ValueError:
+ pass
+
+ # 3. Windows resource scripts (.rc)
+ rc_re = re.compile(r"""^[ ]*#[ ]*include[ ]+["']([^"']+)["']""", re.MULTILINE)
+ for rc in glob_via(ctx.walk_snapshot, root, "*.rc", prune_nested_git=ctx.prune_nested_git):
+ try:
+ content = rc.read_text(encoding="utf-8", errors="ignore")
+ except OSError:
+ continue
+ rc_dir = rc.parent
+ for m in rc_re.finditer(content):
+ inc = m.group(1).strip()
+ resolved = (rc_dir / inc.replace("\\", "/")).resolve()
+ try:
+ rel = str(resolved.relative_to(root)).replace("\\", "/")
+ if rel in graph.nodes:
+ graph.nodes[rel]["build_listed"] = True
+ except ValueError:
+ pass
diff --git a/tests/unit/analysis/test_cpp_reachability.py b/tests/unit/analysis/test_cpp_reachability.py
index d9ed138ac..1c51c8b4b 100644
--- a/tests/unit/analysis/test_cpp_reachability.py
+++ b/tests/unit/analysis/test_cpp_reachability.py
@@ -22,6 +22,7 @@ Asserts:
from __future__ import annotations
import fnmatch
+from pathlib import Path
import networkx as nx
@@ -538,3 +539,56 @@ class TestCppEndToEnd:
report = analyzer.analyze({"min_confidence": 0.0})
paths = {f.file_path for f in report.findings}
assert "apps/io_tester/io_tester.cc" not in paths
+
+
+def test_build_listed_cpp_file_is_reachable() -> None:
+ # Issue 3820: a file stamped build_listed is reachable
+ from repowise.core.analysis.dead_code.cpp_reachability import is_cpp_file_reachable
+
+ g = nx.DiGraph()
+ g.add_node("WorkspacesLib/SteamGameHelper.cpp", build_listed=True, language="cpp")
+ g.add_node("Orphan.cpp", build_listed=False, language="cpp")
+
+ assert is_cpp_file_reachable("WorkspacesLib/SteamGameHelper.cpp", g, {}) is True
+ assert is_cpp_file_reachable("Orphan.cpp", g, {}) is False
+
+
+def test_stamp_cpp_build_listed_ignores_headers(tmp_path: Path) -> None:
+ # Review finding: headers in CMake targets or vcxproj ClInclude are not compilation units
+ from unittest.mock import MagicMock
+
+ from repowise.core.ingestion.graph_warmups import _stamp_cpp_build_listed
+ from repowise.core.ingestion.resolvers import ResolverContext
+
+ (tmp_path / "App.vcxproj").write_text(
+ '<Project>\n'
+ ' <ItemGroup>\n'
+ ' <ClCompile Include="src/main.cpp" />\n'
+ ' <ClInclude Include="include/stale.h" />\n'
+ ' </ItemGroup>\n'
+ '</Project>\n'
+ )
+ (tmp_path / "app.rc").write_text('#include "include/branding.h"\n')
+
+ g = nx.DiGraph()
+ g.add_node("src/main.cpp")
+ g.add_node("include/stale.h")
+ g.add_node("include/branding.h")
+ g.add_node("cmake_src.cpp")
+ g.add_node("cmake_hdr.h")
+
+ target = MagicMock()
+ target.sources = ("cmake_src.cpp",)
+ target.public_headers = ("cmake_hdr.h",)
+ target.private_headers = ()
+ index = MagicMock()
+ index.targets = {"t": target}
+
+ ctx = ResolverContext(path_set=set(), stem_map={}, graph=None, repo_path=tmp_path)
+ _stamp_cpp_build_listed(ctx, g, index)
+
+ assert g.nodes["src/main.cpp"].get("build_listed") is True
+ assert g.nodes["cmake_src.cpp"].get("build_listed") is True
+ assert g.nodes["include/branding.h"].get("build_listed") is True
+ assert "build_listed" not in g.nodes["include/stale.h"]
+ assert "build_listed" not in g.nodes["cmake_hdr.h"]