Skip to content

arch: riscv: Fix warning when C++ is enabled #88734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thughes
Copy link
Contributor

@thughes thughes commented Apr 16, 2025

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 {
| ^

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]>
@thughes thughes marked this pull request as ready for review April 17, 2025 02:54
@github-actions github-actions bot added area: Architectures area: RISCV RISCV Architecture (32-bit & 64-bit) labels Apr 17, 2025
@fkokosinski
Copy link
Member

fkokosinski commented Apr 17, 2025

Hi, thanks for this PR! Unfortunately, I can't seem to reproduce this warning using the following command:

~ % west build -p -b qemu_riscv32 samples/philosophers -- -DCONFIG_CPP=y

Could you perhaps create an issue with information on how to reproduce this warning?

@thughes
Copy link
Contributor Author

thughes commented Apr 17, 2025

Could you perhaps create an issue with information on how to reproduce this warning?

There are two things in my setup that are different than the way you're testing:

  • I'm using LLVM (clang version 20).
  • I'm building a board that hasn't yet been integrated into upstream Zephyr: https://crrev.com/c/6349236.

It should be reproducible if you use LLVM and make sure that the relevant configs are disabled that make struct _cpu_arch and struct _thread_arch empty.

Here's an example of what the failure looks like in CI when building with LLVM in another part of the code: #87088 (comment)

@fkokosinski
Copy link
Member

There are two things in my setup that are different than the way you're testing:

  • I'm using LLVM (clang version 20).

Right. This is a configuration that, as far as I’m aware, is unsupported by Zephyr out of the box.

Unfortunately, I can’t access the link without signing in, but I assume you had to at least add a CMake script in cmake/compiler/clang/riscv.cmake. Would it be possible for you to extract that part, and contribute it as well as a part of this PR? And preferably test it with a platform that has at least one entry in SUPPORTED_EMU_PLATFORMS?

This is something we’re not testing right now in CI, and I’d rather we add this configuration to our suite as well and make it much more easily accessible for maintainers to test and use.

@thughes
Copy link
Contributor Author

thughes commented Apr 18, 2025

This is a configuration that, as far as I’m aware, is unsupported by Zephyr out of the box.

LLVM is officially supported and tested with CI: #85542. For example, see #87088 (comment). However, the CI is currently only building for native-sim.

Unfortunately, I can’t access the link without signing in

https://crrev.com/c/6349236 is publicly accessible. No sign in is required to view it.

I'll see if I can create an upstream change to cover this path in CI. At the very least, it should be possible to demonstrate the size issue with gcc using BUILD_ASSERT(sizeof(struct _thread_arch) >= 1);.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Architectures area: RISCV RISCV Architecture (32-bit & 64-bit)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants