| 1 |
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -Wno-unused-value -Wcast-align -verify %s |
| 2 |
|
| 3 |
// Simple casts. |
| 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. |
| 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 |
template <class A> void DependentAlign() { |
| 48 |
alignas(A) int lut[]{}; |
| 49 |
(long *)lut; // expected-warning {{cast from 'int *' to 'long *'}} |
| 50 |
} |
| 51 |
|
| 52 |
struct __attribute__((aligned(16))) AlignedS { |
| 53 |
char m[16]; |
| 54 |
}; |
| 55 |
|
| 56 |
struct __attribute__((aligned(16))) A { |
| 57 |
char m0[16]; |
| 58 |
char m1[16]; |
| 59 |
AlignedS *getAlignedS() { |
| 60 |
return (AlignedS *)m1; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
struct B0 { |
| 65 |
char m0[16]; |
| 66 |
}; |
| 67 |
|
| 68 |
struct B1 { |
| 69 |
char m0[16]; |
| 70 |
}; |
| 71 |
|
| 72 |
struct C { |
| 73 |
A &m0; |
| 74 |
B0 &m1; |
| 75 |
A m2; |
| 76 |
}; |
| 77 |
|
| 78 |
struct __attribute__((aligned(16))) D0 : B0, B1 { |
| 79 |
}; |
| 80 |
|
| 81 |
struct __attribute__((aligned(16))) D1 : virtual B0 { |
| 82 |
}; |
| 83 |
|
| 84 |
struct B2 { |
| 85 |
char m0[8]; |
| 86 |
}; |
| 87 |
|
| 88 |
struct B3 { |
| 89 |
char m0[8]; |
| 90 |
}; |
| 91 |
|
| 92 |
struct B4 { |
| 93 |
char m0[8]; |
| 94 |
}; |
| 95 |
|
| 96 |
struct D2 : B2, B3 { |
| 97 |
}; |
| 98 |
|
| 99 |
struct __attribute__((aligned(16))) D3 : B4, D2 { |
| 100 |
}; |
| 101 |
|
| 102 |
struct __attribute__((aligned(16))) D4 : virtual D2 { |
| 103 |
}; |
| 104 |
|
| 105 |
struct D5 : virtual D0 { |
| 106 |
char m0[16]; |
| 107 |
AlignedS *get() { |
| 108 |
return (AlignedS *)m0; // expected-warning {{cast from 'char *' to 'AlignedS *'}} |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
struct D6 : virtual D5 { |
| 113 |
}; |
| 114 |
|
| 115 |
struct D7 : virtual D3 { |
| 116 |
}; |
| 117 |
|
| 118 |
void test2(int n, A *a2) { |
| 119 |
__attribute__((aligned(16))) char m[sizeof(A) * 2]; |
| 120 |
char(&m_ref)[sizeof(A) * 2] = m; |
| 121 |
extern char(&m_ref_noinit)[sizeof(A) * 2]; |
| 122 |
__attribute__((aligned(16))) char vararray[10][n]; |
| 123 |
A t0; |
| 124 |
B0 t1; |
| 125 |
C t2 = {.m0 = t0, .m1 = t1}; |
| 126 |
__attribute__((aligned(16))) char t3[5][5][5]; |
| 127 |
__attribute__((aligned(16))) char t4[4][16]; |
| 128 |
D0 t5; |
| 129 |
D1 t6; |
| 130 |
D3 t7; |
| 131 |
D4 t8; |
| 132 |
D6 t9; |
| 133 |
__attribute__((aligned(1))) D7 t10; |
| 134 |
|
| 135 |
A *a; |
| 136 |
a = (A *)&m; |
| 137 |
a = (A *)(m + sizeof(A)); |
| 138 |
a = (A *)(sizeof(A) + m); |
| 139 |
a = (A *)((sizeof(A) * 2 + m) - sizeof(A)); |
| 140 |
a = (A *)((sizeof(A) * 2 + m) - 1); // expected-warning {{cast from 'char *' to 'A *'}} |
| 141 |
a = (A *)(m + 1); // expected-warning {{cast from 'char *' to 'A *'}} |
| 142 |
a = (A *)(1 + m); // expected-warning {{cast from 'char *' to 'A *'}} |
| 143 |
a = (A *)(m + n); // expected-warning {{cast from 'char *' to 'A *'}} |
| 144 |
a = (A *)&*&m[sizeof(A)]; |
| 145 |
a = (A *)(0, 0, &m[sizeof(A)]); |
| 146 |
a = (A *)&(0, 0, *&m[sizeof(A)]); |
| 147 |
a = (A *)&m[n]; // expected-warning {{cast from 'char *' to 'A *'}} |
| 148 |
a = (A *)&m_ref; |
| 149 |
a = (A *)&m_ref_noinit; // expected-warning {{cast from 'char (*)[64]' to 'A *'}} |
| 150 |
a = (A *)(&vararray[4][0]); // expected-warning {{cast from 'char *' to 'A *'}} |
| 151 |
a = (A *)(a2->m0 + sizeof(A)); // expected-warning {{cast from 'char *' to 'A *'}} |
| 152 |
a = (A *)(&t2.m0); |
| 153 |
a = (A *)(&t2.m1); // expected-warning {{cast from 'B0 *' to 'A *'}} |
| 154 |
a = (A *)(&t2.m2); |
| 155 |
a = (A *)(t2.m2.m1); |
| 156 |
a = (A *)(&t3[3][3][0]); // expected-warning {{cast from 'char *' to 'A *'}} |
| 157 |
a = (A *)(&t3[2][2][4]); |
| 158 |
a = (A *)(&t3[0][n][0]); // expected-warning {{cast from 'char *' to 'A *'}} |
| 159 |
a = (A *)&t4[n][0]; |
| 160 |
a = (A *)&t4[n][1]; // expected-warning {{cast from 'char *' to 'A *'}} |
| 161 |
a = (A *)(t4 + 1); |
| 162 |
a = (A *)(t4 + n); |
| 163 |
a = (A *)(static_cast<B1 *>(&t5)); |
| 164 |
a = (A *)(&(static_cast<B1 &>(t5))); |
| 165 |
a = (A *)(static_cast<B0 *>(&t6)); // expected-warning {{cast from 'B0 *' to 'A *'}} |
| 166 |
a = (A *)(static_cast<B2 *>(&t7)); // expected-warning {{cast from 'B2 *' to 'A *'}} |
| 167 |
a = (A *)(static_cast<B3 *>(&t7)); |
| 168 |
a = (A *)(static_cast<B2 *>(&t8)); // expected-warning {{cast from 'B2 *' to 'A *'}} |
| 169 |
a = (A *)(static_cast<B3 *>(&t8)); // expected-warning {{cast from 'B3 *' to 'A *'}} |
| 170 |
a = (A *)(static_cast<D5 *>(&t9)); // expected-warning {{cast from 'D5 *' to 'A *'}} |
| 171 |
a = (A *)(static_cast<D3 *>(&t10)); // expected-warning {{cast from 'D3 *' to 'A *'}} |
| 172 |
} |