[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Parser / cxx-casting.cpp
blob34c3a1a2460b58ac853e34964208cdaa6463090e
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
5 char *const_cast_test(const char *var)
7 return const_cast<char*>(var);
10 struct A {
11 virtual ~A() {}
14 struct B : public A {
17 struct B *dynamic_cast_test(struct A *a)
19 return dynamic_cast<struct B*>(a);
22 char *reinterpret_cast_test()
24 return reinterpret_cast<char*>(0xdeadbeef);
27 double static_cast_test(int i)
29 return static_cast<double>(i);
32 char postfix_expr_test()
34 return reinterpret_cast<char*>(0xdeadbeef)[0];
37 // This was being incorrectly tentatively parsed.
38 namespace test1 {
39 template <class T> class A {}; // expected-note 2{{here}}
40 void foo() { A<int>(*(A<int>*)0); } // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
43 typedef char* c;
44 typedef A* a;
45 void test2(char x, struct B * b) {
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
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
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
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
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 :
78 test1::A LC:B> c; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
79 (void)static_cast LC:c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
80 test1::A<:C B> d; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
81 (void)static_cast<:C c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
83 #define LCC <::
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
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
95 // This note comes from "::D[:F> A5;"
96 template <class T> class D {}; // expected-note{{template is declared here}}
97 template <class T> void E() {};
98 class F {};
100 void test3() {
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
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
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
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
121 ::D< ::F> A3;
122 D< ::F> A4;
123 ::E< ::F>();
124 E< ::F>();
126 // Make sure that parser doesn't expand '[:' to '< ::'
127 ::D[:F> A5; // expected-error {{class template '::D' requires template arguments}} \
128 // expected-error {{expected expression}} \
129 // expected-error {{expected unqualified-id}}
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;
138 struct f {};
139 extern B f(), (*p)();
140 (true ? (B(*)())f : p)();
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}}
149 // PR13619. Must be at end of file.
150 int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}}