Metastatic.Analysis.ControlFlow (Metastatic v0.10.4)

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

Analyzes a document to build its control flow graph.

Analyzes a document to build its control flow graph.

Functions

analyze(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze(Metastatic.language(), term(), keyword()) ::
  {:ok, map()} | {: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!(language_or_doc, source_or_ast_or_opts \\ [], opts \\ [])

@spec analyze!(Metastatic.language(), term(), keyword()) :: map()

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