Skip to content

Commit da49c93

Browse files
committed
arch: riscv: Fix warning when C++ is enabled
When compiling with C++ enabled (CONFIG_CPP), add an unused member to prevent an empty struct; this makes the struct size the same for both C and C++. Fixes the following warnings: In file included from include/zephyr/drivers/gpio.h:22: In file included from include/zephyr/tracing/tracing.h:9: In file included from include/zephyr/kernel.h:17: In file included from include/zephyr/kernel_includes.h:32: In file included from include/zephyr/kernel_structs.h:29: In file included from include/zephyr/arch/structs.h:29: include/zephyr/arch/riscv/structs.h:11:1: error: empty struct has size 0 in C, size 1 in C++ [-Werror,-Wextern-c-compat] 11 | struct _cpu_arch { | ^ In file included from include/zephyr/drivers/gpio.h:22: In file included from include/zephyr/tracing/tracing.h:9: In file included from include/zephyr/kernel.h:17: In file included from include/zephyr/kernel_includes.h:36: In file included from include/zephyr/arch/cpu.h:25: In file included from include/zephyr/arch/riscv/arch.h:18: include/zephyr/arch/riscv/thread.h:68:1: error: empty struct has size 0 in C, size 1 in C++ [-Werror,-Wextern-c-compat] 68 | struct _thread_arch { | ^ Signed-off-by: Tom Hughes <[email protected]>
1 parent c41a265 commit da49c93

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/zephyr/arch/riscv/structs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ struct _cpu_arch {
2222
atomic_ptr_val_t fpu_owner;
2323
uint32_t fpu_state;
2424
#endif
25+
#if defined(CONFIG_CPP) && !defined(CONFIG_USERSPACE) && \
26+
!(defined(CONFIG_SMP) || (CONFIG_MP_MAX_NUM_CPUS > 1)) && !defined(CONFIG_FPU_SHARING)
27+
/* Empty struct has size 0 in C, size 1 in C++. Force them to be the same. */
28+
uint8_t unused_cpp_size_compatibility;
29+
#endif
2530
};
2631

32+
#if defined(CONFIG_CPP)
33+
BUILD_ASSERT(sizeof(struct _cpu_arch) >= 1);
34+
#endif
35+
2736
#endif /* ZEPHYR_INCLUDE_RISCV_STRUCTS_H_ */

include/zephyr/arch/riscv/thread.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,17 @@ struct _thread_arch {
8484
unsigned long m_mode_pmpaddr_regs[PMP_M_MODE_SLOTS];
8585
unsigned long m_mode_pmpcfg_regs[PMP_M_MODE_SLOTS / sizeof(unsigned long)];
8686
#endif
87+
#if defined(CONFIG_CPP) && !defined(CONFIG_FPU_SHARING) && !defined(CONFIG_USERSPACE) && \
88+
!defined(CONFIG_PMP_STACK_GUARD)
89+
/* Empty struct has size 0 in C, size 1 in C++. Force them to be the same. */
90+
uint8_t unused_cpp_size_compatibility;
91+
#endif
8792
};
8893

94+
#if defined(CONFIG_CPP)
95+
BUILD_ASSERT(sizeof(struct _thread_arch) >= 1);
96+
#endif
97+
8998
typedef struct _thread_arch _thread_arch_t;
9099

91100
#endif /* _ASMLANGUAGE */

0 commit comments

Comments
 (0)