rapx/analysis/core/callgraph/
mod.rs

1pub mod default;
2pub mod visitor;
3
4use crate::Analysis;
5use rustc_hir::def_id::DefId;
6use rustc_middle::ty::TyCtxt;
7use std::{collections::HashMap, fmt};
8
9/// This is the data structure used to store function calls.
10/// It contains a HashMap that records the callees of all functions.
11pub struct CallGraph {
12    pub fn_calls: HashMap<DefId, Vec<DefId>>, // caller_id -> Vec<(callee_id)>
13}
14
15pub struct CallGraphDisplay<'a, 'tcx> {
16    pub graph: &'a CallGraph,
17    pub tcx: TyCtxt<'tcx>,
18}
19
20impl<'a, 'tcx> fmt::Display for CallGraphDisplay<'a, 'tcx> {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        writeln!(f, "CallGraph:")?;
23        for (caller, callees) in &self.graph.fn_calls {
24            let caller_name = self.tcx.def_path_str(*caller);
25            writeln!(f, "  {} calls:", caller_name)?;
26            for callee in callees {
27                let callee_name = self.tcx.def_path_str(*callee);
28                writeln!(f, "    -> {}", callee_name)?;
29            }
30        }
31        Ok(())
32    }
33}
34
35/// This trait provides features related to call graph extraction and analysis.
36pub trait CallGraphAnalysis: Analysis {
37    /// Return the call graph.
38    fn get_callgraph(&mut self) -> CallGraph;
39}