Skip to content
Open
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
99 changes: 99 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-default-bitfield.rs

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

9 changes: 9 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-default-consts.rs

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

21 changes: 21 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-default-module.rs

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

18 changes: 18 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-default-rust.rs

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

18 changes: 18 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs

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

9 changes: 9 additions & 0 deletions bindgen-tests/tests/expectations/tests/enum.rs

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

1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/enum-default-bitfield.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// bindgen-flags: --default-enum-style=bitfield --constified-enum-module=Neg
// bindgen-parse-callbacks: enum-type-changer

#include "enum.h"
1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/enum-default-consts.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// bindgen-flags: --default-enum-style=consts --constified-enum-module=Neg
// bindgen-parse-callbacks: enum-type-changer

#include "enum.h"
1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/enum-default-module.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// bindgen-flags: --default-enum-style=moduleconsts --constified-enum-module=Neg
// bindgen-parse-callbacks: enum-type-changer

#include "enum.h"
1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/enum-default-rust.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// bindgen-flags: --default-enum-style=rust --constified-enum-module=Neg
// bindgen-parse-callbacks: enum-type-changer

#include "enum.h"
1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/enum-no-debug-rust.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// bindgen-flags: --no-derive-debug --default-enum-style=rust --constified-enum-module=Neg
// bindgen-parse-callbacks: enum-type-changer

#include "enum.h"
17 changes: 16 additions & 1 deletion bindgen-tests/tests/headers/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ enum NoDebug {
enum Debug {
Debug1,
Debug2,
};
};

enum should_be_u8 {
FirstU8,
SecondU8,
};

enum should_be_u16 {
FirstU16,
SecondU16,
};

enum should_be_i32 {
FirstI32,
SecondI32,
};
15 changes: 15 additions & 0 deletions bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ impl ParseCallbacks for StructFieldRename {
}
}

#[derive(Debug)]
struct EnumTypeChanger;

impl ParseCallbacks for EnumTypeChanger {
fn enum_type_override(&self, enum_name: &str) -> Option<IntKind> {
match enum_name {
"should_be_u8" => Some(IntKind::U8),
"should_be_u16" => Some(IntKind::U16),
"should_be_i32" => Some(IntKind::I32),
_ => None,
}
}
}

#[derive(Debug)]
struct BlocklistedTypeImplementsTrait;

Expand Down Expand Up @@ -191,6 +205,7 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
"type-visibility" => Box::new(TypeVisibility),
"operator-rename" => Box::new(OperatorRename),
"enum-type-changer" => Box::new(EnumTypeChanger),
call_back => {
if let Some(prefix) =
call_back.strip_prefix("remove-function-prefix-")
Expand Down
6 changes: 6 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ pub trait ParseCallbacks: fmt::Debug {
/// the expansion of the macro as a sequence of tokens.
fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}

/// The integer kind an enum should have, given the name of the enum type,
/// or `None` if you want the default to be chosen.
fn enum_type_override(&self, _enum_name: &str) -> Option<IntKind> {
None
}

/// This function should return whether, given an enum variant
/// name, and value, this enum variant will forcibly be a constant.
fn enum_variant_behavior(
Expand Down
14 changes: 13 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3759,7 +3759,7 @@ impl CodeGenerator for Enum {
let variation = self.computed_enum_variation(ctx, item);

let repr_translated;
let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) {
let mut repr = match self.repr().map(|repr| ctx.resolve_type(repr)) {
Some(repr)
if !ctx.options().translate_enum_integer_types &&
!variation.is_rust() =>
Expand Down Expand Up @@ -3906,6 +3906,18 @@ impl CodeGenerator for Enum {
});
}

let repr_from_callback;
if !ctx.options().parse_callbacks.is_empty() {
if let Some(callback_type) = ctx
.options()
.last_callback(|cb| cb.enum_type_override(&name))
{
repr_from_callback =
Type::new(None, None, TypeKind::Int(callback_type), false);
repr = &repr_from_callback;
}
}

let repr = repr.to_rust_ty_or_opaque(ctx, item);
let has_typedef = ctx.is_enum_typedef_combo(item.id());

Expand Down
Loading