1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -Wno-unused-value -Wcast-align -verify %s
2 
3 // Simple casts.
test0(char * P)4 void test0(char *P) {
5   char *a; short *b; int *c;
6 
7   a = (char*) P;
8   a = static_cast<char*>(P);
9   a = reinterpret_cast<char*>(P);
10   typedef char *CharPtr;
11   a = CharPtr(P);
12 
13   b = (short*) P; // expected-warning {{cast from 'char *' to 'short *' increases required alignment from 1 to 2}}
14   b = reinterpret_cast<short*>(P);
15   typedef short *ShortPtr;
16   b = ShortPtr(P); // expected-warning {{cast from 'char *' to 'ShortPtr' (aka 'short *') increases required alignment from 1 to 2}}
17 
18   c = (int*) P; // expected-warning {{cast from 'char *' to 'int *' increases required alignment from 1 to 4}}
19   c = reinterpret_cast<int*>(P);
20   typedef int *IntPtr;
21   c = IntPtr(P); // expected-warning {{cast from 'char *' to 'IntPtr' (aka 'int *') increases required alignment from 1 to 4}}
22 }
23 
24 // Casts from void* are a special case.
test1(void * P)25 void test1(void *P) {
26   char *a; short *b; int *c;
27 
28   a = (char*) P;
29   a = static_cast<char*>(P);
30   a = reinterpret_cast<char*>(P);
31   typedef char *CharPtr;
32   a = CharPtr(P);
33 
34   b = (short*) P;
35   b = static_cast<short*>(P);
36   b = reinterpret_cast<short*>(P);
37   typedef short *ShortPtr;
38   b = ShortPtr(P);
39 
40   c = (int*) P;
41   c = static_cast<int*>(P);
42   c = reinterpret_cast<int*>(P);
43   typedef int *IntPtr;
44   c = IntPtr(P);
45 }
46 
47 struct __attribute__((aligned(16))) AlignedS {
48   char m[16];
49 };
50 
51 struct __attribute__((aligned(16))) A {
52   char m0[16];
53   char m1[16];
getAlignedSA54   AlignedS *getAlignedS() {
55     return (AlignedS *)m1;
56   }
57 };
58 
59 struct B0 {
60   char m0[16];
61 };
62 
63 struct B1 {
64   char m0[16];
65 };
66 
67 struct C {
68   A &m0;
69   B0 &m1;
70   A m2;
71 };
72 
73 struct __attribute__((aligned(16))) D0 : B0, B1 {
74 };
75 
76 struct __attribute__((aligned(16))) D1 : virtual B0 {
77 };
78 
79 struct B2 {
80   char m0[8];
81 };
82 
83 struct B3 {
84   char m0[8];
85 };
86 
87 struct B4 {
88   char m0[8];
89 };
90 
91 struct D2 : B2, B3 {
92 };
93 
94 struct __attribute__((aligned(16))) D3 : B4, D2 {
95 };
96 
97 struct __attribute__((aligned(16))) D4 : virtual D2 {
98 };
99 
100 struct D5 : virtual D0 {
101   char m0[16];
getD5102   AlignedS *get() {
103     return (AlignedS *)m0; // expected-warning {{cast from 'char *' to 'AlignedS *'}}
104   }
105 };
106 
107 struct D6 : virtual D5 {
108 };
109 
110 struct D7 : virtual D3 {
111 };
112 
test2(int n,A * a2)113 void test2(int n, A *a2) {
114   __attribute__((aligned(16))) char m[sizeof(A) * 2];
115   char(&m_ref)[sizeof(A) * 2] = m;
116   extern char(&m_ref_noinit)[sizeof(A) * 2];
117   __attribute__((aligned(16))) char vararray[10][n];
118   A t0;
119   B0 t1;
120   C t2 = {.m0 = t0, .m1 = t1};
121   __attribute__((aligned(16))) char t3[5][5][5];
122   __attribute__((aligned(16))) char t4[4][16];
123   D0 t5;
124   D1 t6;
125   D3 t7;
126   D4 t8;
127   D6 t9;
128   __attribute__((aligned(1))) D7 t10;
129 
130   A *a;
131   a = (A *)&m;
132   a = (A *)(m + sizeof(A));
133   a = (A *)(sizeof(A) + m);
134   a = (A *)((sizeof(A) * 2 + m) - sizeof(A));
135   a = (A *)((sizeof(A) * 2 + m) - 1); // expected-warning {{cast from 'char *' to 'A *'}}
136   a = (A *)(m + 1);                   // expected-warning {{cast from 'char *' to 'A *'}}
137   a = (A *)(1 + m);                   // expected-warning {{cast from 'char *' to 'A *'}}
138   a = (A *)(m + n);                   // expected-warning {{cast from 'char *' to 'A *'}}
139   a = (A *)&*&m[sizeof(A)];
140   a = (A *)(0, 0, &m[sizeof(A)]);
141   a = (A *)&(0, 0, *&m[sizeof(A)]);
142   a = (A *)&m[n]; // expected-warning {{cast from 'char *' to 'A *'}}
143   a = (A *)&m_ref;
144   a = (A *)&m_ref_noinit;        // expected-warning {{cast from 'char (*)[64]' to 'A *'}}
145   a = (A *)(&vararray[4][0]);    // expected-warning {{cast from 'char *' to 'A *'}}
146   a = (A *)(a2->m0 + sizeof(A)); // expected-warning {{cast from 'char *' to 'A *'}}
147   a = (A *)(&t2.m0);
148   a = (A *)(&t2.m1); // expected-warning {{cast from 'B0 *' to 'A *'}}
149   a = (A *)(&t2.m2);
150   a = (A *)(t2.m2.m1);
151   a = (A *)(&t3[3][3][0]); // expected-warning {{cast from 'char *' to 'A *'}}
152   a = (A *)(&t3[2][2][4]);
153   a = (A *)(&t3[0][n][0]); // expected-warning {{cast from 'char *' to 'A *'}}
154   a = (A *)&t4[n][0];
155   a = (A *)&t4[n][1]; // expected-warning {{cast from 'char *' to 'A *'}}
156   a = (A *)(t4 + 1);
157   a = (A *)(t4 + n);
158   a = (A *)(static_cast<B1 *>(&t5));
159   a = (A *)(&(static_cast<B1 &>(t5)));
160   a = (A *)(static_cast<B0 *>(&t6)); // expected-warning {{cast from 'B0 *' to 'A *'}}
161   a = (A *)(static_cast<B2 *>(&t7)); // expected-warning {{cast from 'B2 *' to 'A *'}}
162   a = (A *)(static_cast<B3 *>(&t7));
163   a = (A *)(static_cast<B2 *>(&t8));  // expected-warning {{cast from 'B2 *' to 'A *'}}
164   a = (A *)(static_cast<B3 *>(&t8));  // expected-warning {{cast from 'B3 *' to 'A *'}}
165   a = (A *)(static_cast<D5 *>(&t9));  // expected-warning {{cast from 'D5 *' to 'A *'}}
166   a = (A *)(static_cast<D3 *>(&t10)); // expected-warning {{cast from 'D3 *' to 'A *'}}
167 }
168