casaroli opened a new pull request, #20916: URL: https://github.com/apache/nuttx/pull/20916
## Summary Four small fixes to public headers. Programs written for other C libraries fail to compile against the NuttX headers without them. There is one commit for each fix. No generated code changes. **1. Headers that are not self-contained.** `sys/random.h` uses `ssize_t` and `FAR` (it includes only `<stddef.h>`), `obstack.h` uses `FAR`, `shadow.h` uses `size_t` and `netpacket/vm_sockets.h` uses `struct sockaddr`, but they do not include what declares them. They compile only when the NuttX `<stddef.h>` is found first, or when the program includes the missing header before. A compiler driver that searches its own headers first (GCC's `stddef.h`) fails on `#include <sys/random.h>` alone. Each header now includes what it uses. **2. `FILE` markers in `<stdio.h>`.** Some headers declare their functions with `FILE` parameters only when they see that `<stdio.h>` was included. `gmp.h`, for example, tests the include guards and `FILE` markers of many C libraries (`_STDIO_H`, `__DEFINED_FILE`, `_FILE_DEFINED`, ...). The NuttX guard `__INCLUDE_STDIO_H` is in none of these lists, so a program that includes `<stdio.h>` and then `<gmp.h>` gets no `mpz_out_str()` or `gmp_fprintf()`. `<stdio.h>` now defines, next to the `FILE` typedef, the markers that glibc (`__FILE_defined`) and musl (`__DEFINED_FILE`) define next to theirs. They say only that `FILE` is declared. The include guard does not change. **3. `ffs()`, `ffsl()` and `ffsll()` in `<string.h>`.** POSIX puts them in `<strings.h>`, but other C libraries also declare them in `<string.h>`, and some programs include only `<string.h>`. A configure check can find `ffsl()` in libc and assume that `<string.h>` declares it. `<string.h>` now declares the three functions next to `strcasecmp()` and `strncasecmp()`, which are there for the same reason. The names are in parentheses, so the function-like macros of `<strings.h>` do not expand them. **4. `param` as a parameter name.** A program may define a macro of a common name, for example `#define param vname`, before it includes the system headers. Then every prototype or inline function that names a parameter `param` fails to compile. The prototypes now leave the name out, as other C libraries do, and the inline functions use other names: - `sched_setparam()`, `sched_getparam()`, `sched_setscheduler()` (`<sched.h>`), the `pthread_*schedparam()` functions (`<pthread.h>`), `posix_spawnattr_[gs]etschedparam()` (`<spawn.h>`) and the `nxsched_*_param()` functions (`nuttx/sched.h`, which `<aio.h>` brings in); - `mallopt()` (`<malloc.h>`) and `lcong48()` (`<stdlib.h>`); - the inline functions of `nuttx/lib/math32.h` (which `<stdio.h>` brings in) and `smh_call()` in the arm64 `<arch/syscall.h>` (which `<ucontext.h>` brings in). ## Impact None for code that compiles today. Three more prototypes in `<string.h>`, two more macros in `<stdio.h>`, more includes in four headers, and parameter names removed or renamed. All build modes and architectures. Documentation: no change (`Documentation/standards/posix.rst` already lists `ffs()`, `ffsl()` and `ffsll()`). ## Testing Header sweep: each public header under `include/` (not `include/nuttx`, `include/arch`, `include/cxx`, `include/libcxx`) compiled alone with `-fsyntax-only` and the flags of a `qemu-armv8a:knsh` export. Test programs, compiled with `-Werror=implicit-function-declaration` and run on `qemu-armv8a:knsh` (QEMU with HVF): - `t.c`: includes `<stdio.h>`, then `gmp.h` of GMP 6.3, and calls `gmp_printf()` and `mpz_out_str()`. - `ffsltest.c`: a file that includes only `<string.h>` takes the address of `ffsl` and `ffsll` (as a configure check does) and calls the three functions; then it includes `<strings.h>` and calls them again. - `paramtest.c`: defines `vname` and `param` as macros, includes `<malloc.h>`, `<pthread.h>`, `<sched.h>`, `<spawn.h>`, `<stdio.h>`, `<stdlib.h>` and `<ucontext.h>`, and calls `sched_getparam()`. | | master | this PR | |---|---|---| | sweep, GCC's include directory first | 21 of 233 fail | 17 of 233 fail | | sweep, the export's include directory first | 19 of 233 fail | 17 of 233 fail | | sweep with `-Dparam=@@@` | 67 of 233 fail (48 only because of `param`) | 17 of 233 fail | | `t.c` | implicit declaration of function `__gmpz_out_str` | compiles | | `ffsltest.c` | `'ffsl' undeclared`, implicit declaration of `ffs` | PASS | | `paramtest.c` | `expected ';', ',' or ')' before '.' token` | PASS | | ostest, `qemu-armv8a:knsh` and `qemu-armv8a:nsh` | status 0 | status 0 | The 17 headers that still fail are not changed here: `metal/system/nuttx/*.h` (9) and `sys/syscall_lookup.h` are not meant to be included alone; `crypto/cast.h`, `chachapoly.h`, `cmac.h`, `idgen.h` and `key_wrap.h` are crypto internals that their users include after their prerequisites; `netpacket/bluetooth.h` needs `bt_addr_t` from the Bluetooth configuration; `openamp/virtio_ring.h` is third-party code. Build, each OK with no new warnings: `qemu-armv8a:nsh` (flat, arm64), `qemu-armv8a:knsh` (kernel, arm64), `rv-virt:knsh64` (kernel, rv64), `rv-virt:pnsh64` (protected, rv64), `lm3s6965-ek:qemu-protected` (protected, armv7-m). `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]
