Skip to content

Commit

Permalink
Merge pull request #228 from pacak/rc-0.2.24
Browse files Browse the repository at this point in the history
Release 0.2.24
  • Loading branch information
pacak committed Dec 28, 2023
2 parents a38aca5 + f0a9a42 commit 8955190
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-show-asm"
version = "0.2.23"
version = "0.2.24"
edition = "2021"
description = "A cargo subcommand that displays the generated assembly of Rust source code."
categories = ["development-tools::cargo-plugins", "development-tools::debugging" ]
Expand Down
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [0.2.24] - 2023-12-28
- add an option to keep mangled name, thanks to @osiewicz
- add syntax highlight for mangled names
- bump dependencies

## [0.2.23] - 2023-11-26
- Add an option to strip blank lines and make it default, original behavior is accessible
with `-B` option
Expand Down
8 changes: 4 additions & 4 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::cached_lines::CachedLines;
use crate::demangle::LabelKind;
use crate::{color, demangle, esafeprintln, get_dump_range, safeprintln, Item};
// TODO, use https://sourceware.org/binutils/docs/as/index.html
use crate::opts::{Format, RedundantLabels, ToDump};
use crate::opts::{Format, NameDisplay, RedundantLabels, ToDump};

mod statements;

Expand Down Expand Up @@ -275,9 +275,9 @@ pub fn dump_range(

empty_line = false;
match fmt.name_display {
crate::opts::NameDisplay::Full => safeprintln!("{line:#}"),
crate::opts::NameDisplay::Short => safeprintln!("{line}"),
crate::opts::NameDisplay::Mangled => safeprintln!("{line:-}"),
NameDisplay::Full => safeprintln!("{line:#}"),
NameDisplay::Short => safeprintln!("{line}"),
NameDisplay::Mangled => safeprintln!("{line:-}"),
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions src/asm/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nom::{AsChar, IResult};
use owo_colors::OwoColorize;

use crate::demangle::LabelKind;
use crate::opts::NameDisplay;
use crate::{color, demangle};

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

impl std::fmt::Display for Instruction<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = NameDisplay::from(&*f);
if self.op.starts_with("#DEBUG_VALUE:") {
write!(f, "{}", color!(self.op, OwoColorize::blue))?;
} else {
write!(f, "{}", color!(self.op, OwoColorize::bright_blue))?;
}
if let Some(args) = self.args {
let args = if f.sign_minus() {
// Do not demangle
Cow::from(args)
} else {
demangle::contents(args, f.alternate())
};
let args = demangle::contents(args, display);
let w_label = demangle::color_local_labels(&args);
let w_comment = demangle::color_comment(&w_label);
write!(f, " {w_comment}")?;
Expand Down Expand Up @@ -99,6 +96,7 @@ impl std::fmt::Display for Statement<'_> {

impl std::fmt::Display for Directive<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = NameDisplay::from(&*f);
match self {
Directive::File(ff) => ff.fmt(f),
Directive::Loc(l) => l.fmt(f),
Expand All @@ -107,7 +105,7 @@ impl std::fmt::Display for Directive<'_> {
f.write_str(&format!(".set {}", color!(g, OwoColorize::bright_black)))
}
Directive::SectionStart(s) => {
let dem = demangle::contents(s, f.alternate());
let dem = demangle::contents(s, display);
f.write_str(&format!(
"{} {}",
color!(".section", OwoColorize::bright_black),
Expand Down Expand Up @@ -140,11 +138,12 @@ impl std::fmt::Display for File<'_> {

impl std::fmt::Display for GenericDirective<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = NameDisplay::from(&*f);
write!(
f,
"\t.{}",
color!(
demangle::contents(self.0, f.alternate()),
demangle::contents(self.0, display),
OwoColorize::bright_black
)
)
Expand Down Expand Up @@ -172,13 +171,26 @@ impl std::fmt::Display for Loc<'_> {
}
}

impl From<&std::fmt::Formatter<'_>> for NameDisplay {
fn from(f: &std::fmt::Formatter) -> Self {
if f.sign_minus() {
NameDisplay::Mangled
} else if f.alternate() {
NameDisplay::Full
} else {
NameDisplay::Short
}
}
}

impl std::fmt::Display for Label<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = NameDisplay::from(&*f);
write!(
f,
"{}:",
color!(
demangle::contents(self.id, f.alternate()),
demangle::contents(self.id, display),
OwoColorize::bright_black
)
)
Expand Down
40 changes: 29 additions & 11 deletions src/demangle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::color;
use crate::{color, opts::NameDisplay};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use regex::{Regex, RegexSet, Replacer};
Expand Down Expand Up @@ -102,16 +102,22 @@ pub fn color_comment(input: &str) -> Cow<'_, str> {
}

struct Demangler {
full_name: bool,
display: NameDisplay,
}
impl Replacer for Demangler {
fn replace_append(&mut self, cap: &regex::Captures<'_>, dst: &mut std::string::String) {
if let Ok(dem) = rustc_demangle::try_demangle(&cap[1]) {
use std::fmt::Write;
if self.full_name {
write!(dst, "{:?}", color!(dem, OwoColorize::green)).unwrap();
} else {
write!(dst, "{:#?}", color!(dem, OwoColorize::green)).unwrap();
match self.display {
NameDisplay::Full => {
write!(dst, "{:?}", color!(dem, OwoColorize::green)).unwrap();
}
NameDisplay::Short => {
write!(dst, "{:#?}", color!(dem, OwoColorize::green)).unwrap();
}
NameDisplay::Mangled => {
write!(dst, "{}", color!(&cap[1], OwoColorize::green)).unwrap();
}
}
} else {
dst.push_str(&cap[0]);
Expand All @@ -120,14 +126,16 @@ impl Replacer for Demangler {
}

#[must_use]
pub fn contents(input: &str, full_name: bool) -> Cow<'_, str> {
GLOBAL_LABELS.replace_all(input, Demangler { full_name })
pub fn contents(input: &str, display: NameDisplay) -> Cow<'_, str> {
GLOBAL_LABELS.replace_all(input, Demangler { display })
}

#[cfg(test)]
mod test {
use owo_colors::set_override;

use crate::opts::NameDisplay;

use super::{contents, name};
const MAC: &str =
"__ZN58_$LT$nom..error..ErrorKind$u20$as$u20$core..fmt..Debug$GT$3fmt17hb98704099c11c31fE";
Expand All @@ -146,10 +154,20 @@ mod test {
assert!(name(MAC).is_some());
}

#[test]
fn linux_no_demangle_call() {
set_override(true);
let x = contents(CALL_L, NameDisplay::Mangled);
assert_eq!(
"[rip + \u{1b}[32m_ZN58_$LT$nom..error..ErrorKind$u20$as$u20$core..fmt..Debug$GT$3fmt17hb98704099c11c31fE\u{1b}[39m]",
x
);
}

#[test]
fn linux_demangle_call() {
set_override(true);
let x = contents(CALL_L, false);
let x = contents(CALL_L, NameDisplay::Short);
assert_eq!(
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt\u{1b}[39m]",
x
Expand All @@ -159,7 +177,7 @@ mod test {
#[test]
fn mac_demangle_call() {
set_override(true);
let x = contents(CALL_M, false);
let x = contents(CALL_M, NameDisplay::Short);
assert_eq!(
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt\u{1b}[39m]",
x
Expand All @@ -169,7 +187,7 @@ mod test {
#[test]
fn mac_demangle_call2() {
set_override(true);
let x = contents(CALL_M, true);
let x = contents(CALL_M, NameDisplay::Full);
assert_eq!(
"[rip + \u{1b}[32m<nom::error::ErrorKind as core::fmt::Debug>::fmt::hb98704099c11c31f\u{1b}[39m]",
x
Expand Down
11 changes: 4 additions & 7 deletions src/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
color,
demangle::{self, contents},
get_dump_range,
opts::{Format, NameDisplay, ToDump},
opts::{Format, ToDump},
safeprintln, Item,
};
use std::{
Expand Down Expand Up @@ -90,7 +90,7 @@ fn dump_range(fmt: &Format, strings: &[&str]) {
if line.starts_with("; ") {
safeprintln!("{}", color!(line, OwoColorize::bright_black));
} else {
let line = demangle::contents(line, fmt.name_display == NameDisplay::Full);
let line = demangle::contents(line, fmt.name_display);
safeprintln!("{line}");
}
}
Expand Down Expand Up @@ -168,10 +168,7 @@ pub fn collect_or_dump(
if seen {
safeprintln!("{}", color!(name, OwoColorize::cyan));
safeprintln!("{}", color!(attrs, OwoColorize::cyan));
safeprintln!(
"{}",
contents(&line, fmt.name_display == NameDisplay::Full)
);
safeprintln!("{}", contents(&line, fmt.name_display));
}
} else {
state = State::Skipping;
Expand All @@ -182,7 +179,7 @@ pub fn collect_or_dump(
}
State::Define => {
if seen {
safeprintln!("{}", contents(&line, fmt.name_display == NameDisplay::Full));
safeprintln!("{}", contents(&line, fmt.name_display));
}
if line == "}" {
if let Some(mut cur) = current_item.take() {
Expand Down
4 changes: 2 additions & 2 deletions src/mca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use crate::{
demangle, esafeprintln, get_dump_range,
opts::{Format, NameDisplay, ToDump},
opts::{Format, ToDump},
safeprintln,
};

Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn dump_function(

for line in BufRead::lines(BufReader::new(o)) {
let line = line?;
let line = demangle::contents(&line, fmt.name_display == NameDisplay::Full);
let line = demangle::contents(&line, fmt.name_display);
safeprintln!("{line}");
}

Expand Down

0 comments on commit 8955190

Please sign in to comment.