Skip to content

Commit 7111bba

Browse files
author
bors-servo
authored
Auto merge of #1329 - db48x:allow-const-enum-style, r=emilio
add --constified-enum to output consts when the default is changed
2 parents 4403741 + 3a8df51 commit 7111bba

File tree

6 files changed

+89
-31
lines changed

6 files changed

+89
-31
lines changed

src/codegen/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,8 @@ impl CodeGenerator for Enum {
25202520
EnumVariation::Bitfield
25212521
} else if self.is_rustified_enum(ctx, item) {
25222522
EnumVariation::Rust
2523+
} else if self.is_constified_enum(ctx, item) {
2524+
EnumVariation::Consts
25232525
} else {
25242526
ctx.options().default_enum_style
25252527
};

src/ir/enum_ty.rs

+20-27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clang;
77
use ir::annotations::Annotations;
88
use ir::item::ItemCanonicalPath;
99
use parse::{ClangItemParser, ParseError};
10+
use regex_set::RegexSet;
1011

1112
/// An enum representing custom handling that can be given to a variant.
1213
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -137,44 +138,36 @@ impl Enum {
137138
Ok(Enum::new(repr, variants))
138139
}
139140

140-
/// Whether the enum should be a bitfield
141-
pub fn is_bitfield(&self, ctx: &BindgenContext, item: &Item) -> bool {
141+
fn is_matching_enum(&self, ctx: &BindgenContext, enums: &RegexSet, item: &Item) -> bool {
142142
let path = item.canonical_path(ctx);
143143
let enum_ty = item.expect_type();
144144

145-
ctx.options().bitfield_enums.matches(&path[1..].join("::")) ||
146-
(enum_ty.name().is_none() &&
147-
self.variants().iter().any(|v| {
148-
ctx.options().bitfield_enums.matches(&v.name())
149-
}))
145+
let path_matches = enums.matches(&path[1..].join("::"));
146+
let enum_is_anon = enum_ty.name().is_none();
147+
let a_variant_matches = self.variants().iter().any(|v| {
148+
enums.matches(&v.name())
149+
});
150+
path_matches || (enum_is_anon && a_variant_matches)
151+
}
152+
153+
/// Whether the enum should be a bitfield
154+
pub fn is_bitfield(&self, ctx: &BindgenContext, item: &Item) -> bool {
155+
self.is_matching_enum(ctx, &ctx.options().bitfield_enums, item)
150156
}
151157

152158
/// Whether the enum should be an constified enum module
153-
pub fn is_constified_enum_module(
154-
&self,
155-
ctx: &BindgenContext,
156-
item: &Item,
157-
) -> bool {
158-
let path = item.canonical_path(ctx);
159-
let enum_ty = item.expect_type();
159+
pub fn is_constified_enum_module(&self, ctx: &BindgenContext, item: &Item) -> bool {
160+
self.is_matching_enum(ctx, &ctx.options().constified_enum_modules, item)
161+
}
160162

161-
ctx.options().constified_enum_modules.matches(&path[1..].join("::")) ||
162-
(enum_ty.name().is_none() &&
163-
self.variants().iter().any(|v| {
164-
ctx.options().constified_enum_modules.matches(&v.name())
165-
}))
163+
/// Whether the enum should be an set of constants
164+
pub fn is_constified_enum(&self, ctx: &BindgenContext, item: &Item) -> bool {
165+
self.is_matching_enum(ctx, &ctx.options().constified_enums, item)
166166
}
167167

168168
/// Whether the enum should be a Rust enum
169169
pub fn is_rustified_enum(&self, ctx: &BindgenContext, item: &Item) -> bool {
170-
let path = item.canonical_path(ctx);
171-
let enum_ty = item.expect_type();
172-
173-
ctx.options().rustified_enums.matches(&path[1..].join("::")) ||
174-
(enum_ty.name().is_none() &&
175-
self.variants().iter().any(|v| {
176-
ctx.options().rustified_enums.matches(&v.name())
177-
}))
170+
self.is_matching_enum(ctx, &ctx.options().rustified_enums, item)
178171
}
179172
}
180173

src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,20 @@ impl Builder {
256256
})
257257
.count();
258258

259+
self.options
260+
.constified_enums
261+
.get_items()
262+
.iter()
263+
.map(|item| {
264+
output_vector.push("--constified-enum".into());
265+
output_vector.push(
266+
item.trim_left_matches("^")
267+
.trim_right_matches("$")
268+
.into(),
269+
);
270+
})
271+
.count();
272+
259273
self.options
260274
.blacklisted_types
261275
.get_items()
@@ -771,6 +785,13 @@ impl Builder {
771785
self
772786
}
773787

788+
/// Mark the given enum (or set of enums, if using a pattern) as a set of
789+
/// constants that are not to be put into a module.
790+
pub fn constified_enum<T: AsRef<str>>(mut self, arg: T) -> Builder {
791+
self.options.constified_enums.insert(arg);
792+
self
793+
}
794+
774795
/// Mark the given enum (or set of enums, if using a pattern) as a set of
775796
/// constants that should be put into a module.
776797
///
@@ -1268,6 +1289,9 @@ struct BindgenOptions {
12681289
/// The enum patterns to mark an enum as a module of constants.
12691290
constified_enum_modules: RegexSet,
12701291

1292+
/// The enum patterns to mark an enum as a set of constants.
1293+
constified_enums: RegexSet,
1294+
12711295
/// Whether we should generate builtins or not.
12721296
builtins: bool,
12731297

@@ -1443,6 +1467,7 @@ impl BindgenOptions {
14431467
self.blacklisted_types.build();
14441468
self.opaque_types.build();
14451469
self.bitfield_enums.build();
1470+
self.constified_enums.build();
14461471
self.constified_enum_modules.build();
14471472
self.rustified_enums.build();
14481473
self.no_partialeq_types.build();
@@ -1480,6 +1505,7 @@ impl Default for BindgenOptions {
14801505
default_enum_style: Default::default(),
14811506
bitfield_enums: Default::default(),
14821507
rustified_enums: Default::default(),
1508+
constified_enums: Default::default(),
14831509
constified_enum_modules: Default::default(),
14841510
builtins: false,
14851511
emit_ast: false,

src/options.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,30 @@ where
3636
Arg::with_name("bitfield-enum")
3737
.long("bitfield-enum")
3838
.help("Mark any enum whose name matches <regex> as a set of \
39-
bitfield flags instead of an enumeration.")
39+
bitfield flags.")
4040
.value_name("regex")
4141
.takes_value(true)
4242
.multiple(true)
4343
.number_of_values(1),
4444
Arg::with_name("rustified-enum")
4545
.long("rustified-enum")
46-
.help("Mark any enum whose name matches <regex> as a Rust enum \
47-
instead of a set of constants.")
46+
.help("Mark any enum whose name matches <regex> as a Rust enum.")
47+
.value_name("regex")
48+
.takes_value(true)
49+
.multiple(true)
50+
.number_of_values(1),
51+
Arg::with_name("constified-enum")
52+
.long("constified-enum")
53+
.help("Mark any enum whose name matches <regex> as a series of \
54+
constants.")
4855
.value_name("regex")
4956
.takes_value(true)
5057
.multiple(true)
5158
.number_of_values(1),
5259
Arg::with_name("constified-enum-module")
5360
.long("constified-enum-module")
5461
.help("Mark any enum whose name matches <regex> as a module of \
55-
constants instead of just constants.")
62+
constants.")
5663
.value_name("regex")
5764
.takes_value(true)
5865
.multiple(true)
@@ -326,6 +333,12 @@ where
326333
}
327334
}
328335

336+
if let Some(bitfields) = matches.values_of("constified-enum") {
337+
for regex in bitfields {
338+
builder = builder.constified_enum(regex);
339+
}
340+
}
341+
329342
if let Some(constified_mods) = matches.values_of("constified-enum-module") {
330343
for regex in constified_mods {
331344
builder = builder.constified_enum_module(regex);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
#[repr(u32)]
6+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
7+
pub enum Foo {
8+
Bar = 0,
9+
Qux = 1,
10+
}
11+
pub const Neg_MinusOne: Neg = -1;
12+
pub const Neg_One: Neg = 1;
13+
pub type Neg = i32;

tests/headers/enum-undefault.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// bindgen-flags: --default-enum-style=rust --constified-enum=Neg
2+
3+
enum Foo {
4+
Bar = 0,
5+
Qux
6+
};
7+
8+
enum Neg {
9+
MinusOne = -1,
10+
One = 1,
11+
};

0 commit comments

Comments
 (0)