From 2486eb2f0eaeb42a7441aadfeaddf9bf28a8f382 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 9 Nov 2022 14:00:44 -0500 Subject: [PATCH] Fix duplicated function names Even though this change does name deduplication in a slower way, it avoids name collisions without any breaking changes in the test suite. Fixes #2202 --- .../tests/duplicated-definition-count.rs | 67 +++++++++++++++++++ .../headers/duplicated-definition-count.hpp | 6 ++ bindgen/codegen/mod.rs | 24 ++++--- 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs create mode 100644 bindgen-tests/tests/headers/duplicated-definition-count.hpp diff --git a/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs b/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs new file mode 100644 index 0000000000..8fdffe56e4 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs @@ -0,0 +1,67 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct BitStream { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_BitStream() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(BitStream)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!("Alignment of ", stringify!(BitStream)) + ); +} +extern "C" { + #[link_name = "\u{1}_ZN9BitStream5WriteEPKcj"] + pub fn BitStream_Write( + this: *mut BitStream, + inputByteArray: *const ::std::os::raw::c_char, + numberOfBytes: ::std::os::raw::c_uint, + ); +} +extern "C" { + #[link_name = "\u{1}_ZN9BitStream5WriteEPS_j"] + pub fn BitStream_Write1( + this: *mut BitStream, + bitStream: *mut BitStream, + numberOfBits: ::std::os::raw::c_uint, + ); +} +extern "C" { + #[link_name = "\u{1}_ZN9BitStream6Write1Ev"] + pub fn BitStream_Write11(this: *mut BitStream); +} +impl BitStream { + #[inline] + pub unsafe fn Write( + &mut self, + inputByteArray: *const ::std::os::raw::c_char, + numberOfBytes: ::std::os::raw::c_uint, + ) { + unsafe { BitStream_Write(self, inputByteArray, numberOfBytes) } + } + #[inline] + pub unsafe fn Write1( + &mut self, + bitStream: *mut BitStream, + numberOfBits: ::std::os::raw::c_uint, + ) { + unsafe { BitStream_Write1(self, bitStream, numberOfBits) } + } + #[inline] + pub unsafe fn Write11(&mut self) { + unsafe { BitStream_Write11(self) } + } +} diff --git a/bindgen-tests/tests/headers/duplicated-definition-count.hpp b/bindgen-tests/tests/headers/duplicated-definition-count.hpp new file mode 100644 index 0000000000..2916762617 --- /dev/null +++ b/bindgen-tests/tests/headers/duplicated-definition-count.hpp @@ -0,0 +1,6 @@ +class BitStream { + public: + void Write(const char *inputByteArray, unsigned int numberOfBytes); + void Write(BitStream *bitStream, unsigned numberOfBits); + void Write1(); +}; diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index e0d96cd1b8..c7ac59db42 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2421,7 +2421,7 @@ trait MethodCodegen { &self, ctx: &BindgenContext, methods: &mut Vec, - method_names: &mut HashMap, + method_names: &mut HashSet, result: &mut CodegenResult<'a>, parent: &CompInfo, ); @@ -2432,7 +2432,7 @@ impl MethodCodegen for Method { &self, ctx: &BindgenContext, methods: &mut Vec, - method_names: &mut HashMap, + method_names: &mut HashSet, result: &mut CodegenResult<'a>, _parent: &CompInfo, ) { @@ -2499,16 +2499,22 @@ impl MethodCodegen for Method { return; } - let count = { - let count = method_names.entry(name.clone()).or_insert(0); - *count += 1; - *count - 1 - }; + if method_names.contains(&name) { + let mut count = 1; + let mut new_name; + + while { + new_name = format!("{}{}", name, count); + method_names.contains(&new_name) + } { + count += 1; + } - if count != 0 { - name.push_str(&count.to_string()); + name = new_name; } + method_names.insert(name.clone()); + let mut function_name = function_item.canonical_name(ctx); if times_seen > 0 { write!(&mut function_name, "{}", times_seen).unwrap();