Skip to content

Commit 60c09cf

Browse files
committed
add option to remove type aliases
1 parent 73ce4bc commit 60c09cf

File tree

11 files changed

+178
-8
lines changed

11 files changed

+178
-8
lines changed

bindgen-cli/options.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,12 @@ where
568568
Arg::new("merge-extern-blocks")
569569
.long("merge-extern-blocks")
570570
.help("Deduplicates extern blocks."),
571+
Arg::new("remove-alias")
572+
.long("remove-alias")
573+
.help("Remove type aliases matching <regex>.")
574+
.value_name("regex")
575+
.multiple_occurrences(true)
576+
.number_of_values(1),
571577
Arg::new("V")
572578
.long("version")
573579
.help("Prints the version, and exits"),
@@ -1088,5 +1094,11 @@ where
10881094
builder = builder.merge_extern_blocks(true);
10891095
}
10901096

1097+
if let Some(remove_alias) = matches.values_of("remove-alias") {
1098+
for regex in remove_alias {
1099+
builder = builder.remove_alias(regex);
1100+
}
1101+
}
1102+
10911103
Ok((builder, output, verbose))
10921104
}

bindgen-tests/tests/expectations/tests/remove_alias.rs

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/remove_template_alias.rs

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// bindgen-flags: --remove-alias "int.*"
2+
3+
typedef long long int64;
4+
typedef int int32;
5+
typedef int32 i32;
6+
7+
struct int32_ {
8+
int32 inner;
9+
};
10+
11+
int64 foo();
12+
int32 bar();
13+
i32 baz();
14+
struct int32_ qux();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// bindgen-flags: --remove-alias "Wrapped"
2+
3+
template<typename T>
4+
using Wrapped = T;

bindgen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ lazycell = "1"
4545
lazy_static = "1"
4646
peeking_take_while = "0.1.2"
4747
quote = { version = "1", default-features = false }
48-
syn = { version = "1.0.99", features = ["full", "extra-traits"]}
48+
syn = { version = "1.0.99", features = ["full", "extra-traits", "visit-mut"]}
4949
regex = { version = "1.5", default-features = false , features = ["std", "unicode"] }
5050
which = { version = "4.2.1", optional = true, default-features = false }
5151
shlex = "1"

bindgen/codegen/postprocessing/merge_extern_blocks.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use syn::{Item, ItemForeignMod};
22

3-
pub(super) fn merge_extern_blocks(items: &mut Vec<Item>) {
3+
use crate::BindgenOptions;
4+
5+
pub(super) fn merge_extern_blocks(
6+
items: &mut Vec<Item>,
7+
_options: &BindgenOptions,
8+
) {
49
// Keep all the extern blocks in a different `Vec` for faster search.
510
let mut foreign_mods = Vec::<ItemForeignMod>::new();
611

bindgen/codegen/postprocessing/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use syn::Item;
55
use crate::BindgenOptions;
66

77
mod merge_extern_blocks;
8+
mod remove_alias;
89
mod sort_semantically;
910

1011
use merge_extern_blocks::merge_extern_blocks;
12+
use remove_alias::remove_alias;
1113
use sort_semantically::sort_semantically;
1214

1315
struct PostProcessingPass {
1416
should_run: fn(&BindgenOptions) -> bool,
15-
run: fn(&mut Vec<Item>),
17+
run: fn(&mut Vec<Item>, &BindgenOptions),
1618
}
1719

1820
// TODO: This can be a const fn when mutable references are allowed in const
@@ -21,13 +23,19 @@ macro_rules! pass {
2123
($pass:ident) => {
2224
PostProcessingPass {
2325
should_run: |options| options.$pass,
24-
run: |items| $pass(items),
26+
run: |items, options| $pass(items, options),
2527
}
2628
};
2729
}
2830

29-
const PASSES: &[PostProcessingPass] =
30-
&[pass!(merge_extern_blocks), pass!(sort_semantically)];
31+
const PASSES: &[PostProcessingPass] = &[
32+
pass!(merge_extern_blocks),
33+
pass!(sort_semantically),
34+
PostProcessingPass {
35+
should_run: |options| !options.remove_alias.is_empty(),
36+
run: |items, options| remove_alias(items, options),
37+
},
38+
];
3139

3240
pub(crate) fn postprocessing(
3341
items: Vec<TokenStream>,
@@ -56,7 +64,7 @@ pub(crate) fn postprocessing(
5664

5765
for pass in PASSES {
5866
if (pass.should_run)(options) {
59-
(pass.run)(&mut items);
67+
(pass.run)(&mut items, options);
6068
}
6169
}
6270

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use syn::visit_mut::{visit_type_mut, VisitMut};
2+
use syn::{Item, Type};
3+
4+
use crate::BindgenOptions;
5+
6+
pub(super) fn remove_alias(items: &mut Vec<Item>, options: &BindgenOptions) {
7+
let visitors: Vec<_> = items
8+
.iter()
9+
.enumerate()
10+
.rev()
11+
.filter_map(|(index, item)| {
12+
if let Item::Type(alias_item) = item {
13+
if alias_item.generics.params.is_empty() {
14+
let ident = alias_item.ident.to_string();
15+
if options.remove_alias.matches(&ident) {
16+
return Some((
17+
index,
18+
Visitor {
19+
ident,
20+
ty: alias_item.ty.clone(),
21+
},
22+
));
23+
}
24+
}
25+
}
26+
None
27+
})
28+
.collect();
29+
30+
for (index, mut visitor) in visitors {
31+
items.remove(index);
32+
for item in items.iter_mut() {
33+
visitor.visit_item_mut(item);
34+
}
35+
}
36+
}
37+
38+
struct Visitor {
39+
ident: String,
40+
ty: Box<Type>,
41+
}
42+
43+
impl VisitMut for Visitor {
44+
fn visit_type_mut(&mut self, ty: &mut Type) {
45+
if let Type::Path(type_path) = ty {
46+
if type_path.path.is_ident(&self.ident) {
47+
*ty = self.ty.as_ref().clone();
48+
}
49+
}
50+
51+
visit_type_mut::<_>(self, ty)
52+
}
53+
}

bindgen/codegen/postprocessing/sort_semantically.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use syn::Item;
22

3-
pub(super) fn sort_semantically(items: &mut [Item]) {
3+
use crate::BindgenOptions;
4+
5+
pub(super) fn sort_semantically(items: &mut [Item], _options: &BindgenOptions) {
46
items.sort_by_key(|item| match item {
57
Item::Type(_) => 0,
68
Item::Struct(_) => 1,

0 commit comments

Comments
 (0)