Skip to content

Commit

Permalink
Fix duplicated function names (rust-lang#2341)
Browse files Browse the repository at this point in the history
Even though this change does name deduplication in a slower way, it
avoids name collisions without any breaking changes in the test suite.

Fixes rust-lang#2202
  • Loading branch information
pvdrz authored and LoganBarnett committed Dec 2, 2023
1 parent e02a249 commit 4fa0b34
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 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();
};
24 changes: 15 additions & 9 deletions bindgen/codegen/mod.rs
Expand Up @@ -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,
);
Expand All @@ -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,
) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4fa0b34

Please sign in to comment.