newlib header breaks restricted pointers in C++

Bruno De Fraine Bruno.DeFraine@synopsys.com
Mon Jul 24 22:23:26 GMT 2023


Hello,

As an extension, GCC and clang offer C99-style restricted pointers in C++ mode:
https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html

We notice that this extension is broken when including newlib headers: restricted pointers are treated as ordinary pointers.

We traced this to the following section of newlib/libc/include/sys/cdefs.h:

  /*
   * 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.
   */
  #if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
  #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
  #define	__restrict
  #else
  #define	__restrict	restrict
  #endif
  #endif

While the GCC __restrict extension was indeed introduced in GCC 2.95, it is not limited to this version; the extension is also not limited to C90:
https://gcc.gnu.org/gcc-2.95/c++features.html

This is a suggestion how the logic in the header could be rewritten:

  /*
   * 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 defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
  #define	__restrict	restrict
  #elif !__GNUC_PREREQ__(2, 95)
  #define	__restrict
  #endif

Regards,
Bruno De Fraine


More information about the Newlib mailing list