rapx/analysis/core/
dataflow.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
pub mod debug;
pub mod graph;

use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::process::Command;

use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::Body;
use rustc_middle::ty::TyCtxt;

use graph::Graph;

pub struct DataFlow<'tcx> {
    pub tcx: TyCtxt<'tcx>,
    pub graphs: HashMap<DefId, Graph>,
    pub debug: bool,
}

impl<'tcx> DataFlow<'tcx> {
    pub fn new(tcx: TyCtxt<'tcx>, debug: bool) -> Self {
        Self {
            tcx: tcx,
            graphs: HashMap::new(),
            debug,
        }
    }

    pub fn start(&mut self) {
        self.build_graphs();
        if self.debug {
            self.draw_graphs();
        }
    }

    pub fn build_graphs(&mut self) {
        for local_def_id in self.tcx.iter_local_def_id() {
            let def_kind = self.tcx.def_kind(local_def_id);
            if matches!(def_kind, DefKind::Fn) || matches!(def_kind, DefKind::AssocFn) {
                let hir_map = self.tcx.hir();
                if hir_map.maybe_body_owned_by(local_def_id).is_some() {
                    let def_id = local_def_id.to_def_id();
                    self.build_graph(def_id);
                }
            }
        }
    }

    fn build_graph(&mut self, def_id: DefId) {
        if self.graphs.contains_key(&def_id) {
            return;
        }
        let body: &Body = self.tcx.optimized_mir(def_id);
        let mut graph = Graph::new(def_id, body.span, body.arg_count, body.local_decls.len());
        let basic_blocks = &body.basic_blocks;
        for basic_block_data in basic_blocks.iter() {
            for statement in basic_block_data.statements.iter() {
                graph.add_statm_to_graph(&statement);
            }
            if let Some(terminator) = &basic_block_data.terminator {
                graph.add_terminator_to_graph(&terminator);
            }
        }
        for closure_id in graph.closures.iter() {
            self.build_graph(*closure_id);
        }
        self.graphs.insert(def_id, graph);
    }

    pub fn draw_graphs(&self) {
        let dir_name = "DataflowGraph";

        Command::new("rm")
            .args(&["-rf", dir_name])
            .output()
            .expect("Failed to remove directory.");

        Command::new("mkdir")
            .args(&[dir_name])
            .output()
            .expect("Failed to create directory.");

        for (def_id, graph) in self.graphs.iter() {
            let name = self.tcx.def_path_str(def_id);
            let dot_file_name = format!("DataflowGraph/{}.dot", &name);
            let png_file_name = format!("DataflowGraph/{}.png", &name);
            let mut file = File::create(&dot_file_name).expect("Unable to create file.");
            let dot = graph.to_dot_graph(&self.tcx);
            file.write_all(dot.as_bytes())
                .expect("Unable to write data.");

            Command::new("dot")
                .args(&["-Tpng", &dot_file_name, "-o", &png_file_name])
                .output()
                .expect("Failed to execute Graphviz dot command.");
        }
    }
}