[PR95700,v2] Redefine NULL to nullptr

Message ID 20200630131728.1212645-1-iii@linux.ibm.com
State New
Headers show
Series
  • [PR95700,v2] Redefine NULL to nullptr
Related show

Commit Message

Peter Bergner via Gcc-patches June 30, 2020, 1:17 p.m.
v1: https://gcc.gnu.org/pipermail/gcc-patches/2020-June/548822.html

This is the implementation of Richard's suggestion.  I've decided not
to remove the initial #define NULL 0, since system.h mixes inclusion of
system and helper headers (e.g. libiberty), which might use NULL - if
not today, then in the future.  If system headers do not provide NULL,
removing #define NULL 0 might lead to build errors.  Ideally the order
should be:

  system headers
  #undef NULL
  #define NULL nullptr
  helper headers

but I fear that rearranging includes might break some weird system.

This patch helps with alpine bootstrap, I'm currently testing it on
x86_64-redhat-linux.

---

Bootstrap with musl libc fails with numerous "missing sentinel in
function call" errors.  This is because musl defines NULL as 0L for C++,
but gcc requires sentinel value to be a pointer or __null.

Jonathan Wakely says:

    To be really safe during stage 1, GCC should not use NULL as a
    pointer sentinel in C++ code anyway.

    The bootstrap compiler could define it to 0 or 0u, neither of which
    is guaranteed to be OK to pass as a varargs sentinel where a null
    pointer is expected.  Any of (void*)0 or (void*)NULL or nullptr
    would be safe.

While it is possible to fix this by replacing NULL sentinels with
nullptrs, such approach would generate backporting conflicts, therefore
simply redefine NULL to nullptr at the end of system.h, where it would
not confuse system headers.

gcc/ChangeLog:

2020-06-30  Ilya Leoshkevich  <iii@linux.ibm.com>

	PR bootstrap/95700
	* system.h (NULL): Redefine to nullptr.
---
 gcc/system.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

-- 
2.25.4

Comments

Richard Sandiford June 30, 2020, 2:27 p.m. | #1
Ilya Leoshkevich <iii@linux.ibm.com> writes:
> v1: https://gcc.gnu.org/pipermail/gcc-patches/2020-June/548822.html

>

> This is the implementation of Richard's suggestion.  I've decided not

> to remove the initial #define NULL 0, since system.h mixes inclusion of

> system and helper headers (e.g. libiberty), which might use NULL - if

> not today, then in the future.  If system headers do not provide NULL,

> removing #define NULL 0 might lead to build errors.  Ideally the order

> should be:

>

>   system headers

>   #undef NULL

>   #define NULL nullptr

>   helper headers

>

> but I fear that rearranging includes might break some weird system.

>

> This patch helps with alpine bootstrap, I'm currently testing it on

> x86_64-redhat-linux.

>

> ---

>

> Bootstrap with musl libc fails with numerous "missing sentinel in

> function call" errors.  This is because musl defines NULL as 0L for C++,

> but gcc requires sentinel value to be a pointer or __null.

>

> Jonathan Wakely says:

>

>     To be really safe during stage 1, GCC should not use NULL as a

>     pointer sentinel in C++ code anyway.

>

>     The bootstrap compiler could define it to 0 or 0u, neither of which

>     is guaranteed to be OK to pass as a varargs sentinel where a null

>     pointer is expected.  Any of (void*)0 or (void*)NULL or nullptr

>     would be safe.

>

> While it is possible to fix this by replacing NULL sentinels with

> nullptrs, such approach would generate backporting conflicts, therefore

> simply redefine NULL to nullptr at the end of system.h, where it would

> not confuse system headers.

>

> gcc/ChangeLog:

>

> 2020-06-30  Ilya Leoshkevich  <iii@linux.ibm.com>

>

> 	PR bootstrap/95700

> 	* system.h (NULL): Redefine to nullptr.


LGTM, thanks.  OK if noone objects in the next 24hrs.

Richard

> ---

>  gcc/system.h | 10 ++++++++++

>  1 file changed, 10 insertions(+)

>

> diff --git a/gcc/system.h b/gcc/system.h

> index 544f7ba427f..1eec77e730e 100644

> --- a/gcc/system.h

> +++ b/gcc/system.h

> @@ -1244,4 +1244,14 @@ void gcc_stablesort (void *, size_t, size_t,

>     of the number.  */

>  #define PRsa(n) "%" #n PRIu64 "%c"

>  

> +/* System headers may define NULL to be an integer (e.g. 0L), which cannot be

> +   used safely in certain contexts (e.g. as sentinels).  Redefine NULL to

> +   nullptr in order to make it safer.  Note that this might confuse system

> +   headers, however, by convention they must not be included after this point.

> +*/

> +#ifdef __cplusplus

> +#undef NULL

> +#define NULL nullptr

> +#endif

> +

>  #endif /* ! GCC_SYSTEM_H */

Patch

diff --git a/gcc/system.h b/gcc/system.h
index 544f7ba427f..1eec77e730e 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -1244,4 +1244,14 @@  void gcc_stablesort (void *, size_t, size_t,
    of the number.  */
 #define PRsa(n) "%" #n PRIu64 "%c"
 
+/* System headers may define NULL to be an integer (e.g. 0L), which cannot be
+   used safely in certain contexts (e.g. as sentinels).  Redefine NULL to
+   nullptr in order to make it safer.  Note that this might confuse system
+   headers, however, by convention they must not be included after this point.
+*/
+#ifdef __cplusplus
+#undef NULL
+#define NULL nullptr
+#endif
+
 #endif /* ! GCC_SYSTEM_H */