Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding %s |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2 | #include <stdint.h> |
| 3 | |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 4 | typedef decltype(nullptr) nullptr_t; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5 | |
| 6 | struct A {}; |
| 7 | |
| 8 | int o1(char*); |
| 9 | void o1(uintptr_t); |
| 10 | void o2(char*); // expected-note {{candidate}} |
| 11 | void o2(int A::*); // expected-note {{candidate}} |
| 12 | |
| 13 | nullptr_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 Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 31 | uintptr_t i = nullptr; // expected-error {{cannot initialize}} |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 32 | |
| 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 Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 40 | (void)(0 == nullptr); |
| 41 | (void)(nullptr == 0); |
| 42 | (void)(nullptr <= 0); |
| 43 | (void)(0 <= nullptr); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 44 | (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 Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 47 | (void)(0 ? nullptr : 0); // expected-error {{non-pointer operand type 'int' incompatible with nullptr}} |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 48 | (void)(0 ? nullptr : (void*)0); |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 49 | (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 Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 51 | |
| 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 Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 63 | (void)reinterpret_cast<uintptr_t>(*pn); |
| 64 | |
| 65 | int *ip = *pn; |
| 66 | if (*pn) { } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 67 | |
| 68 | // You can throw nullptr. |
| 69 | throw nullptr; |
| 70 | } |
| 71 | |
| 72 | // Template arguments can be nullptr. |
| 73 | template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()> |
| 74 | struct T {}; |
| 75 | |
| 76 | typedef T<nullptr, nullptr, nullptr, nullptr> NT; |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 77 | |
| 78 | namespace test1 { |
| 79 | template<typename T, typename U> struct is_same { |
| 80 | static const bool value = false; |
| 81 | }; |
| 82 | |
| 83 | template<typename T> struct is_same<T, T> { |
| 84 | static const bool value = true; |
| 85 | }; |
| 86 | |
| 87 | void *g(void*); |
| 88 | bool g(bool); |
| 89 | |
| 90 | // Test that we prefer g(void*) over g(bool). |
| 91 | static_assert(is_same<decltype(g(nullptr)), void*>::value, ""); |
| 92 | } |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 93 | |
| 94 | namespace 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 Carlsson | 6242599 | 2010-11-06 14:58:53 +0000 | [diff] [blame] | 102 | |
| 103 | namespace 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 Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 111 | |
Dmitri Gribenko | d5f55dc | 2012-02-15 13:30:53 +0000 | [diff] [blame] | 112 | static_assert(__is_scalar(nullptr_t), ""); |
| 113 | static_assert(__is_pod(nullptr_t), ""); |
| 114 | static_assert(sizeof(nullptr_t) == sizeof(void*), ""); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 115 | |
Dmitri Gribenko | d5f55dc | 2012-02-15 13:30:53 +0000 | [diff] [blame] | 116 | static_assert(!(nullptr < nullptr), ""); |
| 117 | static_assert(!(nullptr > nullptr), ""); |
| 118 | static_assert( nullptr <= nullptr, ""); |
| 119 | static_assert( nullptr >= nullptr, ""); |
| 120 | static_assert( nullptr == nullptr, ""); |
| 121 | static_assert(!(nullptr != nullptr), ""); |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 122 | |
Dmitri Gribenko | d5f55dc | 2012-02-15 13:30:53 +0000 | [diff] [blame] | 123 | static_assert(!(0 < nullptr), ""); |
| 124 | static_assert(!(0 > nullptr), ""); |
| 125 | static_assert( 0 <= nullptr, ""); |
| 126 | static_assert( 0 >= nullptr, ""); |
| 127 | static_assert( 0 == nullptr, ""); |
| 128 | static_assert(!(0 != nullptr), ""); |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 129 | |
Dmitri Gribenko | d5f55dc | 2012-02-15 13:30:53 +0000 | [diff] [blame] | 130 | static_assert(!(nullptr < 0), ""); |
| 131 | static_assert(!(nullptr > 0), ""); |
| 132 | static_assert( nullptr <= 0, ""); |
| 133 | static_assert( nullptr >= 0, ""); |
| 134 | static_assert( nullptr == 0, ""); |
| 135 | static_assert(!(nullptr != 0), ""); |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 136 | |
| 137 | namespace 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 | |
| 159 | namespace 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 Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 175 | |
| 176 | namespace 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. |
| 180 | struct S { int n; }; |
| 181 | constexpr int null() { return 0; } |
| 182 | void *p = S().n; // expected-error {{cannot initialize}} |
| 183 | void *q = null(); // expected-error {{cannot initialize}} |
| 184 | |
| 185 | } |