| 1 |
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -Wcast-align -verify %s |
| 2 |
|
| 3 |
// Simple casts. |
| 4 |
void test0(char *P) { |
| 5 |
char *a = (char*) P; |
| 6 |
short *b = (short*) P; // expected-warning {{cast from 'char *' to 'short *' increases required alignment from 1 to 2}} |
| 7 |
int *c = (int*) P; // expected-warning {{cast from 'char *' to 'int *' increases required alignment from 1 to 4}} |
| 8 |
} |
| 9 |
|
| 10 |
// Casts from void* are a special case. |
| 11 |
void test1(void *P) { |
| 12 |
char *a = (char*) P; |
| 13 |
short *b = (short*) P; |
| 14 |
int *c = (int*) P; |
| 15 |
|
| 16 |
const volatile void *P2 = P; |
| 17 |
char *d = (char*) P2; |
| 18 |
short *e = (short*) P2; |
| 19 |
int *f = (int*) P2; |
| 20 |
|
| 21 |
const char *g = (const char*) P2; |
| 22 |
const short *h = (const short*) P2; |
| 23 |
const int *i = (const int*) P2; |
| 24 |
|
| 25 |
const volatile char *j = (const volatile char*) P2; |
| 26 |
const volatile short *k = (const volatile short*) P2; |
| 27 |
const volatile int *l = (const volatile int*) P2; |
| 28 |
} |
| 29 |
|
| 30 |
// Aligned struct. |
| 31 |
struct __attribute__((aligned(16))) A { |
| 32 |
char buffer[16]; |
| 33 |
}; |
| 34 |
void test2(char *P) { |
| 35 |
struct A *a = (struct A*) P; // expected-warning {{cast from 'char *' to 'struct A *' increases required alignment from 1 to 16}} |
| 36 |
} |
| 37 |
|
| 38 |
// Incomplete type. |
| 39 |
void test3(char *P) { |
| 40 |
struct B *b = (struct B*) P; |
| 41 |
} |
| 42 |
|
| 43 |
// Do not issue a warning. The aligned attribute changes the alignment of the |
| 44 |
// variables and fields. |
| 45 |
char __attribute__((aligned(4))) a[16]; |
| 46 |
|
| 47 |
struct S0 { |
| 48 |
char a[16]; |
| 49 |
}; |
| 50 |
|
| 51 |
struct S { |
| 52 |
char __attribute__((aligned(4))) a[16]; |
| 53 |
struct S0 __attribute__((aligned(4))) s0; |
| 54 |
}; |
| 55 |
|
| 56 |
void test4(void) { |
| 57 |
struct S s; |
| 58 |
int *i = (int *)s.a; |
| 59 |
i = (int *)&s.s0; |
| 60 |
i = (int *)a; |
| 61 |
} |
| 62 |
|
| 63 |
// No warnings. |
| 64 |
typedef int (*FnTy)(void); |
| 65 |
unsigned int func5(void); |
| 66 |
|
| 67 |
FnTy test5(void) { |
| 68 |
return (FnTy)&func5; |
| 69 |
} |
| 70 |
|
| 71 |
void test6(void) { |
| 72 |
struct { |
| 73 |
int hello; |
| 74 |
doesnotexist world; // expected-error {{unknown type name 'doesnotexist'}} |
| 75 |
} foo; |
| 76 |
void **repro = (void **)&foo.hello; // expected-warning {{cast from 'int *' to 'void **' increases required alignment from 4 to 8}} |
| 77 |
} |