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(health): treat extern C signatures as fixed and a condition-variable wait guard as unhoistable (#4781)

Raghav Chamadiya55aaece04033
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
index 9e1639dc6..eba6d8134 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -335,6 +335,8 @@ class LanguageNodeMap:
# binders (``@RequestBody``) stay out: that parameter is the value object.
# * ``explicit_impl_kinds`` / ``trait_impl_kinds`` -- an explicit
# interface implementation on the member, an enclosing trait impl.
+ # * ``fixed_abi_kinds`` -- an enclosing block whose linkage fixes the
+ # parameter lists inside it (C++ ``extern "C"``).
# * ``public_api_modifiers`` / ``public_api_type_kinds`` -- a member
# carrying one of these, inside types that all carry one too, is
# published API. Only for a language with an assembly-internal level
@@ -348,6 +350,7 @@ class LanguageNodeMap:
bound_param_markers: frozenset[str] = frozenset()
explicit_impl_kinds: frozenset[str] = frozenset()
trait_impl_kinds: frozenset[str] = frozenset()
+ fixed_abi_kinds: frozenset[str] = frozenset()
public_api_modifiers: frozenset[str] = frozenset()
public_api_type_kinds: frozenset[str] = frozenset()
scalar_type_names: frozenset[str] = frozenset()
@@ -923,6 +926,7 @@ _DART = LanguageNodeMap(
_CPP = LanguageNodeMap(
misread_scope=misread_scope_keyword,
fixed_signature_markers=frozenset({"override", "final"}),
+ fixed_abi_kinds=frozenset({"linkage_specification"}),
scalar_type_names=_C_SCALARS
| frozenset({"string", "wstring", "string_view", "wstring_view"}),
function_kinds=frozenset({"function_definition"}),
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/signature.py b/packages/core/src/repowise/core/analysis/health/complexity/signature.py
index bac4b3615..b0fbec339 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/signature.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/signature.py
@@ -171,10 +171,27 @@ def is_signature_fixed(fn_node: Node, lmap: LanguageNodeMap) -> bool:
return (
_in_trait_impl(fn_node, lmap)
or _is_public_api(fn_node, lmap)
+ or _in_fixed_abi(fn_node, lmap)
or _overrides_local_base(fn_node, lmap)
)
+def _in_fixed_abi(fn_node: Node, lmap: LanguageNodeMap) -> bool:
+ """Inside a block whose ABI fixes the parameter list (C++ ``extern "C"``)."""
+ if not lmap.fixed_abi_kinds:
+ return False
+ node = fn_node.parent
+ # A member function keeps C++ linkage even inside an ``extern "C"`` block.
+ stops = lmap.function_kinds | lmap.class_kinds
+ while node is not None and node.type not in stops:
+ if node.type in lmap.fixed_abi_kinds:
+ # ``extern "C++" {`` re-enters C++ linkage inside an ``extern "C"`` header.
+ value = node.child_by_field_name("value")
+ return value is None or _text(value) != '"C++"'
+ node = node.parent
+ return False
+
+
def _class_of_method(fn_node: Node, lmap: LanguageNodeMap) -> Node | None:
holder = fn_node
if holder.parent is not None and holder.parent.type in lmap.decorated_definition_kinds:
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/cpp.py
index dd5d75078..e68cdd132 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/cpp.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/cpp.py
@@ -440,6 +440,46 @@ def _declaration_has_initializer(node: Node) -> bool:
return False
+_CV_WAITS: frozenset[bytes] = frozenset({b"wait", b"wait_for", b"wait_until"})
+_LOOP_KINDS: frozenset[str] = frozenset(
+ {"for_statement", "for_range_loop", "while_statement", "do_statement"}
+)
+
+
+def _waits_on_guard(decl: Node) -> bool:
+ """``std::unique_lock lk(mu);`` handed to ``cv.wait(lk)`` in the same loop:
+ a condition-variable wait needs the lock taken there, so it cannot be hoisted."""
+ guard = next(
+ (
+ c.child_by_field_name("declarator")
+ for c in decl.named_children
+ if c.type in ("init_declarator", "function_declarator")
+ ),
+ None,
+ )
+ if guard is None or guard.type != "identifier":
+ return False
+ loop = decl.parent
+ while loop is not None and loop.type not in _LOOP_KINDS:
+ if loop.type in _CPP_FN_KINDS:
+ return False
+ loop = loop.parent
+ return any(_is_wait_on(n, guard.text) for n in _subtree(loop))
+
+
+def _is_wait_on(node: Node, guard: bytes | None) -> bool:
+ """``cv.wait(guard, ...)`` / ``cv_->wait_for(guard, ...)``."""
+ if node.type != "call_expression":
+ return False
+ fn, args = node.child_by_field_name("function"), node.child_by_field_name("arguments")
+ if fn is None or fn.type != "field_expression":
+ return False
+ if args is None or not args.named_children:
+ return False
+ field = fn.child_by_field_name("field")
+ return field is not None and field.text in _CV_WAITS and args.named_children[0].text == guard
+
+
_CPP_FN_KINDS: frozenset[str] = frozenset({"function_definition", "lambda_expression"})
# ``while ((row = mysql_fetch_row(res)))``: a row cursor over the result set it is passed.
_ROW_CURSORS: frozenset[str] = frozenset({"mysql_fetch_row", "PQgetResult"})
@@ -734,7 +774,7 @@ class CppPerfDialect(IterableSources):
if name in REGEX_TYPES:
return "regex_compile_in_loop" if self._has_literal_pattern(node) else None
if name in LOCK_GUARD_TYPES:
- return "lock_in_loop"
+ return None if _waits_on_guard(node) else "lock_in_loop"
return None
# -- what a loop walks (perf/dialects/iterables.py) ---------------------------
diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.py
index 399deb1e3..3bd979bbb 100644
--- a/tests/unit/health/test_perf_dialects.py
+++ b/tests/unit/health/test_perf_dialects.py
@@ -1719,6 +1719,25 @@ _CPP_CASES = [
[("lock_in_loop", "")],
"the explicit acquire form of the same site",
),
+ (
+ "void m() {\n while (active_) {\n std::unique_lock<std::mutex> lock(mu_);\n"
+ " if (cv_.wait_for(lock, iv_, [this] { return !active_; })) {\n"
+ " return;\n }\n tick();\n }\n}\n",
+ [],
+ "a guard handed to a condition-variable wait is the wait idiom",
+ ),
+ (
+ "void m() {\n for (;;) {\n std::unique_lock<std::mutex> lk{mu};\n"
+ " while (q.empty()) cv->wait(lk);\n q.pop();\n }\n}\n",
+ [],
+ "a wait loop nested under the guard's loop",
+ ),
+ (
+ "void m() {\n while (active_) {\n std::unique_lock<std::mutex> lock(mu_);\n"
+ " cv_.wait(other);\n tick();\n }\n}\n",
+ [("lock_in_loop", "")],
+ "a wait on another lock leaves the guard per iteration",
+ ),
(
"void m() {\n for (auto x : load_all()) {\n use(x);\n }\n}\n",
[],
diff --git a/tests/unit/health/test_signature_facts.py b/tests/unit/health/test_signature_facts.py
index b890550e2..24044b587 100644
--- a/tests/unit/health/test_signature_facts.py
+++ b/tests/unit/health/test_signature_facts.py
@@ -172,6 +172,23 @@ def test_rust_trait_impl_is_fixed_and_inherent_impl_is_not():
assert (fns["own"].typed_param_count, fns["own"].primitive_param_count) == (5, 4)
+def test_cpp_extern_c_linkage_fixes_the_signature():
+ params = "(int a, const char* b, size_t c, int d, uint64_t* e)"
+ fns = _fns(
+ "a.cc",
+ "cpp",
+ f'extern "C" {{\nvoid api{params} {{ g(); }}\n'
+ f'extern "C++" {{ void reentered{params} {{ g(); }} }}\n'
+ f"struct S {{ void member{params} {{ g(); }} }};\n}}\n"
+ f'extern "C" int single{params} {{ return 0; }}\n'
+ f"void own{params} {{ g(); }}\n",
+ )
+ assert fns["api"].signature_fixed and fns["single"].signature_fixed
+ assert not fns["own"].signature_fixed and not fns["reentered"].signature_fixed
+ assert not fns["member"].signature_fixed
+ assert fns["own"].primitive_param_count == 5
+
+
def test_untyped_parameters_are_in_neither_count():
fns = _fns("a.py", "python", "def f(a, b, c, d, e: int):\n pass\n")
assert (fns["f"].typed_param_count, fns["f"].primitive_param_count) == (1, 1)