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
9pub type FnCallMap = HashMap<DefId, Vec<DefId>>; // caller_id -> Vec<(callee_id)>
10
11pub struct FnCallDisplay<'a, 'tcx> {
12    pub fn_calls: &'a FnCallMap,
13    pub tcx: TyCtxt<'tcx>,
14}
15
16impl<'a, 'tcx> fmt::Display for FnCallDisplay<'a, 'tcx> {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        writeln!(f, "CallGraph:")?;
19        for (caller, callees) in self.fn_calls {
20            let caller_name = self.tcx.def_path_str(*caller);
21            writeln!(f, "  {} calls:", caller_name)?;
22            for callee in callees {
23                let callee_name = self.tcx.def_path_str(*callee);
24                writeln!(f, "    -> {}", callee_name)?;
25            }
26        }
27        Ok(())
28    }
29}
30
31/// This trait provides features related to call graph extraction and analysis.
32pub trait CallGraphAnalysis: Analysis {
33    /// Return the call graph.
34    fn get_fn_calls(&self) -> FnCallMap;
35}