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