rapx/analysis/core/
dataflow.rs

1pub mod debug;
2pub mod graph;
3
4use std::collections::HashMap;
5use std::fs::File;
6use std::io::Write;
7use std::process::Command;
8
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::DefId;
11use rustc_middle::mir::Body;
12use rustc_middle::ty::TyCtxt;
13
14use graph::Graph;
15
16pub struct DataFlow<'tcx> {
17    pub tcx: TyCtxt<'tcx>,
18    pub graphs: HashMap<DefId, Graph>,
19    pub debug: bool,
20}
21
22impl<'tcx> DataFlow<'tcx> {
23    pub fn new(tcx: TyCtxt<'tcx>, debug: bool) -> Self {
24        Self {
25            tcx: tcx,
26            graphs: HashMap::new(),
27            debug,
28        }
29    }
30
31    pub fn start(&mut self) {
32        self.build_graphs();
33        if self.debug {
34            self.draw_graphs();
35        }
36    }
37
38    pub fn build_graphs(&mut self) {
39        for local_def_id in self.tcx.iter_local_def_id() {
40            let def_kind = self.tcx.def_kind(local_def_id);
41            if matches!(def_kind, DefKind::Fn) || matches!(def_kind, DefKind::AssocFn) {
42                if self.tcx.hir_maybe_body_owned_by(local_def_id).is_some() {
43                    let def_id = local_def_id.to_def_id();
44                    self.build_graph(def_id);
45                }
46            }
47        }
48    }
49
50    fn build_graph(&mut self, def_id: DefId) {
51        if self.graphs.contains_key(&def_id) {
52            return;
53        }
54        let body: &Body = self.tcx.optimized_mir(def_id);
55        let mut graph = Graph::new(def_id, body.span, body.arg_count, body.local_decls.len());
56        let basic_blocks = &body.basic_blocks;
57        for basic_block_data in basic_blocks.iter() {
58            for statement in basic_block_data.statements.iter() {
59                graph.add_statm_to_graph(&statement);
60            }
61            if let Some(terminator) = &basic_block_data.terminator {
62                graph.add_terminator_to_graph(&terminator);
63            }
64        }
65        for closure_id in graph.closures.iter() {
66            self.build_graph(*closure_id);
67        }
68        self.graphs.insert(def_id, graph);
69    }
70
71    pub fn draw_graphs(&self) {
72        let dir_name = "DataflowGraph";
73
74        Command::new("rm")
75            .args(&["-rf", dir_name])
76            .output()
77            .expect("Failed to remove directory.");
78
79        Command::new("mkdir")
80            .args(&[dir_name])
81            .output()
82            .expect("Failed to create directory.");
83
84        for (def_id, graph) in self.graphs.iter() {
85            let name = self.tcx.def_path_str(def_id);
86            let dot_file_name = format!("DataflowGraph/{}.dot", &name);
87            let png_file_name = format!("DataflowGraph/{}.png", &name);
88            let mut file = File::create(&dot_file_name).expect("Unable to create file.");
89            let dot = graph.to_dot_graph(&self.tcx);
90            file.write_all(dot.as_bytes())
91                .expect("Unable to write data.");
92
93            Command::new("dot")
94                .args(&["-Tpng", &dot_file_name, "-o", &png_file_name])
95                .output()
96                .expect("Failed to execute Graphviz dot command.");
97        }
98    }
99}