[PATCH 2/2] sys/cdefs.h: fix for use __restrict in C++

Corinna Vinschen vinschen@redhat.com
Mon Aug 7 13:15:49 GMT 2023


Hi Sebastian,

On Aug  7 11:28, Sebastian Huber wrote:
> diff --git a/newlib/libc/include/sys/cdefs.h b/newlib/libc/include/sys/cdefs.h
> index 808498c50b..cc1a8a1ccb 100644
> --- a/newlib/libc/include/sys/cdefs.h
> +++ b/newlib/libc/include/sys/cdefs.h
> @@ -412,17 +412,15 @@
>  #endif
>  
>  /*
> - * GCC 2.95 provides `__restrict' as an extension to C90 to support the
> - * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
> - * a way to define the `restrict' type qualifier without disturbing older
> - * software that is unaware of C99 keywords.
> + * We use `__restrict' as a way to define the `restrict' type qualifier
> + * without disturbing older software that is unaware of C99 keywords.
> + * GCC also provides `__restrict' as an extension to support C99-style
> + * restricted pointers in other language modes.
>   */
> -#if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
> -#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
> -#define	__restrict
> -#else
> +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
>  #define	__restrict	restrict
> -#endif
> +#elif !__GNUC_PREREQ__(2, 95)
> +#define	__restrict
>  #endif
>  
>  /*

Turns out, this leads to a build failuer in Cygwin.

We have a definition in aio.h:

  int lio_listio (int, struct aiocb *__restrict const [__restrict], int,
		  struct sigevent *__restrict);

and a matching one in aio.cc:

  int 
  lio_listio (int mode, struct aiocb *__restrict const aiolist[__restrict],
              int nent, struct sigevent *__restrict sig)

The problem is the bracket expression.  The restrict keyword is allowed
in C90, but not in C++.

GLibc has a special definition __restrict_arr in cdefs.h, which is used
in the brackets of the above definition:

  /* ISO C99 also allows to declare arrays as non-overlapping.  The syntax is
       array_name[restrict]
     GCC 3.1 and clang support this.
     This syntax is not usable in C++ mode.  */
  #if (__GNUC_PREREQ (3,1) || __clang_major__ >= 3) && !defined __cplusplus
  # define __restrict_arr __restrict
  #else
  # ifdef __GNUC__
  #  define __restrict_arr        /* Not supported in old GCC.  */
  # else
  #  if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  #   define __restrict_arr       restrict
  #  else 
  /* Some other non-C99 compiler.  */
  #   define __restrict_arr       /* Not supported.  */
  #  endif
  # endif
  #endif  

This doesn't exist in FreeBSD, though.

Do you think it's ok to add it to our cdefs.h, nevertheless?


Thanks,
Corinna



More information about the Newlib mailing list