rapx/analysis/core/dataflow/
default.rs1use super::graph::*;
2use crate::analysis::core::dataflow::*;
3
4pub struct DataFlowAnalyzer<'tcx> {
5 pub tcx: TyCtxt<'tcx>,
6 pub graphs: HashMap<DefId, Graph>,
7 pub debug: bool,
8}
9
10impl<'tcx> DataFlowAnalysis for DataFlowAnalyzer<'tcx> {
11 fn get_fn_dataflow(&self, def_id: DefId) -> Option<DataFlowGraph> {
12 self.graphs.get(&def_id).cloned().map(Into::into)
13 }
14
15 fn get_all_dataflow(&self) -> DataFlowGraphMap {
16 self.graphs
17 .iter()
18 .map(|(&def_id, graph)| (def_id, graph.clone().into()))
19 .collect()
20 }
21
22 fn has_flow_between(&self, def_id: DefId, local1: Local, local2: Local) -> bool {
23 let graph = self.graphs.get(&def_id).unwrap();
24 graph.is_connected(local1, local2)
25 }
26
27 fn collect_equivalent_locals(&self, def_id: DefId, local: Local) -> HashSet<Local> {
28 let graph = self.graphs.get(&def_id).unwrap();
29 graph.collect_equivalent_locals(local, true)
30 }
31
32 fn get_fn_arg2ret(&self, def_id: DefId) -> Arg2Ret {
33 let graph = self.graphs.get(&def_id).unwrap();
34 graph.param_return_deps()
35 }
36
37 fn get_all_arg2ret(&self) -> Arg2RetMap {
38 let mut result = HashMap::new();
39 for (def_id, graph) in &self.graphs {
40 let deps = graph.param_return_deps();
41 result.insert(*def_id, deps);
42 }
43 result
44 }
45}
46
47impl<'tcx> Analysis for DataFlowAnalyzer<'tcx> {
48 fn name(&self) -> &'static str {
49 "DataFlow Analysis"
50 }
51
52 fn run(&mut self) {
53 self.build_graphs();
54 if self.debug {
55 self.draw_graphs();
56 }
57 }
58
59 fn reset(&mut self) {
60 self.graphs.clear();
61 }
62}
63
64impl<'tcx> DataFlowAnalyzer<'tcx> {
65 pub fn new(tcx: TyCtxt<'tcx>, debug: bool) -> Self {
66 Self {
67 tcx: tcx,
68 graphs: HashMap::new(),
69 debug,
70 }
71 }
72
73 pub fn start(&mut self) {
74 self.build_graphs();
75 if self.debug {
76 self.draw_graphs();
77 }
78 }
79
80 pub fn build_graphs(&mut self) {
81 for local_def_id in self.tcx.iter_local_def_id() {
82 let def_kind = self.tcx.def_kind(local_def_id);
83 if matches!(def_kind, DefKind::Fn) || matches!(def_kind, DefKind::AssocFn) {
84 if self.tcx.hir_maybe_body_owned_by(local_def_id).is_some() {
85 let def_id = local_def_id.to_def_id();
86 self.build_graph(def_id);
87 }
88 }
89 }
90 }
91
92 pub fn build_graph(&mut self, def_id: DefId) {
93 if self.graphs.contains_key(&def_id) {
94 return;
95 }
96 let body: &Body = self.tcx.optimized_mir(def_id);
97 let mut graph = Graph::new(def_id, body.span, body.arg_count, body.local_decls.len());
98 let basic_blocks = &body.basic_blocks;
99 for basic_block_data in basic_blocks.iter() {
100 for statement in basic_block_data.statements.iter() {
101 graph.add_statm_to_graph(&statement);
102 }
103 if let Some(terminator) = &basic_block_data.terminator {
104 graph.add_terminator_to_graph(&terminator);
105 }
106 }
107 for closure_id in graph.closures.iter() {
108 self.build_graph(*closure_id);
109 }
110 self.graphs.insert(def_id, graph);
111 }
112
113 pub fn draw_graphs(&self) {
114 let dir_name = "DataflowGraph";
115
116 Command::new("rm")
117 .args(&["-rf", dir_name])
118 .output()
119 .expect("Failed to remove directory.");
120
121 Command::new("mkdir")
122 .args(&[dir_name])
123 .output()
124 .expect("Failed to create directory.");
125
126 for (def_id, graph) in self.graphs.iter() {
127 let name = self.tcx.def_path_str(def_id);
128 let dot_file_name = format!("DataflowGraph/{}.dot", &name);
129 let png_file_name = format!("DataflowGraph/{}.png", &name);
130 let mut file = File::create(&dot_file_name).expect("Unable to create file.");
131 let dot = graph.to_dot_graph(&self.tcx);
132 file.write_all(dot.as_bytes())
133 .expect("Unable to write data.");
134
135 Command::new("dot")
136 .args(&["-Tpng", &dot_file_name, "-o", &png_file_name])
137 .output()
138 .expect("Failed to execute Graphviz dot command.");
139 }
140 }
141}