xref: /minix3/external/bsd/llvm/dist/clang/test/Parser/cxx-casting.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc char *const_cast_test(const char *var)
4*f4a2713aSLionel Sambuc {
5*f4a2713aSLionel Sambuc   return const_cast<char*>(var);
6*f4a2713aSLionel Sambuc }
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc struct A {
9*f4a2713aSLionel Sambuc   virtual ~A() {}
10*f4a2713aSLionel Sambuc };
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc struct B : public A {
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc struct B *dynamic_cast_test(struct A *a)
16*f4a2713aSLionel Sambuc {
17*f4a2713aSLionel Sambuc   return dynamic_cast<struct B*>(a);
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc char *reinterpret_cast_test()
21*f4a2713aSLionel Sambuc {
22*f4a2713aSLionel Sambuc   return reinterpret_cast<char*>(0xdeadbeef);
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc double static_cast_test(int i)
26*f4a2713aSLionel Sambuc {
27*f4a2713aSLionel Sambuc   return static_cast<double>(i);
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc char postfix_expr_test()
31*f4a2713aSLionel Sambuc {
32*f4a2713aSLionel Sambuc   return reinterpret_cast<char*>(0xdeadbeef)[0];
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // This was being incorrectly tentatively parsed.
36*f4a2713aSLionel Sambuc namespace test1 {
37*f4a2713aSLionel Sambuc   template <class T> class A {}; // expected-note 2{{here}}
38*f4a2713aSLionel Sambuc   void foo() { A<int>(*(A<int>*)0); }
39*f4a2713aSLionel Sambuc }
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc typedef char* c;
42*f4a2713aSLionel Sambuc typedef A* a;
43*f4a2713aSLionel Sambuc void test2(char x, struct B * b) {
44*f4a2713aSLionel Sambuc   (void)const_cast<::c>(&x);  // expected-error{{found '<::' after a const_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
45*f4a2713aSLionel Sambuc   (void)dynamic_cast<::a>(b);  // expected-error{{found '<::' after a dynamic_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
46*f4a2713aSLionel Sambuc   (void)reinterpret_cast<::c>(x);  // expected-error{{found '<::' after a reinterpret_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
47*f4a2713aSLionel Sambuc   (void)static_cast<::c>(&x);  // expected-error{{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc   // Do not do digraph correction.
50*f4a2713aSLionel Sambuc   (void)static_cast<: :c>(&x); //\
51*f4a2713aSLionel Sambuc        expected-error {{expected '<' after 'static_cast'}} \
52*f4a2713aSLionel Sambuc        expected-error {{expected expression}}\
53*f4a2713aSLionel Sambuc        expected-error {{expected ']'}}\
54*f4a2713aSLionel Sambuc        expected-note {{to match this '['}}
55*f4a2713aSLionel Sambuc   (void)static_cast<: // expected-error {{expected '<' after 'static_cast'}} \
56*f4a2713aSLionel Sambuc                          expected-note {{to match this '['}}
57*f4a2713aSLionel Sambuc   :c>(&x); // expected-error {{expected expression}} \
58*f4a2713aSLionel Sambuc               expected-error {{expected ']'}}
59*f4a2713aSLionel Sambuc #define LC <:
60*f4a2713aSLionel Sambuc #define C :
61*f4a2713aSLionel Sambuc   test1::A LC:B> c; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
62*f4a2713aSLionel Sambuc   (void)static_cast LC:c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
63*f4a2713aSLionel Sambuc   test1::A<:C B> d; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
64*f4a2713aSLionel Sambuc   (void)static_cast<:C c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc #define LCC <::
67*f4a2713aSLionel Sambuc   test1::A LCC B> e; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
68*f4a2713aSLionel Sambuc   (void)static_cast LCC c>(&x); // expected-error{{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
69*f4a2713aSLionel Sambuc }
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc                                // This note comes from "::D[:F> A5;"
72*f4a2713aSLionel Sambuc template <class T> class D {}; // expected-note{{template is declared here}}
73*f4a2713aSLionel Sambuc template <class T> void E() {};
74*f4a2713aSLionel Sambuc class F {};
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc void test3() {
77*f4a2713aSLionel Sambuc   ::D<::F> A1; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
78*f4a2713aSLionel Sambuc   D<::F> A2; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
79*f4a2713aSLionel Sambuc   ::E<::F>(); // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
80*f4a2713aSLionel Sambuc   E<::F>(); // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
81*f4a2713aSLionel Sambuc 
82*f4a2713aSLionel Sambuc   ::D< ::F> A3;
83*f4a2713aSLionel Sambuc   D< ::F> A4;
84*f4a2713aSLionel Sambuc   ::E< ::F>();
85*f4a2713aSLionel Sambuc   E< ::F>();
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc   // Make sure that parser doesn't expand '[:' to '< ::'
88*f4a2713aSLionel Sambuc   ::D[:F> A5; // expected-error {{class template '::D' requires template arguments}} \
89*f4a2713aSLionel Sambuc               // expected-error {{expected expression}} \
90*f4a2713aSLionel Sambuc               // expected-error {{expected unqualified-id}}
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc // PR13619. Must be at end of file.
94*f4a2713aSLionel Sambuc int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}}
95