-
Notifications
You must be signed in to change notification settings - Fork 788
Description
Problem
Some C libraries choose to use macro-defines where enums are also applicable (likely due to the strange behaviour of enums in C). While this is fine in principle, when creating an ffi to Rust using bindgen, this can result in a set of associated constants having wildly different types.
Consider this example from lm-sensors' libsensors:
#define SENSORS_BUS_TYPE_ANY (-1)
#define SENSORS_BUS_TYPE_I2C 0
#define SENSORS_BUS_TYPE_ISA 1
#define SENSORS_BUS_TYPE_PCI 2
#define SENSORS_BUS_TYPE_SPI 3
#define SENSORS_BUS_TYPE_VIRTUAL 4
#define SENSORS_BUS_TYPE_ACPI 5
#define SENSORS_BUS_TYPE_HID 6
#define SENSORS_BUS_TYPE_MDIO 7
#define SENSORS_BUS_TYPE_SCSI 8passing this through bindgen (here with fit_macro_constants(false)) results in the following set of constants:
pub const SENSORS_BUS_TYPE_ANY: i32 = -1;
pub const SENSORS_BUS_TYPE_I2C: u32 = 0;
pub const SENSORS_BUS_TYPE_ISA: u32 = 1;
pub const SENSORS_BUS_TYPE_PCI: u32 = 2;
pub const SENSORS_BUS_TYPE_SPI: u32 = 3;
pub const SENSORS_BUS_TYPE_VIRTUAL: u32 = 4;
pub const SENSORS_BUS_TYPE_ACPI: u32 = 5;
pub const SENSORS_BUS_TYPE_HID: u32 = 6;
pub const SENSORS_BUS_TYPE_MDIO: u32 = 7;
pub const SENSORS_BUS_TYPE_SCSI: u32 = 8;Note that the ideal type for these constants would actually be c_short as that is how the macros are used in libsensors itself.
In this case, using these constants in a match-statement is impossible, due to SENSORS_BUS_TYPE_ANY receiving a different signedness than the other constants.
If fit_macro_constants were true, it is even conceivable that some values may become u16 while others are u8. As you can see, the current lack of configuration options causes all sorts of problems.
Feature Request
As such, I suggest that there should at minimum be some sort of group_macro_constants_types method on the Builder that accepts a regex pattern and causes the builder to force all constants generated from macros matching the pattern to have the same type, either inferred from the constants or explicitly specified as another parameter.
It may also be useful to allow macro constants matching a pattern to be treated as though they were c-enums or at least allow them to be grouped into a module similar to constified_enum_module's behaviour.