Don't warn when matching %p to nullptr.
[clang.git] / test / SemaCXX / nullptr.cpp
blob666701c3c6ef9bd525cd5f09d7cf39a50b41669f
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -ffreestanding %s
2 #include <stdint.h>
4 typedef decltype(nullptr) nullptr_t;
6 struct A {};
8 int o1(char*);
9 void o1(uintptr_t);
10 void o2(char*); // expected-note {{candidate}}
11 void o2(int A::*); // expected-note {{candidate}}
13 nullptr_t f(nullptr_t null)
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;
30 // Can't convert nullptr to integral implicitly.
31 uintptr_t i = nullptr; // expected-error {{cannot initialize}}
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);
40 (void)(0 == nullptr);
41 (void)(nullptr == 0);
42 (void)(nullptr <= 0);
43 (void)(0 <= nullptr);
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}}
47 (void)(0 ? nullptr : 0); // expected-error {{incompatible operand types}}
48 (void)(0 ? nullptr : (void*)0);
50 // Overloading
51 int t = o1(nullptr);
52 t = o1(null);
53 o2(nullptr); // expected-error {{ambiguous}}
55 // nullptr is an rvalue, null is an lvalue
56 (void)&nullptr; // expected-error {{address expression must be an lvalue}}
57 nullptr_t *pn = &null;
59 // You can reinterpret_cast nullptr to an integer.
60 (void)reinterpret_cast<uintptr_t>(nullptr);
62 // You can throw nullptr.
63 throw nullptr;
66 // Template arguments can be nullptr.
67 template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
68 struct T {};
70 typedef T<nullptr, nullptr, nullptr, nullptr> NT;
72 namespace test1 {
73 template<typename T, typename U> struct is_same {
74 static const bool value = false;
77 template<typename T> struct is_same<T, T> {
78 static const bool value = true;
81 void *g(void*);
82 bool g(bool);
84 // Test that we prefer g(void*) over g(bool).
85 static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
88 namespace test2 {
89 void f(int, ...) __attribute__((sentinel));
91 void g() {
92 // nullptr can be used as the sentinel value.
93 f(10, nullptr);
97 namespace test3 {
98 void f(const char*, ...) __attribute__((format(printf, 1, 2)));
100 void g() {
101 // Don't warn when using nullptr with %p.
102 f("%p", nullptr);