Skip to content

Commit 9281fb2

Browse files
committed
add option to remove type aliases
1 parent d241e95 commit 9281fb2

File tree

10 files changed

+185
-8
lines changed

10 files changed

+185
-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/codegen/postprocessing/merge_extern_blocks.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ use syn::{
33
Item, ItemForeignMod, ItemMod,
44
};
55

6-
pub(super) fn merge_extern_blocks(item_mod: &mut ItemMod) {
6+
use crate::BindgenOptions;
7+
8+
#[inline]
9+
pub(super) fn merge_extern_blocks(
10+
item_mod: &mut ItemMod,
11+
_options: &BindgenOptions,
12+
) {
713
Visitor.visit_item_mod_mut(item_mod)
814
}
915

bindgen/codegen/postprocessing/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@ use syn::{parse2, ItemMod};
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 ItemMod),
17+
run: fn(&mut ItemMod, &BindgenOptions),
1618
}
1719

1820
// TODO: This can be a const fn when mutable references are allowed in const
1921
// context.
2022
macro_rules! pass {
2123
($pass:ident) => {
24+
pass!($pass, |options| options.$pass)
25+
};
26+
($pass:ident, $should_run:expr) => {
2227
PostProcessingPass {
23-
should_run: |options| options.$pass,
24-
run: |item_mod| $pass(item_mod),
28+
should_run: $should_run,
29+
run: |item_mod, options| $pass(item_mod, options),
2530
}
2631
};
2732
}
2833

29-
const PASSES: &[PostProcessingPass] =
30-
&[pass!(merge_extern_blocks), pass!(sort_semantically)];
34+
const PASSES: &[PostProcessingPass] = &[
35+
pass!(merge_extern_blocks),
36+
pass!(sort_semantically),
37+
pass!(remove_alias, |options| !options.remove_alias.is_empty()),
38+
];
3139

3240
pub(crate) fn postprocessing(
3341
items: Vec<TokenStream>,
@@ -51,7 +59,7 @@ pub(crate) fn postprocessing(
5159

5260
for pass in PASSES {
5361
if (pass.should_run)(options) {
54-
(pass.run)(&mut item_mod);
62+
(pass.run)(&mut item_mod, options);
5563
}
5664
}
5765

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

bindgen/codegen/postprocessing/sort_semantically.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ use syn::{
33
Item, ItemMod,
44
};
55

6-
pub(super) fn sort_semantically(item_mod: &mut ItemMod) {
6+
use crate::BindgenOptions;
7+
8+
#[inline]
9+
pub(super) fn sort_semantically(
10+
item_mod: &mut ItemMod,
11+
_options: &BindgenOptions,
12+
) {
713
Visitor.visit_item_mod_mut(item_mod)
814
}
915

bindgen/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ impl Builder {
358358
(&self.options.no_default_types, "--no-default"),
359359
(&self.options.no_hash_types, "--no-hash"),
360360
(&self.options.must_use_types, "--must-use-type"),
361+
(&self.options.remove_alias, "--remove-alias"),
361362
];
362363

363364
for (set, flag) in regex_sets {
@@ -1770,6 +1771,13 @@ impl Builder {
17701771
self.options.c_naming = doit;
17711772
self
17721773
}
1774+
1775+
/// Remove a type alias that matches the provided argument. Regular
1776+
/// expressions are supported.
1777+
pub fn remove_alias<T: Into<String>>(mut self, arg: T) -> Self {
1778+
self.options.remove_alias.insert(arg.into());
1779+
self
1780+
}
17731781
}
17741782

17751783
/// Configuration options for generated bindings.
@@ -2105,6 +2113,9 @@ struct BindgenOptions {
21052113

21062114
/// Deduplicate `extern` blocks.
21072115
merge_extern_blocks: bool,
2116+
2117+
/// The set of type aliases that should be removed.
2118+
remove_alias: RegexSet,
21082119
}
21092120

21102121
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -2142,6 +2153,7 @@ impl BindgenOptions {
21422153
&mut self.no_default_types,
21432154
&mut self.no_hash_types,
21442155
&mut self.must_use_types,
2156+
&mut self.remove_alias,
21452157
];
21462158
let record_matches = self.record_matches;
21472159
for regex_set in &mut regex_sets {
@@ -2261,6 +2273,7 @@ impl Default for BindgenOptions {
22612273
vtable_generation: false,
22622274
sort_semantically: false,
22632275
merge_extern_blocks: false,
2276+
remove_alias: Default::default(),
22642277
}
22652278
}
22662279
}

0 commit comments

Comments
 (0)