casaroli opened a new pull request, #21797:
URL: https://github.com/apache/nuttx/pull/21797

   depends-on: apache/nuttx/pull/21778
   depends-on: apache/nuttx-apps/pull/4041
   
   ## Summary
   
   **include: Do not define bool through the standard headers.** Before C23, 
`<stdbool.h>` defines `bool`, `true` and `false` as macros. The C standard 
reserves these names for `<stdbool.h>`: a program that does not include it may 
use them for its own purposes, and the glibc and musl headers never include 
`<stdbool.h>`. In NuttX, 31 of the 150 public headers of a kernel build export 
gave the macros, for example `<stdio.h>` and `<pthread.h>` through 
`<nuttx/mutex.h>`, `<sys/ioctl.h>` through `<nuttx/fs/ioctl.h>`, `<sched.h>`, 
`<poll.h>` and `<syslog.h>`. Then a program with its own boolean (`typedef int 
bool;`, `enum { false, true };`) does not compile after `#include <stdio.h>`; 
this is common in older C code, and some programs check that `bool` is not a 
macro after their headers.
   
   The change:
   
   - `<nuttx/list.h>`, `<sys/poll.h>` and `<syslog.h>` included `<stdbool.h>` 
but did not use it: remove the include.
   - `<nuttx/mutex.h>`, `<nuttx/fs/ioctl.h>`, `<nuttx/clock.h>`, `<pthread.h>` 
and `<sched.h>` use `uint8_t` and 0/1 instead of `bool`: `nxmutex_is_hold()` 
and the inline `nxmutex`/`nxrmutex` queries, the three flags of `struct 
geometry`, `clock_daysbeforemonth()`, the flags of `pthread_once_t` and 
`pthread_rwlock_t`, and `sched_idletask()`. The definitions of the three 
functions change with them. `bool` in NuttX has the size of `uint8_t`, so no 
structure layout changes.
   - `<errno.h>` includes `<nuttx/irq.h>` only when it defines `set_errno()`, 
that is, not with `NUTTX_NO_EXTENSIONS`.
   - About 105 internal headers (drivers, file systems, `include/nuttx`, libc, 
mm) and `libs/libc/net/lib_inetpton.c` used `bool` but got `<stdbool.h>` only 
through other headers: they now include it themselves.
   
   What is left, by build mode:
   
   - Kernel builds: only `<aio.h>`, `<execinfo.h>` and `<ucontext.h>` of the 
standard headers still give the macros (through `<nuttx/signal.h>`, 
`<nuttx/sched.h>` and `<nuttx/irq.h>`, whose `bool` API is used in the whole 
kernel), and the NuttX-only headers `<debug.h>`, `<dsp.h>`, `<dspb16.h>`, 
`<imx_container.h>` and `<sys/custom_file.h>`.
   - Protected builds: also `<stdio.h>` and the headers that include it: 
outside a kernel build `<stdio.h>` includes `<nuttx/fs/fs.h>`, which uses 
`bool`.
   - Flat builds: as protected, and also `<errno.h>`: in a flat build 
`set_errno()` calls `up_interrupt_context()`, an inline function of the 
architecture `irq.h` headers, which use `bool`.
   
   This PR needs the apps PR (depends-on above): without it, apps code that got 
`bool` only through the standard headers (for example ostest 
`pthread_rwlock.c`) does not build in a kernel build.
   
   ## Impact
   
   - Programs that define their own `bool`, `true` or `false` compile with the 
standard headers.
   - Code that used `bool`, `true` or `false` with only a standard header 
included (for example only `<pthread.h>` or `<sched.h>`) must now include 
`<stdbool.h>`, as with other C libraries. In the tree: nuttx-apps needs the 
apps PR above (39 files); the builds and the scans below find nothing else. 
Out-of-tree code (boards, drivers, applications) that relied on the implicit 
include breaks at compile time, mostly in kernel builds; the fix is one include 
line.
   - Flat-build API: the return type of `nxmutex_is_hold()`, 
`nxmutex_is_locked()`, `nxrmutex_is_hold()`, `nxrmutex_is_recursive()`, 
`nxrmutex_is_locked()` and `sched_idletask()`, the `leap_year` parameter of 
`clock_daysbeforemonth()`, the three flags of `struct geometry` and the flags 
in `pthread_once_t` and `pthread_rwlock_t` change from `bool` to `uint8_t`. The 
values stay 0 and 1, and every in-tree assignment to the `struct geometry` 
flags is a comparison, a `bool` or a 0/1 constant (rpmsgblk copies them from 
its 0/1 message fields). The size and layout of the structures do not change 
(`bool` is one byte in NuttX).
   - No change in the system call interface. Flat, protected and kernel builds: 
the change is in headers and in three function definitions.
   
   ## Testing
   
   Test program `hdrbool.c` (built with `-std=c11`): it includes 25 standard 
headers, then checks that `bool`, `true`, `false` and 
`__bool_true_false_are_defined` are not macros, and defines and uses its own 
`typedef int bool;` and `enum { false, true };`. qemu-armv8a:knsh, QEMU with 
HVF, with the apps PR:
   
   | Case | master | this PR |
   |---|---|---|
   | kernel build: no `bool`/`true`/`false` macros after the standard headers | 
FAIL (macros defined) | PASS |
   | kernel build: the program's own `bool` type and constants work | not 
compiled (macros) | PASS |
   
   ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh, on 
master and with this PR.
   
   Kernel build apps import (qemu-armv8a:knsh export, `make import` with 
`-Werror`; Linux host, NuttX CI image):
   
   | NuttX | apps | Result |
   |---|---|---|
   | master | master | OK |
   | master | the apps PR | OK |
   | this PR | the apps PR | OK |
   | this PR | master | FAIL: `testing/ostest/pthread_rwlock.c`, 
`pthread_rwlock_cancel.c`: `'false' undeclared` |
   
   Header scan (each public header of the qemu-armv8a:knsh export alone in a 
`-std=c11` translation unit, `gcc -E -H`): master 31 of 150 headers give 
`<stdbool.h>`; this PR 9 (`<stdbool.h>` itself, `<aio.h>`, `<execinfo.h>`, 
`<ucontext.h>`, `<debug.h>`, `<dsp.h>`, `<dspb16.h>`, `<imx_container.h>`, 
`<sys/custom_file.h>`).
   
   Source scan: every C file of nuttx-apps (427 with the apps PR), `libs/` (68) 
and `mm/` (27) that uses `bool`, `true` or `false` without including 
`<stdbool.h>`, preprocessed against the export headers of this PR (`gcc -E 
-H`): each one reaches `<stdbool.h>` through its own headers, except 11 apps 
files that get it from a header the scan does not have (the headers of packages 
that are downloaded at build time: lvgl, NimBLE, WAMR, S2OPC; the kernel's 
`mm/mm_heap/mm.h` for heaptest; `bas.c`, which includes `bas_statement.c` after 
`<stdbool.h>`), and the sim-only kernel file `mm/kasan/sim_asan.c` (sim builds 
above).
   
   Builds with `-Werror` (Linux host, NuttX CI image), with the apps PR, each 
OK: sim:nsh, sim:citest, sim:nettest, sim:usrsocktest, qemu-armv8a:nsh, 
qemu-armv8a:knsh, qemu-armv8a:nsh_smp, qemu-armv8a:citest, qemu-armv8a:netnsh, 
mps2-an500:nsh, mps3-an547:nsh, lm3s6965-ek:qemu-protected, rv-virt:nsh, 
rv-virt:knsh64, rv-virt:pnsh64, rv-virt:citest, rv-virt:netnsh, 
esp32s3-devkit:nsh, esp32s3-devkit:knsh, qemu-intel64:nsh, 
qemu-intel64:knsh_romfs, qemu-armv7a:nsh, qemu-armv7a:knsh, nucleo-f072rb:nsh, 
stm32f4discovery:nsh, stm32f746g-disco:nsh, nrf52840-dk:nsh, 
raspberrypi-pico:nsh, imxrt1064-evk:nsh, sabre-6quad:nsh.
   
   `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]

Reply via email to