| 106 | |
| 107 | impl<'tcx, 'compilation> GlobalContext<'tcx, 'compilation> { |
| 108 | pub fn new( |
| 109 | session: &'compilation Session, |
| 110 | tcx: TyCtxt<'tcx>, |
| 111 | analysis_options: AnalysisOption, |
| 112 | ) -> Option<Self> { |
| 113 | // ------------------------------------------------------------ |
| 114 | // Phase 0: 枚举所有本地 MIR 函数(Fn/AssocFn) |
| 115 | // ------------------------------------------------------------ |
| 116 | |
| 117 | let mut all_entries: HashMap<LocalDefId, FuncInfo> = HashMap::new(); |
| 118 | |
| 119 | for &local_def_id in tcx.mir_keys(()).iter() { |
| 120 | let def_id = local_def_id.to_def_id(); |
| 121 | let def_kind = tcx.def_kind(def_id); |
| 122 | |
| 123 | if def_kind == DefKind::Fn || def_kind == DefKind::AssocFn { |
| 124 | let def_id_index = def_id.index.as_u32(); |
| 125 | let def_path = tcx.def_path_str(def_id); |
| 126 | |
| 127 | let is_builtin_derive: bool = match def_kind { |
| 128 | DefKind::Fn => false, |
| 129 | DefKind::AssocFn => { |
| 130 | if let Some(impl_id) = tcx.impl_of_method(def_id) { |
| 131 | tcx.is_builtin_derived(impl_id) |
| 132 | } else { |
| 133 | false |
| 134 | } |
| 135 | } |
| 136 | _ => false, |
| 137 | }; |
| 138 | |
| 139 | let info = FuncInfo { |
| 140 | def_id, |
| 141 | def_id_index, |
| 142 | def_kind, |
| 143 | func_name: def_path, |
| 144 | is_builtin_derive, |
| 145 | callees: Vec::new(), |
| 146 | api_count: 0, |
| 147 | api_vec: Vec::new(), |
| 148 | }; |
| 149 | all_entries.insert(local_def_id, info); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // show_all_entries: 展示然后结束, 或者无分析项 |
| 154 | if analysis_options.show_all_entries || all_entries.is_empty() { |
| 155 | for (_, info) in &all_entries { |
| 156 | info!("{:?}", info); |
| 157 | } |
| 158 | info!("The total function number: {}", &all_entries.len()); |
| 159 | return None; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // entry_point:bin 有 main 就用 main;否则随便选一个占位(lib crate 常见) |
| 164 | // let entry_point = if let Some((entry_local, _entry_type)) = tcx.entry_fn(()) { |
| 165 | // entry_local |