Skip to content

Commit

Permalink
conditional fields of constexpr literal structs
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Aug 12, 2024
1 parent c415766 commit ddd17f7
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,11 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
let ordered_fields = out.bindings().struct_field_names(path);
for (i, ordered_key) in ordered_fields.iter().enumerate() {
if let Some(lit) = fields.get(ordered_key) {
let condition = lit.cfg.to_condition(self.config);
if is_constexpr {
out.new_line();

condition.write_before(self.config, out);
// TODO: Some C++ versions (c++20?) now support designated
// initializers, consider generating them.
write!(out, "/* .{} = */ ", ordered_key);
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef struct {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef struct {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct {
int32_t x;
Expand Down
10 changes: 10 additions & 0 deletions tests/expectations/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ struct ConditionalField {
#endif
;
};
constexpr static const ConditionalField ConditionalField_ZERO = ConditionalField{
#if defined(X11)
/* .field = */ 0
#endif
};
constexpr static const ConditionalField ConditionalField_ONE = ConditionalField{
#if defined(X11)
/* .field = */ 1
#endif
};

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ cdef extern from *:

ctypedef struct ConditionalField:
int32_t field;
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }

ctypedef struct Normal:
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef struct ConditionalField {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg_both.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef struct ConditionalField {
#endif
;
} ConditionalField;
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

typedef struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ struct ConditionalField {
#endif
;
};
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg_tag.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct ConditionalField {
#endif
;
};
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
#define ConditionalField_ONE (ConditionalField){ .field = 1 }

struct Normal {
int32_t x;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/cfg_tag.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ cdef extern from *:

cdef struct ConditionalField:
int32_t field;
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }

cdef struct Normal:
int32_t x;
Expand Down
11 changes: 11 additions & 0 deletions tests/rust/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ struct ConditionalField {
field: i32,
}

impl ConditionalField {
pub const ZERO: Self = Self {
#[cfg(x11)]
field: 0,
};
pub const ONE: Self = Self {
#[cfg(x11)]
field: 1,
};
}

#[cfg(all(unix, x11))]
#[no_mangle]
pub extern "C" fn root(a: FooHandle, c: C)
Expand Down

0 comments on commit ddd17f7

Please sign in to comment.