Function#

class scubatrace.Function(node: Node, file: File | BlockStatement, joern_id: str | None = None)#

Bases: BlockStatement

A function in the source code.

static create(node: Node, parent: File | BlockStatement)#

Factory function to create a Function instance based on the language of the file.

Parameters:
  • node (Node) – The tree-sitter node representing the function.

  • file (File) – The file containing the function.

Returns:

An instance of a language-specific Function subclass corresponding to the file’s language.

Return type:

Function

export_callgraph(path: str, depth: int = -1) MultiDiGraph#

Exports the call graph of the function to a DOT file.

Parameters:
  • path (str) – The path to save the DOT file.

  • depth (int) – The depth of the call graph to export. -1 means no limit.

Returns:

The call graph of the function.

Return type:

nx.MultiDiGraph

export_cfg_dot(path: str, with_cdg: bool = False, with_ddg: bool = False) DiGraph#

Exports the CFG of the function to a DOT file.

Parameters:
  • path (str) – The path to save the DOT file.

  • with_cdg (bool) – Whether to include the Control Dependence Graph (CDG).

  • with_ddg (bool) – Whether to include the Data Dependence Graph (DDG).

slice_by_lines(lines: list[int], *, control_depth: int = 1, data_dependent_depth: int = 1, control_dependent_depth: int = 1) list[Statement]#

Slices the function to retrieve relevant statements based on the specified lines.

Parameters:
  • lines (list[int]) – Slice criteria lines. Note that only support the lines in the function body currently.

  • control_depth (int) – Slice depth for control flow dependencies.

  • data_dependent_depth (int) – Slice depth for data dependencies.

  • control_dependent_depth (int) – Slice depth for control-dependent statements.

Returns:

A list of statements that are sliced based on the specified lines.

Return type:

list[Statement]

slice_by_statements(statements: list[Statement], *, control_depth: int = 1, data_dependent_depth: int = 1, control_dependent_depth: int = 1) list[Statement]#

Slices the function to retrieve relevant statements based on the provided statements.

Parameters:
  • statements (list[Statement]) – Slice criteria statements.

  • control_depth (int) – Slice depth for control flow dependencies.

  • data_dependent_depth (int) – Slice depth for data dependencies.

  • control_dependent_depth (int) – Slice depth for control-dependent statements.

Returns:

A list of statements that are sliced based on the provided statements.

Return type:

list[Statement]

walk_backward(filter: Callable[[Statement], bool] | None = None, stop_by: Callable[[Statement], bool] | None = None, depth: int = -1, base: str = 'call') Generator[Function, None, None]#

Walks backward through the control flow graph of the statement.

Parameters:
  • filter (Callable[[Statement], bool] | None) – A filter function to apply to each statement. If the filter returns True, the statement is yielded.

  • stop_by (Callable[[Statement], bool] | None) – A function to stop the walking when it returns True.

  • depth (int) – The maximum depth to walk backward. Default is -1, which means no limit.

  • base (str) – The base type of the walk. Can be “control”, “data_dependent”, or “control_dependent”.

Yields:

Statement – The statements that match the filter or all statements if no filter is provided.

walk_forward(filter: Callable[[Statement], bool] | None = None, stop_by: Callable[[Statement], bool] | None = None, depth: int = -1, base: str = 'call') Generator[Function, None, None]#

Walks forward through the control flow graph of the statement.

Parameters:
  • filter (Callable[[Statement], bool] | None) – A filter function to apply to each statement. If the filter returns True, the statement is yielded.

  • stop_by (Callable[[Statement], bool] | None) – A function to stop the walking when it returns True.

  • depth (int) – The maximum depth to walk forward. Default is -1, which means no limit.

  • base (str) – The base type of the walk. Can be “control”, “data_dependent”, “control_dependent”, “call”.

Yields:

Statement – The statements that match the filter or all statements if no filter is provided.

property body_end_line: int#

The ending line number of the body of the function.

property body_node: Node | None#

The tree-sitter body node of the function.

property body_start_line: int#

The starting line number of the body of the function.

property callees: dict[Function | FunctionDeclaration, list[Statement]]#

The functions or function declarations that are called by this function and their corresponding call sites.

property callers: dict[Function, list[Statement]]#

The functions that call this function and their corresponding call sites.

property calls: list[Statement]#

Call statements within the function.

property exits: list[Statement]#

The exit statements of the function, such as return statements.

property is_external: bool#

Checks if the function is external (not part of the project).

property lines: dict[int, str]#

A dictionary mapping line numbers to their corresponding lines of text.

property name: str#

The name of the function.

property name_node: Node#

The tree-sitter node representing the name of the function.

property parameter_lines: list[int]#

The lines where the parameters of the function are defined.

property parameters: list[Identifier]#

The parameter statements of the function.

property signature: str#

A unique signature for the function.

property statements: list[Statement]#

Statements in the function.

class scubatrace.FunctionDeclaration(name: str, text: str, file: File)#

Bases: object

Represents a function declaration in the code.

For example, in C/C++, this would be a function prototype without the body.

file: File#

The file where the function declaration is located.

name: str#

The name of the function declaration.

property signature: str#

The unique signature of the function declaration.

text: str#

The text of the function declaration.