Skip to main content

detect_call_cycles

Function detect_call_cycles 

Source
pub fn detect_call_cycles(cpg: &CodePropertyGraph) -> Vec<CallCycle>
Expand description

Detects all call cycles involving one or more functions.

Builds a directed graph of function nodes connected by Edge::Call edges and runs Kosaraju’s algorithm for strongly connected components (SCC). Every SCC with more than one node is reported as a cycle. Self‑loops (single‑node SCCs with a call to themselves) are also reported.

§Examples

use icb_graph::analysis::detect_call_cycles;
use icb_graph::graph::{CodePropertyGraph, Node, Edge};
use icb_common::NodeKind;

let mut cpg = CodePropertyGraph::new();
let a = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("a".into()),
    usr: Some("a".into()),
    start_line: 1, end_line: 1,
});
let b = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("b".into()),
    usr: Some("b".into()),
    start_line: 2, end_line: 2,
});
cpg.graph.add_edge(a, b, Edge::Call);
cpg.graph.add_edge(b, a, Edge::Call);

let cycles = detect_call_cycles(&cpg);
assert_eq!(cycles.len(), 1);
assert!(cycles[0].functions.contains(&"a".into()));