Re: [PATCH] features.h: Fix -Wundef problems

Corinna Vinschen <[email protected]> Mon, 1 Dec 2025 16:49:45 +0100
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hi Stefan,

On Nov 29 23:07, Stefan Tauner wrote:
> -Wundef warns if an undefined identifier is evaluated in an #if
> directive. This would be valid as they are replaced with 0. However, it
> is often an early warning sign and not intentional. To allow for
> enabling -Wundef even outside system directories (where compilers
> ignore such problems unless -Wsystem-headers is enabled) this patch
> adds the required defined() checks.
> 
> glibc also has been supporting this for 10 years now:
>   https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=f248238cf43bd751db29e6f151d6da7645337ff5
> 
> I have not exhaustively tested this but you can see the effect with
> something like the following (+ using -stdc= and/or -D...):
>   echo | gcc -include newlib/libc/include/sys/features.h -Inewlib/libc/include/ -E - -Wundef
> 
> Signed-off-by: Stefan Tauner <[email protected]>
> ---
>  newlib/libc/include/sys/features.h | 51 +++++++++++++++++-------------
>  1 file changed, 29 insertions(+), 22 deletions(-)
> 
> diff --git a/newlib/libc/include/sys/features.h b/newlib/libc/include/sys/features.h
> index f0f5286ac..9dbdd5739 100644
> --- a/newlib/libc/include/sys/features.h
> +++ b/newlib/libc/include/sys/features.h
> @@ -156,15 +156,15 @@ extern "C" {
>  
>  #if !defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && \
>    ((!defined(__STRICT_ANSI__) && !defined(_ANSI_SOURCE)) || \
> -   (_XOPEN_SOURCE - 0) >= 500)
> +  (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500))
>  #define	_POSIX_SOURCE		1
> -#if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) >= 700
> +#if !defined(_XOPEN_SOURCE) || _XOPEN_SOURCE >= 700

GLibc adds `defined', but retains the `(MACRO - 0)' expression, even
today, 10 years later.  I didn't test it, but shouldn't we do the same?


Corinna