blob: 2ec45b37cde646069d393d33442cd293aeae2047 [file] [log] [blame]
Daniel Dunbar53c9ac32010-09-07 22:54:28 +00001// RUN: %clang_cc1 -fsyntax-only -verify -ffreestanding %s
Douglas Gregor08d918a2008-10-24 15:36:09 +00002
Alexis Hunt12048d82009-11-22 07:05:50 +00003#include <stdint.h>
4
Douglas Gregor08d918a2008-10-24 15:36:09 +00005enum test { testval = 1 };
6struct structure { int m; };
7typedef void (*fnptr)();
8
9// Test the conversion to self.
10void self_conversion()
11{
12 // T*->T* is allowed, T->T in general not.
13 int i = 0;
14 (void)reinterpret_cast<int>(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}}
15 structure s;
John McCall85f90552010-03-10 11:27:22 +000016 (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'structure' to 'structure' is not allowed}}
Douglas Gregor08d918a2008-10-24 15:36:09 +000017 int *pi = 0;
18 (void)reinterpret_cast<int*>(pi);
19}
20
21// Test conversion between pointer and integral types, as in /3 and /4.
22void integral_conversion()
23{
24 void *vp = reinterpret_cast<void*>(testval);
Alexis Hunt12048d82009-11-22 07:05:50 +000025 intptr_t i = reinterpret_cast<intptr_t>(vp);
26 (void)reinterpret_cast<float*>(i);
27 fnptr fnp = reinterpret_cast<fnptr>(i);
Douglas Gregor08d918a2008-10-24 15:36:09 +000028 (void)reinterpret_cast<char>(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
Alexis Hunt12048d82009-11-22 07:05:50 +000029 (void)reinterpret_cast<intptr_t>(fnp);
Douglas Gregor08d918a2008-10-24 15:36:09 +000030}
31
32void pointer_conversion()
33{
34 int *p1 = 0;
35 float *p2 = reinterpret_cast<float*>(p1);
36 structure *p3 = reinterpret_cast<structure*>(p2);
37 typedef int **ppint;
38 ppint *deep = reinterpret_cast<ppint*>(p3);
39 (void)reinterpret_cast<fnptr*>(deep);
40}
41
42void constness()
43{
44 int ***const ipppc = 0;
45 // Valid: T1* -> T2 const*
46 int const *icp = reinterpret_cast<int const*>(ipppc);
47 // Invalid: T1 const* -> T2*
Douglas Gregorb472e932011-04-15 17:59:54 +000048 (void)reinterpret_cast<int*>(icp); // expected-error {{reinterpret_cast from 'const int *' to 'int *' casts away qualifiers}}
Douglas Gregor08d918a2008-10-24 15:36:09 +000049 // Invalid: T1*** -> T2 const* const**
Douglas Gregorb472e932011-04-15 17:59:54 +000050 int const *const **icpcpp = reinterpret_cast<int const* const**>(ipppc); // expected-error {{reinterpret_cast from 'int ***' to 'const int *const **' casts away qualifiers}}
Douglas Gregor08d918a2008-10-24 15:36:09 +000051 // Valid: T1* -> T2*
52 int *ip = reinterpret_cast<int*>(icpcpp);
53 // Valid: T* -> T const*
54 (void)reinterpret_cast<int const*>(ip);
55 // Valid: T*** -> T2 const* const* const*
56 (void)reinterpret_cast<int const* const* const*>(ipppc);
57}
58
59void fnptrs()
60{
61 typedef int (*fnptr2)(int);
62 fnptr fp = 0;
63 (void)reinterpret_cast<fnptr2>(fp);
64 void *vp = reinterpret_cast<void*>(fp);
65 (void)reinterpret_cast<fnptr>(vp);
66}
67
68void refs()
69{
70 long l = 0;
71 char &c = reinterpret_cast<char&>(l);
72 // Bad: from rvalue
73 (void)reinterpret_cast<int&>(&c); // expected-error {{reinterpret_cast from rvalue to reference type 'int &'}}
74}
Sebastian Redla5a77a62009-01-27 23:18:31 +000075
76void memptrs()
77{
78 const int structure::*psi = 0;
79 (void)reinterpret_cast<const float structure::*>(psi);
Douglas Gregorb472e932011-04-15 17:59:54 +000080 (void)reinterpret_cast<int structure::*>(psi); // expected-error {{reinterpret_cast from 'const int structure::*' to 'int structure::*' casts away qualifiers}}
Sebastian Redla5a77a62009-01-27 23:18:31 +000081
82 void (structure::*psf)() = 0;
83 (void)reinterpret_cast<int (structure::*)()>(psf);
84
Chris Lattner53fa0492010-09-05 00:04:01 +000085 (void)reinterpret_cast<void (structure::*)()>(psi); // expected-error {{reinterpret_cast from 'const int structure::*' to 'void (structure::*)()' is not allowed}}
John McCall85f90552010-03-10 11:27:22 +000086 (void)reinterpret_cast<int structure::*>(psf); // expected-error {{reinterpret_cast from 'void (structure::*)()' to 'int structure::*' is not allowed}}
Sebastian Redla5a77a62009-01-27 23:18:31 +000087
88 // Cannot cast from integers to member pointers, not even the null pointer
89 // literal.
John McCall85f90552010-03-10 11:27:22 +000090 (void)reinterpret_cast<void (structure::*)()>(0); // expected-error {{reinterpret_cast from 'int' to 'void (structure::*)()' is not allowed}}
91 (void)reinterpret_cast<int structure::*>(0); // expected-error {{reinterpret_cast from 'int' to 'int structure::*' is not allowed}}
Sebastian Redla5a77a62009-01-27 23:18:31 +000092}
Sebastian Redl55db1ec2009-11-18 18:10:53 +000093
Anders Carlsson76f513f2010-06-04 22:47:55 +000094namespace PR5545 {
Sebastian Redl55db1ec2009-11-18 18:10:53 +000095// PR5545
96class A;
97class B;
98void (A::*a)();
99void (B::*b)() = reinterpret_cast<void (B::*)()>(a);
Anders Carlsson76f513f2010-06-04 22:47:55 +0000100}
101
102// <rdar://problem/8018292>
103void const_arrays() {
104 typedef char STRING[10];
105 const STRING *s;
106 const char *c;
107
Douglas Gregorb472e932011-04-15 17:59:54 +0000108 (void)reinterpret_cast<char *>(s); // expected-error {{reinterpret_cast from 'const STRING *' (aka 'char const (*)[10]') to 'char *' casts away qualifiers}}
Anders Carlsson76f513f2010-06-04 22:47:55 +0000109 (void)reinterpret_cast<const STRING *>(c);
110}
Argyrios Kyrtzidis47a12852011-04-22 22:31:13 +0000111
112namespace PR9564 {
113 struct a { int a : 10; }; a x;
114 int *y = &reinterpret_cast<int&>(x.a); // expected-error {{reinterpret_cast of a bit-field to 'int &' needs its address which is not allowed}}
115}