1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding -Wno-null-conversion %s
2f4a2713aSLionel Sambuc #include <stdint.h>
3f4a2713aSLionel Sambuc
4f4a2713aSLionel Sambuc typedef decltype(nullptr) nullptr_t;
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuc struct A {};
7f4a2713aSLionel Sambuc
8f4a2713aSLionel Sambuc int o1(char*);
9f4a2713aSLionel Sambuc void o1(uintptr_t);
10f4a2713aSLionel Sambuc void o2(char*); // expected-note {{candidate}}
11f4a2713aSLionel Sambuc void o2(int A::*); // expected-note {{candidate}}
12f4a2713aSLionel Sambuc
f(nullptr_t null)13f4a2713aSLionel Sambuc nullptr_t f(nullptr_t null)
14f4a2713aSLionel Sambuc {
15f4a2713aSLionel Sambuc // Implicit conversions.
16f4a2713aSLionel Sambuc null = nullptr;
17f4a2713aSLionel Sambuc void *p = nullptr;
18f4a2713aSLionel Sambuc p = null;
19f4a2713aSLionel Sambuc int *pi = nullptr;
20f4a2713aSLionel Sambuc pi = null;
21f4a2713aSLionel Sambuc null = 0;
22f4a2713aSLionel Sambuc int A::*pm = nullptr;
23f4a2713aSLionel Sambuc pm = null;
24f4a2713aSLionel Sambuc void (*pf)() = nullptr;
25f4a2713aSLionel Sambuc pf = null;
26f4a2713aSLionel Sambuc void (A::*pmf)() = nullptr;
27f4a2713aSLionel Sambuc pmf = null;
28f4a2713aSLionel Sambuc bool b = nullptr;
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc // Can't convert nullptr to integral implicitly.
31f4a2713aSLionel Sambuc uintptr_t i = nullptr; // expected-error {{cannot initialize}}
32f4a2713aSLionel Sambuc
33f4a2713aSLionel Sambuc // Operators
34f4a2713aSLionel Sambuc (void)(null == nullptr);
35f4a2713aSLionel Sambuc (void)(null <= nullptr);
36f4a2713aSLionel Sambuc (void)(null == 0);
37f4a2713aSLionel Sambuc (void)(null == (void*)0);
38f4a2713aSLionel Sambuc (void)((void*)0 == nullptr);
39f4a2713aSLionel Sambuc (void)(null <= 0);
40f4a2713aSLionel Sambuc (void)(null <= (void*)0);
41f4a2713aSLionel Sambuc (void)((void*)0 <= nullptr);
42f4a2713aSLionel Sambuc (void)(0 == nullptr);
43f4a2713aSLionel Sambuc (void)(nullptr == 0);
44f4a2713aSLionel Sambuc (void)(nullptr <= 0);
45f4a2713aSLionel Sambuc (void)(0 <= nullptr);
46f4a2713aSLionel Sambuc (void)(1 > nullptr); // expected-error {{invalid operands to binary expression}}
47f4a2713aSLionel Sambuc (void)(1 != nullptr); // expected-error {{invalid operands to binary expression}}
48f4a2713aSLionel Sambuc (void)(1 + nullptr); // expected-error {{invalid operands to binary expression}}
49f4a2713aSLionel Sambuc (void)(0 ? nullptr : 0);
50f4a2713aSLionel Sambuc (void)(0 ? nullptr : (void*)0);
51f4a2713aSLionel Sambuc (void)(0 ? nullptr : A()); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
52f4a2713aSLionel Sambuc (void)(0 ? A() : nullptr); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc // Overloading
55f4a2713aSLionel Sambuc int t = o1(nullptr);
56f4a2713aSLionel Sambuc t = o1(null);
57f4a2713aSLionel Sambuc o2(nullptr); // expected-error {{ambiguous}}
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc // nullptr is an rvalue, null is an lvalue
60f4a2713aSLionel Sambuc (void)&nullptr; // expected-error {{cannot take the address of an rvalue of type 'nullptr_t'}}
61f4a2713aSLionel Sambuc nullptr_t *pn = &null;
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc // You can reinterpret_cast nullptr to an integer.
64f4a2713aSLionel Sambuc (void)reinterpret_cast<uintptr_t>(nullptr);
65f4a2713aSLionel Sambuc (void)reinterpret_cast<uintptr_t>(*pn);
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc // You can't reinterpret_cast nullptr to any integer
68f4a2713aSLionel Sambuc (void)reinterpret_cast<char>(nullptr); // expected-error {{cast from pointer to smaller type 'char' loses information}}
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc int *ip = *pn;
71f4a2713aSLionel Sambuc if (*pn) { }
72f4a2713aSLionel Sambuc
73f4a2713aSLionel Sambuc // You can throw nullptr.
74f4a2713aSLionel Sambuc throw nullptr;
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc // Template arguments can be nullptr.
78f4a2713aSLionel Sambuc template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
79f4a2713aSLionel Sambuc struct T {};
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc typedef T<nullptr, nullptr, nullptr, nullptr> NT;
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc namespace test1 {
84f4a2713aSLionel Sambuc template<typename T, typename U> struct is_same {
85f4a2713aSLionel Sambuc static const bool value = false;
86f4a2713aSLionel Sambuc };
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc template<typename T> struct is_same<T, T> {
89f4a2713aSLionel Sambuc static const bool value = true;
90f4a2713aSLionel Sambuc };
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc void *g(void*);
93f4a2713aSLionel Sambuc bool g(bool);
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc // Test that we prefer g(void*) over g(bool).
96f4a2713aSLionel Sambuc static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc namespace test2 {
100f4a2713aSLionel Sambuc void f(int, ...) __attribute__((sentinel));
101f4a2713aSLionel Sambuc
g()102f4a2713aSLionel Sambuc void g() {
103f4a2713aSLionel Sambuc // nullptr can be used as the sentinel value.
104f4a2713aSLionel Sambuc f(10, nullptr);
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc namespace test3 {
109f4a2713aSLionel Sambuc void f(const char*, ...) __attribute__((format(printf, 1, 2)));
110f4a2713aSLionel Sambuc
g()111f4a2713aSLionel Sambuc void g() {
112f4a2713aSLionel Sambuc // Don't warn when using nullptr with %p.
113f4a2713aSLionel Sambuc f("%p", nullptr);
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc static_assert(__is_scalar(nullptr_t), "");
118f4a2713aSLionel Sambuc static_assert(__is_pod(nullptr_t), "");
119f4a2713aSLionel Sambuc static_assert(sizeof(nullptr_t) == sizeof(void*), "");
120f4a2713aSLionel Sambuc
121f4a2713aSLionel Sambuc static_assert(!(nullptr < nullptr), "");
122f4a2713aSLionel Sambuc static_assert(!(nullptr > nullptr), "");
123f4a2713aSLionel Sambuc static_assert( nullptr <= nullptr, "");
124f4a2713aSLionel Sambuc static_assert( nullptr >= nullptr, "");
125f4a2713aSLionel Sambuc static_assert( nullptr == nullptr, "");
126f4a2713aSLionel Sambuc static_assert(!(nullptr != nullptr), "");
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc static_assert(!(0 < nullptr), "");
129f4a2713aSLionel Sambuc static_assert(!(0 > nullptr), "");
130f4a2713aSLionel Sambuc static_assert( 0 <= nullptr, "");
131f4a2713aSLionel Sambuc static_assert( 0 >= nullptr, "");
132f4a2713aSLionel Sambuc static_assert( 0 == nullptr, "");
133f4a2713aSLionel Sambuc static_assert(!(0 != nullptr), "");
134f4a2713aSLionel Sambuc
135f4a2713aSLionel Sambuc static_assert(!(nullptr < 0), "");
136f4a2713aSLionel Sambuc static_assert(!(nullptr > 0), "");
137f4a2713aSLionel Sambuc static_assert( nullptr <= 0, "");
138f4a2713aSLionel Sambuc static_assert( nullptr >= 0, "");
139f4a2713aSLionel Sambuc static_assert( nullptr == 0, "");
140f4a2713aSLionel Sambuc static_assert(!(nullptr != 0), "");
141f4a2713aSLionel Sambuc
142f4a2713aSLionel Sambuc namespace overloading {
143f4a2713aSLionel Sambuc int &f1(int*);
144f4a2713aSLionel Sambuc float &f1(bool);
145f4a2713aSLionel Sambuc
test_f1()146f4a2713aSLionel Sambuc void test_f1() {
147f4a2713aSLionel Sambuc int &ir = (f1)(nullptr);
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc
150f4a2713aSLionel Sambuc struct ConvertsToNullPtr {
151f4a2713aSLionel Sambuc operator nullptr_t() const;
152f4a2713aSLionel Sambuc };
153f4a2713aSLionel Sambuc
test_conversion(ConvertsToNullPtr ctn)154f4a2713aSLionel Sambuc void test_conversion(ConvertsToNullPtr ctn) {
155f4a2713aSLionel Sambuc (void)(ctn == ctn);
156f4a2713aSLionel Sambuc (void)(ctn != ctn);
157f4a2713aSLionel Sambuc (void)(ctn <= ctn);
158f4a2713aSLionel Sambuc (void)(ctn >= ctn);
159f4a2713aSLionel Sambuc (void)(ctn < ctn);
160f4a2713aSLionel Sambuc (void)(ctn > ctn);
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc }
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc namespace templates {
165f4a2713aSLionel Sambuc template<typename T, nullptr_t Value>
166f4a2713aSLionel Sambuc struct X {
Xtemplates::X167f4a2713aSLionel Sambuc X() { ptr = Value; }
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc T *ptr;
170f4a2713aSLionel Sambuc };
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc X<int, nullptr> x;
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc
175f4a2713aSLionel Sambuc template<int (*fp)(int), int* p, int A::* pmd, int (A::*pmf)(int)>
176f4a2713aSLionel Sambuc struct X2 {};
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc X2<nullptr, nullptr, nullptr, nullptr> x2;
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc
181f4a2713aSLionel Sambuc namespace null_pointer_constant {
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc // Pending implementation of core issue 903, ensure we don't allow any of the
184f4a2713aSLionel Sambuc // C++11 constant evaluation semantics in null pointer constants.
185f4a2713aSLionel Sambuc struct S { int n; };
null()186f4a2713aSLionel Sambuc constexpr int null() { return 0; }
187f4a2713aSLionel Sambuc void *p = S().n; // expected-error {{cannot initialize}}
188f4a2713aSLionel Sambuc void *q = null(); // expected-error {{cannot initialize}}
189f4a2713aSLionel Sambuc
190f4a2713aSLionel Sambuc }
191