[clang] [clang] Add __nullptr as a keyword to C (PR #123119)

Aidan Goldfarb via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 24 12:38:52 PST 2025


https://github.com/AidanGoldfarb updated https://github.com/llvm/llvm-project/pull/123119

>From 51372333df218cfb4fa8dcc0cebee03c0e3ebc5f Mon Sep 17 00:00:00 2001
From: Aidan <aidan.goldfarb at mail.mcgill.ca>
Date: Wed, 15 Jan 2025 15:24:12 -0500
Subject: [PATCH 1/7] __nullptr -> KEYALL, added relevant test

---
 clang/include/clang/Basic/TokenKinds.def | 2 +-
 clang/test/Sema/nullptr.c                | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def
index 2c692c999bdff5..8902a20b07ffa8 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -707,7 +707,7 @@ ALIAS("__decltype"       , decltype     , KEYCXX)
 ALIAS("__imag__"         , __imag       , KEYALL)
 ALIAS("__inline"         , inline       , KEYALL)
 ALIAS("__inline__"       , inline       , KEYALL)
-ALIAS("__nullptr"        , nullptr      , KEYCXX)
+ALIAS("__nullptr"        , nullptr      , KEYALL)
 ALIAS("__real__"         , __real       , KEYALL)
 ALIAS("__restrict"       , restrict     , KEYALL)
 ALIAS("__restrict__"     , restrict     , KEYALL)
diff --git a/clang/test/Sema/nullptr.c b/clang/test/Sema/nullptr.c
index d11765a9c881a1..64095fc00691cd 100644
--- a/clang/test/Sema/nullptr.c
+++ b/clang/test/Sema/nullptr.c
@@ -108,3 +108,6 @@ void test_f1() {
   int ir = (f1)(nullptr);
 }
 
+// __nullptr keyword in C
+void foo(void *);
+void bar() { foo(__nullptr); }
\ No newline at end of file

>From bd9fe6016717b805c37e7fce5ee70dddc6c42eb7 Mon Sep 17 00:00:00 2001
From: Aidan Goldfarb <47676355+AidanGoldfarb at users.noreply.github.com>
Date: Fri, 17 Jan 2025 12:00:04 -0500
Subject: [PATCH 2/7] Added additional tests

static_assert(nullptr == __nullptr) and static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false))
---
 clang/test/Sema/nullptr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/nullptr.c b/clang/test/Sema/nullptr.c
index 64095fc00691cd..ca2a8aa064caf3 100644
--- a/clang/test/Sema/nullptr.c
+++ b/clang/test/Sema/nullptr.c
@@ -110,4 +110,6 @@ void test_f1() {
 
 // __nullptr keyword in C
 void foo(void *);
-void bar() { foo(__nullptr); }
\ No newline at end of file
+void bar() { foo(__nullptr); }
+static_assert(nullptr == __nullptr);
+static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false));

>From 7ec48387c8a3e9b831fd3c5b1400ad5e5ee32e21 Mon Sep 17 00:00:00 2001
From: Aidan Goldfarb <47676355+AidanGoldfarb at users.noreply.github.com>
Date: Sat, 18 Jan 2025 11:56:02 -0500
Subject: [PATCH 3/7] Update nullptr.c

---
 clang/test/Sema/nullptr.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/test/Sema/nullptr.c b/clang/test/Sema/nullptr.c
index ca2a8aa064caf3..bbe3d4c2ece7ff 100644
--- a/clang/test/Sema/nullptr.c
+++ b/clang/test/Sema/nullptr.c
@@ -113,3 +113,8 @@ void foo(void *);
 void bar() { foo(__nullptr); }
 static_assert(nullptr == __nullptr);
 static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false));
+
+void foo(struct S *);
+void bar() { foo(__nullptr); } // Test that it converts properly to an arbitrary pointer type without warning
+_Static_assert(__nullptr == 0); // Test that its value matches that of NULL
+_Static_assert(_Generic(__typeof(__nullptr), int : 0, void * : 0, default : 1)); // Test that it's type is not the same as what NULL would generally have.

>From cfda1e8d3bf61a84fdebd4a0b61ca114811e2af0 Mon Sep 17 00:00:00 2001
From: Aidan Goldfarb <47676355+AidanGoldfarb at users.noreply.github.com>
Date: Sat, 18 Jan 2025 12:04:37 -0500
Subject: [PATCH 4/7] Update ReleaseNotes.rst

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index befa411e882b4c..5561e15e852448 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -325,6 +325,7 @@ C Language Changes
 ------------------
 
 - Extend clang's ``<limits.h>`` to define ``LONG_LONG_*`` macros for Android's bionic.
+- Added support for the ``__nullptr`` keyword. 
 
 C2y Feature Support
 ^^^^^^^^^^^^^^^^^^^

>From 611c7022d6be9794e8242484b011fdf3be328353 Mon Sep 17 00:00:00 2001
From: Aidan Goldfarb <47676355+AidanGoldfarb at users.noreply.github.com>
Date: Sat, 18 Jan 2025 12:20:46 -0500
Subject: [PATCH 5/7] Update nullptr.c

Removed duplicate test
---
 clang/test/Sema/nullptr.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/test/Sema/nullptr.c b/clang/test/Sema/nullptr.c
index bbe3d4c2ece7ff..b5d99ab89f5814 100644
--- a/clang/test/Sema/nullptr.c
+++ b/clang/test/Sema/nullptr.c
@@ -113,8 +113,5 @@ void foo(void *);
 void bar() { foo(__nullptr); }
 static_assert(nullptr == __nullptr);
 static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false));
-
-void foo(struct S *);
-void bar() { foo(__nullptr); } // Test that it converts properly to an arbitrary pointer type without warning
 _Static_assert(__nullptr == 0); // Test that its value matches that of NULL
 _Static_assert(_Generic(__typeof(__nullptr), int : 0, void * : 0, default : 1)); // Test that it's type is not the same as what NULL would generally have.

>From 8d00a2c3e77a79544bf9cd90fa1d2bce9e33b65c Mon Sep 17 00:00:00 2001
From: Aidan Goldfarb <agoldfa7 at u.rochester.edu>
Date: Tue, 21 Jan 2025 10:34:24 -0500
Subject: [PATCH 6/7] Update clang/docs/ReleaseNotes.rst

Co-authored-by: Erich Keane <ekeane at nvidia.com>
---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5561e15e852448..1cee4d764939e1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -325,7 +325,7 @@ C Language Changes
 ------------------
 
 - Extend clang's ``<limits.h>`` to define ``LONG_LONG_*`` macros for Android's bionic.
-- Added support for the ``__nullptr`` keyword. 
+- Exposed the the ``__nullptr`` keyword as an alias for ``nullptr`` in all C language modes.
 
 C2y Feature Support
 ^^^^^^^^^^^^^^^^^^^

>From f2f79542f1acb31233e4c932df905145771b9099 Mon Sep 17 00:00:00 2001
From: Aidan <aidan.goldfarb at mail.mcgill.ca>
Date: Fri, 24 Jan 2025 15:38:39 -0500
Subject: [PATCH 7/7] added c17 tests

---
 clang/test/Sema/nullptr-prec2x.c | 4 ++++
 clang/test/Sema/nullptr.c        | 2 --
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/test/Sema/nullptr-prec2x.c b/clang/test/Sema/nullptr-prec2x.c
index 39479d4343a569..c516c448ca1ab4 100644
--- a/clang/test/Sema/nullptr-prec2x.c
+++ b/clang/test/Sema/nullptr-prec2x.c
@@ -6,3 +6,7 @@ int nullptr; // expected-warning {{'nullptr' is a keyword in C23}}
 
 nullptr_t val; // expected-error {{unknown type name 'nullptr_t'}}
 
+void foo(void *);
+void bar() { foo(__nullptr); } // Test that it converts properly to an arbitrary pointer type without warning
+_Static_assert(__nullptr == 0, "value of __nullptr"); // Test that its value matches that of NULL
+_Static_assert(_Generic(__typeof(__nullptr), int : 0, void * : 0, default : 1), "type of __nullptr"); // Test that it's type is not the same as what NULL would generally have.
diff --git a/clang/test/Sema/nullptr.c b/clang/test/Sema/nullptr.c
index b5d99ab89f5814..ca2a8aa064caf3 100644
--- a/clang/test/Sema/nullptr.c
+++ b/clang/test/Sema/nullptr.c
@@ -113,5 +113,3 @@ void foo(void *);
 void bar() { foo(__nullptr); }
 static_assert(nullptr == __nullptr);
 static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false));
-_Static_assert(__nullptr == 0); // Test that its value matches that of NULL
-_Static_assert(_Generic(__typeof(__nullptr), int : 0, void * : 0, default : 1)); // Test that it's type is not the same as what NULL would generally have.



More information about the cfe-commits mailing list