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): a function that returns a pointer or a reference is a symbol (#2774)

Raghav Chamadiya8442d861f29b
diff --git a/packages/core/src/repowise/core/ingestion/queries/c.scm b/packages/core/src/repowise/core/ingestion/queries/c.scm
index 7cf9d048e..9d626f027 100644
--- a/packages/core/src/repowise/core/ingestion/queries/c.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/c.scm
@@ -14,6 +14,32 @@
)
) @symbol.def
+; A declarator that returns a pointer wraps the function_declarator:
+; ``client *createClient(...)``, ``char **argv(...)``, and a function returning
+; a function pointer, ``int (*handler(int k))(int)``. Queries cannot recurse,
+; so each wrapper depth is listed (three pointer levels, ``T ***f()``, are not
+; matched; none occur in the C codebases checked). A function-pointer variable
+; (``void (*cb)(int);``) has no inner function_declarator and never matches.
+(function_definition
+ declarator: [
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))
+ (pointer_declarator
+ declarator: (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params)))
+ (function_declarator
+ declarator: (parenthesized_declarator
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))))
+ ]
+) @symbol.def
+
(struct_specifier
name: (type_identifier) @symbol.name
) @symbol.def
@@ -66,6 +92,27 @@
)
) @symbol.def
+; ... returning a pointer, the same shapes as the definitions above
+(declaration
+ declarator: [
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))
+ (pointer_declarator
+ declarator: (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params)))
+ (function_declarator
+ declarator: (parenthesized_declarator
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))))
+ ]
+) @symbol.def
+
; ---------------------------------------------------------------------------
; Imports (#include directives)
; ---------------------------------------------------------------------------
diff --git a/packages/core/src/repowise/core/ingestion/queries/cpp.scm b/packages/core/src/repowise/core/ingestion/queries/cpp.scm
index 39e04d666..e3d72665f 100644
--- a/packages/core/src/repowise/core/ingestion/queries/cpp.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/cpp.scm
@@ -110,6 +110,55 @@
)
) @symbol.def
+; A definition returning a pointer or a reference wraps the function_declarator:
+; ``Node *make(...)``, ``char **argv(...)``, ``Foo& Foo::operator=(...)``, and a
+; function returning a function pointer, ``int (*handler(int k))(int)``.
+; Queries cannot recurse, so each wrapper depth is listed (up to two pointer
+; levels; ``T ***f()`` is not matched); the name shapes are
+; the ones the unwrapped patterns above accept. ``reference_declarator`` does
+; not name its declarator field, so its child is matched unnamed.
+(function_definition
+ declarator: [
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: [
+ (identifier) @symbol.name
+ (field_identifier) @symbol.name
+ (qualified_identifier name: (identifier) @symbol.name)
+ (qualified_identifier name: (qualified_identifier name: (identifier) @symbol.name))
+ (qualified_identifier name: (operator_name) @symbol.name)
+ ]
+ parameters: (parameter_list) @symbol.params))
+ (pointer_declarator
+ declarator: (pointer_declarator
+ declarator: (function_declarator
+ declarator: [
+ (identifier) @symbol.name
+ (field_identifier) @symbol.name
+ (qualified_identifier name: (identifier) @symbol.name)
+ (qualified_identifier name: (qualified_identifier name: (identifier) @symbol.name))
+ (qualified_identifier name: (operator_name) @symbol.name)
+ ]
+ parameters: (parameter_list) @symbol.params)))
+ (reference_declarator
+ (function_declarator
+ declarator: [
+ (identifier) @symbol.name
+ (field_identifier) @symbol.name
+ (qualified_identifier name: (identifier) @symbol.name)
+ (qualified_identifier name: (qualified_identifier name: (identifier) @symbol.name))
+ (qualified_identifier name: (operator_name) @symbol.name)
+ ]
+ parameters: (parameter_list) @symbol.params))
+ (function_declarator
+ declarator: (parenthesized_declarator
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))))
+ ]
+) @symbol.def
+
; Class
(class_specifier
name: (type_identifier) @symbol.name
@@ -193,6 +242,31 @@
)
) @symbol.def
+; ... returning a pointer or a reference, the same wrappers as definitions
+(declaration
+ declarator: [
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))
+ (pointer_declarator
+ declarator: (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params)))
+ (reference_declarator
+ (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))
+ (function_declarator
+ declarator: (parenthesized_declarator
+ (pointer_declarator
+ declarator: (function_declarator
+ declarator: (identifier) @symbol.name
+ parameters: (parameter_list) @symbol.params))))
+ ]
+) @symbol.def
+
; In-class member-function declaration: ``void Seek(const Slice&);``
; An abstract class has no out-of-line definition, so without these it reaches
; the method index empty. ``@symbol.def`` is the declarator, not the
diff --git a/tests/unit/ingestion/test_c_pointer_return_symbols.py b/tests/unit/ingestion/test_c_pointer_return_symbols.py
new file mode 100644
index 000000000..8415cf559
--- /dev/null
+++ b/tests/unit/ingestion/test_c_pointer_return_symbols.py
@@ -0,0 +1,83 @@
+"""A function whose declarator returns a pointer or a reference is still a symbol.
+
+``client *createClient(...)`` wraps the ``function_declarator`` in a
+``pointer_declarator``; C++ ``Foo& get()`` wraps it in a
+``reference_declarator``. Each shape must yield a function symbol, and the
+calls in its body must key to it as their caller.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from repowise.core.ingestion.parser import ASTParser
+from tests.unit.ingestion.parser._helpers import _make_file_info
+
+_PARSER = ASTParser()
+
+
+def _parse(src: str, language: str):
+ path = "a.c" if language == "c" else "a.cpp"
+ return _PARSER.parse_file(_make_file_info(path, language), src.encode())
+
+
+def _names(src: str, language: str) -> dict[str, str]:
+ return {s.name: s.kind for s in _parse(src, language).symbols}
+
+
+_SHARED_SHAPES = [
+ ("client *createClient(int fd) { return 0; }", "createClient"),
+ ("char **splitArgs(const char *s) { return 0; }", "splitArgs"),
+ ("static inline struct entry *dictFind(void *d) { return 0; }", "dictFind"),
+ ("const char * const *names(void) { return 0; }", "names"),
+ ("int (*getHandler(int k))(int) { return 0; }", "getHandler"),
+ ("client *declOnly(int fd);", "declOnly"),
+ ("char **declOnly2(void);", "declOnly2"),
+]
+
+
[email protected]("language", ["c", "cpp"])
[email protected](("src", "name"), _SHARED_SHAPES)
+def test_pointer_returning_function_is_a_symbol(language: str, src: str, name: str) -> None:
+ assert _names(src, language).get(name) == "function"
+
+
+ ("src", "name", "kind"),
+ [
+ ("struct Foo {}; Foo& get() { static Foo f; return f; }", "get", "function"),
+ ("struct Foo {}; Foo&& moved() { return Foo(); }", "moved", "function"),
+ ("struct Foo {}; Foo& declared(int);", "declared", "function"),
+ ("Foo* Bar::make(int a) { return 0; }", "make", "method"),
+ ("Foo& Bar::ref() { return *this; }", "ref", "method"),
+ ("Foo& Foo::operator=(const Foo& o) { return *this; }", "operator=", "method"),
+ ("namespace ns { Foo* A::B::make() { return 0; } }", "make", "method"),
+ ("class Bar { Foo* inlineGet() { return 0; } };", "inlineGet", "method"),
+ ],
+)
+def test_cpp_pointer_and_reference_returns(src: str, name: str, kind: str) -> None:
+ assert _names(src, "cpp").get(name) == kind
+
+
[email protected]("language", ["c", "cpp"])
+def test_function_pointer_variable_is_not_a_function(language: str) -> None:
+ src = "void (*on_close)(int);\nint *(*alloc_fn)(unsigned n);\n"
+ names = _names(src, language)
+ assert "on_close" not in names
+ assert "alloc_fn" not in names
+
+
[email protected]("language", ["c", "cpp"])
+def test_function_pointer_struct_field_is_not_a_function(language: str) -> None:
+ src = "struct ops { void *(*alloc)(unsigned n); char **(*split)(const char *s); };\n"
+ names = _names(src, language)
+ assert "alloc" not in names
+ assert "split" not in names
+
+
+def test_call_in_pointer_returning_body_keys_to_it() -> None:
+ src = "void helper(void) {}\nclient *createClient(int fd) { helper(); return 0; }\n"
+ calls = _parse(src, "c").calls
+ assert [c.caller_symbol_id for c in calls if c.target_name == "helper"] == [
+ "a.c::createClient"
+ ]