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

Aidan Goldfarb via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 18 09:04:47 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/4] __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/4] 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/4] 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/4] 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
 ^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list