From 3e9e05d7e7926c3f85a7b3a744a63f06a88ed191 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 6 Feb 2017 23:48:52 -0800 Subject: [PATCH] Print () instead of (void) for function types --- build.rs | 2 +- src/ast.rs | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index dbed195..7fa97fb 100644 --- a/build.rs +++ b/build.rs @@ -68,7 +68,7 @@ fn test_afl_seed_{}() {{ // Ratcheting number that is increased as more libiberty tests start // passing. Once they are all passing, this can be removed and we can enable all // of them by default. -const LIBIBERTY_TEST_THRESHOLD: usize = 14; +const LIBIBERTY_TEST_THRESHOLD: usize = 20; /// Read `tests/libiberty-demangle-expected`, parse its input mangled symbols, /// and expected output demangled symbols, and generate test cases for them. diff --git a/src/ast.rs b/src/ast.rs index e615649..5234021 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -540,10 +540,7 @@ impl Demangle for Encoding { // To maintain compatibility with libiberty, print `()` instead // of `(void)` for functions that take no arguments. - let void = - Type::Builtin(BuiltinType::Standard(StandardBuiltinType::Void)); - if function_args.len() == 1 && - ctx.subs.get_type(&function_args[0]) == Some(&void) { + if function_args.len() == 1 && function_args[0].is_void(ctx.subs) { try!(write!(ctx, ")")); return Ok(()); } @@ -1573,11 +1570,26 @@ pub enum Type { PackExpansion(TypeHandle), } +impl Type { + fn is_void(&self) -> bool { + match *self { + Type::Builtin(BuiltinType::Standard(StandardBuiltinType::Void)) => true, + _ => false, + } + } +} + define_handle! { /// A reference to a parsed `Type` production. pub enum TypeHandle } +impl TypeHandle { + fn is_void(&self, subs: &SubstitutionTable) -> bool { + subs.get_type(self).map_or(false, |ty| ty.is_void()) + } +} + impl Parse for TypeHandle { fn parse<'a, 'b>(subs: &'a mut SubstitutionTable, input: IndexStr<'b>) @@ -2101,6 +2113,13 @@ impl Demangle for BareFunctionType { { try!(self.ret().demangle(ctx, stack)); try!(write!(ctx, " (")); + + // For libiberty compatibility, print `()` instead of `(void)`. + if self.args().len() == 1 && self.args()[0].is_void(ctx.subs) { + try!(write!(ctx, ")")); + return Ok(()); + } + let mut need_comma = false; for arg in self.args() { if need_comma {