| 1 |
/* Test valid usage of C23 nullptr. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
// { dg-require-effective-target thread_fence } |
| 4 |
/* { dg-options "-std=c23 -pedantic-errors -Wall -Wextra -Wno-unused-variable" } */ |
| 5 |
/* { dg-xfail-if "__atomic not supported" { "avr-*-*" } } */ |
| 6 |
|
| 7 |
#include <stdarg.h> |
| 8 |
|
| 9 |
typedef __typeof__(nullptr) nullptr_t; |
| 10 |
|
| 11 |
void f1 (nullptr_t) { } |
| 12 |
void f2 (int *) { } |
| 13 |
void f3 (_Bool) { } |
| 14 |
nullptr_t cmp (void) { return nullptr; } |
| 15 |
|
| 16 |
/* The type nullptr_t shall not be converted to any type other than void, bool |
| 17 |
or a pointer type. No type other than nullptr_t or a null pointer constant |
| 18 |
shall be converted to nullptr_t. */ |
| 19 |
void |
| 20 |
test1 (void) |
| 21 |
{ |
| 22 |
const nullptr_t nptr = nullptr; |
| 23 |
static nullptr_t static_nptr; |
| 24 |
int *p1 = nullptr; |
| 25 |
void *p2 = nullptr; |
| 26 |
float *p3 = nullptr; |
| 27 |
void (*p4)(int) = nullptr; |
| 28 |
int (*p5)[10] = nullptr; |
| 29 |
int *p6 = nptr; |
| 30 |
void *p7 = nptr; |
| 31 |
float *p8 = nptr; |
| 32 |
void (*p9)(int) = nptr; |
| 33 |
int (*p10)[10] = nptr; |
| 34 |
int *p11 = (int *) nullptr; |
| 35 |
int *p12 = (int *) nptr; |
| 36 |
int *p13 = (nullptr); |
| 37 |
int *p14 = _Generic(0, int : nullptr); |
| 38 |
if (nullptr || p1 || p2 || p3 || p4 || p5 || p6 || p7 || p8 || p9 || p10 |
| 39 |
|| p11 || p12 || p13 || p14) |
| 40 |
__builtin_abort (); |
| 41 |
|
| 42 |
_Bool b1 = nullptr; |
| 43 |
_Bool b2 = (_Bool) nullptr; |
| 44 |
_Bool b3 = nptr; |
| 45 |
_Bool b4 = (_Bool) nptr; |
| 46 |
_Bool b5 = _Generic(0, int : nullptr); |
| 47 |
if (b1 || b2 || b3 || b4 || b5 || (_Bool) nullptr || (_Bool) nptr) |
| 48 |
__builtin_abort (); |
| 49 |
|
| 50 |
__auto_type a1 = nullptr; |
| 51 |
__auto_type a2 = nptr; |
| 52 |
|
| 53 |
/* We can convert nullptr_t to nullptr_t. */ |
| 54 |
__typeof__(nullptr) x = nullptr; |
| 55 |
f1 (x); |
| 56 |
f1 (nullptr); |
| 57 |
f1 (_Generic(0, int : nullptr)); |
| 58 |
f2 (x); |
| 59 |
f2 (nullptr); |
| 60 |
f3 (nullptr); |
| 61 |
|
| 62 |
const nullptr_t np1 = nullptr; |
| 63 |
const nullptr_t np2 = np1; |
| 64 |
(void) nullptr; |
| 65 |
(void) np1; |
| 66 |
(void) np2; |
| 67 |
(void) cmp (); |
| 68 |
(void)(nullptr_t) nullptr; |
| 69 |
|
| 70 |
const nullptr_t n = 0; |
| 71 |
(void) (nullptr_t) 0; |
| 72 |
|
| 73 |
f1 (0); |
| 74 |
f1 ((void *) 0); |
| 75 |
f1 (0L); |
| 76 |
nullptr_t n2; |
| 77 |
n2 = (void *) 0; |
| 78 |
n2 = 123 - 123; |
| 79 |
(void) n2; |
| 80 |
} |
| 81 |
|
| 82 |
/* Test valid comparison. */ |
| 83 |
void |
| 84 |
test2 (int *p) |
| 85 |
{ |
| 86 |
/* If both operands have type nullptr_t or one operand has type nullptr_t |
| 87 |
and the other is a null pointer constant, they compare equal. */ |
| 88 |
const nullptr_t nptr = nullptr; |
| 89 |
int r = 0; |
| 90 |
|
| 91 |
/* Both operands have type nullptr_t. */ |
| 92 |
r |= nullptr != nullptr; |
| 93 |
r |= cmp () != nullptr; |
| 94 |
r |= nullptr != cmp (); |
| 95 |
r |= !(nullptr == nullptr); |
| 96 |
r |= !(cmp () == nullptr); |
| 97 |
r |= !(nullptr == cmp ()); |
| 98 |
r |= nptr != nptr; |
| 99 |
r |= cmp () != nptr; |
| 100 |
r |= nptr != cmp (); |
| 101 |
r |= !(nptr == nptr); |
| 102 |
r |= !(cmp () == nptr); |
| 103 |
r |= !(nptr == cmp ()); |
| 104 |
|
| 105 |
/* One operand has type nullptr_t and the other is a null pointer constant. */ |
| 106 |
r |= nullptr != (void *) 0; |
| 107 |
r |= _Generic(0, int : nullptr) != (void *) 0; |
| 108 |
r |= (nullptr) != (void *) 0; |
| 109 |
r |= !(nullptr == (void *) 0); |
| 110 |
r |= (void *) 0 != nullptr; |
| 111 |
r |= (void *) 0 != (nullptr); |
| 112 |
r |= !((void *) 0 == nullptr); |
| 113 |
r |= nullptr != 0; |
| 114 |
r |= _Generic(0, int : nullptr) != 0; |
| 115 |
r |= (nullptr) != 0; |
| 116 |
r |= 0 != nullptr; |
| 117 |
r |= 0 != (nullptr); |
| 118 |
r |= !(nullptr == 0); |
| 119 |
r |= !(0 == nullptr); |
| 120 |
r |= nullptr != 0u; |
| 121 |
r |= 0u != nullptr; |
| 122 |
r |= !(nullptr == 0u); |
| 123 |
r |= !(0u == nullptr); |
| 124 |
r |= nptr != (void *) 0; |
| 125 |
r |= !(nptr == (void *) 0); |
| 126 |
r |= (void *) 0 != nptr; |
| 127 |
r |= !((void *) 0 == nptr); |
| 128 |
r |= nptr != 0; |
| 129 |
r |= 0 != nptr; |
| 130 |
r |= !(nptr == 0); |
| 131 |
r |= !(0 == nptr); |
| 132 |
r |= nptr != 0u; |
| 133 |
r |= 0u != nptr; |
| 134 |
r |= !(nptr == 0u); |
| 135 |
r |= !(0u == nptr); |
| 136 |
r |= nptr != _Generic(0, int : nullptr); |
| 137 |
r |= _Generic(0, int : nullptr) != nptr; |
| 138 |
if (r) |
| 139 |
__builtin_abort (); |
| 140 |
|
| 141 |
/* One operand is a pointer and the other is a null pointer constant. */ |
| 142 |
(void) (p == nullptr); |
| 143 |
(void) (p != nullptr); |
| 144 |
(void) (nullptr == p); |
| 145 |
(void) (nullptr != p); |
| 146 |
(void) (p == (nullptr)); |
| 147 |
(void) (p != (nullptr)); |
| 148 |
(void) ((nullptr) == p); |
| 149 |
(void) ((nullptr) != p); |
| 150 |
(void) ((void *)nullptr == nullptr); |
| 151 |
(void) ((void *)nullptr != nullptr); |
| 152 |
(void) (nullptr == (void *)nullptr); |
| 153 |
(void) (nullptr != (void *)nullptr); |
| 154 |
(void) (p == _Generic(0, int : nullptr)); |
| 155 |
(void) (p != _Generic(0, int : nullptr)); |
| 156 |
(void) (_Generic(0, int : nullptr) == p); |
| 157 |
(void) (_Generic(0, int : nullptr) != p); |
| 158 |
|
| 159 |
/* "(nullptr_t)nullptr" has type nullptr_t but isn't an NPC; these |
| 160 |
comparisons are valid after C23 CD comments GB-071 and FR-073 were |
| 161 |
resolved by the wording in N3077. */ |
| 162 |
(void) ((nullptr_t)nullptr == p); |
| 163 |
(void) ((nullptr_t)nullptr != p); |
| 164 |
(void) (p == (nullptr_t)nullptr); |
| 165 |
(void) (p != (nullptr_t)nullptr); |
| 166 |
(void) (cmp () == p); |
| 167 |
(void) (cmp () != p); |
| 168 |
(void) (p == cmp ()); |
| 169 |
(void) (p != cmp ()); |
| 170 |
/* "(void *)nullptr" is not an NPC, either. */ |
| 171 |
(void) ((void *)nullptr == cmp ()); |
| 172 |
(void) ((void *)nullptr != cmp ()); |
| 173 |
(void) (cmp () == (void *)nullptr); |
| 174 |
(void) (cmp () != (void *)nullptr); |
| 175 |
} |
| 176 |
|
| 177 |
/* Test ?:. */ |
| 178 |
void |
| 179 |
test3 (int *p, _Bool b) |
| 180 |
{ |
| 181 |
int x = nullptr ? 1 : 2; |
| 182 |
(void) x; |
| 183 |
const nullptr_t nptr = nullptr; |
| 184 |
/* One of the following shall hold for the second and third operands: |
| 185 |
-- both operands have nullptr_t type. */ |
| 186 |
__auto_type r1 = b ? nullptr : nullptr; |
| 187 |
__auto_type r2 = b ? nptr : nptr; |
| 188 |
/* -- one operand is a pointer and the other is a null pointer constant |
| 189 |
or has type nullptr_t; */ |
| 190 |
__auto_type r3 = b ? p : nullptr; |
| 191 |
__auto_type r4 = b ? nullptr : p; |
| 192 |
__auto_type r5 = b ? nptr : p; |
| 193 |
__auto_type r6 = b ? p : nptr; |
| 194 |
__auto_type r7 = b ? 0 : p; |
| 195 |
__auto_type r8 = b ? p : 0; |
| 196 |
__auto_type r9 = b ? p : cmp (); |
| 197 |
__auto_type r10 = b ? cmp () : p; |
| 198 |
__auto_type r11 = b ? p : _Generic(0, int : nullptr); |
| 199 |
__auto_type r12 = b ? _Generic(0, int : nullptr) : p; |
| 200 |
} |
| 201 |
|
| 202 |
void test_arg1 (const nullptr_t, _Atomic nullptr_t, volatile nullptr_t) { } |
| 203 |
void test_arg2 (_Atomic int *, const int *, volatile int *) { } |
| 204 |
void test_arg3 (_Atomic _Bool, const _Bool, volatile _Bool) { } |
| 205 |
nullptr_t retn (void) { return nullptr; } |
| 206 |
_Atomic int *ai (void) { return nullptr; } |
| 207 |
const int *ci (void) { return nullptr; } |
| 208 |
volatile int *vi (void) { return nullptr; } |
| 209 |
_Bool retb (void) { return nullptr; } |
| 210 |
|
| 211 |
/* Simple assignment. */ |
| 212 |
void |
| 213 |
test4 (void) |
| 214 |
{ |
| 215 |
/* -- the left operand has an atomic, qualified, or unqualified version of |
| 216 |
the nullptr_t type and the type of the right is nullptr_t; */ |
| 217 |
nullptr_t n1; |
| 218 |
const nullptr_t n2 = nullptr; |
| 219 |
_Atomic nullptr_t n3 = nullptr; |
| 220 |
volatile nullptr_t n4 = nullptr; |
| 221 |
_Atomic volatile nullptr_t n5 = nullptr; |
| 222 |
n1 = nullptr; |
| 223 |
n3 = nullptr; |
| 224 |
n4 = nullptr; |
| 225 |
n5 = nullptr; |
| 226 |
n5 = _Generic(0, int : nullptr); |
| 227 |
/* -- the left operand is an atomic, qualified, or unqualified pointer, |
| 228 |
and the type of the right is nullptr_t; */ |
| 229 |
int *p1 = cmp (); |
| 230 |
_Atomic int *p2 = cmp (); |
| 231 |
const int *volatile p3 = cmp (); |
| 232 |
const int *const *const p4 = cmp (); |
| 233 |
double (*const p5)(void) = n1; |
| 234 |
p2 = _Generic(0, int : nullptr); |
| 235 |
p3 = nullptr; |
| 236 |
/* -- the left operand is an atomic, qualified, or unqualified bool, and |
| 237 |
the type of the right is nullptr_t; */ |
| 238 |
_Bool b1; |
| 239 |
b1 = cmp (); |
| 240 |
const _Bool b2 = nullptr; |
| 241 |
_Atomic _Bool b3; |
| 242 |
b3 = n1; |
| 243 |
(void) b1; |
| 244 |
(void) b3; |
| 245 |
(void) n3; |
| 246 |
(void) n4; |
| 247 |
(void) n5; |
| 248 |
(void) p2; |
| 249 |
(void) p3; |
| 250 |
|
| 251 |
test_arg1 (nullptr, nullptr, nullptr); |
| 252 |
test_arg2 (nullptr, nullptr, nullptr); |
| 253 |
test_arg3 (nullptr, nullptr, nullptr); |
| 254 |
} |
| 255 |
|
| 256 |
/* var_arg etc. */ |
| 257 |
static void |
| 258 |
test5 (int i, ...) |
| 259 |
{ |
| 260 |
(void) i; |
| 261 |
va_list ap; |
| 262 |
va_start (ap, i); |
| 263 |
if (va_arg (ap, void *)) |
| 264 |
__builtin_abort (); |
| 265 |
} |
| 266 |
|
| 267 |
/* Operand of alignas, sizeof or typeof operators. */ |
| 268 |
void |
| 269 |
test6 (void) |
| 270 |
{ |
| 271 |
_Static_assert (sizeof (nullptr) == sizeof (void *), "sizeof (nullptr)"); |
| 272 |
_Static_assert (sizeof (nullptr_t) == sizeof (void *), "sizeof (nullptr_t)"); |
| 273 |
_Static_assert (sizeof (nullptr) == sizeof (char *), "sizeof (nullptr)"); |
| 274 |
_Static_assert (sizeof (nullptr_t) == sizeof (char *), "sizeof (nullptr_t)"); |
| 275 |
_Static_assert (_Alignof (nullptr_t) == _Alignof (char *), "_Alignof (nullptr_t)"); |
| 276 |
__typeof__(nullptr) t = nullptr; |
| 277 |
f1 (t); |
| 278 |
_Alignas (nullptr_t) char i1 = 'q'; |
| 279 |
|
| 280 |
_Static_assert (_Generic (nullptr, nullptr_t: 1, default: 0) == 1, "_Generic"); |
| 281 |
_Static_assert (_Generic (t, nullptr_t: 1, default: 0) == 1, "_Generic"); |
| 282 |
_Static_assert (_Generic (cmp (), nullptr_t: 1, default: 0) == 1, "_Generic"); |
| 283 |
_Static_assert (_Generic (0, nullptr_t: 1, int: 2, default: 0) == 2, "_Generic"); |
| 284 |
_Static_assert (_Generic ((void *)0, nullptr_t: 1, void *: 2, default: 0) == 2, "_Generic"); |
| 285 |
_Static_assert (_Generic (nullptr, nullptr_t: 1, void *: 2, default: 0) == 1, "_Generic"); |
| 286 |
} |
| 287 |
|
| 288 |
/* Play with !, ||, &&. */ |
| 289 |
void |
| 290 |
test7 (void) |
| 291 |
{ |
| 292 |
if (nullptr) |
| 293 |
__builtin_abort (); |
| 294 |
if (1 && nullptr) |
| 295 |
__builtin_abort (); |
| 296 |
if (0 || nullptr) |
| 297 |
__builtin_abort (); |
| 298 |
if (nullptr && 1) |
| 299 |
__builtin_abort (); |
| 300 |
if (nullptr || 0) |
| 301 |
__builtin_abort (); |
| 302 |
if (!nullptr) |
| 303 |
{ |
| 304 |
} |
| 305 |
else |
| 306 |
__builtin_abort (); |
| 307 |
while (nullptr) |
| 308 |
__builtin_abort (); |
| 309 |
int i = 0; |
| 310 |
do |
| 311 |
++i; |
| 312 |
while (nullptr); |
| 313 |
if (i != 1) |
| 314 |
__builtin_abort (); |
| 315 |
for (;nullptr;) |
| 316 |
__builtin_abort (); |
| 317 |
} |
| 318 |
|
| 319 |
int |
| 320 |
main (void) |
| 321 |
{ |
| 322 |
int i = 42; |
| 323 |
test1 (); |
| 324 |
test2 (&i); |
| 325 |
test3 (&i, 0); |
| 326 |
test4 (); |
| 327 |
test5 (42, nullptr); |
| 328 |
test6 (); |
| 329 |
test7 (); |
| 330 |
} |