blob: 91757cf51edfb4ebdf5aeb97e86ef80360141df0 [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding %s
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002#include <stdint.h>
3
Anders Carlssonc8df0b62010-11-05 00:12:09 +00004typedef decltype(nullptr) nullptr_t;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00005
6struct A {};
7
8int o1(char*);
9void o1(uintptr_t);
10void o2(char*); // expected-note {{candidate}}
11void o2(int A::*); // expected-note {{candidate}}
12
13nullptr_t f(nullptr_t null)
14{
15 // Implicit conversions.
16 null = nullptr;
17 void *p = nullptr;
18 p = null;
19 int *pi = nullptr;
20 pi = null;
21 null = 0;
22 int A::*pm = nullptr;
23 pm = null;
24 void (*pf)() = nullptr;
25 pf = null;
26 void (A::*pmf)() = nullptr;
27 pmf = null;
28 bool b = nullptr;
29
30 // Can't convert nullptr to integral implicitly.
Eli Friedmancfdc81a2009-12-19 08:11:05 +000031 uintptr_t i = nullptr; // expected-error {{cannot initialize}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000032
33 // Operators
34 (void)(null == nullptr);
35 (void)(null <= nullptr);
36 (void)(null == (void*)0);
37 (void)((void*)0 == nullptr);
38 (void)(null <= (void*)0);
39 (void)((void*)0 <= nullptr);
Anders Carlsson0c8209e2010-11-04 03:17:43 +000040 (void)(0 == nullptr);
41 (void)(nullptr == 0);
42 (void)(nullptr <= 0);
43 (void)(0 <= nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000044 (void)(1 > nullptr); // expected-error {{invalid operands to binary expression}}
45 (void)(1 != nullptr); // expected-error {{invalid operands to binary expression}}
46 (void)(1 + nullptr); // expected-error {{invalid operands to binary expression}}
Chandler Carruth7ef93242011-02-19 00:13:59 +000047 (void)(0 ? nullptr : 0); // expected-error {{non-pointer operand type 'int' incompatible with nullptr}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000048 (void)(0 ? nullptr : (void*)0);
Chandler Carruth82214a82011-02-18 23:54:50 +000049 (void)(0 ? nullptr : A()); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
50 (void)(0 ? A() : nullptr); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000051
52 // Overloading
53 int t = o1(nullptr);
54 t = o1(null);
55 o2(nullptr); // expected-error {{ambiguous}}
56
57 // nullptr is an rvalue, null is an lvalue
58 (void)&nullptr; // expected-error {{address expression must be an lvalue}}
59 nullptr_t *pn = &null;
60
61 // You can reinterpret_cast nullptr to an integer.
62 (void)reinterpret_cast<uintptr_t>(nullptr);
Douglas Gregor84ee2ee2011-05-21 23:15:46 +000063 (void)reinterpret_cast<uintptr_t>(*pn);
64
65 int *ip = *pn;
66 if (*pn) { }
Sebastian Redl6e8ed162009-05-10 18:38:11 +000067
68 // You can throw nullptr.
69 throw nullptr;
70}
71
72// Template arguments can be nullptr.
73template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
74struct T {};
75
76typedef T<nullptr, nullptr, nullptr, nullptr> NT;
Anders Carlssonc8df0b62010-11-05 00:12:09 +000077
78namespace test1 {
79template<typename T, typename U> struct is_same {
80 static const bool value = false;
81};
82
83template<typename T> struct is_same<T, T> {
84 static const bool value = true;
85};
86
87void *g(void*);
88bool g(bool);
89
90// Test that we prefer g(void*) over g(bool).
91static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
92}
Anders Carlsson343e6ff2010-11-05 15:21:33 +000093
94namespace test2 {
95 void f(int, ...) __attribute__((sentinel));
96
97 void g() {
98 // nullptr can be used as the sentinel value.
99 f(10, nullptr);
100 }
101}
Anders Carlsson62425992010-11-06 14:58:53 +0000102
103namespace test3 {
104 void f(const char*, ...) __attribute__((format(printf, 1, 2)));
105
106 void g() {
107 // Don't warn when using nullptr with %p.
108 f("%p", nullptr);
109 }
110}
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000111
112int array0[__is_scalar(nullptr_t)? 1 : -1];
113int array1[__is_pod(nullptr_t)? 1 : -1];
114int array2[sizeof(nullptr_t) == sizeof(void*)? 1 : -1];
115
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000116int relational0[nullptr < nullptr? -1 : 1];
117int relational1[nullptr > nullptr? -1 : 1];
118int relational2[nullptr <= nullptr? 1 : -1];
119int relational3[nullptr >= nullptr? 1 : -1];
120int equality[nullptr == nullptr? 1 : -1];
121int inequality[nullptr != nullptr? -1 : 1];
Richard Smith26f2cac2012-02-14 22:35:28 +0000122
123int relational0_a[0 < nullptr? -1 : 1];
124int relational1_a[0 > nullptr? -1 : 1];
125int relational2_a[0 <= nullptr? 1 : -1];
126int relational3_a[0 >= nullptr? 1 : -1];
127int equality_a[0 == nullptr? 1 : -1];
128int inequality_a[0 != nullptr? -1 : 1];
129
130int relationalnullptr_b[nullptr < 0? -1 : 1];
131int relational1_b[nullptr > 0? -1 : 1];
132int relational2_b[nullptr <= 0? 1 : -1];
133int relational3_b[nullptr >= 0? 1 : -1];
134int equality_b[nullptr == 0? 1 : -1];
135int inequality_b[nullptr != 0? -1 : 1];
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000136
137namespace overloading {
138 int &f1(int*);
139 float &f1(bool);
140
141 void test_f1() {
142 int &ir = (f1)(nullptr);
143 }
144
145 struct ConvertsToNullPtr {
146 operator nullptr_t() const;
147 };
148
149 void test_conversion(ConvertsToNullPtr ctn) {
150 (void)(ctn == ctn);
151 (void)(ctn != ctn);
152 (void)(ctn <= ctn);
153 (void)(ctn >= ctn);
154 (void)(ctn < ctn);
155 (void)(ctn > ctn);
156 }
157}
158
159namespace templates {
160 template<typename T, nullptr_t Value>
161 struct X {
162 X() { ptr = Value; }
163
164 T *ptr;
165 };
166
167 X<int, nullptr> x;
168
169
170 template<int (*fp)(int), int* p, int A::* pmd, int (A::*pmf)(int)>
171 struct X2 {};
172
173 X2<nullptr, nullptr, nullptr, nullptr> x2;
174}
Richard Smith70488e22012-02-14 21:38:30 +0000175
176namespace null_pointer_constant {
177
178// Pending implementation of core issue 903, ensure we don't allow any of the
179// C++11 constant evaluation semantics in null pointer constants.
180struct S { int n; };
181constexpr int null() { return 0; }
182void *p = S().n; // expected-error {{cannot initialize}}
183void *q = null(); // expected-error {{cannot initialize}}
184
185}