casaroli opened a new pull request, #21103: URL: https://github.com/apache/nuttx/pull/21103
## Summary Two fixes of macros in public headers, one commit each. ### sched-cpu-s Four CPU set macros of `<sched.h>` are wrong: - `CPU_ZERO_S(n, s)` expands to `CPU_ZERO_S(s)`. The preprocessor does not expand a macro in its own expansion, so the result is a call of an undeclared function `CPU_ZERO_S()` with one argument. It now expands to `CPU_ZERO(s)`, as the other `_S` macros expand to their fixed-size forms. - `CPU_ALLOC()` ends with a semicolon and has no parentheses around the cast, so it cannot be used in an expression, for example `if ((set = CPU_ALLOC(n)) == NULL)`. - `CPU_COUNT(s)` uses `*s` without parentheses, so `CPU_COUNT(sets + 1)` counts the bits of `*sets + 1`. - `cpu_set_t` is volatile, so `CPU_FREE(s)` passes a pointer to volatile to `free()`, and the compiler warns that the qualifier is discarded. It now casts the pointer to `void *`. ### stdbool-c23 In C23, `bool`, `true` and `false` are keywords, and the `<stdbool.h>` of GCC and Clang defines only `__bool_true_false_are_defined`. The `<stdbool.h>` of NuttX still defines `bool` as a macro (`_Bool` or `uint8_t`), and several public headers include it (`<pthread.h>`, `<sched.h>`, `<sys/poll.h>`, `<syslog.h>`). A C23 program then sees a macro named `bool` that other C libraries do not have, and a program that checks `#ifdef bool` stops with an `#error`. This PR defines the three macros only if the compiler does not have the keywords. GCC 13 and later and Clang 15 and later have the keywords also in their C2X mode, where `__STDC_VERSION__` is `202000L`. Older GCC and Clang versions in that mode, and all other compilers below the C23 value `202311L`, keep the macros, as before. The type does not change: the C23 keyword `bool` has the size of `_Bool`. ## Impact - sched-cpu-s: `CPU_ZERO_S()`, `CPU_ALLOC()` and `CPU_FREE()` can be used as on other systems. The in-tree users (`CPU_COUNT(&cpuset)` in `sched/sched/sched_smp.c` and in ostest) do not change. All build modes and architectures. Documentation: no change. - stdbool-c23: C23 programs see the keywords, as with the compiler's own `<stdbool.h>`. Before C23, and for C++, nothing changes. NuttX itself builds with an older `-std` and does not change. No code in nuttx or nuttx-apps tests `#ifdef bool`. All build modes and architectures. Documentation: no change. ## Testing The branches were first tested one by one (below). Then all commits of this PR together, on master b79dc18e015: cpusets.c, stdboolc23.c (with -std=gnu23) on `qemu-armv8a:knsh` (QEMU with HVF) all PASS, and ostest exits with status 0 on `qemu-armv8a:knsh` and `qemu-armv8a:nsh`. ### sched-cpu-s Test program `cpusets.c`: `if ((s = CPU_ALLOC(n)) == NULL)`; `CPU_ZERO_S()` of a set with a bit, then `CPU_COUNT_S()` is 0; `CPU_SET_S()`, `CPU_ISSET_S()`, `CPU_CLR_S()`; `CPU_FREE()`; `CPU_COUNT(sets + 1)` with two empty sets is 0. | | master | this PR | |---|---|---| | `cpusets.c` on `qemu-armv8a:knsh` (kernel, arm64, QEMU with HVF) | does not compile: `CPU_ALLOC()` "expected ')' before ';'", implicit `CPU_ZERO_S()`, `CPU_FREE()` discards `volatile` | all PASS | | ostest, `qemu-armv8a:knsh` and `qemu-armv8a:nsh` | status 0 | status 0 | Build, each OK (master aeab6fbf989 with this change and other small independent fixes): `qemu-armv8a:nsh_smp` (flat, SMP: `sched_smp.c` uses `CPU_COUNT()`), `rv-virt:pnsh64` (protected, rv64), `rv-virt:knsh64` (kernel, rv64), `mps2-an500:nsh` (flat, armv7-m), `mps2-an500:knsh` (protected, armv7-m). No test in nuttx-apps covers these macros. ### stdbool-c23 Test program `stdboolc23.c`, built with `-std=gnu23`: after `<pthread.h>` and `<stdbool.h>`, only `__bool_true_false_are_defined` is a macro; `sizeof(bool)` is 1 and `true` is 1. | | master | this PR | |---|---|---| | `stdboolc23.c` on `qemu-armv8a:knsh` (kernel, arm64, QEMU with HVF, GCC 14.2 `-std=gnu23`) | FAIL: `bool`, `true`, `false` are macros | PASS | | ostest, `qemu-armv8a:knsh` and `qemu-armv8a:nsh` | status 0 | status 0 | Build, each OK (master aeab6fbf989 with this change and other small independent fixes): `rv-virt:pnsh64` (protected, rv64), `rv-virt:knsh64` (kernel, rv64), `mps2-an500:nsh` (flat, armv7-m), `mps2-an500:knsh` (protected, armv7-m), `qemu-armv8a:nsh_smp`. Compiler check of `<stdbool.h>` alone (compile a file that uses `bool`, count the `bool`/`true`/`false` macros): arm-none-eabi-gcc 13.2 and 15.3, aarch64-none-elf-gcc 14.2 with `-std=gnu89`, `gnu99`, `gnu17`: OK, 3 macros as before; the same compilers with `gnu2x` (and `gnu23` for GCC 14 and 15): OK, no macros. Clang 22 with `gnu89`/`gnu17`: 3 macros; `gnu2x`/`c23`: no macros. Clang as version 14 in C2X mode (`202000L`), GCC as version 12 in C2X mode, and a compiler without `__GNUC__` in C2X mode: OK, 3 macros as before. g++ 14.2 (`gnu++17`) and clang++ 22 (`c++20`): OK. `tools/checkpatch.sh -c -u -m -g` passes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
