Daniel Dunbar | c79f767 | 2010-09-07 22:54:28 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -ffreestanding %s |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2 | #include <stdint.h> |
| 3 | |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 4 | typedef decltype(nullptr) nullptr_t; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 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. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 31 | uintptr_t i = nullptr; // expected-error {{cannot initialize}} |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 32 | |
| 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 Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 40 | (void)(0 == nullptr); |
| 41 | (void)(nullptr == 0); |
| 42 | (void)(nullptr <= 0); |
| 43 | (void)(0 <= nullptr); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 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); |
| 49 | |
| 50 | // Overloading |
| 51 | int t = o1(nullptr); |
| 52 | t = o1(null); |
| 53 | o2(nullptr); // expected-error {{ambiguous}} |
| 54 | |
| 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; |
| 58 | |
| 59 | // You can reinterpret_cast nullptr to an integer. |
| 60 | (void)reinterpret_cast<uintptr_t>(nullptr); |
| 61 | |
| 62 | // You can throw nullptr. |
| 63 | throw nullptr; |
| 64 | } |
| 65 | |
| 66 | // Template arguments can be nullptr. |
| 67 | template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()> |
| 68 | struct T {}; |
| 69 | |
| 70 | typedef T<nullptr, nullptr, nullptr, nullptr> NT; |
Anders Carlsson | c8df0b6 | 2010-11-05 00:12:09 +0000 | [diff] [blame] | 71 | |
| 72 | namespace test1 { |
| 73 | template<typename T, typename U> struct is_same { |
| 74 | static const bool value = false; |
| 75 | }; |
| 76 | |
| 77 | template<typename T> struct is_same<T, T> { |
| 78 | static const bool value = true; |
| 79 | }; |
| 80 | |
| 81 | void *g(void*); |
| 82 | bool g(bool); |
| 83 | |
| 84 | // Test that we prefer g(void*) over g(bool). |
| 85 | static_assert(is_same<decltype(g(nullptr)), void*>::value, ""); |
| 86 | } |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 87 | |
| 88 | namespace test2 { |
| 89 | void f(int, ...) __attribute__((sentinel)); |
| 90 | |
| 91 | void g() { |
| 92 | // nullptr can be used as the sentinel value. |
| 93 | f(10, nullptr); |
| 94 | } |
| 95 | } |
Anders Carlsson | 6242599 | 2010-11-06 14:58:53 +0000 | [diff] [blame^] | 96 | |
| 97 | namespace test3 { |
| 98 | void f(const char*, ...) __attribute__((format(printf, 1, 2))); |
| 99 | |
| 100 | void g() { |
| 101 | // Don't warn when using nullptr with %p. |
| 102 | f("%p", nullptr); |
| 103 | } |
| 104 | } |