1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding %s
26e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl#include <stdint.h>
36e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
4c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssontypedef decltype(nullptr) nullptr_t;
56e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
66e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlstruct A {};
76e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
86e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlint o1(char*);
96e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlvoid o1(uintptr_t);
106e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlvoid o2(char*); // expected-note {{candidate}}
116e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlvoid o2(int A::*); // expected-note {{candidate}}
126e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
136e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlnullptr_t f(nullptr_t null)
146e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl{
156e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // Implicit conversions.
166e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  null = nullptr;
176e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  void *p = nullptr;
186e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  p = null;
196e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  int *pi = nullptr;
206e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  pi = null;
216e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  null = 0;
226e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  int A::*pm = nullptr;
236e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  pm = null;
246e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  void (*pf)() = nullptr;
256e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  pf = null;
266e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  void (A::*pmf)() = nullptr;
276e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  pmf = null;
286e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  bool b = nullptr;
296e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
306e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // Can't convert nullptr to integral implicitly.
31cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  uintptr_t i = nullptr; // expected-error {{cannot initialize}}
326e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
336e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // Operators
346e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(null == nullptr);
356e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(null <= nullptr);
3650d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  (void)(null == 0);
376e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(null == (void*)0);
386e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)((void*)0 == nullptr);
3950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  (void)(null <= 0);
406e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(null <= (void*)0);
416e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)((void*)0 <= nullptr);
420c8209e40b405fd32f047e95aafdc94054406a58Anders Carlsson  (void)(0 == nullptr);
430c8209e40b405fd32f047e95aafdc94054406a58Anders Carlsson  (void)(nullptr == 0);
440c8209e40b405fd32f047e95aafdc94054406a58Anders Carlsson  (void)(nullptr <= 0);
450c8209e40b405fd32f047e95aafdc94054406a58Anders Carlsson  (void)(0 <= nullptr);
466e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(1 > nullptr); // expected-error {{invalid operands to binary expression}}
476e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(1 != nullptr); // expected-error {{invalid operands to binary expression}}
486e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(1 + nullptr); // expected-error {{invalid operands to binary expression}}
4950d61c8ccfc633b13cdf594ea3cd3a217076debeRichard Smith  (void)(0 ? nullptr : 0);
506e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)(0 ? nullptr : (void*)0);
5182214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(0 ? nullptr : A()); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
5282214a80c0163e01e4d8dec1426023c89277dbb4Chandler Carruth  (void)(0 ? A() : nullptr); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
536e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
546e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // Overloading
556e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  int t = o1(nullptr);
566e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  t = o1(null);
576e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  o2(nullptr); // expected-error {{ambiguous}}
586e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
596e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr is an rvalue, null is an lvalue
603fa3feab35096b608f1d79bb541798b37a55e7b9Richard Smith  (void)&nullptr; // expected-error {{cannot take the address of an rvalue of type 'nullptr_t'}}
616e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  nullptr_t *pn = &null;
626e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
636e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // You can reinterpret_cast nullptr to an integer.
646e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  (void)reinterpret_cast<uintptr_t>(nullptr);
6584ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  (void)reinterpret_cast<uintptr_t>(*pn);
6684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
67eb82a420c8898ea71e13d82e6646d33f49fc574cEli Friedman  // You can't reinterpret_cast nullptr to any integer
68eb82a420c8898ea71e13d82e6646d33f49fc574cEli Friedman  (void)reinterpret_cast<char>(nullptr); // expected-error {{cast from pointer to smaller type 'char' loses information}}
69eb82a420c8898ea71e13d82e6646d33f49fc574cEli Friedman
7084ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  int *ip = *pn;
7184ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  if (*pn) { }
726e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
736e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // You can throw nullptr.
746e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  throw nullptr;
756e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl}
766e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
776e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl// Template arguments can be nullptr.
786e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redltemplate <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
796e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redlstruct T {};
806e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
816e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redltypedef T<nullptr, nullptr, nullptr, nullptr> NT;
82c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson
83c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssonnamespace test1 {
84c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssontemplate<typename T, typename U> struct is_same {
85c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson  static const bool value = false;
86c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson};
87c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson
88c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssontemplate<typename T> struct is_same<T, T> {
89c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson  static const bool value = true;
90c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson};
91c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson
92c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssonvoid *g(void*);
93c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssonbool g(bool);
94c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson
95c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson// Test that we prefer g(void*) over g(bool).
96c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlssonstatic_assert(is_same<decltype(g(nullptr)), void*>::value, "");
97c8df0b634ee5ca8573852fdc96252b8bba5a10dbAnders Carlsson}
98343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson
99343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlssonnamespace test2 {
100343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson  void f(int, ...) __attribute__((sentinel));
101343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson
102343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson  void g() {
103343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson    // nullptr can be used as the sentinel value.
104343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson    f(10, nullptr);
105343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson  }
106343e6ffa70459b1afdb488c69fb651434a5016c0Anders Carlsson}
107624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson
108624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlssonnamespace test3 {
109624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson  void f(const char*, ...) __attribute__((format(printf, 1, 2)));
110624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson
111624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson  void g() {
112624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson    // Don't warn when using nullptr with %p.
113624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson    f("%p", nullptr);
114624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson  }
115624259984448cf19f4e94b7e31c7c32e99a39ea5Anders Carlsson}
11684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
117d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(__is_scalar(nullptr_t), "");
118d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(__is_pod(nullptr_t), "");
119d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(sizeof(nullptr_t) == sizeof(void*), "");
120d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenko
121d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr < nullptr), "");
122d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr > nullptr), "");
123d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr <= nullptr, "");
124d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr >= nullptr, "");
125d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr == nullptr, "");
126d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr != nullptr), "");
127d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenko
128d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(0 < nullptr), "");
129d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(0 > nullptr), "");
130d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  0 <= nullptr, "");
131d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  0 >= nullptr, "");
132d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  0 == nullptr, "");
133d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(0 != nullptr), "");
134d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenko
135d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr < 0), "");
136d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr > 0), "");
137d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr <= 0, "");
138d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr >= 0, "");
139d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(  nullptr == 0, "");
140d5f55dcb0de1cb03c93adc9a44a5ed24e4cb505dDmitri Gribenkostatic_assert(!(nullptr != 0), "");
14184ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
14284ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregornamespace overloading {
14384ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  int &f1(int*);
14484ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  float &f1(bool);
14584ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
14684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  void test_f1() {
14784ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    int &ir = (f1)(nullptr);
14884ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  }
14984ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
15084ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  struct ConvertsToNullPtr {
15184ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    operator nullptr_t() const;
15284ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  };
15384ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
15484ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  void test_conversion(ConvertsToNullPtr ctn) {
15584ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn == ctn);
15684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn != ctn);
15784ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn <= ctn);
15884ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn >= ctn);
15984ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn < ctn);
16084ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    (void)(ctn > ctn);
16184ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  }
16284ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor}
16384ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
16484ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregornamespace templates {
16584ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  template<typename T, nullptr_t Value>
16684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  struct X {
16784ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    X() { ptr = Value; }
16884ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
16984ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor    T *ptr;
17084ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  };
17184ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
17284ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  X<int, nullptr> x;
17384ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
17484ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
17584ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  template<int (*fp)(int), int* p, int A::* pmd, int (A::*pmf)(int)>
17684ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  struct X2 {};
17784ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor
17884ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor  X2<nullptr, nullptr, nullptr, nullptr> x2;
17984ee2eeec9f63e4938bafd060105fa4f0c9cd89bDouglas Gregor}
18070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
18170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithnamespace null_pointer_constant {
18270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
18370488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith// Pending implementation of core issue 903, ensure we don't allow any of the
18470488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith// C++11 constant evaluation semantics in null pointer constants.
18570488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithstruct S { int n; };
18670488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithconstexpr int null() { return 0; }
18770488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithvoid *p = S().n; // expected-error {{cannot initialize}}
18870488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithvoid *q = null(); // expected-error {{cannot initialize}}
18970488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith
19070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith}
191