Skip to content

Commit 7f8bddb

Browse files
committed
put foreign signatures in gacx.fn_sig
1 parent 5214eda commit 7f8bddb

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

c2rust-analyze/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ pub struct GlobalAnalysisCtxt<'tcx> {
277277
pub fn_callers: HashMap<DefId, Vec<DefId>>,
278278

279279
pub fn_sigs: HashMap<DefId, LFnSig<'tcx>>,
280+
280281
/// `DefId`s of functions where analysis failed, and a [`PanicDetail`] explaining the reason
281282
/// for each failure.
282283
pub fns_failed: HashMap<DefId, PanicDetail>,

c2rust-analyze/src/main.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,26 +321,35 @@ where
321321
}
322322
}
323323

324+
fn gather_foreign_sigs<'tcx>(gacx: &mut GlobalAnalysisCtxt<'tcx>, tcx: TyCtxt<'tcx>) {
325+
for did in tcx
326+
.hir_crate_items(())
327+
.foreign_items()
328+
.map(|item| item.def_id.to_def_id())
329+
.filter(|did| matches!(tcx.def_kind(did), DefKind::Fn | DefKind::AssocFn))
330+
{
331+
let sig = tcx.erase_late_bound_regions(tcx.fn_sig(did));
332+
let inputs = sig
333+
.inputs()
334+
.iter()
335+
.map(|&ty| gacx.assign_pointer_ids_with_info(ty, PointerInfo::ANNOTATED))
336+
.collect::<Vec<_>>();
337+
let inputs = gacx.lcx.mk_slice(&inputs);
338+
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
339+
let lsig = LFnSig { inputs, output };
340+
gacx.fn_sigs.insert(did, lsig);
341+
}
342+
}
343+
324344
fn mark_foreign_fixed<'tcx>(
325345
gacx: &mut GlobalAnalysisCtxt<'tcx>,
326346
gasn: &mut GlobalAssignment,
327347
tcx: TyCtxt<'tcx>,
328348
) {
329349
// FIX the inputs and outputs of function declarations in extern blocks
330-
for did in gacx.foreign_mentioned_tys.clone() {
331-
if let DefKind::Fn = tcx.def_kind(did) {
332-
let sig = tcx.erase_late_bound_regions(tcx.fn_sig(did));
333-
let inputs = sig
334-
.inputs()
335-
.iter()
336-
.map(|&ty| gacx.assign_pointer_ids_with_info(ty, PointerInfo::ANNOTATED))
337-
.collect::<Vec<_>>();
338-
for input in inputs {
339-
make_ty_fixed(gasn, input);
340-
}
341-
342-
let output = gacx.assign_pointer_ids_with_info(sig.output(), PointerInfo::ANNOTATED);
343-
make_ty_fixed(gasn, output)
350+
for (did, lsig) in gacx.fn_sigs.iter() {
351+
if tcx.is_foreign_item(did) {
352+
make_sig_fixed(gasn, lsig);
344353
}
345354
}
346355

@@ -560,6 +569,7 @@ fn run(tcx: TyCtxt) {
560569
// track all types mentioned in extern blocks, we
561570
// don't want to rewrite those
562571
gacx.foreign_mentioned_tys = foreign_mentioned_tys(tcx);
572+
gather_foreign_sigs(&mut gacx, tcx);
563573

564574
let mut gasn =
565575
GlobalAssignment::new(gacx.num_pointers(), PermissionSet::UNIQUE, FlagSet::empty());

0 commit comments

Comments
 (0)