blob: 6172c9f8d9e3def3a1c5ed1da0f0f50345766128 [file] [log] [blame]
Douglas Gregor2f639b92008-10-24 15:36:09 +00001// RUN: clang -fsyntax-only -verify %s
2
3enum test { testval = 1 };
4struct structure { int m; };
5typedef void (*fnptr)();
6
7// Test the conversion to self.
8void self_conversion()
9{
10 // T*->T* is allowed, T->T in general not.
11 int i = 0;
12 (void)reinterpret_cast<int>(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}}
13 structure s;
14 (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'struct structure' to 'struct structure' is not allowed}}
15 int *pi = 0;
16 (void)reinterpret_cast<int*>(pi);
17}
18
19// Test conversion between pointer and integral types, as in /3 and /4.
20void integral_conversion()
21{
22 void *vp = reinterpret_cast<void*>(testval);
23 long l = reinterpret_cast<long>(vp);
24 (void)reinterpret_cast<float*>(l);
25 fnptr fnp = reinterpret_cast<fnptr>(l);
26 (void)reinterpret_cast<char>(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
27 (void)reinterpret_cast<long>(fnp);
28}
29
30void pointer_conversion()
31{
32 int *p1 = 0;
33 float *p2 = reinterpret_cast<float*>(p1);
34 structure *p3 = reinterpret_cast<structure*>(p2);
35 typedef int **ppint;
36 ppint *deep = reinterpret_cast<ppint*>(p3);
37 (void)reinterpret_cast<fnptr*>(deep);
38}
39
40void constness()
41{
42 int ***const ipppc = 0;
43 // Valid: T1* -> T2 const*
44 int const *icp = reinterpret_cast<int const*>(ipppc);
45 // Invalid: T1 const* -> T2*
46 (void)reinterpret_cast<int*>(icp); // expected-error {{reinterpret_cast from 'int const *' to 'int *' casts away constness}}
47 // Invalid: T1*** -> T2 const* const**
48 int const *const **icpcpp = reinterpret_cast<int const* const**>(ipppc); // expected-error {{reinterpret_cast from 'int ***const' to 'int const *const **' casts away constness}}
49 // Valid: T1* -> T2*
50 int *ip = reinterpret_cast<int*>(icpcpp);
51 // Valid: T* -> T const*
52 (void)reinterpret_cast<int const*>(ip);
53 // Valid: T*** -> T2 const* const* const*
54 (void)reinterpret_cast<int const* const* const*>(ipppc);
55}
56
57void fnptrs()
58{
59 typedef int (*fnptr2)(int);
60 fnptr fp = 0;
61 (void)reinterpret_cast<fnptr2>(fp);
62 void *vp = reinterpret_cast<void*>(fp);
63 (void)reinterpret_cast<fnptr>(vp);
64}
65
66void refs()
67{
68 long l = 0;
69 char &c = reinterpret_cast<char&>(l);
70 // Bad: from rvalue
71 (void)reinterpret_cast<int&>(&c); // expected-error {{reinterpret_cast from rvalue to reference type 'int &'}}
72}