Skip to content

Commit 18d2e23

Browse files
committed
Merge pull request #228 from pacak/rc-0.2.24
Release 0.2.24
2 parents 2a0cc57 + 5ddf259 commit 18d2e23

File tree

8 files changed

+67
-35
lines changed

8 files changed

+67
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-show-asm"
3-
version = "0.2.23"
3+
version = "0.2.24"
44
edition = "2021"
55
description = "A cargo subcommand that displays the generated assembly of Rust source code."
66
categories = ["development-tools::cargo-plugins", "development-tools::debugging" ]

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [0.2.24] - 2023-12-28
4+
- add an option to keep mangled name, thanks to @osiewicz
5+
- add syntax highlight for mangled names
6+
- bump dependencies
7+
38
## [0.2.23] - 2023-11-26
49
- Add an option to strip blank lines and make it default, original behavior is accessible
510
with `-B` option

src/asm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::cached_lines::CachedLines;
44
use crate::demangle::LabelKind;
55
use crate::{color, demangle, esafeprintln, get_dump_range, safeprintln, Item};
66
// TODO, use https://sourceware.org/binutils/docs/as/index.html
7-
use crate::opts::{Format, RedundantLabels, ToDump};
7+
use crate::opts::{Format, NameDisplay, RedundantLabels, ToDump};
88

99
mod statements;
1010

@@ -275,9 +275,9 @@ pub fn dump_range(
275275

276276
empty_line = false;
277277
match fmt.name_display {
278-
crate::opts::NameDisplay::Full => safeprintln!("{line:#}"),
279-
crate::opts::NameDisplay::Short => safeprintln!("{line}"),
280-
crate::opts::NameDisplay::Mangled => safeprintln!("{line:-}"),
278+
NameDisplay::Full => safeprintln!("{line:#}"),
279+
NameDisplay::Short => safeprintln!("{line}"),
280+
NameDisplay::Mangled => safeprintln!("{line:-}"),
281281
}
282282
}
283283
}

src/asm/statements.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use nom::{AsChar, IResult};
1212
use owo_colors::OwoColorize;
1313

1414
use crate::demangle::LabelKind;
15+
use crate::opts::NameDisplay;
1516
use crate::{color, demangle};
1617

1718
#[derive(Clone, Debug)]
@@ -51,18 +52,14 @@ impl<'a> Instruction<'a> {
5152

5253
impl std::fmt::Display for Instruction<'_> {
5354
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55+
let display = NameDisplay::from(&*f);
5456
if self.op.starts_with("#DEBUG_VALUE:") {
5557
write!(f, "{}", color!(self.op, OwoColorize::blue))?;
5658
} else {
5759
write!(f, "{}", color!(self.op, OwoColorize::bright_blue))?;
5860
}
5961
if let Some(args) = self.args {
60-
let args = if f.sign_minus() {
61-
// Do not demangle
62-
Cow::from(args)
63-
} else {
64-
demangle::contents(args, f.alternate())
65-
};
62+
let args = demangle::contents(args, display);
6663
let w_label = demangle::color_local_labels(&args);
6764
let w_comment = demangle::color_comment(&w_label);
6865
write!(f, " {w_comment}")?;
@@ -99,6 +96,7 @@ impl std::fmt::Display for Statement<'_> {
9996

10097
impl std::fmt::Display for Directive<'_> {
10198
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99+
let display = NameDisplay::from(&*f);
102100
match self {
103101
Directive::File(ff) => ff.fmt(f),
104102
Directive::Loc(l) => l.fmt(f),
@@ -107,7 +105,7 @@ impl std::fmt::Display for Directive<'_> {
107105
f.write_str(&format!(".set {}", color!(g, OwoColorize::bright_black)))
108106
}
109107
Directive::SectionStart(s) => {
110-
let dem = demangle::contents(s, f.alternate());
108+
let dem = demangle::contents(s, display);
111109
f.write_str(&format!(
112110
"{} {}",
113111
color!(".section", OwoColorize::bright_black),
@@ -140,11 +138,12 @@ impl std::fmt::Display for File<'_> {
140138

141139
impl std::fmt::Display for GenericDirective<'_> {
142140
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141+
let display = NameDisplay::from(&*f);
143142
write!(
144143
f,
145144
"\t.{}",
146145
color!(
147-
demangle::contents(self.0, f.alternate()),
146+
demangle::contents(self.0, display),
148147
OwoColorize::bright_black
149148
)
150149
)
@@ -172,13 +171,26 @@ impl std::fmt::Display for Loc<'_> {
172171
}
173172
}
174173

174+
impl From<&std::fmt::Formatter<'_>> for NameDisplay {
175+
fn from(f: &std::fmt::Formatter) -> Self {
176+
if f.sign_minus() {
177+
NameDisplay::Mangled
178+
} else if f.alternate() {
179+
NameDisplay::Full
180+
} else {
181+
NameDisplay::Short
182+
}
183+
}
184+
}
185+
175186
impl std::fmt::Display for Label<'_> {
176187
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188+
let display = NameDisplay::from(&*f);
177189
write!(
178190
f,
179191
"{}:",
180192
color!(
181-
demangle::contents(self.id, f.alternate()),
193+
demangle::contents(self.id, display),
182194
OwoColorize::bright_black
183195
)
184196
)

src/demangle.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::color;
1+
use crate::{color, opts::NameDisplay};
22
use once_cell::sync::Lazy;
33
use owo_colors::OwoColorize;
44
use regex::{Regex, RegexSet, Replacer};
@@ -102,16 +102,22 @@ pub fn color_comment(input: &str) -> Cow<'_, str> {
102102
}
103103

104104
struct Demangler {
105-
full_name: bool,
105+
display: NameDisplay,
106106
}
107107
impl Replacer for Demangler {
108108
fn replace_append(&mut self, cap: &regex::Captures<'_>, dst: &mut std::string::String) {
109109
if let Ok(dem) = rustc_demangle::try_demangle(&cap[1]) {
110110
use std::fmt::Write;
111-
if self.full_name {
112-
write!(dst, "{:?}", color!(dem, OwoColorize::green)).unwrap();
113-
} else {
114-
write!(dst, "{:#?}", color!(dem, OwoColorize::green)).unwrap();
111+
match self.display {
112+
NameDisplay::Full => {
113+
write!(dst, "{:?}", color!(dem, OwoColorize::green)).unwrap();
114+
}
115+
NameDisplay::Short => {
116+
write!(dst, "{:#?}", color!(dem, OwoColorize::green)).unwrap();
117+
}
118+
NameDisplay::Mangled => {
119+
write!(dst, "{}", color!(&cap[1], OwoColorize::green)).unwrap();
120+
}
115121
}
116122
} else {
117123
dst.push_str(&cap[0]);
@@ -120,14 +126,16 @@ impl Replacer for Demangler {
120126
}
121127

122128
#[must_use]
123-
pub fn contents(input: &str, full_name: bool) -> Cow<'_, str> {
124-
GLOBAL_LABELS.replace_all(input, Demangler { full_name })
129+
pub fn contents(input: &str, display: NameDisplay) -> Cow<'_, str> {
130+
GLOBAL_LABELS.replace_all(input, Demangler { display })
125131
}
126132

127133
#[cfg(test)]
128134
mod test {
129135
use owo_colors::set_override;
130136

137+
use crate::opts::NameDisplay;
138+
131139
use super::{contents, name};
132140
const MAC: &str =
133141
"__ZN58_$LT$nom..error..ErrorKind$u20$as$u20$core..fmt..Debug$GT$3fmt17hb98704099c11c31fE";
@@ -146,10 +154,20 @@ mod test {
146154
assert!(name(MAC).is_some());
147155
}
148156

157+
#[test]
158+
fn linux_no_demangle_call() {
159+
set_override(true);
160+
let x = contents(CALL_L, NameDisplay::Mangled);
161+
assert_eq!(
162+
"[rip + \u{1b}[32m_ZN58_$LT$nom..error..ErrorKind$u20$as$u20$core..fmt..Debug$GT$3fmt17hb98704099c11c31fE\u{1b}[39m]",
163+
x
164+
);
165+
}
166+
149167
#[test]
150168
fn linux_demangle_call() {
151169
set_override(true);
152-
let x = contents(CALL_L, false);
170+
let x = contents(CALL_L, NameDisplay::Short);
153171
assert_eq!(
154172
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt\u{1b}[39m]",
155173
x
@@ -159,7 +177,7 @@ mod test {
159177
#[test]
160178
fn mac_demangle_call() {
161179
set_override(true);
162-
let x = contents(CALL_M, false);
180+
let x = contents(CALL_M, NameDisplay::Short);
163181
assert_eq!(
164182
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt\u{1b}[39m]",
165183
x
@@ -169,7 +187,7 @@ mod test {
169187
#[test]
170188
fn mac_demangle_call2() {
171189
set_override(true);
172-
let x = contents(CALL_M, true);
190+
let x = contents(CALL_M, NameDisplay::Full);
173191
assert_eq!(
174192
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt::hb98704099c11c31f\u{1b}[39m]",
175193
x

src/llvm.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
color,
99
demangle::{self, contents},
1010
get_dump_range,
11-
opts::{Format, NameDisplay, ToDump},
11+
opts::{Format, ToDump},
1212
safeprintln, Item,
1313
};
1414
use std::{
@@ -90,7 +90,7 @@ fn dump_range(fmt: &Format, strings: &[&str]) {
9090
if line.starts_with("; ") {
9191
safeprintln!("{}", color!(line, OwoColorize::bright_black));
9292
} else {
93-
let line = demangle::contents(line, fmt.name_display == NameDisplay::Full);
93+
let line = demangle::contents(line, fmt.name_display);
9494
safeprintln!("{line}");
9595
}
9696
}
@@ -168,10 +168,7 @@ pub fn collect_or_dump(
168168
if seen {
169169
safeprintln!("{}", color!(name, OwoColorize::cyan));
170170
safeprintln!("{}", color!(attrs, OwoColorize::cyan));
171-
safeprintln!(
172-
"{}",
173-
contents(&line, fmt.name_display == NameDisplay::Full)
174-
);
171+
safeprintln!("{}", contents(&line, fmt.name_display));
175172
}
176173
} else {
177174
state = State::Skipping;
@@ -182,7 +179,7 @@ pub fn collect_or_dump(
182179
}
183180
State::Define => {
184181
if seen {
185-
safeprintln!("{}", contents(&line, fmt.name_display == NameDisplay::Full));
182+
safeprintln!("{}", contents(&line, fmt.name_display));
186183
}
187184
if line == "}" {
188185
if let Some(mut cur) = current_item.take() {

src/mca.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use crate::{
88
demangle, esafeprintln, get_dump_range,
9-
opts::{Format, NameDisplay, ToDump},
9+
opts::{Format, ToDump},
1010
safeprintln,
1111
};
1212

@@ -87,7 +87,7 @@ pub fn dump_function(
8787

8888
for line in BufRead::lines(BufReader::new(o)) {
8989
let line = line?;
90-
let line = demangle::contents(&line, fmt.name_display == NameDisplay::Full);
90+
let line = demangle::contents(&line, fmt.name_display);
9191
safeprintln!("{line}");
9292
}
9393

0 commit comments

Comments
 (0)