Skip to content
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

Add a -s/--silent option to silence user-facing messages #362

Merged
merged 2 commits into from
Jan 15, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ Show the code rustc generates for any function
Strip redundant labels entirely
- **`-v`**, **`--verbose`** —
more verbose output, can be specified multiple times
- **`-s`**, **`--silent`** —
print less user-forward information to make consumption by tools easier
- **` --simplify`** —
Try to strip some of the non-assembly instruction information
- **` --include-constants`** —
Expand Down
4 changes: 3 additions & 1 deletion src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ fn load_rust_sources(
}
let sources = std::fs::read_to_string(&filepath).expect("Can't read a file");
if sources.is_empty() {
safeprintln!("Ignoring empty file {filepath:?}!");
if fmt.verbosity > 0 {
safeprintln!("Ignoring empty file {filepath:?}!");
}
(path, None)
} else {
if fmt.verbosity > 2 {
Expand Down
2 changes: 1 addition & 1 deletion src/disasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn dump_slices(
}

let insns = cs.disasm_all(code, addr as u64)?;
if insns.is_empty() {
if insns.is_empty() && fmt.verbosity > 0 {
safeprintln!("No instructions - empty code block?");
}

Expand Down
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ pub struct Item {

pub fn suggest_name<'a>(
search: &str,
name_display: &NameDisplay,
fmt: &Format,
items: impl IntoIterator<Item = &'a Item>,
) -> ! {
let mut count = 0usize;
let names: BTreeMap<&String, Vec<usize>> =
items.into_iter().fold(BTreeMap::new(), |mut m, item| {
count += 1;
let entry = match name_display {
let entry = match fmt.name_display {
NameDisplay::Full => &item.hashed,
NameDisplay::Short => &item.name,
NameDisplay::Mangled => &item.mangled_name,
Expand All @@ -123,15 +123,19 @@ pub fn suggest_name<'a>(
m
});

if names.is_empty() {
if search.is_empty() {
safeprintln!("This target defines no functions (or cargo-show-asm can't find them)");
if fmt.verbosity > 0 {
if names.is_empty() {
if search.is_empty() {
safeprintln!(
"This target defines no functions (or cargo-show-asm can't find them)"
);
} else {
safeprintln!("No matching functions, try relaxing your search request");
}
safeprintln!("You can pass --everything to see the demangled contents of a file");
} else {
safeprintln!("No matching functions, try relaxing your search request");
safeprintln!("Try one of those by name or a sequence number");
}
safeprintln!("You can pass --everything to see the demangled contents of a file");
} else {
safeprintln!("Try one of those by name or a sequence number");
}

#[allow(clippy::cast_sign_loss)]
Expand Down Expand Up @@ -170,7 +174,7 @@ pub fn pick_dump_item<K: Clone>(
Some(range.clone())
} else {
let actual = items.len();
safeprintln!("You asked to display item #{value} (zero based), but there's only {actual} items");
esafeprintln!("You asked to display item #{value} (zero based), but there's only {actual} items");
std::process::exit(1);
}
}
Expand All @@ -192,13 +196,13 @@ pub fn pick_dump_item<K: Clone>(
range.1.clone()
} else if let Some(value) = nth {
let filtered = filtered.len();
safeprintln!("You asked to display item #{value} (zero based), but there's only {filtered} matching items");
esafeprintln!("You asked to display item #{value} (zero based), but there's only {filtered} matching items");
std::process::exit(1);
} else {
if filtered.is_empty() {
safeprintln!("Can't find any items matching {function:?}");
esafeprintln!("Can't find any items matching {function:?}");
} else {
suggest_name(&function, &fmt.name_display, filtered.iter().map(|x| x.0));
suggest_name(&function, &fmt, filtered.iter().map(|x| x.0));
}
std::process::exit(1);
};
Expand All @@ -213,7 +217,7 @@ pub fn pick_dump_item<K: Clone>(
} else {
// Otherwise, print suggestions and exit
let items = items.keys();
suggest_name("", &fmt.name_display, items);
suggest_name("", &fmt, items);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn spawn_cargo(
"--color",
if format.color { "always" } else { "never" },
])
.args(std::iter::repeat("-v").take(format.verbosity))
.args(std::iter::repeat("-v").take(format.verbosity.saturating_sub(1)))
// Workspace location.
.arg("--manifest-path")
.arg(&cargo.manifest_path)
Expand Down
12 changes: 9 additions & 3 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,19 @@ pub enum CompileMode {
}

fn verbosity() -> impl Parser<usize> {
short('v')
let verbose = short('v')
.long("verbose")
.help("more verbose output, can be specified multiple times")
.req_flag(())
.many()
.map(|v| v.len())
.hide_usage()
.count();
let silent = short('s')
.long("silent")
.help("print less user-forward information to make consumption by tools easier")
.req_flag(())
.hide_usage()
.count();
construct!(verbose, silent).map(|(v, q)| (v + 1).saturating_sub(q))
}

fn manifest_path() -> impl Parser<PathBuf> {
Expand Down
Loading