Skip to main content

rapx/analysis/dataflow/
mod.rs

1pub mod debug;
2pub mod default;
3pub mod graph;
4
5use std::{
6    collections::{HashMap, HashSet},
7    fmt::{self},
8};
9
10pub mod types;
11use crate::{analysis::Analysis, utils::source::get_fn_name_byid};
12pub use types::*;
13
14use rustc_hir::def_id::DefId;
15use rustc_index::IndexVec;
16use rustc_middle::mir::Local;
17
18pub type Graph = DataflowGraph;
19pub type GraphNode = DataflowNode;
20pub type GraphEdge = DataflowEdge;
21
22pub type Arg2Ret = IndexVec<Local, bool>;
23pub type Arg2RetMap = HashMap<DefId, IndexVec<Local, bool>>;
24pub type DataflowGraphMap = HashMap<DefId, DataflowGraph>;
25
26pub struct Arg2RetMapWrapper(pub Arg2RetMap);
27
28/// This trait provides features related to dataflow analysis.
29pub trait DataflowAnalysis: Analysis {
30    fn get_fn_dataflow(&self, def_id: DefId) -> Option<DataflowGraph>;
31    fn get_all_dataflow(&self) -> DataflowGraphMap;
32    fn has_flow_between(&self, def_id: DefId, local1: Local, local2: Local) -> bool;
33    fn collect_equivalent_locals(&self, def_id: DefId, local: Local) -> HashSet<Local>;
34    fn get_fn_arg2ret(&self, def_id: DefId) -> Arg2Ret;
35    fn get_all_arg2ret(&self) -> Arg2RetMap;
36}
37
38impl fmt::Display for Arg2RetMapWrapper {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        writeln!(f, "=== Print dataflow analysis results ===")?;
41        for (def_id, arg2ret) in &self.0 {
42            let fn_name = get_fn_name_byid(def_id);
43            writeln!(f, "Function: {:?}", fn_name)?;
44            for (local, depends) in arg2ret.iter_enumerated() {
45                if local.as_u32() > 0 && *depends {
46                    writeln!(f, "  Argument {:?} ---> Return value _0", local)?;
47                }
48            }
49        }
50        Ok(())
51    }
52}