mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Define NULL as __null in C++ mode when using GCC or Clang.
@ 2019-07-09 19:19 James Y Knight
  2019-07-09 19:38 ` Rich Felker
  0 siblings, 1 reply; 17+ messages in thread
From: James Y Knight @ 2019-07-09 19:19 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 958 bytes --]

Both GCC and Clang ship their own stddef.h which does this (musl's
stddef.h is simply ignored). But, musl also defines the macro in a
number of other headers. Thus, depending on which header you include
last, you'll end up with a different definition of NULL.

Mostly, getting musl's definition simply degrades warning diagnostics
in C++ slightly -- e.g. GCC can no longer emit this warning:
  warning: passing NULL to non-pointer argument 1 of 'int foo(long int)'
[-Wconversion-null]

If you're using Clang's modules support, it can also break
compilation. In that case, the conflicting definitions may be detected
as a module incompatibility.

A different (potentially better) fix would be to always retrieve the
definition of NULL from the compiler's stddef.h (via #define
__need_NULL #include <stddef.h>). It may also be best to delete the
musl stddef.h entirely for clarity, since it's currently ignored,
anyhow.

But, this seemed the more minimal fix.

[-- Attachment #1.2: Type: text/html, Size: 1084 bytes --]

[-- Attachment #2: 0001-Define-NULL-as-__null-in-C-mode-when-using-GCC-or-Cl.patch --]
[-- Type: text/x-patch, Size: 3931 bytes --]

From 3d898d4825c28f08ade92c40822fa5bfa2ef1f1f Mon Sep 17 00:00:00 2001
From: James Y Knight <jyknight@google.com>
Date: Tue, 9 Jul 2019 15:03:03 -0400
Subject: [PATCH] Define NULL as __null in C++ mode when using GCC or Clang.

Both GCC and Clang ship their own stddef.h which does this (musl's
stddef.h is simply ignored). But, musl also defines the macro in a
number of other headers. Thus, depending on which header you include
last, you'll end up with a different definition of NULL.

Mostly, getting musl's definition simply degrades warning diagnostics
in C++ slightly -- e.g. GCC can no longer emit this warning:
  warning: passing NULL to non-pointer argument 1 of 'int foo(long int)' [-Wconversion-null]

If you're using Clang's modules support, it can also break
compilation. In that case, the conflicting definitions may be detected
as a module incompatibility.

A different (potentially better) fix would be to always retrieve the
definition of NULL from the compiler's stddef.h (via #define
__need_NULL #include <stddef.h>). It may also be best to delete the
musl stddef.h entirely for clarity, since it's currently ignored,
anyhow.

But, this seemed the more minimal fix.
---
 include/locale.h | 4 ++++
 include/stddef.h | 4 ++++
 include/stdio.h  | 4 ++++
 include/stdlib.h | 4 ++++
 include/string.h | 4 ++++
 include/time.h   | 4 ++++
 include/unistd.h | 4 ++++
 include/wchar.h  | 4 ++++
 8 files changed, 32 insertions(+)

diff --git a/include/locale.h b/include/locale.h
index ce384381..80c2d2db 100644
--- a/include/locale.h
+++ b/include/locale.h
@@ -8,7 +8,11 @@ extern "C" {
 #include <features.h>
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/stddef.h b/include/stddef.h
index bd753853..415a2d91 100644
--- a/include/stddef.h
+++ b/include/stddef.h
@@ -2,7 +2,11 @@
 #define _STDDEF_H
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/stdio.h b/include/stdio.h
index 3604198c..9e30d1ad 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -26,7 +26,11 @@ extern "C" {
 #include <bits/alltypes.h>
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/stdlib.h b/include/stdlib.h
index 42ca8336..a272a4f4 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -8,7 +8,11 @@ extern "C" {
 #include <features.h>
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/string.h b/include/string.h
index 795a2abc..4d344d72 100644
--- a/include/string.h
+++ b/include/string.h
@@ -8,7 +8,11 @@ extern "C" {
 #include <features.h>
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/time.h b/include/time.h
index 672b3fc3..0eec373c 100644
--- a/include/time.h
+++ b/include/time.h
@@ -8,7 +8,11 @@ extern "C" {
 #include <features.h>
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/unistd.h b/include/unistd.h
index 9485da7a..391b58ba 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -16,7 +16,11 @@ extern "C" {
 #define SEEK_END 2
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
diff --git a/include/wchar.h b/include/wchar.h
index 88eb55b1..bfdbaebb 100644
--- a/include/wchar.h
+++ b/include/wchar.h
@@ -39,7 +39,11 @@ extern "C" {
 #endif
 
 #ifdef __cplusplus
+#ifdef __GNUC__
+#define NULL __null
+#else
 #define NULL 0L
+#endif
 #else
 #define NULL ((void*)0)
 #endif
-- 
2.22.0.410.gd8fdbe21b5-goog


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2019-07-10 21:44 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-09 19:19 [PATCH] Define NULL as __null in C++ mode when using GCC or Clang James Y Knight
2019-07-09 19:38 ` Rich Felker
2019-07-09 23:04   ` James Y Knight
2019-07-10  2:03     ` Szabolcs Nagy
2019-07-10  6:34       ` Florian Weimer
2019-07-10  8:46         ` Szabolcs Nagy
2019-07-10 16:18         ` James Y Knight
2019-07-10 16:44           ` Rich Felker
2019-07-10 17:35             ` James Y Knight
2019-07-10 20:11               ` A. Wilcox
2019-07-10 20:19                 ` Michael Everitt
2019-07-10 20:45                 ` Rich Felker
2019-07-10 20:48               ` Rich Felker
2019-07-10 21:11                 ` Szabolcs Nagy
2019-07-10 21:16                   ` Rich Felker
2019-07-10 21:44                     ` Szabolcs Nagy
2019-07-10 16:01       ` James Y Knight

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).