diff --git a/src/bindgen/language_backend/clike.rs b/src/bindgen/language_backend/clike.rs index b2c1ad86..2901aeac 100644 --- a/src/bindgen/language_backend/clike.rs +++ b/src/bindgen/language_backend/clike.rs @@ -927,6 +927,7 @@ 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(); @@ -949,7 +950,13 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { } else { write!(out, ".{} = ", ordered_key); } - self.write_literal(out, &lit.value); + if condition.is_some() { + write!(out, "__{export_name}_{ordered_key}("); + self.write_literal(out, &lit.value); + write!(out, ")") + } else { + self.write_literal(out, &lit.value); + } } } } @@ -960,6 +967,22 @@ impl LanguageBackend for CLikeLanguageBackend<'_> { write!(out, " "); } write!(out, "}}"); + + if self.config.language == Language::C { + for ordered_key in ordered_fields.iter() { + if let Some(lit) = fields.get(ordered_key) { + if let Some(condition) = lit.cfg.to_condition(self.config) { + out.new_line(); + condition.write_before(self.config, out); + let define = format!("#define __{export_name}_{ordered_key}(v)"); + write!(out, "{define} (v)"); + write!(out, "\n#else\n"); + write!(out, "{define}"); + condition.write_after(self.config, out); + } + } + } + } } } } diff --git a/tests/expectations/cfg.c b/tests/expectations/cfg.c index 9285f300..e9aafa18 100644 --- a/tests/expectations/cfg.c +++ b/tests/expectations/cfg.c @@ -77,6 +77,18 @@ typedef struct { #endif ; } ConditionalField; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif typedef struct { int32_t x; diff --git a/tests/expectations/cfg.compat.c b/tests/expectations/cfg.compat.c index 053c4052..84c8f9b7 100644 --- a/tests/expectations/cfg.compat.c +++ b/tests/expectations/cfg.compat.c @@ -95,6 +95,18 @@ typedef struct { #endif ; } ConditionalField; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif typedef struct { int32_t x; diff --git a/tests/expectations/cfg.cpp b/tests/expectations/cfg.cpp index d7b6250d..1cd0447a 100644 --- a/tests/expectations/cfg.cpp +++ b/tests/expectations/cfg.cpp @@ -201,6 +201,12 @@ struct ConditionalField { #endif ; }; +constexpr static const ConditionalField ConditionalField_ZERO = ConditionalField{ + /* .field = */ 0 +}; +constexpr static const ConditionalField ConditionalField_ONE = ConditionalField{ + /* .field = */ 1 +}; struct Normal { int32_t x; diff --git a/tests/expectations/cfg.pyx b/tests/expectations/cfg.pyx index e23fc703..803fd7a7 100644 --- a/tests/expectations/cfg.pyx +++ b/tests/expectations/cfg.pyx @@ -57,6 +57,8 @@ cdef extern from *: ctypedef struct ConditionalField: int32_t field; + const ConditionalField ConditionalField_ZERO # = { 0 } + const ConditionalField ConditionalField_ONE # = { 1 } ctypedef struct Normal: int32_t x; diff --git a/tests/expectations/cfg_both.c b/tests/expectations/cfg_both.c index 510e34e8..d413d308 100644 --- a/tests/expectations/cfg_both.c +++ b/tests/expectations/cfg_both.c @@ -77,6 +77,18 @@ typedef struct ConditionalField { #endif ; } ConditionalField; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif typedef struct Normal { int32_t x; diff --git a/tests/expectations/cfg_both.compat.c b/tests/expectations/cfg_both.compat.c index 2377824e..56ba1bd3 100644 --- a/tests/expectations/cfg_both.compat.c +++ b/tests/expectations/cfg_both.compat.c @@ -95,6 +95,18 @@ typedef struct ConditionalField { #endif ; } ConditionalField; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif typedef struct Normal { int32_t x; diff --git a/tests/expectations/cfg_tag.c b/tests/expectations/cfg_tag.c index 8cc88538..8dc87c53 100644 --- a/tests/expectations/cfg_tag.c +++ b/tests/expectations/cfg_tag.c @@ -77,6 +77,18 @@ struct ConditionalField { #endif ; }; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif struct Normal { int32_t x; diff --git a/tests/expectations/cfg_tag.compat.c b/tests/expectations/cfg_tag.compat.c index ec976353..fd125e61 100644 --- a/tests/expectations/cfg_tag.compat.c +++ b/tests/expectations/cfg_tag.compat.c @@ -95,6 +95,18 @@ struct ConditionalField { #endif ; }; +#define ConditionalField_ZERO (ConditionalField){ .field = __ConditionalField_field(0) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif +#define ConditionalField_ONE (ConditionalField){ .field = __ConditionalField_field(1) } +#if defined(X11) +#define __ConditionalField_field(v) (v) +#else +#define __ConditionalField_field(v) +#endif struct Normal { int32_t x; diff --git a/tests/expectations/cfg_tag.pyx b/tests/expectations/cfg_tag.pyx index e508bb30..6a0d44d7 100644 --- a/tests/expectations/cfg_tag.pyx +++ b/tests/expectations/cfg_tag.pyx @@ -57,6 +57,8 @@ cdef extern from *: cdef struct ConditionalField: int32_t field; + const ConditionalField ConditionalField_ZERO # = { 0 } + const ConditionalField ConditionalField_ONE # = { 1 } cdef struct Normal: int32_t x; diff --git a/tests/rust/cfg.rs b/tests/rust/cfg.rs index 70893609..0efe3546 100644 --- a/tests/rust/cfg.rs +++ b/tests/rust/cfg.rs @@ -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)