blob: b1ae591865eb1dce7e8dc1063aeebd6db9e1d1d3 [file] [log] [blame]
Richard Smith55858492011-04-14 21:45:45 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Li85dec552015-12-10 01:07:17 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
Chris Lattner3e30f7c2007-01-23 06:12:15 +00004
5char *const_cast_test(const char *var)
6{
7 return const_cast<char*>(var);
8}
9
Chris Lattner3e30f7c2007-01-23 06:12:15 +000010struct A {
11 virtual ~A() {}
12};
13
14struct B : public A {
15};
16
17struct B *dynamic_cast_test(struct A *a)
18{
19 return dynamic_cast<struct B*>(a);
20}
Chris Lattner3e30f7c2007-01-23 06:12:15 +000021
22char *reinterpret_cast_test()
23{
24 return reinterpret_cast<char*>(0xdeadbeef);
25}
26
27double static_cast_test(int i)
28{
29 return static_cast<double>(i);
30}
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +000031
32char postfix_expr_test()
33{
34 return reinterpret_cast<char*>(0xdeadbeef)[0];
Daniel Dunbara45cf5b2009-03-24 02:24:46 +000035}
John McCalle7db86d2010-04-30 03:11:01 +000036
37// This was being incorrectly tentatively parsed.
38namespace test1 {
Richard Smith55858492011-04-14 21:45:45 +000039 template <class T> class A {}; // expected-note 2{{here}}
Nick Lewycky2eeddfb2016-05-14 17:44:14 +000040 void foo() { A<int>(*(A<int>*)0); } // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
John McCalle7db86d2010-04-30 03:11:01 +000041}
Richard Smith55858492011-04-14 21:45:45 +000042
43typedef char* c;
44typedef A* a;
45void test2(char x, struct B * b) {
Charles Li85dec552015-12-10 01:07:17 +000046 (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 Smith55858492011-04-14 21:45:45 +000065
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 Blaikiee7504912013-03-05 06:21:38 +000078 test1::A LC:B> c; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
Richard Smith55858492011-04-14 21:45:45 +000079 (void)static_cast LC:c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
David Blaikiee7504912013-03-05 06:21:38 +000080 test1::A<:C B> d; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
Richard Smith55858492011-04-14 21:45:45 +000081 (void)static_cast<:C c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
82
83#define LCC <::
Charles Li85dec552015-12-10 01:07:17 +000084 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 Smith55858492011-04-14 21:45:45 +000093}
Richard Trieu01fc0012011-09-19 19:01:00 +000094
Richard Trieu02e25db2011-09-20 20:03:50 +000095 // This note comes from "::D[:F> A5;"
96template <class T> class D {}; // expected-note{{template is declared here}}
Richard Trieu01fc0012011-09-19 19:01:00 +000097template <class T> void E() {};
98class F {};
99
100void test3() {
Charles Li85dec552015-12-10 01:07:17 +0000101 ::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 Trieu01fc0012011-09-19 19:01:00 +0000120
121 ::D< ::F> A3;
122 D< ::F> A4;
123 ::E< ::F>();
124 E< ::F>();
Richard Trieu02e25db2011-09-20 20:03:50 +0000125
126 // Make sure that parser doesn't expand '[:' to '< ::'
David Blaikiee7504912013-03-05 06:21:38 +0000127 ::D[:F> A5; // expected-error {{class template '::D' requires template arguments}} \
Richard Trieu02e25db2011-09-20 20:03:50 +0000128 // expected-error {{expected expression}} \
Richard Smith4f605af2012-08-18 00:55:03 +0000129 // expected-error {{expected unqualified-id}}
Richard Trieu01fc0012011-09-19 19:01:00 +0000130}
Richard Smith62e66302012-08-20 17:37:52 +0000131
Richard Smith87e11a42014-05-15 02:43:47 +0000132// Ensure that a C-style cast doesn't turn off colon protection.
133void 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 Smith5a477c52014-07-15 00:11:48 +0000143void 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 Smith62e66302012-08-20 17:37:52 +0000149// PR13619. Must be at end of file.
150int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}}