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.

refactor(dataflow): split the C# and Go dialect hooks this week made too complex (#3547)

Raghav Chamadiya9b43f3018d33
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
index e5faafdcb..e1bd9ce1e 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
@@ -70,6 +70,23 @@ _SCOPE_BOUNDARIES = frozenset(
_CALLEE_NAME_KINDS = frozenset({"generic_name", "qualified_name"})
+def _callee(invocation: Node) -> Node | None:
+ return invocation.child_by_field_name("function") or (
+ invocation.named_children[0] if invocation.named_children else None
+ )
+
+
+def _call_arguments(invocation: Node) -> list[Node]:
+ args = invocation.child_by_field_name("arguments") or (
+ invocation.named_children[1] if len(invocation.named_children) > 1 else None
+ )
+ return args.named_children if args is not None else []
+
+
+def _has_token(arg: Node, token: str) -> bool:
+ return any(c.type == token for c in arg.children)
+
+
class CSharpDefUseDialect(BaseDefUseDialect):
language = "csharp"
member_access_kinds = frozenset({_MEMBER_ACCESS, _CONDITIONAL_ACCESS})
@@ -101,26 +118,7 @@ class CSharpDefUseDialect(BaseDefUseDialect):
return
t = node.type
if t == _INVOCATION:
- fn = node.child_by_field_name("function") or (
- node.named_children[0] if node.named_children else None
- )
- if fn is not None and (
- fn.type in self.member_access_kinds or fn.type not in _CALLEE_NAME_KINDS
- ):
- self.collect_reads(fn, out)
- args = node.child_by_field_name("arguments") or (
- node.named_children[1] if len(node.named_children) > 1 else None
- )
- if args is not None:
- for arg in args.named_children:
- if arg.type == _ARGUMENT:
- # 'out' argument writes the target rather than reading it.
- has_out = any(c.type == "out" for c in arg.children)
- if not has_out:
- for c in arg.named_children:
- self.collect_reads(c, out)
- else:
- self.collect_reads(arg, out)
+ self._invocation_reads(node, out)
return
if t in self.member_access_kinds:
receiver = node.child_by_field_name("expression") or (
@@ -145,6 +143,19 @@ class CSharpDefUseDialect(BaseDefUseDialect):
for child in node.named_children:
self.collect_reads(child, out)
+ def _invocation_reads(self, node: Node, out: list[Occurrence]) -> None:
+ fn = _callee(node)
+ if fn is not None and (
+ fn.type in self.member_access_kinds or fn.type not in _CALLEE_NAME_KINDS
+ ):
+ self.collect_reads(fn, out)
+ for arg in _call_arguments(node):
+ if arg.type != _ARGUMENT:
+ self.collect_reads(arg, out)
+ elif not _has_token(arg, "out"): # 'out' writes the target rather than reading it
+ for c in arg.named_children:
+ self.collect_reads(c, out)
+
# -- public contract ------------------------------------------------------
def statement_def_use(
@@ -202,79 +213,19 @@ class CSharpDefUseDialect(BaseDefUseDialect):
return
t = node.type
if t in _ASSIGN_KINDS:
- left = node.child_by_field_name("left")
- self._targets(left, defs, uses)
- op = node.child_by_field_name("operator")
- if op is not None and op.text not in (b"=", None): # compound: read too
- self.collect_reads(left, uses)
- self._process(node.child_by_field_name("right"), defs, uses)
+ self._assignment(node, defs, uses)
return
if t in _UPDATE_KINDS: # ``x++`` / ``--x`` -- read-modify-write
- has_update = any(
- c.type in ("++", "--") or c.text in (b"++", b"--") for c in node.children
- )
- if has_update:
- arg = node.child_by_field_name("argument") or (
- node.named_children[0] if node.named_children else None
- )
- self._targets(arg, defs, uses)
- self.collect_reads(arg, uses)
- return
- for child in node.named_children:
- self._process(child, defs, uses)
+ self._update(node, defs, uses)
return
if t in _DECL_KINDS:
- for child in node.named_children:
- if child.type == _DECLARATOR:
- start = len(defs)
- name_node = child.child_by_field_name("name")
- self._targets(name_node, defs, uses)
- self._declare(defs, start, child)
- for sub in child.named_children:
- if sub != name_node:
- self._process(sub, defs, uses)
- else:
- self._process(child, defs, uses)
+ self._declaration(node, defs, uses)
return
if t == _INVOCATION:
- fn = node.child_by_field_name("function") or (
- node.named_children[0] if node.named_children else None
- )
- if fn is not None:
- if fn.type in self.member_access_kinds:
- self.collect_reads(fn, uses)
- elif fn.type not in _CALLEE_NAME_KINDS:
- self._process(fn, defs, uses)
- args = node.child_by_field_name("arguments") or (
- node.named_children[1] if len(node.named_children) > 1 else None
- )
- if args is not None:
- for arg in args.named_children:
- if arg.type == _ARGUMENT:
- has_out = any(c.type == "out" for c in arg.children)
- has_ref = any(c.type == "ref" for c in arg.children)
- if has_out:
- # 'out' argument is a write (def)
- for c in arg.named_children:
- self._targets(c, defs, uses)
- elif has_ref:
- # 'ref' argument is both a write (def) and a read (use)
- for c in arg.named_children:
- self._targets(c, defs, uses)
- self.collect_reads(c, uses)
- else:
- for c in arg.named_children:
- self._process(c, defs, uses)
- else:
- self._process(arg, defs, uses)
+ self._invocation(node, defs, uses)
return
if t == _IS_PATTERN:
- self._process(
- node.child_by_field_name("expression") or node.child_by_field_name("left"),
- defs,
- uses,
- )
- self._targets(node.child_by_field_name("pattern"), defs, uses)
+ self._is_pattern(node, defs, uses)
return
if t in (_DECLARATION_PATTERN, "var_pattern", "recursive_pattern", _DECLARATION_EXPR):
self._targets(node, defs, uses)
@@ -294,6 +245,74 @@ class CSharpDefUseDialect(BaseDefUseDialect):
for child in node.named_children:
self._process(child, defs, uses)
+ def _assignment(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ left = node.child_by_field_name("left")
+ self._targets(left, defs, uses)
+ op = node.child_by_field_name("operator")
+ if op is not None and op.text not in (b"=", None): # compound: read too
+ self.collect_reads(left, uses)
+ self._process(node.child_by_field_name("right"), defs, uses)
+
+ def _update(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ has_update = any(c.type in ("++", "--") or c.text in (b"++", b"--") for c in node.children)
+ if has_update:
+ arg = node.child_by_field_name("argument") or (
+ node.named_children[0] if node.named_children else None
+ )
+ self._targets(arg, defs, uses)
+ self.collect_reads(arg, uses)
+ return
+ for child in node.named_children:
+ self._process(child, defs, uses)
+
+ def _declaration(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ for child in node.named_children:
+ if child.type != _DECLARATOR:
+ self._process(child, defs, uses)
+ continue
+ start = len(defs)
+ name_node = child.child_by_field_name("name")
+ self._targets(name_node, defs, uses)
+ self._declare(defs, start, child)
+ for sub in child.named_children:
+ if sub != name_node:
+ self._process(sub, defs, uses)
+
+ def _invocation(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ fn = _callee(node)
+ if fn is not None:
+ if fn.type in self.member_access_kinds:
+ self.collect_reads(fn, uses)
+ elif fn.type not in _CALLEE_NAME_KINDS:
+ self._process(fn, defs, uses)
+ for arg in _call_arguments(node):
+ if arg.type == _ARGUMENT:
+ self._argument(arg, defs, uses)
+ else:
+ self._process(arg, defs, uses)
+
+ def _argument(self, arg: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ """An ``out`` argument is a write (def); a ``ref`` one is both a write
+ and a read (use)."""
+ if _has_token(arg, "out"):
+ for c in arg.named_children:
+ self._targets(c, defs, uses)
+ elif _has_token(arg, "ref"):
+ for c in arg.named_children:
+ self._targets(c, defs, uses)
+ self.collect_reads(c, uses)
+ else:
+ for c in arg.named_children:
+ self._process(c, defs, uses)
+
+ def _is_pattern(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
+ self._process(
+ node.child_by_field_name("expression") or node.child_by_field_name("left"),
+ defs,
+ uses,
+ )
+ self._targets(node.child_by_field_name("pattern"), defs, uses)
+
# -- write-target extraction ----------------------------------------------
def _targets(self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]) -> None:
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go_values.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go_values.py
index c932cf667..a34f5493a 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go_values.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/go_values.py
@@ -158,20 +158,8 @@ def _declared_type(ident: Node, depth: int) -> Node | str | None:
stack = [fn]
while stack:
node = stack.pop()
- t = node.type
- if t in ("parameter_declaration", "var_spec"):
- if any(c.type == "identifier" and c.text == name for c in node.named_children):
- typ = node.child_by_field_name("type")
- found.append(typ if typ is not None else _bound_type(node, name, depth))
- elif t == "variadic_parameter_declaration":
- # ``p ...T`` is a ``[]T`` inside the function; its ``type`` is ``T``.
- if any(c.type == "identifier" and c.text == name for c in node.named_children):
- found.append(_REF)
- elif t == _SHORT_VAR and _binds(node, name):
- found.append(_bound_type(node, name, depth))
- elif t == _RANGE_CLAUSE and _binds(node, name):
- found.append(None)
- if node is fn or t not in _FUNCTION_KINDS:
+ found.extend(_declaration_type(node, name, depth))
+ if node is fn or node.type not in _FUNCTION_KINDS:
stack.extend(node.named_children)
if not found:
return _NOT_LOCAL
@@ -181,6 +169,29 @@ def _declared_type(ident: Node, depth: int) -> Node | str | None:
return found[0] if len(kinds) == 1 else None
+def _declaration_type(node: Node, name: bytes | None, depth: int) -> list[Node | str | None]:
+ """The type *node* declares *name* with, as a one-item list; empty when
+ *node* does not declare *name*."""
+ t = node.type
+ if t in ("parameter_declaration", "var_spec"):
+ if _has_name(node, name):
+ typ = node.child_by_field_name("type")
+ return [typ if typ is not None else _bound_type(node, name, depth)]
+ elif t == "variadic_parameter_declaration":
+ # ``p ...T`` is a ``[]T`` inside the function; its ``type`` is ``T``.
+ if _has_name(node, name):
+ return [_REF]
+ elif t == _SHORT_VAR and _binds(node, name):
+ return [_bound_type(node, name, depth)]
+ elif t == _RANGE_CLAUSE and _binds(node, name):
+ return [None]
+ return []
+
+
+def _has_name(decl: Node, name: bytes | None) -> bool:
+ return any(c.type == "identifier" and c.text == name for c in decl.named_children)
+
+
def _binds(decl: Node, name: bytes | None) -> bool:
left = decl.child_by_field_name("left")
return left is not None and any(
@@ -220,51 +231,62 @@ def _expr_type(expr: Node, depth: int) -> Node | str | None:
if t == "composite_literal":
return expr.child_by_field_name("type")
if t == "unary_expression":
- op = expr.child_by_field_name("operator")
- if op is not None and op.text == b"&":
- return _REF
- # ``p := *q`` copies what ``q`` points at. ``*`` is the only other
- # operator a pointer operand takes, so the operator needs no test.
- operand = expr.child_by_field_name("operand")
- pointer = _resolve(_expr_type(operand, depth + 1), expr) if operand is not None else None
- if pointer is not None and pointer != _REF and pointer.type == "pointer_type":
- return pointer.named_children[0] if pointer.named_children else None
- return None
+ return _unary_type(expr, depth)
if t == "type_assertion_expression":
return expr.child_by_field_name("type")
if t == "identifier":
typ = _declared_type(expr, depth + 1)
return None if typ is _NOT_LOCAL else typ
if t in (_SELECTOR, _INDEX):
- operand = expr.child_by_field_name("operand")
- inner = _expr_type(operand, depth + 1) if operand is not None else None
- if inner is None:
- return None
- if inner == _REF:
- return None # through a pointer: the pointee's type is not at hand
- resolved = _resolve(inner, expr)
- if resolved is None or resolved == _REF:
- return None
- if t == _INDEX:
- return resolved.child_by_field_name("element")
- if resolved.type == "pointer_type": # ``p.f`` reads through ``*T`` too
- resolved = _resolve(
- resolved.named_children[0] if resolved.named_children else None, expr
- )
- if resolved is None or resolved == _REF or resolved.type != "struct_type":
- return None
- field = expr.child_by_field_name("field")
- return _field_type(resolved, field.text if field is not None else None)
+ return _member_type(expr, depth)
if t == "call_expression":
- fn = expr.child_by_field_name("function")
- if fn is None or fn.type != "identifier":
- return None
- if fn.text in _ALLOCATORS:
- return _REF
- return _result_type(expr, fn.text)
+ return _call_type(expr)
+ return None
+
+
+def _unary_type(expr: Node, depth: int) -> Node | str | None:
+ op = expr.child_by_field_name("operator")
+ if op is not None and op.text == b"&":
+ return _REF
+ # ``p := *q`` copies what ``q`` points at. ``*`` is the only other
+ # operator a pointer operand takes, so the operator needs no test.
+ operand = expr.child_by_field_name("operand")
+ pointer = _resolve(_expr_type(operand, depth + 1), expr) if operand is not None else None
+ if pointer is not None and pointer != _REF and pointer.type == "pointer_type":
+ return pointer.named_children[0] if pointer.named_children else None
return None
+def _member_type(expr: Node, depth: int) -> Node | str | None:
+ """The type of a field (``x.f``) or an element (``x[i]``) of a local."""
+ operand = expr.child_by_field_name("operand")
+ inner = _expr_type(operand, depth + 1) if operand is not None else None
+ if inner is None:
+ return None
+ if inner == _REF:
+ return None # through a pointer: the pointee's type is not at hand
+ resolved = _resolve(inner, expr)
+ if resolved is None or resolved == _REF:
+ return None
+ if expr.type == _INDEX:
+ return resolved.child_by_field_name("element")
+ if resolved.type == "pointer_type": # ``p.f`` reads through ``*T`` too
+ resolved = _resolve(resolved.named_children[0] if resolved.named_children else None, expr)
+ if resolved is None or resolved == _REF or resolved.type != "struct_type":
+ return None
+ field = expr.child_by_field_name("field")
+ return _field_type(resolved, field.text if field is not None else None)
+
+
+def _call_type(expr: Node) -> Node | str | None:
+ fn = expr.child_by_field_name("function")
+ if fn is None or fn.type != "identifier":
+ return None
+ if fn.text in _ALLOCATORS:
+ return _REF
+ return _result_type(expr, fn.text)
+
+
def _result_type(at: Node, name: bytes | None) -> Node | None:
"""The single result type of the top-level function *name* in this file."""
for decl in _root(at).named_children: