Skip to content

Commit 1214042

Browse files
committed
fix: duplicated defid in callees by checking share_same_source_code
close #37
1 parent 28fdbc2 commit 1214042

File tree

4 files changed

+244
-1719
lines changed

4 files changed

+244
-1719
lines changed

src/functions/cache.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ pub fn get_source_code(inst: &Instance) -> Option<SourceCode> {
4545
get_cache_func(inst, |cf| cf.src.clone())
4646
}
4747

48+
/// NOTE: this function doesn't auto insert SourceCode if Instance isn't handled.
49+
pub fn share_same_source_code(a: &Instance, b: &Instance) -> bool {
50+
get_cache(|cache| {
51+
let defid_a = a.def.def_id();
52+
let defid_b = b.def.def_id();
53+
let src_a = cache.set.get(&defid_a);
54+
let src_b = cache.set.get(&defid_b);
55+
match (src_a, src_b) {
56+
(Some(cache_a), Some(cache_b)) => match (cache_a, cache_b) {
57+
(Some(func_a), Some(func_b)) => func_a.src == func_b.src,
58+
(None, None) => defid_a == defid_b,
59+
(None, Some(_)) => false,
60+
(Some(_), None) => false,
61+
},
62+
(None, None) => defid_a == defid_b,
63+
(None, Some(_)) => false,
64+
(Some(_), None) => false,
65+
}
66+
})
67+
}
68+
4869
pub fn cmp_callees(a: &Instance, b: &Instance) -> Ordering {
4970
get_cache(|cache| {
5071
cache.get_or_insert(a);

src/functions/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct Function {
5353

5454
/// Recursive fnction calls inside the body.
5555
/// The elements are sorted by file path and fn source code to keep hash value stable.
56-
callees: IndexSet<Instance>,
56+
callees: Vec<Instance>,
5757
}
5858

5959
impl Function {
@@ -75,7 +75,11 @@ impl Function {
7575

7676
let mut callees = IndexSet::new();
7777
callgraph.recursive_callees(item, &mut callees);
78+
79+
// Multiple instances may share the same defid (or rather SourceCode), so deduplicate them.
80+
let mut callees: Vec<_> = callees.into_iter().collect();
7881
callees.sort_by(cache::cmp_callees);
82+
callees.dedup_by(|a, b| cache::share_same_source_code(a, b));
7983

8084
let this = Function { instance, attrs, callees };
8185
filter(&this).then_some(this)

0 commit comments

Comments
 (0)