Clang Project

clang_source_code/test/Parser/cxx-casting.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
5char *const_cast_test(const char *var)
6{
7  return const_cast<char*>(var);
8}
9
10struct 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}
21
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}
31
32char postfix_expr_test()
33{
34  return reinterpret_cast<char*>(0xdeadbeef)[0];
35}
36
37// This was being incorrectly tentatively parsed.
38namespace 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}}
41}
42
43typedef char* c;
44typedef A* a;
45void 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
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
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 :
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{{}}
82
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
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
93}
94
95                               // This note comes from "::D[:F> A5;"
96template <class T> class D {}; // expected-note{{template is declared here}}
97template <class T> void E() {};
98class F {};
99
100void 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
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
120
121  ::D< ::F> A3;
122  D< ::F> A4;
123  ::E< ::F>();
124  E< ::F>();
125
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}}
130}
131
132// 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
143void 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
149// PR13619. Must be at end of file.
150int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}}
151