Skip to content

Improve macro stats printing #143223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,30 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
}
for (bytes, lines, uses, name, kind) in macro_stats {
let mut name = ExpnKind::Macro(kind, *name).descr();
let uses_with_underscores = thousands::usize_with_underscores(uses);
let avg_lines = lines as f64 / uses as f64;
let avg_bytes = bytes as f64 / uses as f64;
if name.len() >= name_w {
// If the name is long, print it on a line by itself, then
// set the name to empty and print things normally, to show the
// stats on the next line.

// Ensure the "Macro Name" and "Uses" columns are as compact as possible.
let mut uses_w = uses_w;
if name.len() + uses_with_underscores.len() >= name_w + uses_w {
// The name would abut or overlap the uses value. Print the name
// on a line by itself, then set the name to empty and print things
// normally, to show the stats on the next line.
_ = writeln!(s, "{prefix} {:<name_w$}", name);
name = String::new();
}
} else if name.len() >= name_w {
// The name won't abut or overlap with the uses value, but it does
// overlap with the empty part of the uses column. Shrink the width
// of the uses column to account for the excess name length.
uses_w = uses_with_underscores.len() + 1
};

_ = writeln!(
s,
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
name,
thousands::usize_with_underscores(uses),
uses_with_underscores,
thousands::usize_with_underscores(lines),
thousands::f64p1_with_underscores(avg_lines),
thousands::usize_with_underscores(bytes),
Expand Down
9 changes: 7 additions & 2 deletions tests/ui/stats/macro-stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ fn opt(x: Option<u32>) {
}
}

macro_rules! this_is_a_really_really_long_macro_name {
macro_rules! long_name_that_fits_on_a_single_line {
() => {}
}
long_name_that_fits_on_a_single_line!();

macro_rules! long_name_that_doesnt_fit_on_one_line {
($t:ty) => {
fn f(_: $t) {}
}
}
this_is_a_really_really_long_macro_name!(u32!()); // AstFragmentKind::{Items,Ty}
long_name_that_doesnt_fit_on_one_line!(u32!()); // AstFragmentKind::{Items,Ty}

macro_rules! trait_tys {
() => {
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/stats/macro-stats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ macro-stats #[derive(Copy)] 1 2 2.0
macro-stats p! 1 3 3.0 32 32.0
macro-stats trait_impl_tys! 1 2 2.0 28 28.0
macro-stats foreign_item! 1 1 1.0 21 21.0
macro-stats this_is_a_really_really_long_macro_name!
macro-stats long_name_that_doesnt_fit_on_one_line!
macro-stats 1 1 1.0 18 18.0
macro-stats impl_const! 1 1 1.0 17 17.0
macro-stats trait_tys! 1 2 2.0 15 15.0
macro-stats n99! 2 2 1.0 4 2.0
macro-stats none! 1 1 1.0 4 4.0
macro-stats u32! 1 1 1.0 3 3.0
macro-stats long_name_that_fits_on_a_single_line! 1 1 1.0 0 0.0
macro-stats #[test] 1 1 1.0 0 0.0
macro-stats ===================================================================================
Loading