blob: 7385fd42ad38a5807f656f612b7e83ecafbb9e89 [file] [log] [blame]
Daniel Dunbarc79f7672010-09-07 22:54:28 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -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}}
47 (void)(0 ? nullptr : 0); // expected-error {{incompatible operand types}}
48 (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);
63
64 // You can throw nullptr.
65 throw nullptr;
66}
67
68// Template arguments can be nullptr.
69template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
70struct T {};
71
72typedef T<nullptr, nullptr, nullptr, nullptr> NT;
Anders Carlssonc8df0b62010-11-05 00:12:09 +000073
74namespace test1 {
75template<typename T, typename U> struct is_same {
76 static const bool value = false;
77};
78
79template<typename T> struct is_same<T, T> {
80 static const bool value = true;
81};
82
83void *g(void*);
84bool g(bool);
85
86// Test that we prefer g(void*) over g(bool).
87static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
88}
Anders Carlsson343e6ff2010-11-05 15:21:33 +000089
90namespace test2 {
91 void f(int, ...) __attribute__((sentinel));
92
93 void g() {
94 // nullptr can be used as the sentinel value.
95 f(10, nullptr);
96 }
97}
Anders Carlsson62425992010-11-06 14:58:53 +000098
99namespace test3 {
100 void f(const char*, ...) __attribute__((format(printf, 1, 2)));
101
102 void g() {
103 // Don't warn when using nullptr with %p.
104 f("%p", nullptr);
105 }
106}