diff --git a/include/zephyr/device.h b/include/zephyr/device.h index 4f8c9a260a7d859..292506e4a2f9dc4 100644 --- a/include/zephyr/device.h +++ b/include/zephyr/device.h @@ -1080,11 +1080,22 @@ device_get_dt_nodelabels(const struct device *dev) (.pm_constraints = (constraints_),)) \ IF_ENABLED(CONFIG_PM_POLICY_DEVICE_CONSTRAINTS, \ (.pm_constraints_size = (constraints_size_),)) \ - IF_ENABLED(CONFIG_PM_DEVICE, ({ .pm_base = (pm_),})) /**/ \ + IF_ENABLED(CONFIG_PM_DEVICE, Z_DEVICE_INIT_PM_BASE(pm_)) /**/ \ IF_ENABLED(CONFIG_DEVICE_DT_METADATA, \ (.dt_meta = &Z_DEVICE_DT_METADATA_NAME_GET(dev_id_),)) \ } +/* + * Anonymous unions require C11. Some pre-C11 gcc versions have early support for anonymous + * unions but they require these braces when combined with C99 designated initializers. For + * more details see https://docs.zephyrproject.org/latest/develop/languages/cpp/ + */ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__) < 201100 +# define Z_DEVICE_INIT_PM_BASE(pm_) ({ .pm_base = (pm_),}) +#else +# define Z_DEVICE_INIT_PM_BASE(pm_) (.pm_base = (pm_),) +#endif + /** * @brief Device section name (used for sorting purposes). * @@ -1157,10 +1168,7 @@ device_get_dt_nodelabels(const struct device *dev) Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)) = { \ .init_fn = {COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (.dev_rw), (.dev)) = \ (init_fn_)}, \ - { \ - COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (.dev_rw), (.dev)) = \ - &DEVICE_NAME_GET(dev_id), \ - }, \ + Z_DEVICE_INIT_ENTRY_DEV(node_id, dev_id), \ } #define Z_DEFER_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, init_fn_) \ @@ -1169,12 +1177,23 @@ device_get_dt_nodelabels(const struct device *dev) Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)) = { \ .init_fn = {COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (.dev_rw), (.dev)) = \ (init_fn_)}, \ - { \ - COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (.dev_rw), (.dev)) = \ - &DEVICE_NAME_GET(dev_id), \ - }, \ + Z_DEVICE_INIT_ENTRY_DEV(node_id, dev_id), \ } +/* + * Anonymous unions require C11. Some pre-C11 gcc versions have early support for anonymous + * unions but they require these braces when combined with C99 designated initializers. For + * more details see https://docs.zephyrproject.org/latest/develop/languages/cpp/ + */ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__) < 201100 +# define Z_DEVICE_INIT_ENTRY_DEV(node_id, dev_id) { Z_DEV_ENTRY_DEV(node_id, dev_id) } +#else +# define Z_DEVICE_INIT_ENTRY_DEV(node_id, dev_id) Z_DEV_ENTRY_DEV(node_id, dev_id) +#endif + +#define Z_DEV_ENTRY_DEV(node_id, dev_id) \ + COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (.dev_rw), (.dev)) = &DEVICE_NAME_GET(dev_id) + /** * @brief Define a @ref device and all other required objects. *