]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-demangler/main.rs
Rollup merge of #76082 - jyn514:top-level-links, r=ollie27,GuillaumeGomez
[rust.git] / src / tools / rust-demangler / main.rs
1 //! Demangles rustc mangled names.
2 //!
3 //! This tool uses https://crates.io/crates/rustc-demangle to convert an input buffer of
4 //! newline-separated mangled names into their demangled translations.
5 //!
6 //! This tool can be leveraged by other applications that support third-party demanglers.
7 //! It takes a list of mangled names (one per line) on standard input, and prints a corresponding
8 //! list of demangled names. The tool is designed to support other programs that can leverage a
9 //! third-party demangler, such as `llvm-cov`, via the `-Xdemangler=<path-to-demangler>` option.
10 //!
11 //! To use `rust-demangler`, first build the tool with:
12 //!
13 //! ```shell
14 //! $ ./x.py build rust-demangler
15 //! ```
16 //!
17 //! Then, with `llvm-cov` for example, add the `-Xdemangler=...` option:
18 //!
19 //! ```shell
20 //! $ TARGET="${PWD}/build/x86_64-unknown-linux-gnu"
21 //! $ "${TARGET}"/llvm/bin/llvm-cov show --Xdemangler="${TARGET}"/stage0-tools-bin/rust-demangler \
22 //!   --instr-profile=main.profdata ./main --show-line-counts-or-regions
23 //! ```
24
25 use rustc_demangle::demangle;
26 use std::io::{self, Read, Write};
27
28 fn main() -> io::Result<()> {
29     let mut buffer = String::new();
30     io::stdin().read_to_string(&mut buffer)?;
31     let lines = buffer.lines();
32     let mut demangled = Vec::new();
33     for mangled in lines {
34         demangled.push(demangle(mangled).to_string());
35     }
36     demangled.push("".to_string());
37     io::stdout().write_all(demangled.join("\n").as_bytes())?;
38     Ok(())
39 }