diff options
author | Christian Poveda Ruiz <31802960+pvdrz@users.noreply.github.com> | 2022-11-11 11:48:39 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 11:48:39 -0500 |
commit | 67dfa7ed41303a74277bf9ad4e2e2344c8ffb505 (patch) | |
tree | 3f6994bb2d3fbf5d133270cad32dd1f1663527d9 | |
parent | db4ea32e2d810e765a4c40479e053d0a61a875cf (diff) |
Fix duplicated function names (#2341)
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
-rw-r--r-- | bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs | 67 | ||||
-rw-r--r-- | bindgen-tests/tests/headers/duplicated-definition-count.hpp | 6 | ||||
-rw-r--r-- | bindgen/codegen/mod.rs | 24 |
3 files changed, 88 insertions, 9 deletions
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 00000000..8fdffe56 --- /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::<BitStream>(), + 1usize, + concat!("Size of: ", stringify!(BitStream)) + ); + assert_eq!( + ::std::mem::align_of::<BitStream>(), + 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 00000000..29167626 --- /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 e0d96cd1..c7ac59db 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2421,7 +2421,7 @@ trait MethodCodegen { &self, ctx: &BindgenContext, methods: &mut Vec<proc_macro2::TokenStream>, - method_names: &mut HashMap<String, usize>, + method_names: &mut HashSet<String>, result: &mut CodegenResult<'a>, parent: &CompInfo, ); @@ -2432,7 +2432,7 @@ impl MethodCodegen for Method { &self, ctx: &BindgenContext, methods: &mut Vec<proc_macro2::TokenStream>, - method_names: &mut HashMap<String, usize>, + method_names: &mut HashSet<String>, 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(); |