Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/trailmark/parsers/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

def node_text(node: Node) -> str:
"""Get the UTF-8 text content of a tree-sitter node."""
return node.text.decode("utf-8") if node.text else ""
return text.decode("utf-8") if (text := node.text) else ""


def make_location(node: Node, file_path: str) -> SourceLocation:
Expand Down Expand Up @@ -412,6 +412,7 @@ def collect_body_info(
*,
skip_types: frozenset[str] = frozenset(),
named_types: bool = False,
boundaries: list[Node] | None = None,
) -> None:
"""Collect branches, exceptions, and calls from a function body.

Expand All @@ -424,7 +425,12 @@ def collect_body_info(
from trailmark.parsers._walk import owned_syntax

selected = (branch_types | throw_types | {call_type}) - skip_types
if boundaries is not None:
selected |= skip_types
for node in owned_syntax(body, selected, boundaries=skip_types):
if boundaries is not None and node.type in skip_types:
boundaries.append(node)
continue
_collect_body_node(
node,
file_path,
Expand All @@ -440,6 +446,8 @@ def collect_body_info(
while stack:
node = stack.pop()
if node.type in skip_types:
if boundaries is not None:
boundaries.append(node)
continue
_collect_body_node(
node, file_path, branch_types, call_type, throw_types, branches, exception_types, calls
Expand Down
156 changes: 73 additions & 83 deletions src/trailmark/parsers/cpp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from trailmark.parsers._evidence import collect_source_evidence
from trailmark.parsers._exceptions import exception_syntax
from trailmark.parsers._nesting import bounded_nesting
from trailmark.parsers._walk import owned_syntax, walk_owned
from trailmark.parsers._walk import walk_owned
from trailmark.parsers.c._references import (
REFERENCE_CANDIDATES,
REFERENCE_SYNTAX,
Expand Down Expand Up @@ -676,33 +676,42 @@ def _extract_function(
func_id = _owned_id(container_id, func_name.replace("::", "."))
kind = NodeKind.FUNCTION

boundaries: list[Node] = []
unit, calls = _function_unit(
node, file_path, func_id=func_id, func_name=func_name, kind=kind, generic_node=generic_node
node,
file_path,
func_id=func_id,
func_name=func_name,
kind=kind,
generic_node=generic_node,
boundaries=boundaries,
)
declarator = node.child_by_field_name("declarator")
if declarator is not None and _missing_qualifier(declarator):
unit = replace(unit, attributes=(*unit.attributes, ("cpp_declarator_recovered", True)))
body = node.child_by_field_name("body")
branches, exception_types = list(unit.branches), list(unit.exception_types)
func_id = add_callable(graph, unit)
add_contains_edge(graph, container_id, func_id)

_extract_deferred_callables(
body,
file_path,
module_id,
func_id,
graph,
branches=branches,
exception_types=exception_types,
calls=calls,
)
graph.nodes[func_id] = replace(
graph.nodes[func_id],
branches=tuple(branches),
exception_types=tuple(exception_types),
cyclomatic_complexity=compute_complexity(branches),
)
if body is not None and boundaries:
branches, exception_types = list(unit.branches), list(unit.exception_types)
_extract_deferred_callables(
body,
boundaries,
file_path,
module_id,
func_id,
graph,
branches=branches,
exception_types=exception_types,
calls=calls,
)
graph.nodes[func_id] = replace(
graph.nodes[func_id],
branches=tuple(branches),
exception_types=tuple(exception_types),
cyclomatic_complexity=compute_complexity(branches),
)
_add_call_edges(calls, func_id, module_id, file_path, graph)


Expand All @@ -715,45 +724,28 @@ def _add_call_edges(
) -> None:
"""Create CALLS edges from collected call expressions."""
for call_name, call_node in calls:
target_id = _resolve_call_target(
call_name,
module_id,
)
confidence = _call_confidence(call_name)
qualified = "." in call_name or "->" in call_name or "::" in call_name
target_id = call_name if qualified else f"{module_id}:{call_name}"
confidence = EdgeConfidence.INFERRED if qualified else EdgeConfidence.CERTAIN
edge = guard_call(
CodeEdge(
source_id=func_id,
target_id=target_id,
kind=EdgeKind.CALLS,
confidence=confidence,
location=make_location(call_node, file_path),
attributes=(("bare_name", call_name),)
if re.fullmatch(r"[A-Za-z_]\w*", call_name)
else (),
attributes=(("bare_name", call_name),) if _BARE_NAME.fullmatch(call_name) else (),
),
call_node,
graph=graph,
call_name=call_name,
)
graph.edges.append(
replace(edge, attributes=(*edge.attributes, *call_attributes(edge, call_node, graph)))
)

extra = call_attributes(edge, call_node, graph)
# Appending no attributes would copy an equal edge.
graph.edges.append(replace(edge, attributes=(*edge.attributes, *extra)) if extra else edge)

def _call_confidence(call_name: str) -> EdgeConfidence:
"""Determine confidence for a C++ call expression."""
if "." in call_name or "->" in call_name or "::" in call_name:
return EdgeConfidence.INFERRED
return EdgeConfidence.CERTAIN


def _resolve_call_target(
call_name: str,
module_id: str,
) -> str:
"""Construct an initial target; container binding follows the complete file walk."""
if "::" in call_name or "." in call_name or "->" in call_name:
return call_name
return f"{module_id}:{call_name}"
_BARE_NAME = re.compile(r"[A-Za-z_]\w*")


def _get_function_name(node: Node) -> str:
Expand Down Expand Up @@ -901,7 +893,8 @@ def _extract_include(node: Node, graph: CodeGraph) -> None:


def _extract_deferred_callables(
body: Node | None,
body: Node,
boundaries: list[Node],
file_path: str,
module_id: str,
func_id: str,
Expand All @@ -911,41 +904,38 @@ def _extract_deferred_callables(
exception_types: list[TypeRef],
calls: list[tuple[str, Node]],
) -> None:
if body is not None:
# owned_syntax finds the same boundaries in the same pre-order without
# visiting every node; walk_owned still enters a root that is a boundary.
deferred_nodes = (
walk_owned(body, boundaries=_DEFERRED_BOUNDARIES)
if body.type in _DEFERRED_BOUNDARIES
else owned_syntax(body, _DEFERRED_TYPES, boundaries=_DEFERRED_BOUNDARIES)
)
for deferred in deferred_nodes:
if deferred.type == "lambda_expression":
_extract_function(
deferred,
file_path,
module_id,
func_id,
graph,
name_override=(
f"$lambda@{deferred.start_point.row + 1}_{deferred.start_point.column}"
),
)
_collect_captures(deferred, file_path, branches, exception_types, calls)
if (
deferred.parent is not None
and deferred.parent.type == "call_expression"
and deferred.parent.child_by_field_name("function") == deferred
):
calls.append(
(
f"{func_id}.$lambda@{deferred.start_point.row + 1}_"
f"{deferred.start_point.column}",
deferred.parent,
)
deferred_nodes = (
walk_owned(body, boundaries=_DEFERRED_BOUNDARIES)
if body.type in _DEFERRED_BOUNDARIES
else boundaries
)
for deferred in deferred_nodes:
if deferred.type == "lambda_expression":
_extract_function(
deferred,
file_path,
module_id,
func_id,
graph,
name_override=(
f"$lambda@{deferred.start_point.row + 1}_{deferred.start_point.column}"
),
)
_collect_captures(deferred, file_path, branches, exception_types, calls)
if (
deferred.parent is not None
and deferred.parent.type == "call_expression"
and deferred.parent.child_by_field_name("function") == deferred
):
calls.append(
(
f"{func_id}.$lambda@{deferred.start_point.row + 1}_"
f"{deferred.start_point.column}",
deferred.parent,
)
elif deferred.type in {"class_specifier", "struct_specifier"}:
_visit_top_level_node(deferred, file_path, module_id, None, graph)
)
elif deferred.type in {"class_specifier", "struct_specifier"}:
_visit_top_level_node(deferred, file_path, module_id, None, graph)


def _function_unit(
Expand All @@ -956,6 +946,7 @@ def _function_unit(
func_name: str,
kind: NodeKind,
generic_node: Node | None,
boundaries: list[Node],
) -> tuple[CodeUnit, list[tuple[str, Node]]]:
body = node.child_by_field_name("body")

Expand All @@ -973,10 +964,9 @@ def _function_unit(
branches,
exception_types,
calls,
skip_types=frozenset(
{"lambda_expression", "class_specifier", "struct_specifier", "function_definition"}
),
skip_types=_DEFERRED_BOUNDARIES,
named_types=True,
boundaries=boundaries,
)

complexity = compute_complexity(branches)
Expand Down
Loading