blob: 6f660366e99897bf0c77c49faab0a35ec29c10a1 [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
116// FIXME: when we implement constexpr, this will be testable.
117#if 0
118int relational0[nullptr < nullptr? -1 : 1];
119int relational1[nullptr > nullptr? -1 : 1];
120int relational2[nullptr <= nullptr? 1 : -1];
121int relational3[nullptr >= nullptr? 1 : -1];
122int equality[nullptr == nullptr? 1 : -1];
123int inequality[nullptr != nullptr? -1 : 1];
124#endif
125
126namespace overloading {
127 int &f1(int*);
128 float &f1(bool);
129
130 void test_f1() {
131 int &ir = (f1)(nullptr);
132 }
133
134 struct ConvertsToNullPtr {
135 operator nullptr_t() const;
136 };
137
138 void test_conversion(ConvertsToNullPtr ctn) {
139 (void)(ctn == ctn);
140 (void)(ctn != ctn);
141 (void)(ctn <= ctn);
142 (void)(ctn >= ctn);
143 (void)(ctn < ctn);
144 (void)(ctn > ctn);
145 }
146}
147
148namespace templates {
149 template<typename T, nullptr_t Value>
150 struct X {
151 X() { ptr = Value; }
152
153 T *ptr;
154 };
155
156 X<int, nullptr> x;
157
158
159 template<int (*fp)(int), int* p, int A::* pmd, int (A::*pmf)(int)>
160 struct X2 {};
161
162 X2<nullptr, nullptr, nullptr, nullptr> x2;
163}