summaryrefslogtreecommitdiff
path: root/bindgen/codegen/postprocessing/merge_extern_blocks.rs
diff options
context:
space:
mode:
authorChristian Poveda <christian.poveda@ferrous-systems.com>2022-09-23 21:36:14 -0500
committerChristian Poveda <christian.poveda@ferrous-systems.com>2022-10-04 20:47:17 -0500
commit0296f9e86c7756e718b6b82836ce1e09b5f8d08a (patch)
treeb5954c6680b243c0b1671a80ea973ef90877e462 /bindgen/codegen/postprocessing/merge_extern_blocks.rs
parenta900f8f863d1313ad76603234aaeea22bb9ba7b3 (diff)
split the repo into a workspace
remove `clap` dependency :tada: update the book installation instructions
Diffstat (limited to 'bindgen/codegen/postprocessing/merge_extern_blocks.rs')
-rw-r--r--bindgen/codegen/postprocessing/merge_extern_blocks.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/bindgen/codegen/postprocessing/merge_extern_blocks.rs b/bindgen/codegen/postprocessing/merge_extern_blocks.rs
new file mode 100644
index 00000000..2b761494
--- /dev/null
+++ b/bindgen/codegen/postprocessing/merge_extern_blocks.rs
@@ -0,0 +1,46 @@
+use syn::{Item, ItemForeignMod};
+
+pub(super) fn merge_extern_blocks(items: &mut Vec<Item>) {
+ // Keep all the extern blocks in a different `Vec` for faster search.
+ let mut foreign_mods = Vec::<ItemForeignMod>::new();
+
+ for item in std::mem::take(items) {
+ match item {
+ Item::ForeignMod(ItemForeignMod {
+ attrs,
+ abi,
+ brace_token,
+ items: foreign_items,
+ }) => {
+ let mut exists = false;
+ for foreign_mod in &mut foreign_mods {
+ // Check if there is a extern block with the same ABI and
+ // attributes.
+ if foreign_mod.attrs == attrs && foreign_mod.abi == abi {
+ // Merge the items of the two blocks.
+ foreign_mod.items.extend_from_slice(&foreign_items);
+ exists = true;
+ break;
+ }
+ }
+ // If no existing extern block had the same ABI and attributes, store
+ // it.
+ if !exists {
+ foreign_mods.push(ItemForeignMod {
+ attrs,
+ abi,
+ brace_token,
+ items: foreign_items,
+ });
+ }
+ }
+ // If the item is not an extern block, we don't have to do anything.
+ _ => items.push(item),
+ }
+ }
+
+ // Move all the extern blocks alongside the rest of the items.
+ for foreign_mod in foreign_mods {
+ items.push(Item::ForeignMod(foreign_mod));
+ }
+}