Skip to content

Commit 9d320de

Browse files
authored
Merge pull request #12 from os-checker/callee-source-code
Feat: add source code for callees
2 parents 55b1ea8 + 95404b1 commit 9d320de

File tree

4 files changed

+1495
-383
lines changed

4 files changed

+1495
-383
lines changed

src/functions/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn analyze(tcx: TyCtxt, src_map: &SourceMap) -> Vec<SerFunction> {
3333
mono_items
3434
.iter()
3535
.filter_map(|item| Function::new(item, &callgraph, tcx, src_map, |x| !x.attrs.is_empty()))
36-
.map(SerFunction::new)
36+
.map(|fun| SerFunction::new(fun, tcx, src_map))
3737
.collect()
3838
}
3939

@@ -51,6 +51,7 @@ pub struct Function {
5151
/// Raw function string, including name, signature, and body.
5252
func: String,
5353
/// Recursive fnction calls inside the body.
54+
/// This set has insertion order, which is nice to check out call contexts.
5455
callees: IndexSet<Instance>,
5556
}
5657

src/functions/serialization.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1+
use rustc_middle::ty::TyCtxt;
2+
use rustc_span::source_map::SourceMap;
13
use serde::Serialize;
2-
use stable_mir::{CrateDef, DefId};
4+
use stable_mir::{CrateDef, DefId, mir::mono::Instance};
35

46
/// A Rust funtion with its file source, attributes, and raw function content.
57
#[derive(Debug, Serialize)]
68
pub struct SerFunction {
79
/// DefId in stable_mir.
8-
pub def_id: String,
10+
def_id: String,
911
/// Every funtion must be declared in a specific file, even for those
1012
/// generated by macros.
11-
pub file: String,
13+
file: String,
1214
/// Attributes are attached the function, but it seems that attributes
1315
/// and function must be separated to query.
14-
pub attrs: Vec<String>,
16+
attrs: Vec<String>,
1517
/// Raw function string, including name, signature, and body.
16-
pub func: String,
18+
func: String,
1719
/// Recursive fnction calls inside the body.
18-
pub callees: Vec<String>,
20+
callees: Vec<Callee>,
1921
}
2022

2123
impl SerFunction {
22-
pub fn new(fun: super::Function) -> Self {
24+
pub fn new(fun: super::Function, tcx: TyCtxt, src_map: &SourceMap) -> Self {
2325
SerFunction {
2426
def_id: format_def_id(&fun.def_id),
2527
file: fun.file,
2628
attrs: fun.attrs.iter().map(|a| a.as_str().to_owned()).collect(),
2729
func: fun.func,
28-
callees: fun.callees.into_iter().map(|x| format_def_id(&x.def.def_id())).collect(),
30+
callees: fun.callees.iter().map(|x| Callee::new(x, tcx, src_map)).collect(),
2931
}
3032
}
3133
}
3234

3335
fn format_def_id(def_id: &DefId) -> String {
3436
format!("{def_id:?}")
3537
}
38+
39+
#[derive(Debug, Serialize)]
40+
pub struct Callee {
41+
def_id: String,
42+
func: String,
43+
}
44+
45+
impl Callee {
46+
fn new(inst: &Instance, tcx: TyCtxt, src_map: &SourceMap) -> Self {
47+
let def_id = format_def_id(&inst.def.def_id());
48+
let func = inst
49+
.body()
50+
.map(|body| super::source_code_with(body.span, tcx, src_map))
51+
.unwrap_or_default();
52+
Callee { def_id, func }
53+
}
54+
}

0 commit comments

Comments
 (0)