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