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