rapx/analysis/core/api_dependency/
utils.rs

1#![allow(warnings, unused)]
2use super::fuzzable;
3use rustc_hir::LangItem;
4use rustc_hir::def_id::DefId;
5use rustc_middle::ty::{self, FnSig, GenericArgsRef, Ty, TyCtxt, TyKind};
6use rustc_span::sym;
7
8pub fn is_def_id_public(fn_def_id: impl Into<DefId>, tcx: TyCtxt<'_>) -> bool {
9    let fn_def_id: DefId = fn_def_id.into();
10    let local_id = fn_def_id.expect_local();
11    rap_trace!(
12        "vis: {:?} (path: {}) => {:?}",
13        fn_def_id,
14        tcx.def_path_str(fn_def_id),
15        tcx.effective_visibilities(()).effective_vis(local_id)
16    );
17
18    tcx.effective_visibilities(()).is_directly_public(local_id)
19        || tcx.effective_visibilities(()).is_exported(local_id)
20}
21
22pub fn is_fuzzable_ty<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
23    rap_trace!("check fuzzable ty: {}.", ty);
24    let is_fuzzable = fuzzable::is_fuzzable_ty(ty, tcx, 0);
25    rap_trace!("is_fuzzable({}) = {}.", ty, is_fuzzable);
26    is_fuzzable
27}
28
29pub fn is_fuzzable_api<'tcx>(fn_did: DefId, args: GenericArgsRef<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
30    let fn_sig = fn_sig_with_generic_args(fn_did, args, tcx);
31    fn_sig
32        .inputs()
33        .iter()
34        .copied()
35        .all(|ty| is_fuzzable_ty(ty, tcx))
36}
37
38pub fn fn_sig_with_generic_args<'tcx>(
39    fn_did: DefId,
40    args: &[ty::GenericArg<'tcx>],
41    tcx: TyCtxt<'tcx>,
42) -> FnSig<'tcx> {
43    let early_fn_sig = tcx.fn_sig(fn_did);
44    let binder_fn_sig = early_fn_sig.instantiate(tcx, args);
45    tcx.liberate_late_bound_regions(fn_did, binder_fn_sig)
46}
47
48pub fn fn_requires_monomorphization<'tcx>(fn_did: DefId, tcx: TyCtxt<'_>) -> bool {
49    tcx.generics_of(fn_did).requires_monomorphization(tcx)
50}
51
52pub fn is_ty_eq<'tcx>(ty1: Ty<'tcx>, ty2: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
53    let ty1 = tcx.erase_and_anonymize_regions(ty1);
54    let ty2 = tcx.erase_and_anonymize_regions(ty2);
55    return ty1 == ty2;
56}
57
58pub fn ty_complexity<'tcx>(ty: Ty<'tcx>) -> usize {
59    match ty.kind() {
60        // Reference, Array, Slice
61        TyKind::Ref(_, inner_ty, _) | TyKind::Array(inner_ty, _) | TyKind::Slice(inner_ty) => {
62            ty_complexity(*inner_ty) + 1
63        }
64
65        // Tuple
66        TyKind::Tuple(tys) => tys.iter().fold(0, |ans, ty| ans.max(ty_complexity(ty))) + 1,
67
68        // ADT
69        TyKind::Adt(_, args) => {
70            args.iter().fold(0, |ans, arg| {
71                if let Some(ty) = arg.as_type() {
72                    ans.max(ty_complexity(ty))
73                } else {
74                    ans
75                }
76            }) + 1
77        }
78
79        // the depth of other primitive type default to 1
80        _ => 1,
81    }
82}
83
84pub fn is_ty_unstable<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
85    ty == tcx.types.f16
86}