Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Charles Li | 85dec55 | 2015-12-10 01:07:17 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
Chris Lattner | 3e30f7c | 2007-01-23 06:12:15 +0000 | [diff] [blame] | 4 | |
| 5 | char *const_cast_test(const char *var) |
| 6 | { |
| 7 | return const_cast<char*>(var); |
| 8 | } |
| 9 | |
Chris Lattner | 3e30f7c | 2007-01-23 06:12:15 +0000 | [diff] [blame] | 10 | struct A { |
| 11 | virtual ~A() {} |
| 12 | }; |
| 13 | |
| 14 | struct B : public A { |
| 15 | }; |
| 16 | |
| 17 | struct B *dynamic_cast_test(struct A *a) |
| 18 | { |
| 19 | return dynamic_cast<struct B*>(a); |
| 20 | } |
Chris Lattner | 3e30f7c | 2007-01-23 06:12:15 +0000 | [diff] [blame] | 21 | |
| 22 | char *reinterpret_cast_test() |
| 23 | { |
| 24 | return reinterpret_cast<char*>(0xdeadbeef); |
| 25 | } |
| 26 | |
| 27 | double static_cast_test(int i) |
| 28 | { |
| 29 | return static_cast<double>(i); |
| 30 | } |
Argyrios Kyrtzidis | 16d63a7 | 2008-08-16 19:45:32 +0000 | [diff] [blame] | 31 | |
| 32 | char postfix_expr_test() |
| 33 | { |
| 34 | return reinterpret_cast<char*>(0xdeadbeef)[0]; |
Daniel Dunbar | a45cf5b | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 35 | } |
John McCall | e7db86d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 36 | |
| 37 | // This was being incorrectly tentatively parsed. |
| 38 | namespace test1 { |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 39 | template <class T> class A {}; // expected-note 2{{here}} |
Nick Lewycky | 2eeddfb | 2016-05-14 17:44:14 +0000 | [diff] [blame] | 40 | void foo() { A<int>(*(A<int>*)0); } // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}} |
John McCall | e7db86d | 2010-04-30 03:11:01 +0000 | [diff] [blame] | 41 | } |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 42 | |
| 43 | typedef char* c; |
| 44 | typedef A* a; |
| 45 | void test2(char x, struct B * b) { |
Charles Li | 85dec55 | 2015-12-10 01:07:17 +0000 | [diff] [blame] | 46 | (void)const_cast<::c>(&x); |
| 47 | #if __cplusplus <= 199711L |
| 48 | // expected-error@-2 {{found '<::' after a const_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 49 | #endif |
| 50 | |
| 51 | (void)dynamic_cast<::a>(b); |
| 52 | #if __cplusplus <= 199711L |
| 53 | // expected-error@-2 {{found '<::' after a dynamic_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 54 | #endif |
| 55 | |
| 56 | (void)reinterpret_cast<::c>(x); |
| 57 | #if __cplusplus <= 199711L |
| 58 | // expected-error@-2 {{found '<::' after a reinterpret_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 59 | #endif |
| 60 | |
| 61 | (void)static_cast<::c>(&x); |
| 62 | #if __cplusplus <= 199711L |
| 63 | // expected-error@-2 {{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 64 | #endif |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 65 | |
| 66 | // Do not do digraph correction. |
| 67 | (void)static_cast<: :c>(&x); //\ |
| 68 | expected-error {{expected '<' after 'static_cast'}} \ |
| 69 | expected-error {{expected expression}}\ |
| 70 | expected-error {{expected ']'}}\ |
| 71 | expected-note {{to match this '['}} |
| 72 | (void)static_cast<: // expected-error {{expected '<' after 'static_cast'}} \ |
| 73 | expected-note {{to match this '['}} |
| 74 | :c>(&x); // expected-error {{expected expression}} \ |
| 75 | expected-error {{expected ']'}} |
| 76 | #define LC <: |
| 77 | #define C : |
David Blaikie | e750491 | 2013-03-05 06:21:38 +0000 | [diff] [blame] | 78 | test1::A LC:B> c; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}} |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 79 | (void)static_cast LC:c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}} |
David Blaikie | e750491 | 2013-03-05 06:21:38 +0000 | [diff] [blame] | 80 | test1::A<:C B> d; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}} |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 81 | (void)static_cast<:C c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}} |
| 82 | |
| 83 | #define LCC <:: |
Charles Li | 85dec55 | 2015-12-10 01:07:17 +0000 | [diff] [blame] | 84 | test1::A LCC B> e; |
| 85 | #if __cplusplus <= 199711L |
| 86 | // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 87 | #endif |
| 88 | |
| 89 | (void)static_cast LCC c>(&x); |
| 90 | #if __cplusplus <= 199711L |
| 91 | // expected-error@-2 {{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 92 | #endif |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 93 | } |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 94 | |
Richard Trieu | 02e25db | 2011-09-20 20:03:50 +0000 | [diff] [blame] | 95 | // This note comes from "::D[:F> A5;" |
| 96 | template <class T> class D {}; // expected-note{{template is declared here}} |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 97 | template <class T> void E() {}; |
| 98 | class F {}; |
| 99 | |
| 100 | void test3() { |
Charles Li | 85dec55 | 2015-12-10 01:07:17 +0000 | [diff] [blame] | 101 | ::D<::F> A1; |
| 102 | #if __cplusplus <= 199711L |
| 103 | // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 104 | #endif |
| 105 | |
| 106 | D<::F> A2; |
| 107 | #if __cplusplus <= 199711L |
| 108 | // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 109 | #endif |
| 110 | |
| 111 | ::E<::F>(); |
| 112 | #if __cplusplus <= 199711L |
| 113 | // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 114 | #endif |
| 115 | |
| 116 | E<::F>(); |
| 117 | #if __cplusplus <= 199711L |
| 118 | // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}} |
| 119 | #endif |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 120 | |
| 121 | ::D< ::F> A3; |
| 122 | D< ::F> A4; |
| 123 | ::E< ::F>(); |
| 124 | E< ::F>(); |
Richard Trieu | 02e25db | 2011-09-20 20:03:50 +0000 | [diff] [blame] | 125 | |
| 126 | // Make sure that parser doesn't expand '[:' to '< ::' |
David Blaikie | e750491 | 2013-03-05 06:21:38 +0000 | [diff] [blame] | 127 | ::D[:F> A5; // expected-error {{class template '::D' requires template arguments}} \ |
Richard Trieu | 02e25db | 2011-09-20 20:03:50 +0000 | [diff] [blame] | 128 | // expected-error {{expected expression}} \ |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 129 | // expected-error {{expected unqualified-id}} |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 130 | } |
Richard Smith | 62e6630 | 2012-08-20 17:37:52 +0000 | [diff] [blame] | 131 | |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 132 | // Ensure that a C-style cast doesn't turn off colon protection. |
| 133 | void PR19748() { |
| 134 | struct A {}; |
| 135 | int A = 0, b; |
| 136 | int test1 = true ? (int)A : b; |
| 137 | |
| 138 | struct f {}; |
| 139 | extern B f(), (*p)(); |
| 140 | (true ? (B(*)())f : p)(); |
| 141 | } |
| 142 | |
Richard Smith | 5a477c5 | 2014-07-15 00:11:48 +0000 | [diff] [blame] | 143 | void PR19751(int n) { |
| 144 | struct T { void operator++(int); }; |
| 145 | (T())++; // ok, not an ill-formed cast to function type |
| 146 | (T())++n; // expected-error {{C-style cast from 'int' to 'T ()' is not allowed}} |
| 147 | } |
| 148 | |
Richard Smith | 62e6630 | 2012-08-20 17:37:52 +0000 | [diff] [blame] | 149 | // PR13619. Must be at end of file. |
| 150 | int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}} |