Skip to main content

callers_of

Function callers_of 

Source
pub fn callers_of<'a>(
    cpg: &'a CodePropertyGraph,
    func_name: &str,
) -> Vec<(&'a Node, &'a Node)>
Expand description

For a given function name, returns all functions that directly call it.

Each returned tuple contains the caller function node and the callee definition node. The enclosing function is found by walking up Edge::AstChild edges from the call site.

ยงExamples

use icb_graph::graph::{CodePropertyGraph, Node, Edge};
use icb_graph::query::callers_of;
use icb_common::NodeKind;

let mut cpg = CodePropertyGraph::new();
let foo = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("foo".into()),
    usr: Some("foo".into()),
    start_line: 1,
    end_line: 2,
});
let bar = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("bar".into()),
    usr: Some("bar".into()),
    start_line: 5,
    end_line: 6,
});
let call = cpg.graph.add_node(Node {
    kind: NodeKind::CallSite,
    name: Some("bar".into()),
    usr: None,
    start_line: 2,
    end_line: 2,
});
cpg.graph.add_edge(foo, call, Edge::AstChild);
cpg.graph.add_edge(call, bar, Edge::Call);

let callers = callers_of(&cpg, "bar");
assert_eq!(callers.len(), 1);
assert_eq!(callers[0].0.name.as_deref(), Some("foo"));