Skip to content

Commit 7fd5833

Browse files
AidanGoldfarbAidanerichkeaneAaronBallman
authored
[clang] Add __nullptr as a keyword to C (#123119)
This PR resolves #121503. --------- Co-authored-by: Aidan <[email protected]> Co-authored-by: Erich Keane <[email protected]> Co-authored-by: Aaron Ballman <[email protected]>
1 parent 0e372c3 commit 7fd5833

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ available in all language modes.
514514
__nullptr
515515
---------
516516

517-
``__nullptr`` is an alternate spelling for ``nullptr``, but is also available in
518-
C++ modes prior to C++11. Note that it's currently not availbale in C despite
519-
C23 having support for ``nullptr``.
517+
``__nullptr`` is an alternate spelling for ``nullptr``. It is available in all C and C++ language modes.
520518

521519
__signed, __signed__
522520
--------------------

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ C Language Changes
388388

389389
- Extend clang's ``<limits.h>`` to define ``LONG_LONG_*`` macros for Android's bionic.
390390
- Macro ``__STDC_NO_THREADS__`` is no longer necessary for MSVC 2022 1939 and later.
391+
- Exposed the the ``__nullptr`` keyword as an alias for ``nullptr`` in all C language modes.
391392

392393
C2y Feature Support
393394
^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ ALIAS("__decltype" , decltype , KEYCXX)
707707
ALIAS("__imag__" , __imag , KEYALL)
708708
ALIAS("__inline" , inline , KEYALL)
709709
ALIAS("__inline__" , inline , KEYALL)
710-
ALIAS("__nullptr" , nullptr , KEYCXX)
710+
ALIAS("__nullptr" , nullptr , KEYALL)
711711
ALIAS("__real__" , __real , KEYALL)
712712
ALIAS("__restrict" , restrict , KEYALL)
713713
ALIAS("__restrict__" , restrict , KEYALL)

clang/test/Sema/nullptr-prec2x.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ int nullptr; // expected-warning {{'nullptr' is a keyword in C23}}
66

77
nullptr_t val; // expected-error {{unknown type name 'nullptr_t'}}
88

9+
void foo(void *);
10+
void bar() { foo(__nullptr); } // Test that it converts properly to an arbitrary pointer type without warning
11+
_Static_assert(__nullptr == 0, "value of __nullptr"); // Test that its value matches that of NULL
12+
_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.

clang/test/Sema/nullptr.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ void test_f1() {
108108
int ir = (f1)(nullptr);
109109
}
110110

111+
// __nullptr keyword in C
112+
void foo(void *);
113+
void bar() { foo(__nullptr); }
114+
static_assert(nullptr == __nullptr);
115+
static_assert(__nullptr == 0); // Test that its value matches that of NULL
116+
static_assert(_Generic(typeof(__nullptr), nullptr_t: true, default: false));
117+
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.

0 commit comments

Comments
 (0)