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.

feat(dead-code): cpp quick wins — broad tests/ glob, plural benchmarks, embedded pybind11 module, compiler-builtin macros (#286)

Raghav Chamadiya5a4598ec3545
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
index 94b82d2c1..730fe9979 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -225,6 +225,34 @@ _ENTRY_POINT_SYMBOL_NAMES: frozenset[str] = frozenset({
"INSTANCE", # Kotlin object singleton field
"Companion", # Kotlin companion-object accessor
})
+
+
+# Compiler-intrinsic preprocessor macros C/C++ libraries redefine as a
+# fallback for compilers that don't ship them (``#if !defined(__has_include)
+# \n#define __has_include(h) 0``). The tree-sitter cpp grammar extracts the
+# ``#define`` as a ``preproc_function_def`` symbol, but the call sites are
+# preprocessor ``#if __has_include(...)`` directives, which the static
+# graph cannot observe — so without this skip every such fallback flags
+# as an unused export.
+_CPP_BUILTIN_MACROS: frozenset[str] = frozenset({
+ "__has_include",
+ "__has_include_next",
+ "__has_feature",
+ "__has_extension",
+ "__has_attribute",
+ "__has_cpp_attribute",
+ "__has_c_attribute",
+ "__has_declspec_attribute",
+ "__has_builtin",
+ "__has_warning",
+ "__builtin_expect",
+ "__builtin_unreachable",
+ "__builtin_assume",
+ "__builtin_constant_p",
+ "__is_identifier",
+ "__FILE__", "__LINE__", "__DATE__", "__TIME__",
+ "__func__", "__FUNCTION__", "__PRETTY_FUNCTION__",
+})
from .dynamic_markers import find_dynamic_edge_files, find_dynamic_import_files
from .models import DeadCodeFindingData, DeadCodeKind, DeadCodeReport
@@ -705,6 +733,15 @@ class DeadCodeAnalyzer:
continue
if sym_name in _ENTRY_POINT_SYMBOL_NAMES:
continue
+ # Compiler-builtin macros defined as a fallback
+ # (``#if !defined(__has_include)\n#define __has_include(h) 0``).
+ # The tree-sitter cpp grammar emits the ``#define`` as a
+ # ``preproc_function_def`` symbol, but the name is a
+ # compiler intrinsic — there will never be a static caller
+ # because the real call sites are preprocessor
+ # ``#if __has_include(...)`` directives, not C/C++ calls.
+ if sym.get("language") in ("cpp", "c") and sym_name in _CPP_BUILTIN_MACROS:
+ continue
# Rust proc-macro entry points — invoked by the compiler,
# not by call edges in the dependency graph.
if sym.get("language") == "rust":
diff --git a/packages/core/src/repowise/core/analysis/dead_code/constants.py b/packages/core/src/repowise/core/analysis/dead_code/constants.py
index e06e314cb..d2960e03c 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/constants.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/constants.py
@@ -285,6 +285,8 @@ _NEVER_FLAG_PATTERNS: tuple[str, ...] = (
"*/tests/**/*_perf.cpp",
"*/tests/**/*_benchmark.cc",
"*/tests/**/*_benchmark.cpp",
+ "*/tests/**/*_benchmarks.cc",
+ "*/tests/**/*_benchmarks.cpp",
"*/tests/**/*_fuzz.cc",
"*/tests/**/*_fuzz.cpp",
"*/tests/perf/*.cc",
@@ -298,6 +300,17 @@ _NEVER_FLAG_PATTERNS: tuple[str, ...] = (
"*/tests/integration/*.cc",
"*/tests/integration/*.cpp",
"*/tests/manual/*",
+ # Broad test-tree coverage for project layouts that don't follow the
+ # ``*_test.{cc,cpp}`` suffix convention. nlohmann/json uses ``tests/src/
+ # unit-*.cpp``, ``tests/abi/diag/diag_off.cpp``, and ``tests/cmake_*/
+ # project/*.cpp`` — none match the suffix globs above. Anything inside a
+ # repo-rooted ``tests/`` tree is framework-discovered by the build
+ # system, not statically imported.
+ "*/tests/**/*.cc",
+ "*/tests/**/*.cpp",
+ "*/tests/**/*.cxx",
+ "*/tests/**/*.h",
+ "*/tests/**/*.hpp",
"tests/**/*_test.cc",
"tests/**/*_test.cpp",
"tests/**/*_unittest.cc",
@@ -314,6 +327,14 @@ _NEVER_FLAG_PATTERNS: tuple[str, ...] = (
"tests/fuzz/*.cc",
"tests/fuzz/*.cpp",
"tests/manual/*",
+ # Repo-rooted broad coverage matching the ``*/tests/**`` block above —
+ # nlohmann/json's tree (``tests/src/unit-*.cpp``, ``tests/abi/...``)
+ # lives under the repo root with no leading prefix.
+ "tests/**/*.cc",
+ "tests/**/*.cpp",
+ "tests/**/*.cxx",
+ "tests/**/*.h",
+ "tests/**/*.hpp",
# File-suffix conventions for tests dropped outside a standard test
# directory (GoogleTest / Google Benchmark / libFuzzer / Catch2).
"*_test.cc",
@@ -328,6 +349,8 @@ _NEVER_FLAG_PATTERNS: tuple[str, ...] = (
"*_perf.cpp",
"*_benchmark.cc",
"*_benchmark.cpp",
+ "*_benchmarks.cc",
+ "*_benchmarks.cpp",
"*_fuzz.cc",
"*_fuzz.cpp",
# Conventional port / example skeleton headers — projects ship them
diff --git a/packages/core/src/repowise/core/analysis/dead_code/dynamic_markers.py b/packages/core/src/repowise/core/analysis/dead_code/dynamic_markers.py
index bcef04bdf..9810938c7 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/dynamic_markers.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/dynamic_markers.py
@@ -306,6 +306,7 @@ _DYNAMIC_IMPORT_MARKERS: dict[str, tuple[str, ...]] = {
"PLUGINLIB_EXPORT_CLASS(",
"RCLCPP_COMPONENTS_REGISTER_NODE(",
"PYBIND11_MODULE(",
+ "PYBIND11_EMBEDDED_MODULE(",
"BOOST_PYTHON_MODULE(",
"NAPI_MODULE(",
"EM_JS(",
@@ -352,6 +353,7 @@ _DYNAMIC_IMPORT_MARKERS: dict[str, tuple[str, ...]] = {
"PLUGINLIB_EXPORT_CLASS(",
"RCLCPP_COMPONENTS_REGISTER_NODE(",
"PYBIND11_MODULE(",
+ "PYBIND11_EMBEDDED_MODULE(",
"BOOST_PYTHON_MODULE(",
"NAPI_MODULE(",
"__attribute__((constructor))",
@@ -373,6 +375,7 @@ _DYNAMIC_IMPORT_MARKERS: dict[str, tuple[str, ...]] = {
"TEST_CASE(",
"BENCHMARK(",
"PYBIND11_MODULE(",
+ "PYBIND11_EMBEDDED_MODULE(",
"__attribute__((constructor))",
"__attribute__((used))",
"[[gnu::retain]]",
@@ -390,6 +393,7 @@ _DYNAMIC_IMPORT_MARKERS: dict[str, tuple[str, ...]] = {
"QML_ELEMENT",
"BOOST_CLASS_EXPORT",
"PYBIND11_MODULE(",
+ "PYBIND11_EMBEDDED_MODULE(",
"__attribute__((constructor))",
"__attribute__((used))",
"[[gnu::retain]]",
@@ -404,6 +408,7 @@ _DYNAMIC_IMPORT_MARKERS: dict[str, tuple[str, ...]] = {
"QML_ELEMENT",
"BOOST_CLASS_EXPORT",
"PYBIND11_MODULE(",
+ "PYBIND11_EMBEDDED_MODULE(",
"__attribute__((constructor))",
"__attribute__((used))",
"[[gnu::retain]]",
diff --git a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/cpp_macros.py b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/cpp_macros.py
index c17c2eda6..53490ac73 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/cpp_macros.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/synthetic_symbols/cpp_macros.py
@@ -41,7 +41,7 @@ if TYPE_CHECKING:
# over a precompiled regex on the source text.
_PYBIND11_MODULE_RE = re.compile(
- r"\bPYBIND11_MODULE\s*\(\s*([A-Za-z_]\w*)\s*,",
+ r"\bPYBIND11(?:_EMBEDDED)?_MODULE\s*\(\s*([A-Za-z_]\w*)\s*,",
)
_BOOST_PY_MODULE_RE = re.compile(
r"\bBOOST_PYTHON_MODULE(?:_INIT)?\s*\(\s*([A-Za-z_]\w*)\s*\)",
diff --git a/packages/core/src/repowise/core/ingestion/graph_warmups.py b/packages/core/src/repowise/core/ingestion/graph_warmups.py
index 75ce458b8..2620dab6b 100644
--- a/packages/core/src/repowise/core/ingestion/graph_warmups.py
+++ b/packages/core/src/repowise/core/ingestion/graph_warmups.py
@@ -166,6 +166,7 @@ def _warmup_cpp(ctx: "ResolverContext") -> None:
# as an entry point so the dead-code analyzer treats it as live.
_CPP_ENTRY_MARKERS = (
"PYBIND11_MODULE",
+ "PYBIND11_EMBEDDED_MODULE",
"BOOST_PYTHON_MODULE",
"NAPI_MODULE",
"REGISTER_OP",
diff --git a/tests/unit/dead_code/test_cpp_followups.py b/tests/unit/dead_code/test_cpp_followups.py
new file mode 100644
index 000000000..1a08249c5
--- /dev/null
+++ b/tests/unit/dead_code/test_cpp_followups.py
@@ -0,0 +1,87 @@
+"""Regression tests for the Phase 4 follow-up quick wins:
+
+* broader ``tests/**`` glob coverage (nlohmann/json layouts under
+ ``tests/abi/``, ``tests/src/``, ``tests/cmake_*/`` were previously
+ uncovered),
+* plural ``*_benchmarks.{cc,cpp}`` glob (abseil's ``randen_benchmarks.cc``
+ shape),
+* ``PYBIND11_EMBEDDED_MODULE`` recognised as an entry-point marker,
+* compiler-intrinsic preprocessor macros (``__has_include`` redefined as
+ a fallback) skipped from unused_export.
+"""
+
+from __future__ import annotations
+
+import fnmatch
+
+from repowise.core.analysis.dead_code.constants import _NEVER_FLAG_PATTERNS
+
+
+def _matches(path: str) -> bool:
+ return any(fnmatch.fnmatch(path, pat) for pat in _NEVER_FLAG_PATTERNS)
+
+
+class TestTestsTreeBroadGlob:
+ def test_nlohmann_abi_diag_off(self) -> None:
+ assert _matches("tests/abi/diag/diag_off.cpp")
+
+ def test_nlohmann_cmake_target_subproject(self) -> None:
+ assert _matches("tests/cmake_target_include_directories/project/Bar.cpp")
+
+ def test_nlohmann_unit_macro(self) -> None:
+ assert _matches("tests/src/unit-udt_macro.cpp")
+
+ def test_nested_tests_header(self) -> None:
+ assert _matches("tests/fixtures/utils.h")
+
+ def test_prefixed_tests_tree(self) -> None:
+ assert _matches("packages/foo/tests/integration/runner.cpp")
+
+
+class TestPluralBenchmarksGlob:
+ def test_plural_repo_root(self) -> None:
+ assert _matches("absl/random/internal/randen_benchmarks.cc")
+
+ def test_plural_repo_root_cpp(self) -> None:
+ assert _matches("perf/random_benchmarks.cpp")
+
+
+class TestPybind11EmbeddedModuleMarker:
+ def test_marker_is_recognised_by_warmup(self) -> None:
+ from repowise.core.ingestion.graph_warmups import _CPP_ENTRY_MARKERS
+
+ assert "PYBIND11_EMBEDDED_MODULE" in _CPP_ENTRY_MARKERS
+ assert "PYBIND11_MODULE" in _CPP_ENTRY_MARKERS # not regressed
+
+ def test_synthesis_regex_matches_embedded_variant(self) -> None:
+ from repowise.core.ingestion.extractors.synthetic_symbols.cpp_macros import (
+ _PYBIND11_MODULE_RE,
+ )
+
+ src = "PYBIND11_EMBEDDED_MODULE(embed_test, m) { m.def(\"add\", &add); }"
+ m = _PYBIND11_MODULE_RE.search(src)
+ assert m is not None
+ assert m.group(1) == "embed_test"
+
+ def test_synthesis_regex_still_matches_plain_variant(self) -> None:
+ from repowise.core.ingestion.extractors.synthetic_symbols.cpp_macros import (
+ _PYBIND11_MODULE_RE,
+ )
+
+ m = _PYBIND11_MODULE_RE.search(
+ "PYBIND11_MODULE(my_mod, m) { m.def(\"x\", &x); }"
+ )
+ assert m is not None
+ assert m.group(1) == "my_mod"
+
+
+class TestCompilerBuiltinMacroSkip:
+ def test_has_include_in_builtins(self) -> None:
+ from repowise.core.analysis.dead_code.analyzer import _CPP_BUILTIN_MACROS
+
+ assert "__has_include" in _CPP_BUILTIN_MACROS
+ assert "__has_attribute" in _CPP_BUILTIN_MACROS
+ assert "__has_cpp_attribute" in _CPP_BUILTIN_MACROS
+ # The well-known runtime intrinsics common in libc/libcxx code.
+ assert "__builtin_expect" in _CPP_BUILTIN_MACROS
+ assert "__FILE__" in _CPP_BUILTIN_MACROS