-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to deduplicate impl blocks
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
bindgen-tests/tests/expectations/tests/merge_impl_blocks.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// bindgen-flags: --merge-impl-blocks --newtype-enum '.*' --enable-cxx-namespaces -- --target=x86_64-unknown-linux | ||
int foo(); | ||
enum Foo { | ||
Bar = 1 << 1, | ||
Baz = 1 << 2, | ||
Duplicated = 1 << 2, | ||
Negative = -3, | ||
}; | ||
int bar(); | ||
|
||
namespace ns { | ||
int foo(); | ||
enum Bar { | ||
B0 = 1, | ||
B1 = B0 + 3, | ||
B2 = B0 + 2, | ||
B3 = B0 - 2, | ||
}; | ||
int bar(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use syn::{ | ||
visit_mut::{visit_file_mut, visit_item_mod_mut, VisitMut}, | ||
File, Item, ItemImpl, ItemMod, | ||
}; | ||
|
||
pub(super) fn merge_impl_blocks(file: &mut File) { | ||
Visitor.visit_file_mut(file); | ||
} | ||
|
||
struct Visitor; | ||
|
||
impl VisitMut for Visitor { | ||
fn visit_file_mut(&mut self, file: &mut File) { | ||
visit_items(&mut file.items); | ||
visit_file_mut(self, file); | ||
} | ||
|
||
fn visit_item_mod_mut(&mut self, item_mod: &mut ItemMod) { | ||
if let Some((_, ref mut items)) = item_mod.content { | ||
visit_items(items); | ||
} | ||
visit_item_mod_mut(self, item_mod); | ||
} | ||
} | ||
|
||
fn visit_items(items: &mut Vec<Item>) { | ||
// Keep all the impl blocks in a different `Vec` for faster search. | ||
let mut impl_blocks = Vec::<ItemImpl>::new(); | ||
|
||
for item in std::mem::take(items) { | ||
if let Item::Impl(ItemImpl { | ||
attrs, | ||
defaultness, | ||
unsafety, | ||
impl_token, | ||
generics, | ||
trait_: None, // don't merge `impl <Trait> for T` blocks | ||
self_ty, | ||
brace_token, | ||
items: impl_block_items, | ||
}) = item | ||
{ | ||
let mut exists = false; | ||
for impl_block in &mut impl_blocks { | ||
// Check if there is an equivalent impl block | ||
if impl_block.attrs == attrs && | ||
impl_block.unsafety == unsafety && | ||
impl_block.generics == generics && | ||
impl_block.self_ty == self_ty | ||
{ | ||
// Merge the items of the two blocks. | ||
impl_block.items.extend_from_slice(&impl_block_items); | ||
exists = true; | ||
break; | ||
} | ||
} | ||
// If no matching impl block was found, store it. | ||
if !exists { | ||
impl_blocks.push(ItemImpl { | ||
attrs, | ||
defaultness, | ||
unsafety, | ||
impl_token, | ||
generics, | ||
trait_: None, | ||
self_ty, | ||
brace_token, | ||
items: impl_block_items, | ||
}); | ||
} | ||
} else { | ||
// If the item is not an mergeable impl block, we don't have to do | ||
// anything and just push it back. | ||
items.push(item); | ||
} | ||
} | ||
|
||
// Move all the impl blocks alongside the rest of the items. | ||
for impl_block in impl_blocks { | ||
items.push(Item::Impl(impl_block)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters