Skip to content

Commit 14fa2d0

Browse files
youknowoneemilio
authored andcommitted
conditional fields of constexpr literal structs
1 parent 021f3f3 commit 14fa2d0

File tree

11 files changed

+40
-3
lines changed

11 files changed

+40
-3
lines changed

src/bindgen/language_backend/clike.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,19 +912,19 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
912912
let ordered_fields = out.bindings().struct_field_names(path);
913913
for (i, ordered_key) in ordered_fields.iter().enumerate() {
914914
if let Some(lit) = fields.get(ordered_key) {
915+
let condition = lit.cfg.to_condition(self.config);
915916
if is_constexpr {
916917
out.new_line();
917918

919+
condition.write_before(self.config, out);
918920
// TODO: Some C++ versions (c++20?) now support designated
919921
// initializers, consider generating them.
920922
write!(out, "/* .{} = */ ", ordered_key);
921923
self.write_literal(out, &lit.value);
922924
if i + 1 != ordered_fields.len() {
923925
write!(out, ",");
924-
if !is_constexpr {
925-
write!(out, " ");
926-
}
927926
}
927+
condition.write_after(self.config, out);
928928
} else {
929929
if i > 0 {
930930
write!(out, ", ");

tests/expectations/cfg.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ typedef struct {
7777
#endif
7878
;
7979
} ConditionalField;
80+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
81+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
8082

8183
typedef struct {
8284
int32_t x;

tests/expectations/cfg.compat.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ typedef struct {
9595
#endif
9696
;
9797
} ConditionalField;
98+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
99+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
98100

99101
typedef struct {
100102
int32_t x;

tests/expectations/cfg.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ struct ConditionalField {
201201
#endif
202202
;
203203
};
204+
constexpr static const ConditionalField ConditionalField_ZERO = ConditionalField{
205+
#if defined(X11)
206+
/* .field = */ 0
207+
#endif
208+
};
209+
constexpr static const ConditionalField ConditionalField_ONE = ConditionalField{
210+
#if defined(X11)
211+
/* .field = */ 1
212+
#endif
213+
};
204214

205215
struct Normal {
206216
int32_t x;

tests/expectations/cfg.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ cdef extern from *:
5757

5858
ctypedef struct ConditionalField:
5959
int32_t field;
60+
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
61+
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }
6062

6163
ctypedef struct Normal:
6264
int32_t x;

tests/expectations/cfg_both.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ typedef struct ConditionalField {
7777
#endif
7878
;
7979
} ConditionalField;
80+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
81+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
8082

8183
typedef struct Normal {
8284
int32_t x;

tests/expectations/cfg_both.compat.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ typedef struct ConditionalField {
9595
#endif
9696
;
9797
} ConditionalField;
98+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
99+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
98100

99101
typedef struct Normal {
100102
int32_t x;

tests/expectations/cfg_tag.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ struct ConditionalField {
7777
#endif
7878
;
7979
};
80+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
81+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
8082

8183
struct Normal {
8284
int32_t x;

tests/expectations/cfg_tag.compat.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ struct ConditionalField {
9595
#endif
9696
;
9797
};
98+
#define ConditionalField_ZERO (ConditionalField){ .field = 0 }
99+
#define ConditionalField_ONE (ConditionalField){ .field = 1 }
98100

99101
struct Normal {
100102
int32_t x;

tests/expectations/cfg_tag.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ cdef extern from *:
5757

5858
cdef struct ConditionalField:
5959
int32_t field;
60+
const ConditionalField ConditionalField_ZERO # = <ConditionalField>{ 0 }
61+
const ConditionalField ConditionalField_ONE # = <ConditionalField>{ 1 }
6062

6163
cdef struct Normal:
6264
int32_t x;

tests/rust/cfg.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ struct ConditionalField {
4949
field: i32,
5050
}
5151

52+
impl ConditionalField {
53+
pub const ZERO: Self = Self {
54+
#[cfg(x11)]
55+
field: 0,
56+
};
57+
pub const ONE: Self = Self {
58+
#[cfg(x11)]
59+
field: 1,
60+
};
61+
}
62+
5263
#[cfg(all(unix, x11))]
5364
#[no_mangle]
5465
pub extern "C" fn root(a: FooHandle, c: C)

0 commit comments

Comments
 (0)