Skip to content

Commit 67dfa7e

Browse files
authored
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
1 parent db4ea32 commit 67dfa7e

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class BitStream {
2+
public:
3+
void Write(const char *inputByteArray, unsigned int numberOfBytes);
4+
void Write(BitStream *bitStream, unsigned numberOfBits);
5+
void Write1();
6+
};

bindgen/codegen/mod.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ trait MethodCodegen {
24212421
&self,
24222422
ctx: &BindgenContext,
24232423
methods: &mut Vec<proc_macro2::TokenStream>,
2424-
method_names: &mut HashMap<String, usize>,
2424+
method_names: &mut HashSet<String>,
24252425
result: &mut CodegenResult<'a>,
24262426
parent: &CompInfo,
24272427
);
@@ -2432,7 +2432,7 @@ impl MethodCodegen for Method {
24322432
&self,
24332433
ctx: &BindgenContext,
24342434
methods: &mut Vec<proc_macro2::TokenStream>,
2435-
method_names: &mut HashMap<String, usize>,
2435+
method_names: &mut HashSet<String>,
24362436
result: &mut CodegenResult<'a>,
24372437
_parent: &CompInfo,
24382438
) {
@@ -2499,16 +2499,22 @@ impl MethodCodegen for Method {
24992499
return;
25002500
}
25012501

2502-
let count = {
2503-
let count = method_names.entry(name.clone()).or_insert(0);
2504-
*count += 1;
2505-
*count - 1
2506-
};
2502+
if method_names.contains(&name) {
2503+
let mut count = 1;
2504+
let mut new_name;
2505+
2506+
while {
2507+
new_name = format!("{}{}", name, count);
2508+
method_names.contains(&new_name)
2509+
} {
2510+
count += 1;
2511+
}
25072512

2508-
if count != 0 {
2509-
name.push_str(&count.to_string());
2513+
name = new_name;
25102514
}
25112515

2516+
method_names.insert(name.clone());
2517+
25122518
let mut function_name = function_item.canonical_name(ctx);
25132519
if times_seen > 0 {
25142520
write!(&mut function_name, "{}", times_seen).unwrap();

0 commit comments

Comments
 (0)