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(cpp): discover project export macros in headers beside target sources (#3706)

Amanf09f5d233172
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/cpp_workspace.py b/packages/core/src/repowise/core/ingestion/resolvers/cpp_workspace.py
index 6c0c4d01c..5b557bd7b 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/cpp_workspace.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/cpp_workspace.py
@@ -51,6 +51,10 @@ from ..external_systems.cmake import (
)
from ..source_text import decode_source
+# One list of C/C++ header extensions: the resolver's, so the fallback scan
+# below cannot drift from what the include resolver treats as a header.
+from .cpp import _HEADER_EXTS
+
if TYPE_CHECKING:
from .context import ResolverContext
@@ -360,6 +364,33 @@ def _register_target(index: CppWorkspaceIndex, t: CppTarget) -> None:
index.public_header_includes.setdefault(rel, hdr)
+_MAX_HEADER_CANDIDATES = 200
+
+
+def _headers_near_target_sources(index: CppWorkspaceIndex, path_set: set[str]) -> list[str]:
+ """Headers in the directories of each target's own sources.
+
+ Fallback for export-macro discovery when no target declares public
+ headers. Deterministic (targets and paths are sorted) and capped, so a
+ huge tree cannot make the scan unbounded.
+ """
+ src_dirs: set[str] = set()
+ for tid in sorted(index.targets):
+ for src in index.targets[tid].sources:
+ src_dirs.add(PurePosixPath(src.replace("\\", "/")).parent.as_posix())
+ if not src_dirs:
+ return []
+ found: list[str] = []
+ for p in sorted(path_set):
+ if not p.lower().endswith(_HEADER_EXTS):
+ continue
+ if PurePosixPath(p.replace("\\", "/")).parent.as_posix() in src_dirs:
+ found.append(p)
+ if len(found) > _MAX_HEADER_CANDIDATES:
+ break
+ return found
+
+
def build_cpp_workspace_index(ctx: ResolverContext) -> CppWorkspaceIndex:
index = CppWorkspaceIndex()
if ctx.repo_path is None:
@@ -422,6 +453,11 @@ def build_cpp_workspace_index(ctx: ResolverContext) -> CppWorkspaceIndex:
header_candidates.append(p)
if len(header_candidates) > 200:
break
+ if not header_candidates:
+ # No target declares public headers and there is no ``include/``
+ # tree (Flutter FFI plugins, small shared libraries): the header
+ # that defines the project's export macro sits beside the sources.
+ header_candidates.extend(_headers_near_target_sources(index, path_set))
for rel in header_candidates:
data = source_map.get(rel) if source_map is not None else None
if data is not None:
diff --git a/tests/integration/test_cpp_dead_code.py b/tests/integration/test_cpp_dead_code.py
index 0f6f658c0..616b94d93 100644
--- a/tests/integration/test_cpp_dead_code.py
+++ b/tests/integration/test_cpp_dead_code.py
@@ -138,3 +138,43 @@ class TestLivePathNotFlagged:
# from unreachable_file is the assertion that matters.
unreachable = _paths(cpp_report, DeadCodeKind.UNREACHABLE_FILE)
assert not any("brew_test.cc" in p for p in unreachable)
+
+
+def test_ffi_plugin_export_macro_functions_not_unused_exports(tmp_path: Path) -> None:
+ """Header beside the sources (Flutter FFI plugin layout), issue #3689.
+
+ ``FFI_PLUGIN_EXPORT`` is defined in ``dartjni.h`` next to the ``.c``
+ file, with no public-header declaration in CMake and no ``include/``.
+ The functions it tags are the plugin's public surface and must not be
+ reported as ``unused_export``.
+ """
+ root = tmp_path / "plugin"
+ src = root / "src" / "health_connect"
+ src.mkdir(parents=True)
+ (src / "CMakeLists.txt").write_text(
+ "cmake_minimum_required(VERSION 3.10)\n"
+ "project(health_connect VERSION 0.0.1 LANGUAGES C)\n"
+ 'add_library(health_connect SHARED\n "./health_connect.c"\n)\n'
+ )
+ (src / "dartjni.h").write_text(
+ "#pragma once\n#if _WIN32\n#define FFI_PLUGIN_EXPORT __declspec(dllexport)\n"
+ "#else\n#define FFI_PLUGIN_EXPORT\n#endif\n"
+ "FFI_PLUGIN_EXPORT int SpawnJvm(int args);\n"
+ )
+ (src / "health_connect.c").write_text(
+ '#include "dartjni.h"\n'
+ "FFI_PLUGIN_EXPORT\nint SpawnJvm(int args) { return args + 1; }\n"
+ "FFI_PLUGIN_EXPORT\nint GetVersion(void) { return 3; }\n"
+ )
+
+ parser = ASTParser()
+ builder = GraphBuilder(repo_path=root)
+ for fi in FileTraverser(root).traverse():
+ builder.add_file(parser.parse_file(fi, Path(fi.abs_path).read_bytes()))
+ graph = builder.build()
+ builder.add_framework_edges([])
+
+ report = DeadCodeAnalyzer(graph, git_meta_map={}).analyze({"min_confidence": 0.0})
+ unused = _names(report, DeadCodeKind.UNUSED_EXPORT)
+ assert "SpawnJvm" not in unused
+ assert "GetVersion" not in unused
diff --git a/tests/unit/ingestion/test_cpp_workspace.py b/tests/unit/ingestion/test_cpp_workspace.py
index 7a3e37874..c35f055c0 100644
--- a/tests/unit/ingestion/test_cpp_workspace.py
+++ b/tests/unit/ingestion/test_cpp_workspace.py
@@ -5,6 +5,7 @@ from __future__ import annotations
from pathlib import Path
import networkx as nx
+import pytest
from repowise.core.ingestion.resolvers.context import ResolverContext
from repowise.core.ingestion.resolvers.cpp import (
@@ -128,6 +129,72 @@ class COFFEE_EXPORT Brewer { };
assert "COFFEE_EXPORT" in index.project_export_macros
+def _ffi_plugin_fixture(tmp_path, extra_header: str = ""):
+ _write(tmp_path, "src/health_connect/CMakeLists.txt", """
+ cmake_minimum_required(VERSION 3.10)
+ project(health_connect VERSION 0.0.1 LANGUAGES C)
+ add_library(health_connect SHARED
+ "./health_connect.c"
+ )
+ """)
+ _write(tmp_path, "src/health_connect/dartjni.h", """
+#pragma once
+#if _WIN32
+#define FFI_PLUGIN_EXPORT __declspec(dllexport)
+#else
+#define FFI_PLUGIN_EXPORT
+#endif
+FFI_PLUGIN_EXPORT int SpawnJvm(int args);
+""" + extra_header)
+ _write(tmp_path, "src/health_connect/health_connect.c", """
+#include "dartjni.h"
+FFI_PLUGIN_EXPORT
+int SpawnJvm(int args) { return args + 1; }
+""")
+ paths = [
+ "src/health_connect/CMakeLists.txt",
+ "src/health_connect/dartjni.h",
+ "src/health_connect/health_connect.c",
+ ]
+ return _make_ctx(tmp_path, paths)
+
+
+def test_export_macro_found_in_header_beside_sources(tmp_path):
+ # Flutter FFI plugin layout: no public headers declared, no include/.
+ index = build_cpp_workspace_index(_ffi_plugin_fixture(tmp_path))
+ assert "FFI_PLUGIN_EXPORT" in index.project_export_macros
+
+
[email protected]("header", ["api.hh", "api.h++", "api.hpp", "api.hxx"])
+def test_export_macro_found_in_other_header_extensions(tmp_path, header):
+ _write(tmp_path, "CMakeLists.txt", "add_library(coffee SHARED src/brew.cc)\n")
+ _write(tmp_path, f"src/{header}", "#define COFFEE_EXPORT __declspec(dllexport)\n")
+ _write(tmp_path, "src/brew.cc", "")
+ paths = [f"src/{header}", "src/brew.cc", "CMakeLists.txt"]
+ index = build_cpp_workspace_index(_make_ctx(tmp_path, paths))
+ assert "COFFEE_EXPORT" in index.project_export_macros
+
+
+def test_non_export_macro_beside_sources_is_filtered(tmp_path):
+ ctx = _ffi_plugin_fixture(tmp_path, "#define BUFFER_SIZE 4096\n")
+ index = build_cpp_workspace_index(ctx)
+ assert "BUFFER_SIZE" not in index.project_export_macros
+ assert "FFI_PLUGIN_EXPORT" in index.project_export_macros
+
+
+def test_include_dir_fallback_unchanged_by_source_dir_scan(tmp_path):
+ # include/ still wins; a header beside the sources is not scanned.
+ _write(tmp_path, "CMakeLists.txt", "add_library(coffee STATIC src/brew.cc)\n")
+ _write(tmp_path, "include/coffee/api.h",
+ "#define COFFEE_EXPORT __declspec(dllexport)\n")
+ _write(tmp_path, "src/brew.cc", "")
+ _write(tmp_path, "src/local.h", "#define LOCAL_EXPORT __declspec(dllexport)\n")
+ paths = ["include/coffee/api.h", "src/brew.cc", "src/local.h", "CMakeLists.txt"]
+ index = build_cpp_workspace_index(_make_ctx(tmp_path, paths))
+ assert "COFFEE_EXPORT" in index.project_export_macros
+ assert "LOCAL_EXPORT" not in index.project_export_macros
+
+
def test_no_workspace_falls_back_to_stem(tmp_path):
_write(tmp_path, "src/util.h", "")
paths = ["src/util.h", "src/main.cc"]