Skip to content

Commit

Permalink
Introduce DemangleOptions::simplify_template_parameters()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rocallahan authored and khuey committed Nov 21, 2021
1 parent 4b9da59 commit a4ff4cb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
8 changes: 8 additions & 0 deletions examples/cppfilt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ fn main() {
.long("no-params")
.help("Do not display function arguments"),
)
.arg(
Arg::with_name("hide-expression-literal-types")
.long("hide-expression-literal-types")
.help("Hide types in template parameter expression literals"),
)
.arg(
Arg::with_name("mangled_names")
.multiple(true)
Expand All @@ -120,6 +125,9 @@ fn main() {
if matches.is_present("noparams") {
options = options.no_params();
}
if matches.is_present("hide-expression-literal-types") {
options = options.hide_expression_literal_types();
}
if matches.is_present("noreturntype") {
options = options.no_return_type();
}
Expand Down
23 changes: 16 additions & 7 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ where
// unless that call is via the toplevel call to `MangledName::demangle`.
show_return_type: bool,

// Whether to show types of expression literals.
show_expression_literal_types: bool,

// recursion protection.
state: Cell<DemangleState>,
}
Expand Down Expand Up @@ -601,6 +604,7 @@ where
is_template_argument_pack: false,
show_params: !options.no_params,
show_return_type: !options.no_return_type,
show_expression_literal_types: !options.hide_expression_literal_types,
state: Cell::new(DemangleState { recursion_level: 0 }),
}
}
Expand Down Expand Up @@ -6703,13 +6707,16 @@ where
start,
end,
) => {
write!(ctx, "(")?;
ty.demangle(ctx, scope)?;
if ctx.show_expression_literal_types {
write!(ctx, "(")?;
ty.demangle(ctx, scope)?;
write!(ctx, ")")?;
}
let start = if start < end && ctx.input[start] == b'n' {
write!(ctx, ")-[")?;
write!(ctx, "-[")?;
start + 1
} else {
write!(ctx, ")[")?;
write!(ctx, "[")?;
start
};
let s = ::std::str::from_utf8(&ctx.input[start..end]).map_err(|e| {
Expand All @@ -6725,9 +6732,11 @@ where
end,
) => write_literal(ctx, start, end),
ExprPrimary::Literal(ref ty, start, end) => {
write!(ctx, "(")?;
ty.demangle(ctx, scope)?;
write!(ctx, ")")?;
if ctx.show_expression_literal_types {
write!(ctx, "(")?;
ty.demangle(ctx, scope)?;
write!(ctx, ")")?;
}
write_literal(ctx, start, end)
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl ParseOptions {
pub struct DemangleOptions {
no_params: bool,
no_return_type: bool,
hide_expression_literal_types: bool,
recursion_limit: Option<NonZeroU32>,
}

Expand All @@ -128,6 +129,15 @@ impl DemangleOptions {
self
}

/// Hide type annotations in template value parameters.
/// These are not needed to distinguish template instances
/// so this can make it easier to match user-provided
/// template instance names.
pub fn hide_expression_literal_types(mut self) -> Self {
self.hide_expression_literal_types = true;
self
}

/// Set the limit on recursion depth during the demangling phase. A low
/// limit will cause valid symbols to be rejected, but a high limit may
/// allow pathological symbols to overflow the stack during demangling.
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ macro_rules! demangles_no_param_and_no_return_type {
};
}

macro_rules! demangles_simplify_template_parameters {
( $mangled:ident , $demangled:expr ) => {
demangles_simplify_template_parameters!($mangled, stringify!($mangled), $demangled);
};
( $name:ident , $mangled:expr , $demangled:expr ) => {
#[test]
fn $name() {
let options = DemangleOptions::new().hide_expression_literal_types();
assert_demangles_as($mangled, $demangled, Some(options));
}
};
}

macro_rules! demangles_no_return_type {
( $mangled:ident , $demangled:expr ) => {
demangles_no_return_type!($mangled, stringify!($mangled), $demangled);
Expand Down Expand Up @@ -577,3 +590,8 @@ demangles!(
_ZNKSt6__ndk112basic_stringIDuNS_11char_traitsIDuEENS_9allocatorIDuEEE5c_strEv,
"std::__ndk1::basic_string<char8_t, std::__ndk1::char_traits<char8_t>, std::__ndk1::allocator<char8_t> >::c_str() const"
);

demangles_simplify_template_parameters!(
_ZN11SmiTagging2ILs4EE13kSmiShiftSizeE,
"SmiTagging2<4>::kSmiShiftSize"
);

0 comments on commit a4ff4cb

Please sign in to comment.