Skip to content

Commit

Permalink
analyze: refactor: move some TestAttr handling into helper fns
Browse files Browse the repository at this point in the history
  • Loading branch information
spernsteiner committed May 2, 2024
1 parent ac5e674 commit 9f3129d
Showing 1 changed file with 59 additions and 35 deletions.
94 changes: 59 additions & 35 deletions c2rust-analyze/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,41 +1088,14 @@ fn run(tcx: TyCtxt) {
// Run dataflow solver and borrowck analysis
// ----------------------------------

// For testing, putting #[c2rust_analyze_test::fail_before_analysis] on a function marks it as
// failed at this point.
for &ldid in &all_fn_ldids {
if !util::has_test_attr(tcx, ldid, TestAttr::FailBeforeAnalysis) {
continue;
}
gacx.mark_fn_failed(
ldid.to_def_id(),
DontRewriteFnReason::FAKE_INVALID_FOR_TESTING,
PanicDetail::new("explicit fail_before_analysis for testing".to_owned()),
);
}

// For testing, putting #[c2rust_analyze_test::force_non_null_args] on a function marks its
// arguments as `NON_NULL` and also adds `NON_NULL` to the `updates_forbidden` mask.
for &ldid in &all_fn_ldids {
if !util::has_test_attr(tcx, ldid, TestAttr::ForceNonNullArgs) {
continue;
}

let info = func_info.get_mut(&ldid).unwrap();
let mut asn = gasn.and(&mut info.lasn);
let mut updates_forbidden = g_updates_forbidden.and_mut(&mut info.l_updates_forbidden);

let lsig = &gacx.fn_sigs[&ldid.to_def_id()];
for arg_lty in lsig.inputs.iter().copied() {
for lty in arg_lty.iter() {
let ptr = lty.label;
if !ptr.is_none() {
asn.perms_mut()[ptr].insert(PermissionSet::NON_NULL);
updates_forbidden[ptr].insert(PermissionSet::NON_NULL);
}
}
}
}
apply_test_attr_fail_before_analysis(&mut gacx, &all_fn_ldids);
apply_test_attr_force_non_null_args(
&mut gacx,
&all_fn_ldids,
&mut func_info,
&mut gasn,
&mut g_updates_forbidden,
);

eprintln!("=== ADT Metadata ===");
eprintln!("{:?}", gacx.adt_metadata);
Expand Down Expand Up @@ -1850,6 +1823,57 @@ fn make_sig_fixed(gasn: &mut GlobalAssignment, lsig: &LFnSig) {
}
}

/// For testing, putting #[c2rust_analyze_test::fail_before_analysis] on a function marks it as
/// failed at this point.
fn apply_test_attr_fail_before_analysis(
gacx: &mut GlobalAnalysisCtxt,
all_fn_ldids: &[LocalDefId],
) {
let tcx = gacx.tcx;
for &ldid in all_fn_ldids {
if !util::has_test_attr(tcx, ldid, TestAttr::FailBeforeAnalysis) {
continue;
}
gacx.mark_fn_failed(
ldid.to_def_id(),
DontRewriteFnReason::FAKE_INVALID_FOR_TESTING,
PanicDetail::new("explicit fail_before_analysis for testing".to_owned()),
);
}
}

/// For testing, putting #[c2rust_analyze_test::force_non_null_args] on a function marks its
/// arguments as `NON_NULL` and also adds `NON_NULL` to the `updates_forbidden` mask.
fn apply_test_attr_force_non_null_args(
gacx: &mut GlobalAnalysisCtxt,
all_fn_ldids: &[LocalDefId],
func_info: &mut HashMap<LocalDefId, FuncInfo>,
gasn: &mut GlobalAssignment,
g_updates_forbidden: &mut GlobalPointerTable<PermissionSet>,
) {
let tcx = gacx.tcx;
for &ldid in all_fn_ldids {
if !util::has_test_attr(tcx, ldid, TestAttr::ForceNonNullArgs) {
continue;
}

let info = func_info.get_mut(&ldid).unwrap();
let mut asn = gasn.and(&mut info.lasn);
let mut updates_forbidden = g_updates_forbidden.and_mut(&mut info.l_updates_forbidden);

let lsig = &gacx.fn_sigs[&ldid.to_def_id()];
for arg_lty in lsig.inputs.iter().copied() {
for lty in arg_lty.iter() {
let ptr = lty.label;
if !ptr.is_none() {
asn.perms_mut()[ptr].insert(PermissionSet::NON_NULL);
updates_forbidden[ptr].insert(PermissionSet::NON_NULL);
}
}
}
}
}

fn local_span(decl: &LocalDecl) -> Span {
let mut span = decl.source_info.span;
if let Some(ref info) = decl.local_info {
Expand Down

0 comments on commit 9f3129d

Please sign in to comment.