Skip to main content

rapx/check/safedrop/
graph.rs

1use super::{bug_records::*, drop::*};
2use crate::analysis::{
3    alias::default::graph::AliasGraph, heap_ownership::HeapOwnershipResultMap,
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: HeapOwnershipResultMap,
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: HeapOwnershipResultMap,
26    ) -> Self {
27        Self::from_alias_graph(
28            AliasGraph::from_path_graph(tcx, def_id, path_graph),
29            adt_owner,
30        )
31    }
32
33    fn from_alias_graph(alias_graph: AliasGraph<'tcx>, adt_owner: HeapOwnershipResultMap) -> Self {
34        let mut drop_record = Vec::<DropRecord>::new();
35        for v in &alias_graph.values {
36            drop_record.push(DropRecord::false_record(v.index));
37        }
38        SafeDropGraph {
39            alias_graph,
40            bug_records: BugRecords::new(),
41            drop_record,
42            adt_owner,
43        }
44    }
45}
46
47impl<'tcx> std::fmt::Display for SafeDropGraph<'tcx> {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        writeln!(f, "SafeDropGraph {{")?;
50        writeln!(f, "  AliasGraph: {}", self.alias_graph)?;
51        write!(f, "}}")
52    }
53}