warn-cast-align.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -Wcast-align -verify %s
  2. // Simple casts.
  3. void test0(char *P) {
  4. char *a = (char*) P;
  5. short *b = (short*) P; // expected-warning {{cast from 'char *' to 'short *' increases required alignment from 1 to 2}}
  6. int *c = (int*) P; // expected-warning {{cast from 'char *' to 'int *' increases required alignment from 1 to 4}}
  7. }
  8. // Casts from void* are a special case.
  9. void test1(void *P) {
  10. char *a = (char*) P;
  11. short *b = (short*) P;
  12. int *c = (int*) P;
  13. const volatile void *P2 = P;
  14. char *d = (char*) P2;
  15. short *e = (short*) P2;
  16. int *f = (int*) P2;
  17. const char *g = (const char*) P2;
  18. const short *h = (const short*) P2;
  19. const int *i = (const int*) P2;
  20. const volatile char *j = (const volatile char*) P2;
  21. const volatile short *k = (const volatile short*) P2;
  22. const volatile int *l = (const volatile int*) P2;
  23. }
  24. // Aligned struct.
  25. struct __attribute__((aligned(16))) A {
  26. char buffer[16];
  27. };
  28. void test2(char *P) {
  29. struct A *a = (struct A*) P; // expected-warning {{cast from 'char *' to 'struct A *' increases required alignment from 1 to 16}}
  30. }
  31. // Incomplete type.
  32. void test3(char *P) {
  33. struct B *b = (struct B*) P;
  34. }