Skip to content

Commit 3271b3d

Browse files
ahomescuthedataking
authored andcommitted
Use log crate for debugging output in c2rust-analyze
Replace all eprintln! macros with uses of the log crate for more efficient and fine-grained debugging output.
1 parent 68de84c commit 3271b3d

File tree

19 files changed

+149
-139
lines changed

19 files changed

+149
-139
lines changed

c2rust-analyze/src/analyze.rs

+42-43
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use crate::type_desc::Ownership;
2525
use crate::util;
2626
use crate::util::Callee;
2727
use crate::util::TestAttr;
28-
use ::log::warn;
2928
use c2rust_pdg::graph::Graphs;
3029
use c2rust_pdg::info::NodeInfo;
30+
use log::{debug, info, warn};
3131
use rustc_hir::def::DefKind;
3232
use rustc_hir::def_id::CrateNum;
3333
use rustc_hir::def_id::DefId;
@@ -244,15 +244,15 @@ fn update_pointer_info<'tcx>(acx: &mut AnalysisCtxt<'_, 'tcx>, mir: &Body<'tcx>)
244244
_ => continue,
245245
};
246246

247-
eprintln!(
247+
debug!(
248248
"update_pointer_info: visit assignment: {:?}[{}]: {:?}",
249249
bb, i, stmt
250250
);
251251

252252
if !pl.is_indirect() {
253253
// This is a write directly to `pl.local`.
254254
*write_count.entry(pl.local).or_insert(0) += 1;
255-
eprintln!(" record write to LHS {:?}", pl);
255+
debug!(" record write to LHS {:?}", pl);
256256
}
257257

258258
let ref_pl = match *rv {
@@ -264,7 +264,7 @@ fn update_pointer_info<'tcx>(acx: &mut AnalysisCtxt<'_, 'tcx>, mir: &Body<'tcx>)
264264
// For simplicity, we consider taking the address of a local to be a write. We
265265
// expect this not to happen for the sorts of temporary refs we're looking for.
266266
if !ref_pl.is_indirect() {
267-
eprintln!(" record write to ref target {:?}", ref_pl);
267+
debug!(" record write to ref target {:?}", ref_pl);
268268
*write_count.entry(ref_pl.local).or_insert(0) += 1;
269269
}
270270

@@ -385,7 +385,7 @@ fn mark_foreign_fixed<'tcx>(
385385
let fields = adt_def.all_fields();
386386
for field in fields {
387387
let field_lty = gacx.field_ltys[&field.did];
388-
eprintln!(
388+
debug!(
389389
"adding FIXED permission for {adt_did:?} field {:?}",
390390
field.did
391391
);
@@ -573,9 +573,9 @@ struct FuncInfo<'tcx> {
573573
}
574574

575575
fn run(tcx: TyCtxt) {
576-
eprintln!("all defs:");
576+
debug!("all defs:");
577577
for ldid in tcx.hir_crate_items(()).definitions() {
578-
eprintln!("{:?}", ldid);
578+
debug!("{:?}", ldid);
579579
}
580580

581581
// Load the list of fixed defs early, so any errors are reported immediately.
@@ -591,9 +591,9 @@ fn run(tcx: TyCtxt) {
591591
// Follow a postorder traversal, so that callers are visited after their callees. This means
592592
// callee signatures will usually be up to date when we visit the call site.
593593
let all_fn_ldids = fn_body_owners_postorder(tcx);
594-
eprintln!("callgraph traversal order:");
594+
debug!("callgraph traversal order:");
595595
for &ldid in &all_fn_ldids {
596-
eprintln!(" {:?}", ldid);
596+
debug!(" {:?}", ldid);
597597
}
598598

599599
populate_field_users(&mut gacx, &all_fn_ldids);
@@ -629,9 +629,9 @@ fn run(tcx: TyCtxt) {
629629

630630
// Collect all `static` items.
631631
let all_static_dids = all_static_items(tcx);
632-
eprintln!("statics:");
632+
debug!("statics:");
633633
for &did in &all_static_dids {
634-
eprintln!(" {:?}", did);
634+
debug!(" {:?}", did);
635635
}
636636

637637
// Assign global `PointerId`s for types of `static` items.
@@ -834,7 +834,7 @@ fn run(tcx: TyCtxt) {
834834
// Remap pointers based on equivalence classes, so all members of an equivalence class now use
835835
// the same `PointerId`.
836836
let (global_counter, global_equiv_map) = global_equiv.renumber();
837-
eprintln!("global_equiv_map = {global_equiv_map:?}");
837+
debug!("global_equiv_map = {global_equiv_map:?}");
838838
pointee_type::remap_pointers_global(
839839
&mut global_pointee_types,
840840
&global_equiv_map,
@@ -849,7 +849,7 @@ fn run(tcx: TyCtxt) {
849849

850850
let info = func_info.get_mut(&ldid).unwrap();
851851
let (local_counter, local_equiv_map) = info.local_equiv.renumber(&global_equiv_map);
852-
eprintln!("local_equiv_map = {local_equiv_map:?}");
852+
debug!("local_equiv_map = {local_equiv_map:?}");
853853
pointee_type::remap_pointers_local(
854854
&mut global_pointee_types,
855855
&mut info.local_pointee_types,
@@ -1043,8 +1043,8 @@ fn run(tcx: TyCtxt) {
10431043
&mut g_updates_forbidden,
10441044
);
10451045

1046-
eprintln!("=== ADT Metadata ===");
1047-
eprintln!("{:?}", gacx.adt_metadata);
1046+
debug!("=== ADT Metadata ===");
1047+
debug!("{:?}", gacx.adt_metadata);
10481048

10491049
let mut loop_count = 0;
10501050
loop {
@@ -1110,14 +1110,14 @@ fn run(tcx: TyCtxt) {
11101110
let added = new & !old;
11111111
let removed = old & !new;
11121112
let kept = old & new;
1113-
eprintln!(
1113+
debug!(
11141114
"changed {:?}: added {:?}, removed {:?}, kept {:?}",
11151115
ptr, added, removed, kept
11161116
);
11171117
num_changed += 1;
11181118
}
11191119
}
1120-
eprintln!(
1120+
debug!(
11211121
"iteration {}: {} global pointers changed",
11221122
loop_count, num_changed
11231123
);
@@ -1126,7 +1126,7 @@ fn run(tcx: TyCtxt) {
11261126
break;
11271127
}
11281128
}
1129-
eprintln!("reached fixpoint in {} iterations", loop_count);
1129+
info!("reached fixpoint in {} iterations", loop_count);
11301130

11311131
// Do final processing on each function.
11321132
for &ldid in &all_fn_ldids {
@@ -1404,7 +1404,7 @@ fn run2<'tcx>(
14041404
assert!(i < 100);
14051405
func_reports.clear();
14061406
all_rewrites.clear();
1407-
eprintln!("\n--- start rewriting ---");
1407+
info!("--- start rewriting ---");
14081408

14091409
// Update non-rewritten items first. This has two purposes. First, it clears the
14101410
// `new_keys()` lists, which we check at the end of the loop to see whether we've reached a
@@ -1536,7 +1536,7 @@ fn run2<'tcx>(
15361536
let mut adt_reports = HashMap::<DefId, String>::new();
15371537
for &def_id in gacx.adt_metadata.table.keys() {
15381538
if gacx.foreign_mentioned_tys.contains(&def_id) {
1539-
eprintln!("Avoiding rewrite for foreign-mentioned type: {def_id:?}");
1539+
debug!("Avoiding rewrite for foreign-mentioned type: {def_id:?}");
15401540
continue;
15411541
}
15421542
if fixed_defs.contains(&def_id) {
@@ -1587,7 +1587,7 @@ fn run2<'tcx>(
15871587

15881588
// Print labeling and rewrites for the current function.
15891589

1590-
eprintln!("\nfinal labeling for {:?}:", name);
1590+
debug!("\nfinal labeling for {:?}:", name);
15911591
let lcx1 = crate::labeled_ty::LabeledTyCtxt::new(tcx);
15921592
let lcx2 = crate::labeled_ty::LabeledTyCtxt::new(tcx);
15931593
for (local, decl) in mir.local_decls.iter_enumerated() {
@@ -1602,12 +1602,11 @@ fn run2<'tcx>(
16021602
);
16031603
}
16041604

1605-
eprintln!("\ntype assignment for {:?}:", name);
1605+
debug!("\ntype assignment for {:?}:", name);
16061606
rewrite::dump_rewritten_local_tys(&acx, &asn, pointee_types, &mir, describe_local);
16071607

1608-
eprintln!();
16091608
if let Some(report) = func_reports.remove(&ldid) {
1610-
eprintln!("{}", report);
1609+
debug!("{}", report);
16111610
}
16121611

16131612
info.acx_data.set(acx.into_data());
@@ -1673,7 +1672,7 @@ fn run2<'tcx>(
16731672
}
16741673

16751674
// Print results for `static` items.
1676-
eprintln!("\nfinal labeling for static items:");
1675+
debug!("\nfinal labeling for static items:");
16771676
let lcx1 = crate::labeled_ty::LabeledTyCtxt::new(tcx);
16781677
let lcx2 = crate::labeled_ty::LabeledTyCtxt::new(tcx);
16791678
let mut static_dids = gacx.static_tys.keys().cloned().collect::<Vec<_>>();
@@ -1691,10 +1690,10 @@ fn run2<'tcx>(
16911690
&gasn.flags,
16921691
);
16931692
}
1694-
eprintln!("\n{statics_report}");
1693+
debug!("\n{statics_report}");
16951694

16961695
// Print results for ADTs and fields
1697-
eprintln!("\nfinal labeling for fields:");
1696+
debug!("\nfinal labeling for fields:");
16981697
let mut field_dids = gacx.field_ltys.keys().cloned().collect::<Vec<_>>();
16991698
field_dids.sort();
17001699
for did in field_dids {
@@ -1704,7 +1703,7 @@ fn run2<'tcx>(
17041703
if pid != PointerId::NONE {
17051704
let ty_perms = gasn.perms[pid];
17061705
let ty_flags = gasn.flags[pid];
1707-
eprintln!("{name:}: ({pid}) perms = {ty_perms:?}, flags = {ty_flags:?}");
1706+
debug!("{name:}: ({pid}) perms = {ty_perms:?}, flags = {ty_flags:?}");
17081707
}
17091708

17101709
// Emit annotations for fields
@@ -1739,7 +1738,7 @@ fn run2<'tcx>(
17391738
adt_dids.sort();
17401739
for did in adt_dids {
17411740
if let Some(report) = adt_reports.remove(&did) {
1742-
eprintln!("\n{}", report);
1741+
debug!("\n{}", report);
17431742
}
17441743
}
17451744

@@ -1778,17 +1777,17 @@ fn run2<'tcx>(
17781777
// ----------------------------------
17791778

17801779
// Report errors that were caught previously
1781-
eprintln!("\nerror details:");
1780+
debug!("\nerror details:");
17821781
for ldid in tcx.hir().body_owners() {
17831782
if let Some(detail) = gacx.fns_failed.get(&ldid.to_def_id()) {
17841783
if !detail.has_backtrace() {
17851784
continue;
17861785
}
1787-
eprintln!("\nerror in {:?}:\n{}", ldid, detail.to_string_full());
1786+
debug!("\nerror in {:?}:{}", ldid, detail.to_string_full());
17881787
}
17891788
}
17901789

1791-
eprintln!("\nerror summary:");
1790+
debug!("\nerror summary:");
17921791
fn sorted_def_ids(it: impl IntoIterator<Item = DefId>) -> Vec<DefId> {
17931792
let mut v = it.into_iter().collect::<Vec<_>>();
17941793
v.sort();
@@ -1802,27 +1801,27 @@ fn run2<'tcx>(
18021801
Some(detail) => detail.to_string_short(),
18031802
None => "(no panic)".into(),
18041803
};
1805-
eprintln!("analysis of {def_id:?} failed: {flags:?}, {detail_str}");
1804+
debug!("analysis of {def_id:?} failed: {flags:?}, {detail_str}");
18061805
}
18071806

18081807
for def_id in sorted_def_ids(gacx.dont_rewrite_statics.keys()) {
18091808
let flags = gacx.dont_rewrite_statics.get(def_id);
1810-
eprintln!("analysis of {def_id:?} failed: {flags:?}");
1809+
debug!("analysis of {def_id:?} failed: {flags:?}");
18111810
}
18121811

18131812
for def_id in sorted_def_ids(gacx.dont_rewrite_fields.keys()) {
18141813
let flags = gacx.dont_rewrite_fields.get(def_id);
1815-
eprintln!("analysis of {def_id:?} failed: {flags:?}");
1814+
debug!("analysis of {def_id:?} failed: {flags:?}");
18161815
}
18171816

1818-
eprintln!(
1817+
info!(
18191818
"\nsaw errors in {} / {} functions",
18201819
gacx.fns_failed.len(),
18211820
all_fn_ldids.len()
18221821
);
18231822

18241823
if !known_perm_error_fns.is_empty() {
1825-
eprintln!(
1824+
info!(
18261825
"saw permission errors in {} known fns",
18271826
known_perm_error_fns.len()
18281827
);
@@ -2184,7 +2183,7 @@ fn print_labeling_for_var<'tcx>(
21842183
perms[lty.label]
21852184
}
21862185
});
2187-
eprintln!("{}: addr_of = {:?}, type = {:?}", desc, addr_of1, ty1);
2186+
debug!("{}: addr_of = {:?}, type = {:?}", desc, addr_of1, ty1);
21882187

21892188
let addr_of2 = flags[addr_of_ptr];
21902189
let ty2 = lcx2.relabel(lty, &mut |lty| {
@@ -2194,14 +2193,14 @@ fn print_labeling_for_var<'tcx>(
21942193
flags[lty.label]
21952194
}
21962195
});
2197-
eprintln!(
2196+
debug!(
21982197
"{}: addr_of flags = {:?}, type flags = {:?}",
21992198
desc, addr_of2, ty2
22002199
);
22012200

22022201
let addr_of3 = addr_of_ptr;
22032202
let ty3 = lty;
2204-
eprintln!("{}: addr_of = {:?}, type = {:?}", desc, addr_of3, ty3);
2203+
debug!("{}: addr_of = {:?}, type = {:?}", desc, addr_of3, ty3);
22052204
}
22062205

22072206
fn print_function_pointee_types<'tcx>(
@@ -2210,9 +2209,9 @@ fn print_function_pointee_types<'tcx>(
22102209
mir: &Body<'tcx>,
22112210
pointee_types: PointerTable<PointeeTypes<'tcx>>,
22122211
) {
2213-
eprintln!("\npointee types for {}", name);
2212+
debug!("\npointee types for {}", name);
22142213
for (local, decl) in mir.local_decls.iter_enumerated() {
2215-
eprintln!(
2214+
debug!(
22162215
"{:?} ({}): addr_of = {:?}, type = {:?}",
22172216
local,
22182217
describe_local(acx.tcx(), decl),
@@ -2235,7 +2234,7 @@ fn print_function_pointee_types<'tcx>(
22352234
if tys.ltys.is_empty() && !tys.incomplete {
22362235
continue;
22372236
}
2238-
eprintln!(
2237+
debug!(
22392238
" pointer {:?}: {:?}{}",
22402239
ptr,
22412240
tys.ltys,

c2rust-analyze/src/borrowck/def_use.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::borrowck::atoms::{AllFacts, AtomMaps, Loan, Path, SubPoint};
2+
use log::debug;
23
use rustc_middle::mir::visit::{
34
MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor,
45
};
@@ -96,7 +97,7 @@ struct DefUseVisitor<'tcx, 'a> {
9697
impl<'tcx> Visitor<'tcx> for DefUseVisitor<'tcx, '_> {
9798
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
9899
self.super_place(place, context, location);
99-
eprintln!(
100+
debug!(
100101
"visit place {:?} with context {:?} = {:?} at {:?}",
101102
place,
102103
context,
@@ -132,7 +133,7 @@ impl<'tcx> Visitor<'tcx> for DefUseVisitor<'tcx, '_> {
132133
}
133134

134135
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
135-
eprintln!(
136+
debug!(
136137
"visit local {:?} with context {:?} = {:?} at {:?}",
137138
local,
138139
context,
@@ -157,7 +158,7 @@ impl<'tcx> Visitor<'tcx> for DefUseVisitor<'tcx, '_> {
157158

158159
fn visit_statement(&mut self, stmt: &Statement<'tcx>, location: Location) {
159160
self.super_statement(stmt, location);
160-
eprintln!("visit stmt {:?} at {:?}", stmt, location);
161+
debug!("visit stmt {:?} at {:?}", stmt, location);
161162

162163
if let StatementKind::StorageDead(local) = stmt.kind {
163164
// Observed: `StorageDead` emits `path_moved_at_base` at the `Mid` point.
@@ -194,7 +195,7 @@ impl<'tcx> LoanInvalidatedAtVisitor<'tcx, '_> {
194195
context: PlaceContext,
195196
location: Location,
196197
) {
197-
eprintln!(
198+
debug!(
198199
"access loan {:?} (kind {:?}) at location {:?} (context {:?} = {:?})",
199200
loan,
200201
borrow_kind,
@@ -223,7 +224,7 @@ impl<'tcx> LoanInvalidatedAtVisitor<'tcx, '_> {
223224
impl<'tcx> Visitor<'tcx> for LoanInvalidatedAtVisitor<'tcx, '_> {
224225
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
225226
//self.super_place(place, context, location);
226-
eprintln!(
227+
debug!(
227228
"loan_invalidated_at: visit place {:?} with context {:?} = {:?} at {:?}",
228229
place,
229230
context,
@@ -260,7 +261,7 @@ impl<'tcx> Visitor<'tcx> for LoanInvalidatedAtVisitor<'tcx, '_> {
260261
}
261262

262263
fn visit_local(&mut self, local: Local, context: PlaceContext, location: Location) {
263-
eprintln!(
264+
debug!(
264265
"loan_invalidated_at: visit local {:?} with context {:?} = {:?} at {:?}",
265266
local,
266267
context,

0 commit comments

Comments
 (0)