-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvisitor.rs
161 lines (138 loc) · 5.87 KB
/
visitor.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::{
def_id::{DefId, LocalDefId},
intravisit,
itemlikevisit::ItemLikeVisitor,
Block, BodyId, HirId, Impl, ItemKind,
};
use rustc_middle::ty::{Ty, TyCtxt, TyKind};
use rustc_span::Span;
/// Maps `HirId` of a type to `BodyId` of related impls.
/// Free-standing (top level) functions and default trait impls have `None` as a key.
pub type RelatedItemMap = FxHashMap<Option<HirId>, Vec<(BodyId, Span)>>;
/// Creates `AdtItemMap` with the given HIR map.
/// You might want to use `RudraCtxt`'s `related_item_cache` field instead of
/// directly using this collector.
pub struct RelatedFnCollector<'tcx> {
tcx: TyCtxt<'tcx>,
hash_map: RelatedItemMap,
}
impl<'tcx> RelatedFnCollector<'tcx> {
pub fn collect(tcx: TyCtxt<'tcx>) -> RelatedItemMap {
let mut collector = RelatedFnCollector {
tcx,
hash_map: RelatedItemMap::default(),
};
tcx.hir().visit_all_item_likes(&mut collector);
collector.hash_map
}
}
impl<'tcx> ItemLikeVisitor<'tcx> for RelatedFnCollector<'tcx> {
fn visit_item(&mut self, item: &'tcx rustc_hir::Item<'tcx>) {
let hir_map = self.tcx.hir();
match &item.kind {
ItemKind::Impl(Impl {
unsafety: _unsafety,
generics: _generics,
self_ty,
items: impl_items,
..
}) => {
let key = Some(self_ty.hir_id);
let entry = self.hash_map.entry(key).or_insert(Vec::new());
entry.extend(impl_items.iter().filter_map(|impl_item_ref| {
let hir_id = impl_item_ref.id.hir_id();
hir_map
.maybe_body_owned_by(hir_id)
.map(|body_id| (body_id, impl_item_ref.span))
}));
}
// Free-standing (top level) functions and default trait impls have `None` as a key.
ItemKind::Trait(_is_auto, _unsafety, _generics, _generic_bounds, trait_items) => {
let key = None;
let entry = self.hash_map.entry(key).or_insert(Vec::new());
entry.extend(trait_items.iter().filter_map(|trait_item_ref| {
let hir_id = trait_item_ref.id.hir_id();
hir_map
.maybe_body_owned_by(hir_id)
.map(|body_id| (body_id, trait_item_ref.span))
}));
}
ItemKind::Fn(_fn_sig, _generics, body_id) => {
let key = None;
let entry = self.hash_map.entry(key).or_insert(Vec::new());
entry.push((*body_id, item.span));
}
_ => (),
}
}
fn visit_trait_item(&mut self, _trait_item: &'tcx rustc_hir::TraitItem<'tcx>) {
// We don't process items inside trait blocks
}
fn visit_impl_item(&mut self, _impl_item: &'tcx rustc_hir::ImplItem<'tcx>) {
// We don't process items inside impl blocks
}
fn visit_foreign_item(&mut self, _foreign_item: &'tcx rustc_hir::ForeignItem<'tcx>) {
// We don't process foreign items
}
}
pub struct ContainsUnsafe<'tcx> {
tcx: TyCtxt<'tcx>,
contains_unsafe: bool,
}
impl<'tcx> ContainsUnsafe<'tcx> {
/// Given a `BodyId`, returns if the corresponding body contains unsafe code in it.
/// Note that it only checks the function body, so this function will return false for
/// body ids of functions that are defined as unsafe.
pub fn contains_unsafe(tcx: TyCtxt<'tcx>, body_id: BodyId) -> bool {
use intravisit::Visitor;
let mut visitor = ContainsUnsafe {
tcx,
contains_unsafe: false,
};
let body = visitor.tcx.hir().body(body_id);
visitor.visit_body(body);
visitor.contains_unsafe
}
}
impl<'tcx> intravisit::Visitor<'tcx> for ContainsUnsafe<'tcx> {
type Map = rustc_middle::hir::map::Map<'tcx>;
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
intravisit::NestedVisitorMap::OnlyBodies(self.tcx.hir())
}
fn visit_block(&mut self, block: &'tcx Block<'tcx>) {
use rustc_hir::BlockCheckMode;
if let BlockCheckMode::UnsafeBlock(_unsafe_source) = block.rules {
self.contains_unsafe = true;
}
intravisit::walk_block(self, block);
}
}
/// (`DefId` of ADT) => Vec<(HirId of relevant impl block, impl_self_ty)>
/// We use this map to quickly access associated impl blocks per ADT.
/// `impl_self_ty` in the return value may differ from `tcx.type_of(ADT.DefID)`,
/// as different instantiations of the same ADT are distinct `Ty`s.
/// (e.g. Foo<i32, i64>, Foo<String, i32>)
pub type AdtImplMap<'tcx> = FxHashMap<DefId, Vec<(LocalDefId, Ty<'tcx>)>>;
/// Create & initialize `AdtImplMap`.
/// `AdtImplMap` is initialized before analysis of each crate,
/// avoiding quadratic complexity of scanning all impl blocks for each ADT.
pub fn create_adt_impl_map<'tcx>(tcx: TyCtxt<'tcx>) -> AdtImplMap<'tcx> {
let mut map = FxHashMap::default();
for item in tcx.hir().items() {
if let ItemKind::Impl(Impl { self_ty, .. }) = item.kind {
// `Self` type of the given impl block.
let impl_self_ty = tcx.type_of(self_ty.hir_id.owner);
if let TyKind::Adt(impl_self_adt_def, _impl_substs) = impl_self_ty.kind() {
// We use `AdtDef.did` as key for `AdtImplMap`.
// For any crazy instantiation of the same generic ADT (Foo<i32>, Foo<String>, etc..),
// `AdtDef.did` refers to the original ADT definition.
// Thus it can be used to map & collect impls for all instantitations of the same ADT.
map.entry(impl_self_adt_def.did)
.or_insert_with(|| Vec::new())
.push((item.def_id, impl_self_ty));
}
}
}
map
}