Skip to content

Commit e0761a5

Browse files
committed
Improve macro-stats printing.
By allowing long names to overlap with the "Uses" field when it has spare space. This avoids unnecessary line breaks in the output.
1 parent 25a6fd3 commit e0761a5

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,20 +341,30 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
341341
}
342342
for (bytes, lines, uses, name, kind) in macro_stats {
343343
let mut name = ExpnKind::Macro(kind, *name).descr();
344+
let uses_with_underscores = thousands::usize_with_underscores(uses);
344345
let avg_lines = lines as f64 / uses as f64;
345346
let avg_bytes = bytes as f64 / uses as f64;
346-
if name.len() >= name_w {
347-
// If the name is long, print it on a line by itself, then
348-
// set the name to empty and print things normally, to show the
349-
// stats on the next line.
347+
348+
// Ensure the "Macro Name" and "Uses" columns are as compact as possible.
349+
let mut uses_w = uses_w;
350+
if name.len() + uses_with_underscores.len() >= name_w + uses_w {
351+
// The name would abut or overlap the uses value. Print the name
352+
// on a line by itself, then set the name to empty and print things
353+
// normally, to show the stats on the next line.
350354
_ = writeln!(s, "{prefix} {:<name_w$}", name);
351355
name = String::new();
352-
}
356+
} else if name.len() >= name_w {
357+
// The name won't abut or overlap with the uses value, but it does
358+
// overlap with the empty part of the uses column. Shrink the width
359+
// of the uses column to account for the excess name length.
360+
uses_w = uses_with_underscores.len() + 1
361+
};
362+
353363
_ = writeln!(
354364
s,
355365
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
356366
name,
357-
thousands::usize_with_underscores(uses),
367+
uses_with_underscores,
358368
thousands::usize_with_underscores(lines),
359369
thousands::f64p1_with_underscores(avg_lines),
360370
thousands::usize_with_underscores(bytes),

tests/ui/stats/macro-stats.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ macro-stats trait_tys! 1 2 2.0
2222
macro-stats n99! 2 2 1.0 4 2.0
2323
macro-stats none! 1 1 1.0 4 4.0
2424
macro-stats u32! 1 1 1.0 3 3.0
25-
macro-stats long_name_that_fits_on_a_single_line!
26-
macro-stats 1 1 1.0 0 0.0
25+
macro-stats long_name_that_fits_on_a_single_line! 1 1 1.0 0 0.0
2726
macro-stats #[test] 1 1 1.0 0 0.0
2827
macro-stats ===================================================================================

0 commit comments

Comments
 (0)