Debian Codegraph @

examples/cppfilt.rs

raw symbols SCIP
1 // For clippy.
2 #![allow(unknown_lints)]
3
4 extern crate cpp_demangle;
5
6 // For command line integration
7 extern crate clap;
8
9 use clap::Parser;
10 use cpp_demangle::{BorrowedSymbol, DemangleOptions};
11 use std::io::{self, BufRead, Cursor, Write};
12 use std::process;
13
14 /// Find the index of the first (potential) occurrence of a mangled C++ symbol
15 /// in the given `haystack`.
16 fn find_mangled(haystack: &[u8]) -> Option<usize> {
17 if haystack.is_empty() {
18 return None;
19 }
20
21 for i in 0..haystack.len() - 1 {
22 if haystack[i] == b'_' {
23 match (
24 haystack[i + 1],
25 haystack.get(i + 2),
26 haystack.get(i + 3),
27 haystack.get(i + 4),
28 ) {
29 (b'Z', _, _, _) | (b'_', Some(b'Z'), _, _) | (b'_', Some(b'_'), Some(b'Z'), _) => {
30 return Some(i)
31 }
32 (b'_', Some(b'_'), Some(b'_'), Some(b'Z')) => return Some(i),
33 _ => (),
34 }
35 }
36 }
37
38 None
39 }
40
41 /// Print the given `line` to `out`, with all mangled C++ symbols replaced with
42 /// their demangled form.
43 fn demangle_line<W>(out: &mut W, line: &[u8], options: DemangleOptions) -> io::Result<()>
44 where
45 W: Write,
46 {
47 let mut line = line;
48
49 while let Some(idx) = find_mangled(line) {
50 write!(out, "{}", String::from_utf8_lossy(&line[..idx]))?;
51
52 if let Ok((sym, tail)) = BorrowedSymbol::with_tail(&line[idx..]) {
53 let demangled = sym
54 .demangle_with_options(&options)
55 .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
56 write!(out, "{}", demangled)?;
57 line = tail;
58 } else {
59 write!(out, "_Z")?;
60 line = &line[2..];
61 }
62 }
63
64 write!(out, "{}", String::from_utf8_lossy(line))
65 }
66
67 /// Print all the lines from the given `input` to `out`, with all mangled C++
68 /// symbols replaced with their demangled form.
69 fn demangle_all<R, W>(input: &mut R, out: &mut W, options: DemangleOptions) -> io::Result<()>
70 where
71 R: BufRead,
72 W: Write,
73 {
74 let mut buf = vec![];
75
76 while input.read_until(b'\n', &mut buf)? > 0 {
77 let nl = buf.ends_with(&[b'\n']);
78 if nl {
79 buf.pop();
80 }
81 demangle_line(out, &buf[..], options)?;
82 if nl {
83 write!(out, "\n")?;
84 }
85 buf.clear();
86 }
87
88 Ok(())
89 }
90
91 /// A c++filt clone as an example of how to use the cpp_demangle crate!
92 #[derive(Parser)]
93 #[clap(version, author)]
94 struct Cli {
95 #[clap(short = 'p', long)]
96 /// Do not display function arguments.
97 no_params: bool,
98 /// Do not display function return types.
99 #[clap(long)]
100 no_return_type: bool,
101 #[clap(long)]
102 /// Hide types in template parameter expression literals
103 hide_expression_literal_types: bool,
104 mangled_names: Vec<String>,
105 }
106
107 fn main() {
108 let cli = Cli::parse();
109
110 let stdin = io::stdin();
111 let mut stdin = stdin.lock();
112
113 let stdout = io::stdout();
114 let mut stdout = stdout.lock();
115
116 let stderr = io::stderr();
117 let mut stderr = stderr.lock();
118
119 let mut options = DemangleOptions::new();
120 if cli.no_params {
121 options = options.no_params();
122 }
123 if cli.hide_expression_literal_types {
124 options = options.hide_expression_literal_types();
125 }
126 if cli.no_return_type {
127 options = options.no_return_type();
128 }
129
130 let demangle_result = if !cli.mangled_names.is_empty() {
131 let mut input = Cursor::new(cli.mangled_names.into_iter().fold(
132 String::new(),
133 |mut accumulated, name| {
134 accumulated.push_str(&name);
135 accumulated.push_str("\n");
136 accumulated
137 },
138 ));
139 demangle_all(&mut input, &mut stdout, options)
140 } else {
141 demangle_all(&mut stdin, &mut stdout, options)
142 };
143
144 let code = match demangle_result {
145 Ok(_) => 0,
146 Err(e) => {
147 let _ = writeln!(&mut stderr, "error: {}", e);
148 1
149 }
150 };
151
152 process::exit(code);
153 }