Skip to content

Commit 172fc60

Browse files
authored
Rollup merge of #143223 - nnethercote:improve-macro-stats-printing, r=petrochenkov
Improve macro stats printing Fix a small formatting issue that has been annoying me. r? ``@petrochenkov``
2 parents d3c02b4 + e0761a5 commit 172fc60

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
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.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ fn opt(x: Option<u32>) {
4949
}
5050
}
5151

52-
macro_rules! this_is_a_really_really_long_macro_name {
52+
macro_rules! long_name_that_fits_on_a_single_line {
53+
() => {}
54+
}
55+
long_name_that_fits_on_a_single_line!();
56+
57+
macro_rules! long_name_that_doesnt_fit_on_one_line {
5358
($t:ty) => {
5459
fn f(_: $t) {}
5560
}
5661
}
57-
this_is_a_really_really_long_macro_name!(u32!()); // AstFragmentKind::{Items,Ty}
62+
long_name_that_doesnt_fit_on_one_line!(u32!()); // AstFragmentKind::{Items,Ty}
5863

5964
macro_rules! trait_tys {
6065
() => {

tests/ui/stats/macro-stats.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ macro-stats #[derive(Copy)] 1 2 2.0
1515
macro-stats p! 1 3 3.0 32 32.0
1616
macro-stats trait_impl_tys! 1 2 2.0 28 28.0
1717
macro-stats foreign_item! 1 1 1.0 21 21.0
18-
macro-stats this_is_a_really_really_long_macro_name!
18+
macro-stats long_name_that_doesnt_fit_on_one_line!
1919
macro-stats 1 1 1.0 18 18.0
2020
macro-stats impl_const! 1 1 1.0 17 17.0
2121
macro-stats trait_tys! 1 2 2.0 15 15.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! 1 1 1.0 0 0.0
2526
macro-stats #[test] 1 1 1.0 0 0.0
2627
macro-stats ===================================================================================

0 commit comments

Comments
 (0)