Skip to content

Commit a4ff4cb

Browse files
rocallahankhuey
authored andcommitted
Introduce DemangleOptions::simplify_template_parameters()
In a debugger, users may input identifiers containing template parameter values and expect some aspects of the identifer (e.g. the exact type of numeric parameters) to be inferred. For example, given "template <short N> struct X<N>", the debugger should accept "X<3>" and not just "X<(short)3>". A debugger can make this work by canonicalizing incoming identifiers containing template parameter values into this simplified form. When these identifiers are being produced via cpp_demangle, it is more efficient and more reliable to have cpp_demangle itself produce this simplified form.
1 parent 4b9da59 commit a4ff4cb

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

examples/cppfilt.rs

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ fn main() {
100100
.long("no-params")
101101
.help("Do not display function arguments"),
102102
)
103+
.arg(
104+
Arg::with_name("hide-expression-literal-types")
105+
.long("hide-expression-literal-types")
106+
.help("Hide types in template parameter expression literals"),
107+
)
103108
.arg(
104109
Arg::with_name("mangled_names")
105110
.multiple(true)
@@ -120,6 +125,9 @@ fn main() {
120125
if matches.is_present("noparams") {
121126
options = options.no_params();
122127
}
128+
if matches.is_present("hide-expression-literal-types") {
129+
options = options.hide_expression_literal_types();
130+
}
123131
if matches.is_present("noreturntype") {
124132
options = options.no_return_type();
125133
}

src/ast.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ where
553553
// unless that call is via the toplevel call to `MangledName::demangle`.
554554
show_return_type: bool,
555555

556+
// Whether to show types of expression literals.
557+
show_expression_literal_types: bool,
558+
556559
// recursion protection.
557560
state: Cell<DemangleState>,
558561
}
@@ -601,6 +604,7 @@ where
601604
is_template_argument_pack: false,
602605
show_params: !options.no_params,
603606
show_return_type: !options.no_return_type,
607+
show_expression_literal_types: !options.hide_expression_literal_types,
604608
state: Cell::new(DemangleState { recursion_level: 0 }),
605609
}
606610
}
@@ -6703,13 +6707,16 @@ where
67036707
start,
67046708
end,
67056709
) => {
6706-
write!(ctx, "(")?;
6707-
ty.demangle(ctx, scope)?;
6710+
if ctx.show_expression_literal_types {
6711+
write!(ctx, "(")?;
6712+
ty.demangle(ctx, scope)?;
6713+
write!(ctx, ")")?;
6714+
}
67086715
let start = if start < end && ctx.input[start] == b'n' {
6709-
write!(ctx, ")-[")?;
6716+
write!(ctx, "-[")?;
67106717
start + 1
67116718
} else {
6712-
write!(ctx, ")[")?;
6719+
write!(ctx, "[")?;
67136720
start
67146721
};
67156722
let s = ::std::str::from_utf8(&ctx.input[start..end]).map_err(|e| {
@@ -6725,9 +6732,11 @@ where
67256732
end,
67266733
) => write_literal(ctx, start, end),
67276734
ExprPrimary::Literal(ref ty, start, end) => {
6728-
write!(ctx, "(")?;
6729-
ty.demangle(ctx, scope)?;
6730-
write!(ctx, ")")?;
6735+
if ctx.show_expression_literal_types {
6736+
write!(ctx, "(")?;
6737+
ty.demangle(ctx, scope)?;
6738+
write!(ctx, ")")?;
6739+
}
67316740
write_literal(ctx, start, end)
67326741
}
67336742
}

src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl ParseOptions {
107107
pub struct DemangleOptions {
108108
no_params: bool,
109109
no_return_type: bool,
110+
hide_expression_literal_types: bool,
110111
recursion_limit: Option<NonZeroU32>,
111112
}
112113

@@ -128,6 +129,15 @@ impl DemangleOptions {
128129
self
129130
}
130131

132+
/// Hide type annotations in template value parameters.
133+
/// These are not needed to distinguish template instances
134+
/// so this can make it easier to match user-provided
135+
/// template instance names.
136+
pub fn hide_expression_literal_types(mut self) -> Self {
137+
self.hide_expression_literal_types = true;
138+
self
139+
}
140+
131141
/// Set the limit on recursion depth during the demangling phase. A low
132142
/// limit will cause valid symbols to be rejected, but a high limit may
133143
/// allow pathological symbols to overflow the stack during demangling.

tests/tests.rs

+18
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ macro_rules! demangles_no_param_and_no_return_type {
9393
};
9494
}
9595

96+
macro_rules! demangles_simplify_template_parameters {
97+
( $mangled:ident , $demangled:expr ) => {
98+
demangles_simplify_template_parameters!($mangled, stringify!($mangled), $demangled);
99+
};
100+
( $name:ident , $mangled:expr , $demangled:expr ) => {
101+
#[test]
102+
fn $name() {
103+
let options = DemangleOptions::new().hide_expression_literal_types();
104+
assert_demangles_as($mangled, $demangled, Some(options));
105+
}
106+
};
107+
}
108+
96109
macro_rules! demangles_no_return_type {
97110
( $mangled:ident , $demangled:expr ) => {
98111
demangles_no_return_type!($mangled, stringify!($mangled), $demangled);
@@ -577,3 +590,8 @@ demangles!(
577590
_ZNKSt6__ndk112basic_stringIDuNS_11char_traitsIDuEENS_9allocatorIDuEEE5c_strEv,
578591
"std::__ndk1::basic_string<char8_t, std::__ndk1::char_traits<char8_t>, std::__ndk1::allocator<char8_t> >::c_str() const"
579592
);
593+
594+
demangles_simplify_template_parameters!(
595+
_ZN11SmiTagging2ILs4EE13kSmiShiftSizeE,
596+
"SmiTagging2<4>::kSmiShiftSize"
597+
);

0 commit comments

Comments
 (0)