Add an alternate --demangle mode to coverage-dump

The coverage-dump tool already needs `rustc_demangle` for its own purposes, so
the amount of extra code needed for a demangle mode is very small.
This commit is contained in:
Zalathar 2024-05-30 16:06:14 +10:00
parent bf8fff783f
commit 9abfebdf1e
2 changed files with 24 additions and 0 deletions

View file

@ -6,3 +6,8 @@ The output format is mostly arbitrary, so it's OK to change the output as long
as any affected tests are also re-blessed. However, the output should be
consistent across different executions on different platforms, so avoid
printing any information that is platform-specific or non-deterministic.
## Demangle mode
When run as `coverage-dump --demangle`, this tool instead functions as a
command-line demangler that can be invoked by `llvm-cov`.

View file

@ -7,6 +7,13 @@ fn main() -> anyhow::Result<()> {
let args = std::env::args().collect::<Vec<_>>();
// The coverage-dump tool already needs `rustc_demangle` in order to read
// coverage metadata, so it's very easy to also have a separate mode that
// turns it into a command-line demangler for use by coverage-run tests.
if &args[1..] == &["--demangle"] {
return demangle();
}
let llvm_ir_path = args.get(1).context("LLVM IR file not specified")?;
let llvm_ir = std::fs::read_to_string(llvm_ir_path).context("couldn't read LLVM IR file")?;
@ -15,3 +22,15 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
fn demangle() -> anyhow::Result<()> {
use std::fmt::Write as _;
let stdin = std::io::read_to_string(std::io::stdin())?;
let mut output = String::with_capacity(stdin.len());
for line in stdin.lines() {
writeln!(output, "{:#}", rustc_demangle::demangle(line))?;
}
print!("{output}");
Ok(())
}