Search documentation of Metastatic

# Metastatic.Analysis.ControlFlow (Metastatic v0.13.2)

View Source

Control flow graph construction at the MetaAST level.

Builds a control flow graph (CFG) from MetaAST, identifying control flow paths, entry/exit points, and providing graph analysis capabilities. Works across all supported languages.

## CFG Components

- Nodes - Entry, exit, statements, conditionals, loops
- Edges - Control flow connections (conditional/unconditional)
- Entry point - Where execution begins
- Exit points - Where execution ends

## Usage

    alias Metastatic.{Document, Analysis.ControlFlow}

    # Build CFG
    ast = {:conditional, [], [{:variable, [], "x"},
      {:literal, [subtype: :integer], 1},
      {:literal, [subtype: :integer], 2}]}
    doc = Document.new(ast, :python)
    {:ok, result} = ControlFlow.analyze(doc)

    result.node_count    # Number of nodes
    result.edge_count    # Number of edges
    result.has_cycles?   # Contains cycles?

## Examples

    # Simple literal creates minimal CFG
    iex> ast = {:literal, [subtype: :integer], 42}
    iex> doc = Metastatic.Document.new(ast, :python)
    iex> {:ok, result} = Metastatic.Analysis.ControlFlow.analyze(doc)
    iex> result.node_count >= 1
    true

## Summary

### Functions

analyze(doc)

Analyzes a document to build its control flow graph.

analyze!(doc)

Analyzes a document to build its control flow graph.

## Functions

# analyze(doc)

    @spec analyze(Metastatic.Document.t()) :: {:ok, term()} | {:error, term()}

Analyzes a document to build its control flow graph.

Accepts either a `Metastatic.Document` struct or a `{language, native_ast}` tuple.

Returns `{:ok, result}` where result contains the CFG and analysis.

## Examples

    iex> ast = {:literal, [subtype: :integer], 42}
    iex> doc = Metastatic.Document.new(ast, :elixir)
    iex> {:ok, result} = Metastatic.Analysis.ControlFlow.analyze(doc)
    iex> is_integer(result.node_count)
    true

# analyze!(doc)

    @spec analyze!(Metastatic.Document.t()) :: term()

Analyzes a document to build its control flow graph.

Accepts either a `Metastatic.Document` struct or a `{language, native_ast}` tuple.

Returns `{:ok, result}` where result contains the CFG and analysis.

## Examples

    iex> ast = {:literal, [subtype: :integer], 42}
    iex> doc = Metastatic.Document.new(ast, :elixir)
    iex> {:ok, result} = Metastatic.Analysis.ControlFlow.analyze(doc)
    iex> is_integer(result.node_count)
    true

Unlike not-banged version, this one either returns a result or raises