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): read a namespace or type opened after a macro as a scope, not a function (#2961)

Raghav Chamadiyaee70916a4f5b
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
index 23da38570..5f002b56c 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/ast_utils.py
@@ -212,6 +212,16 @@ def _find_function_entry_name(node: Node, lmap: LanguageNodeMap) -> str:
return f"<anonymous@{node.start_point[0] + 1}>"
+def is_misread_scope(node: Node, lmap: LanguageNodeMap) -> bool:
+ """True for a namespace or type the grammar misread as a function."""
+ return lmap.misread_scope is not None and bool(lmap.misread_scope(node))
+
+
+def is_function_node(node: Node, lmap: LanguageNodeMap) -> bool:
+ """True for a function definition, False for a scope the grammar misread as one."""
+ return node.type in lmap.function_kinds and not is_misread_scope(node, lmap)
+
+
def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
"""All function / method definition nodes in the file.
@@ -219,7 +229,8 @@ def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
bodies but do **not** recurse below a function or lambda. Lambdas found
before any function boundary are module-level executable units (for
example route callbacks) and get their own entry; lambdas inside an
- already-collected function still roll up into that function.
+ already-collected function still roll up into that function. A scope
+ the grammar misread as a function is descended like a class body.
"""
out: list[Node] = []
stack: list[Node] = [root]
@@ -228,7 +239,7 @@ def _collect_function_nodes(root: Node, lmap: LanguageNodeMap) -> list[Node]:
if node.type in lmap.lambda_kinds and _is_test_suite_callback(node, lmap):
stack.extend(node.children)
continue
- if node.type in lmap.function_kinds or node.type in lmap.lambda_kinds:
+ if is_function_node(node, lmap) or node.type in lmap.lambda_kinds:
out.append(node)
continue
for child in node.children:
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
index 0a51aa77c..efa2a73f9 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
@@ -21,7 +21,7 @@ from typing import TYPE_CHECKING, NamedTuple
from ....ingestion.python_overload import is_python_overload
from ....test_paths import is_test_related_path
-from .ast_utils import _IDENTIFIER_SUFFIX, _find_name
+from .ast_utils import _IDENTIFIER_SUFFIX, _find_name, is_function_node, is_misread_scope
from .languages import LanguageNodeMap, get_language_map
from .models import ClassComplexity, CohesionGroup, FunctionComplexity
from .nloc import CodeLineIndex
@@ -92,6 +92,14 @@ def _text(node: Node) -> str:
return node.text.decode("utf-8", errors="replace") if node.text is not None else ""
+def _is_own_method(node: Node, lmap: LanguageNodeMap, language: str, source: str) -> bool:
+ """A method of this class: not a nested type the grammar misread as a
+ function (its members are not ours), not a Python ``@overload`` stub."""
+ if not is_function_node(node, lmap):
+ return False
+ return language != "python" or not is_python_overload(node, source)
+
+
def _collect_class_body(
class_node: Node, lmap: LanguageNodeMap, language: str, source: str
) -> _ClassBody:
@@ -109,9 +117,8 @@ def _collect_class_body(
if node.type in lmap.class_kinds:
body.nested.append(node)
elif node.type in lmap.function_kinds:
- if language == "python" and is_python_overload(node, source):
- continue
- body.methods.append(node)
+ if _is_own_method(node, lmap, language, source):
+ body.methods.append(node)
elif node.type in lmap.field_decl_kinds:
body.field_decls.append(node)
elif node.type not in lmap.nested_type_kinds:
@@ -622,7 +629,10 @@ def _collect_classes(
return []
source_str = source.decode("utf-8", errors="replace")
classes: list[ClassComplexity] = []
- for class_node in class_nodes:
+ # A class the grammar fused into a misread scope's head runs on over the
+ # code after it, so its member list is not the class's own.
+ trusted = [n for n in class_nodes if n.parent is None or not is_misread_scope(n.parent, lmap)]
+ for class_node in trusted:
body = _collect_class_body(class_node, lmap, language, source_str)
method_fcs = [fc_by_node_id[m.id] for m in body.methods if m.id in fc_by_node_id]
# Keep nodes and FCs aligned (a method missing from the function
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 2f26dbe4e..2eba7f205 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -40,8 +40,13 @@ without that mapping count explicit receivers only; (2) flat
from __future__ import annotations
+from collections.abc import Callable
from dataclasses import dataclass
+from tree_sitter import Node
+
+from ....ingestion.cpp_export_macros import misread_scope_keyword
+
@dataclass(frozen=True)
class LanguageNodeMap:
@@ -275,6 +280,16 @@ class LanguageNodeMap:
public_api_type_kinds: frozenset[str] = frozenset()
scalar_type_names: frozenset[str] = frozenset()
+ # -- Grammar misreads ----------------------------------------------------
+ # * ``misread_scope`` -- truthy for a ``function_kinds`` node that is
+ # really a namespace or type the grammar misread (C/C++: a macro line
+ # before ``namespace x {``). Such a node is a container: its members
+ # are walked, it is never scored. None means no such misread. Checked
+ # where a scope could be taken for a function (function collection,
+ # class bodies, perf naming); passes that only run inside a collected
+ # function's body never meet one.
+ misread_scope: Callable[[Node], object] | None = None
+
# Scalar type names C and C++ share, Win32's typedefs included because C/C++ on
@@ -644,6 +659,7 @@ _DART = LanguageNodeMap(
)
_CPP = LanguageNodeMap(
+ misread_scope=misread_scope_keyword,
fixed_signature_markers=frozenset({"override", "final"}),
scalar_type_names=_C_SCALARS
| frozenset({"string", "wstring", "string_view", "wstring_view"}),
@@ -706,6 +722,7 @@ _CPP = LanguageNodeMap(
)
_C = LanguageNodeMap(
+ misread_scope=misread_scope_keyword,
scalar_type_names=_C_SCALARS,
function_kinds=frozenset({"function_definition"}),
lambda_kinds=frozenset(),
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
index 6c5f4ba93..63b09156d 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/perf_walk.py
@@ -26,7 +26,12 @@ from typing import TYPE_CHECKING
from ..perf.dialects import PERF_DIALECTS
from ..perf.dialects.base import BasePerfDialect as BasePerfDialectClass
from ..perf.loop_facts import LoopFacts
-from .ast_utils import _dart_signature_sibling, _find_function_entry_name, _find_name
+from .ast_utils import (
+ _dart_signature_sibling,
+ _find_function_entry_name,
+ _find_name,
+ is_function_node,
+)
from .languages import LanguageNodeMap
from .models import PerfFnFacts, PerfHit
@@ -524,7 +529,7 @@ def _collect_perf_hits(
next_loop_line = loop_line if (body_scope or not entering_fn) else 0
next_func = func_name
next_start = func_start
- if t in fn_kinds:
+ if t in fn_kinds and is_function_node(node, lmap):
next_func = _perf_func_name(node) or func_name
next_start = node.start_point[0] + 1
elif t in lambda_kinds and func_name is None:
diff --git a/packages/core/src/repowise/core/ingestion/cpp_export_macros.py b/packages/core/src/repowise/core/ingestion/cpp_export_macros.py
index a5df4804c..bdca57a4e 100644
--- a/packages/core/src/repowise/core/ingestion/cpp_export_macros.py
+++ b/packages/core/src/repowise/core/ingestion/cpp_export_macros.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from collections.abc import Iterator
from dataclasses import dataclass, field
from functools import cached_property
@@ -16,6 +17,90 @@ from .extractors import node_text as _node_text
_CPP_TYPE_SPECIFIER_NODES = frozenset({"class_specifier", "struct_specifier", "enum_specifier"})
_CPP_EXPORT_FORWARD_DECLARATION_NODES = frozenset({"declaration", "field_declaration"})
+# Keywords that open a C/C++ scope. tree-sitter reads an unknown macro line
+# right before one (``FMT_BEGIN_NAMESPACE``, ``ABSL_NAMESPACE_BEGIN``) as a
+# return type, so the namespace or type comes back as a ``function_definition``
+# whose head holds the keyword as a stray identifier or ERROR token.
+_CPP_SCOPE_KEYWORDS = frozenset({"namespace", "class", "struct", "union", "enum"})
+# Name tokens a stray keyword is read as: ``field_identifier`` inside a class body.
+_CPP_NAME_TOKENS = frozenset({"identifier", "field_identifier"})
+# Declarators a misread scope can have: its bare name, qualified or not, or a
+# stray ``template <...>`` (``MACRO template <typename T> class X {``). A real
+# function wraps each of these in a function_declarator.
+_CPP_SCOPE_DECLARATORS = _CPP_NAME_TOKENS | {"qualified_identifier", "template_function"}
+
+
+def _keyword_token(node: Node) -> str | None:
+ if node.type in _CPP_SCOPE_KEYWORDS:
+ return node.type
+ if node.type in _CPP_NAME_TOKENS and node.text is not None:
+ word = node.text.decode("utf-8", errors="replace")
+ return word if word in _CPP_SCOPE_KEYWORDS else None
+ return None
+
+
+def _macro_named_type_keyword(node: Node) -> str | None:
+ """``class MACRO X {``: a bodiless specifier named after the macro, then the real name."""
+ type_node = node.child_by_field_name("type")
+ declarator = node.child_by_field_name("declarator")
+ if type_node is None or type_node.type not in _CPP_TYPE_SPECIFIER_NODES:
+ return None
+ if declarator is None or declarator.type != "identifier":
+ return None
+ if type_node.child_by_field_name("body") is not None:
+ return None
+ return type_node.type.removesuffix("_specifier")
+
+
+def misread_scope_keyword(node: Node) -> str | None:
+ """The keyword of a namespace or type that tree-sitter read as a function.
+
+ Two macro shapes: ``MACRO namespace x {`` / ``MACRO class X {`` (the
+ keyword sits in the definition's head) and ``class MACRO X {`` (the macro
+ reads as a bodiless class name and the real name as the declarator).
+ None for every real function definition.
+ """
+ if node.type != "function_definition":
+ return None
+ macro_named = _macro_named_type_keyword(node)
+ if macro_named is not None:
+ return macro_named
+ # A real function behind a macro (``API struct S *f()``) can also carry the
+ # keyword in its head, but its declarator wraps a function_declarator.
+ declarator = node.child_by_field_name("declarator")
+ if declarator is None or declarator.type not in _CPP_SCOPE_DECLARATORS:
+ return None
+ keywords = (_keyword_token(token) for token in _head_tokens(node))
+ return next((keyword for keyword in keywords if keyword is not None), None)
+
+
+def _head_tokens(node: Node) -> Iterator[Node]:
+ """The definition's children before its body, looking one level into ERROR nodes."""
+ body = node.child_by_field_name("body")
+ for child in node.children:
+ if body is not None and child.id == body.id:
+ return
+ yield from (child.children if child.type == "ERROR" else (child,))
+
+
+def _misread_namespace_ids(matches: list[dict]) -> frozenset[int]:
+ """Misread namespaces enclosing any matched definition.
+
+ Their contents are module-level declarations, so they must not count as
+ the callable ancestor that drops a nested definition.
+ """
+ found: set[int] = set()
+ seen: set[int] = set()
+ def_nodes = (node for capture_dict in matches for node in capture_dict.get("symbol.def", []))
+ for def_node in def_nodes:
+ ancestor = def_node.parent
+ while ancestor is not None and ancestor.id not in seen:
+ seen.add(ancestor.id)
+ if misread_scope_keyword(ancestor) == "namespace":
+ found.add(ancestor.id)
+ ancestor = ancestor.parent
+ return frozenset(found)
+
@dataclass(frozen=True)
class _CppExportType:
@@ -72,10 +157,12 @@ class CppExportTypes:
capture_ids: set[int] = field(default_factory=set)
macro_names: set[str] = field(default_factory=set)
macro_def_ids: set[int] = field(default_factory=set)
+ namespace_ids: frozenset[int] = frozenset()
@cached_property
- def parent_ids(self) -> frozenset[int]:
- return frozenset(self.parents)
+ def container_ids(self) -> frozenset[int]:
+ """Misread nodes that hold declarations, not code: never a callable ancestor."""
+ return frozenset(self.parents) | self.namespace_ids
def add(
self,
@@ -98,7 +185,7 @@ class CppExportTypes:
def collect_cpp_export_types(matches: list[dict], src: str) -> CppExportTypes:
"""Recover every macro-decorated type the query matched in one C++ file."""
- found = CppExportTypes()
+ found = CppExportTypes(namespace_ids=_misread_namespace_ids(matches))
cpp_export_matches = [
capture_dict
for capture_dict in matches
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index 6b79b97bb..9b94f529c 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -1550,7 +1550,7 @@ class ASTParser:
config,
language,
src,
- cpp_exports.parent_ids,
+ cpp_exports.container_ids,
keep_nested=object_owner is not None,
)
if kind is None:
@@ -1638,7 +1638,7 @@ class ASTParser:
config: LanguageConfig,
language: str,
src: str,
- export_type_parent_ids: frozenset[int],
+ container_ids: frozenset[int],
*,
keep_nested: bool = False,
) -> str | None:
@@ -1655,7 +1655,7 @@ class ASTParser:
if (
not keep_nested
and node_type not in _MODULE_ANCHORED_NODE_TYPES
- and _has_callable_ancestor(def_node, config.symbol_node_types, export_type_parent_ids)
+ and _has_callable_ancestor(def_node, config.symbol_node_types, container_ids)
):
return None
diff --git a/packages/core/src/repowise/core/ingestion/parser_helpers.py b/packages/core/src/repowise/core/ingestion/parser_helpers.py
index ad35a4af3..9f67bfd4a 100644
--- a/packages/core/src/repowise/core/ingestion/parser_helpers.py
+++ b/packages/core/src/repowise/core/ingestion/parser_helpers.py
@@ -155,8 +155,8 @@ def _has_callable_ancestor(
from the top-level symbol list. Class bodies don't count — methods
inside classes have only a ``class`` ancestor before the module root.
- ``ignored_node_ids`` covers grammar-recovery nodes that a language query
- has positively identified as type containers rather than callables.
+ ``ignored_node_ids`` covers grammar-recovery nodes that a language has
+ positively identified as type or namespace containers rather than callables.
"""
ancestor = node.parent
while ancestor is not None:
diff --git a/tests/unit/health/test_complexity_walker.py b/tests/unit/health/test_complexity_walker.py
index aa28b4a4b..67ad7904d 100644
--- a/tests/unit/health/test_complexity_walker.py
+++ b/tests/unit/health/test_complexity_walker.py
@@ -539,6 +539,109 @@ def test_cpp_class_cohesion():
assert splintered.field_count == 2
+_CPP_MACRO_SCOPES = b"""namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace base_internal {
+int GetCpuType(int x) { if (x) { return 1; } return 0; }
+} // namespace base_internal
+ABSL_NAMESPACE_END
+} // namespace absl
+FMT_END_EXPORT
+namespace detail {
+int helper() { return 3; }
+}
+template <typename T>
+class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
+ int size() const { return n_; }
+ int n_;
+};
+EXPORT_MACRO class Queue {
+ int pop() { return 0; }
+};
+struct foo *make_foo(void) { return 0; }
+FMT_BEGIN_NAMESPACE
+FMT_EXPORT template <typename Context> class Store {
+ int put() { return 1; }
+};
+"""
+
+
+def test_cpp_namespace_or_type_after_a_macro_is_not_a_function(tmp_path):
+ # A macro line before ``namespace x {`` / ``class X {`` makes tree-sitter
+ # read the scope as a function: it must be walked, never scored.
+ p = tmp_path / "scopes.h"
+ p.write_bytes(_CPP_MACRO_SCOPES)
+ results = walk_file_complexity(str(p), "cpp", _CPP_MACRO_SCOPES)
+ if not results:
+ pytest.skip("tree-sitter language pack missing for cpp")
+ names = {r.name for r in results}
+ assert {"GetCpuType", "helper", "size", "put", "pop", "make_foo"} <= names
+ assert not names & {"namespace", "base_internal", "FixedArray", "Store", "Queue"}
+ assert _find(results, "GetCpuType").ccn == 2
+
+
[email protected]("language", ["cpp", "c"])
+def test_function_behind_a_macro_with_an_aggregate_return_type_stays_a_function(
+ tmp_path, language
+):
+ # The stray ``struct S`` lands in the head as it does for a misread scope,
+ # but the declarator is a real function_declarator.
+ source = b"""int plain(void) { return 0; }
+MYAPI struct S fn1(void) { return s; }
+MYAPI enum E fn2(void) { return A; }
+MYAPI union U fn4(void) { return u; }
+EXPORT_API const struct S *fn5(int a) { if (a) { return 0; } return 0; }
+"""
+ if language == "cpp":
+ source += b"MYAPI class C fn3() { return C(); }\n"
+ p = tmp_path / ("api.h" if language == "cpp" else "api.c")
+ p.write_bytes(source)
+ results = walk_file_complexity(str(p), language, source)
+ if not results:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ expected = {"fn1", "fn2", "fn4", "fn5"} | ({"fn3"} if language == "cpp" else set())
+ assert "plain" in {r.name for r in results}
+ assert expected <= {r.name for r in results}
+
+
+def test_cpp_class_body_skips_a_nested_type_read_as_a_function(tmp_path):
+ source = b"""class Outer {
+ public:
+ int get() { return x_; }
+ EXPORT_MACRO class Inner { int a() { return 1; } int b() { return 2; } };
+ int x_;
+};
+"""
+ p = tmp_path / "outer.h"
+ p.write_bytes(source)
+ classes = {c.name: c for c in walk_file(str(p), "cpp", source).classes}
+ if "Outer" not in classes:
+ pytest.skip("tree-sitter language pack missing for cpp")
+ assert classes["Outer"].method_count == 1
+
+
+def test_cpp_class_fused_into_a_misread_head_is_not_a_cohesion_unit(tmp_path):
+ # When the grammar loses the ``;`` after a class (a macro or recovery
+ # upstream), ``class Fused {...} MACRO class Next {`` reads as one function
+ # whose return type is ``Fused``; its extent and members are not its own.
+ source = b"""class Fused {
+ int push() { return 1; }
+}
+FMT_EXPORT class Next {
+ int get() { return 2; }
+};
+class Plain { int get() { return x_; } int x_; };
+"""
+ p = tmp_path / "fused.h"
+ p.write_bytes(source)
+ fcx = walk_file(str(p), "cpp", source)
+ if not fcx.functions:
+ pytest.skip("tree-sitter language pack missing for cpp")
+ names = {c.name for c in fcx.classes}
+ assert "Plain" in names
+ assert "Fused" not in names
+
+
def test_cpp_assertion_blocks():
results = _walk("cpp/assertions.cpp", "cpp")
many = _find(results, "testManyAsserts")
diff --git a/tests/unit/ingestion/parser/test_cpp.py b/tests/unit/ingestion/parser/test_cpp.py
index 6a4e6c378..ab3d773bf 100644
--- a/tests/unit/ingestion/parser/test_cpp.py
+++ b/tests/unit/ingestion/parser/test_cpp.py
@@ -784,6 +784,59 @@ class TestCppParser:
assert symbols[("PlainOptions", "PlainOptions")].kind == "method"
assert not any(symbol.name == "MYLIB_EXPORT" for symbol in result.symbols)
+ def test_keeps_definitions_inside_a_namespace_opened_after_a_macro(
+ self, parser: ASTParser
+ ) -> None:
+ # tree-sitter reads the macro line as a return type and the namespace
+ # as a function, which used to drop everything inside it as nested.
+ source = b"""namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace base_internal {
+int GetCpuType() { return 1; }
+class Reader {
+ public:
+ int Read() { return 2; }
+};
+} // namespace base_internal
+ABSL_NAMESPACE_END
+} // namespace absl
+FMT_END_EXPORT
+namespace detail {
+int helper() { return 3; }
+}
+"""
+ fi = _make_file_info("cpp_pkg/cpu_detect.cc", "cpp")
+ result = parser.parse_file(fi, source)
+ symbols = {(symbol.parent_name, symbol.name): symbol.kind for symbol in result.symbols}
+
+ assert symbols[(None, "GetCpuType")] == "function"
+ assert symbols[(None, "Reader")] == "class"
+ assert symbols[("Reader", "Read")] == "method"
+ assert symbols[(None, "helper")] == "function"
+
+ def test_keeps_a_macro_function_with_an_aggregate_return_type(self, parser: ASTParser) -> None:
+ source = b"""namespace api {
+MYAPI struct S make_s() { return S(); }
+EXPORT_API const struct S *find_s(int a) { return nullptr; }
+}
+"""
+ fi = _make_file_info("cpp_pkg/api.cc", "cpp")
+ result = parser.parse_file(fi, source)
+ names = {symbol.name for symbol in result.symbols}
+
+ assert {"make_s", "find_s"} <= names
+
+ def test_still_drops_a_function_nested_in_a_real_function(self, parser: ASTParser) -> None:
+ source = b"""MYLIB_API int outer() {
+ struct Local { int f() { return 1; } };
+ return Local().f();
+}
+"""
+ fi = _make_file_info("cpp_pkg/outer.cc", "cpp")
+ result = parser.parse_file(fi, source)
+
+ assert [symbol.name for symbol in result.symbols if symbol.name == "f"] == []
+
def test_preserves_conditional_export_macro_body_recovery(self, parser: ASTParser) -> None:
fi = _make_file_info("cpp_pkg/conditional_options.hpp", "cpp")
result = parser.parse_file(fi, CPP_CONDITIONAL_EXPORT_MACRO_TYPE)