Skip to main content

rapx/check/safedrop/
graph.rs

1use super::{bug_records::*, drop::*};
2use crate::analysis::{
3    alias::default::graph::AliasGraph, owned_heap::OHAResultMap,
4    path::graph::PathGraph,
5};
6use rustc_middle::ty::TyCtxt;
7use rustc_span::def_id::DefId;
8use std::fmt;
9
10/// We represent each target function with the `SafeDropGraph` struct and then perform analysis
11/// based on the struct.
12pub struct SafeDropGraph<'tcx> {
13    pub alias_graph: AliasGraph<'tcx>,
14    pub bug_records: BugRecords,
15    pub drop_record: Vec<DropRecord>,
16    // analysis of heap item
17    pub adt_owner: OHAResultMap,
18}
19
20impl<'tcx> SafeDropGraph<'tcx> {
21    pub fn from_path_graph(
22        tcx: TyCtxt<'tcx>,
23        def_id: DefId,
24        path_graph: PathGraph<'tcx>,
25        adt_owner: OHAResultMap,
26    ) -> Self {
27        Self::from_alias_graph(AliasGraph::from_path_graph(tcx, def_id, path_graph), adt_owner)
28    }
29
30    fn from_alias_graph(alias_graph: AliasGraph<'tcx>, adt_owner: OHAResultMap) -> Self {
31        let mut drop_record = Vec::<DropRecord>::new();
32        for v in &alias_graph.values {
33            drop_record.push(DropRecord::false_record(v.index));
34        }
35        SafeDropGraph {
36            alias_graph,
37            bug_records: BugRecords::new(),
38            drop_record,
39            adt_owner,
40        }
41    }
42}
43
44impl<'tcx> std::fmt::Display for SafeDropGraph<'tcx> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        writeln!(f, "SafeDropGraph {{")?;
47        writeln!(f, "  AliasGraph: {}", self.alias_graph)?;
48        write!(f, "}}")
49    }
50}