Skip to content

Commit bb62ad6

Browse files
authored
Merge pull request #855 from photoszzt/move_dependency_out
Pull out the loop that generates dependency map into its own function.
2 parents 167e84c + 6db4237 commit bb62ad6

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

src/ir/analysis/derive_debug.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Determining which types for which we can emit `#[derive(Debug)]`.
22
3-
use super::{ConstrainResult, MonotoneFramework};
3+
use super::{ConstrainResult, MonotoneFramework, generate_dependencies};
44
use std::collections::HashSet;
55
use std::collections::HashMap;
66
use ir::context::{BindgenContext, ItemId};
@@ -9,7 +9,6 @@ use ir::traversal::EdgeKind;
99
use ir::ty::RUST_DERIVE_IN_ARRAY_LIMIT;
1010
use ir::ty::TypeKind;
1111
use ir::comp::Field;
12-
use ir::traversal::Trace;
1312
use ir::comp::FieldMethods;
1413
use ir::derive::CanTriviallyDeriveDebug;
1514
use ir::comp::CompKind;
@@ -99,24 +98,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
9998

10099
fn new(ctx: &'ctx BindgenContext<'gen>) -> CannotDeriveDebug<'ctx, 'gen> {
101100
let cannot_derive_debug = HashSet::new();
102-
let mut dependencies = HashMap::new();
103-
104-
for &item in ctx.whitelisted_items() {
105-
dependencies.entry(item).or_insert(vec![]);
106-
107-
{
108-
// We reverse our natural IR graph edges to find dependencies
109-
// between nodes.
110-
item.trace(ctx, &mut |sub_item: ItemId, edge_kind| {
111-
if ctx.whitelisted_items().contains(&sub_item) &&
112-
Self::consider_edge(edge_kind) {
113-
dependencies.entry(sub_item)
114-
.or_insert(vec![])
115-
.push(item);
116-
}
117-
}, &());
118-
}
119-
}
101+
let dependencies = generate_dependencies(ctx, Self::consider_edge);
120102

121103
CannotDeriveDebug {
122104
ctx,

src/ir/analysis/has_vtable.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Determining which types has vtable
2-
use super::{ConstrainResult, MonotoneFramework};
2+
use super::{ConstrainResult, MonotoneFramework, generate_dependencies};
33
use std::collections::HashSet;
44
use std::collections::HashMap;
55
use ir::context::{BindgenContext, ItemId};
66
use ir::traversal::EdgeKind;
77
use ir::ty::TypeKind;
8-
use ir::traversal::Trace;
98

109
/// An analysis that finds for each IR item whether it has vtable or not
1110
///
@@ -68,24 +67,7 @@ impl<'ctx, 'gen> MonotoneFramework for HasVtableAnalysis<'ctx, 'gen> {
6867

6968
fn new(ctx: &'ctx BindgenContext<'gen>) -> HasVtableAnalysis<'ctx, 'gen> {
7069
let have_vtable = HashSet::new();
71-
let mut dependencies = HashMap::new();
72-
73-
for &item in ctx.whitelisted_items() {
74-
dependencies.entry(item).or_insert(vec![]);
75-
76-
{
77-
// We reverse our natural IR graph edges to find dependencies
78-
// between nodes.
79-
item.trace(ctx, &mut |sub_item: ItemId, edge_kind| {
80-
if ctx.whitelisted_items().contains(&sub_item) &&
81-
Self::consider_edge(edge_kind) {
82-
dependencies.entry(sub_item)
83-
.or_insert(vec![])
84-
.push(item);
85-
}
86-
}, &());
87-
}
88-
}
70+
let dependencies = generate_dependencies(ctx, Self::consider_edge);
8971

9072
HasVtableAnalysis {
9173
ctx,

src/ir/analysis/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ mod has_vtable;
4646
pub use self::has_vtable::HasVtableAnalysis;
4747
pub use self::has_vtable::HasVtable;
4848

49+
use ir::context::{BindgenContext, ItemId};
50+
use ir::traversal::{EdgeKind, Trace};
51+
use std::collections::HashMap;
4952
use std::fmt;
5053

5154
/// An analysis in the monotone framework.
@@ -134,6 +137,30 @@ pub fn analyze<Analysis>(extra: Analysis::Extra) -> Analysis::Output
134137
analysis.into()
135138
}
136139

140+
/// Generate the dependency map for analysis
141+
pub fn generate_dependencies<F>(ctx: &BindgenContext, consider_edge: F) -> HashMap<ItemId, Vec<ItemId>>
142+
where F: Fn(EdgeKind) -> bool {
143+
let mut dependencies = HashMap::new();
144+
145+
for &item in ctx.whitelisted_items() {
146+
dependencies.entry(item).or_insert(vec![]);
147+
148+
{
149+
// We reverse our natural IR graph edges to find dependencies
150+
// between nodes.
151+
item.trace(ctx, &mut |sub_item: ItemId, edge_kind| {
152+
if ctx.whitelisted_items().contains(&sub_item) &&
153+
consider_edge(edge_kind) {
154+
dependencies.entry(sub_item)
155+
.or_insert(vec![])
156+
.push(item);
157+
}
158+
}, &());
159+
}
160+
}
161+
dependencies
162+
}
163+
137164
#[cfg(test)]
138165
mod tests {
139166
use super::*;

0 commit comments

Comments
 (0)