Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to deduplicate extern blocks #2258

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,10 @@ impl Builder {
output_vector.push("--sort-semantically".into());
}

if self.options.merge_extern_blocks {
output_vector.push("--merge-extern-blocks".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1481,14 +1485,20 @@ impl Builder {
self
}

/// If true, enables the sorting of the output in a predefined manner
/// If true, enables the sorting of the output in a predefined manner.
///
/// TODO: Perhaps move the sorting order out into a config
pub fn sort_semantically(mut self, doit: bool) -> Self {
self.options.sort_semantically = doit;
self
}

/// If true, merges extern blocks.
pub fn merge_extern_blocks(mut self, doit: bool) -> Self {
self.options.merge_extern_blocks = doit;
self
}

/// Generate the Rust bindings using the options built up thus far.
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
// Add any extra arguments from the environment to the clang command line.
Expand Down Expand Up @@ -2019,8 +2029,11 @@ struct BindgenOptions {
/// Emit vtable functions.
vtable_generation: bool,

/// Sort the code generation
/// Sort the code generation.
sort_semantically: bool,

/// Deduplicate `extern` blocks.
merge_extern_blocks: bool,
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand All @@ -2031,7 +2044,7 @@ impl ::std::panic::UnwindSafe for BindgenOptions {}
impl BindgenOptions {
/// Whether any of the enabled options requires `syn`.
fn require_syn(&self) -> bool {
self.sort_semantically
self.sort_semantically || self.merge_extern_blocks
}

fn build(&mut self) {
Expand Down Expand Up @@ -2175,6 +2188,7 @@ impl Default for BindgenOptions {
force_explicit_padding: false,
vtable_generation: false,
sort_semantically: false,
merge_extern_blocks: false,
}
}
}
Expand Down Expand Up @@ -2480,6 +2494,59 @@ impl Bindings {
.unwrap()
.1;

if options.merge_extern_blocks {
// Here we will store all the items after deduplication.
let mut items = Vec::new();

// Keep all the extern blocks in a different `Vec` for faster search.
let mut foreign_mods = Vec::<syn::ItemForeignMod>::new();
for item in syn_parsed_items {
match item {
syn::Item::ForeignMod(syn::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(syn::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 alongiside the rest of the items.
for foreign_mod in foreign_mods {
items.push(syn::Item::ForeignMod(foreign_mod));
}

syn_parsed_items = items;
}

if options.sort_semantically {
syn_parsed_items.sort_by_key(|item| match item {
syn::Item::Type(_) => 0,
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ where
Arg::new("sort-semantically")
.long("sort-semantically")
.help("Enables sorting of code generation in a predefined manner."),
Arg::new("merge-extern-blocks")
.long("merge-extern-blocks")
.help("Deduplicates extern blocks."),
Arg::new("V")
.long("version")
.help("Prints the version, and exits"),
Expand Down Expand Up @@ -1007,5 +1010,9 @@ where
builder = builder.sort_semantically(true);
}

if matches.is_present("merge-extern-blocks") {
builder = builder.merge_extern_blocks(true);
}

Ok((builder, output, verbose))
}
37 changes: 37 additions & 0 deletions tests/expectations/tests/merge-extern-blocks.rs

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

6 changes: 6 additions & 0 deletions tests/headers/merge-extern-blocks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-flags: --merge-extern-blocks -- --target=x86_64-unknown-linux
int foo();
typedef struct Point {
int x;
} Point;
int bar();