empty1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // RUN: %clang_cc1 %s -triple %itanium_abi_triple -fsyntax-only -verify -Wc++-compat
  2. // Note: Empty C structs are 4 bytes in the Microsoft ABI.
  3. struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
  4. };
  5. union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
  6. };
  7. struct emp_3 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
  8. int : 0;
  9. };
  10. union emp_4 { // expected-warning {{union has size 0 in C, size 1 in C++}}
  11. int : 0;
  12. };
  13. struct emp_5 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
  14. int : 0;
  15. int : 0;
  16. };
  17. union emp_6 { // expected-warning {{union has size 0 in C, size 1 in C++}}
  18. int : 0;
  19. int : 0;
  20. };
  21. struct emp_7 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
  22. struct emp_1 f1;
  23. };
  24. union emp_8 { // expected-warning {{union has size 0 in C, size 1 in C++}}
  25. struct emp_1 f1;
  26. };
  27. struct emp_9 { // expected-warning {{struct has size 0 in C, non-zero size in C++}}
  28. struct emp_1 f1;
  29. union emp_2 f2;
  30. };
  31. // Checks for pointer subtraction (PR15683)
  32. struct emp_1 *func_1p(struct emp_1 *x) { return x - 5; }
  33. int func_1() {
  34. struct emp_1 v[1];
  35. return v - v; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
  36. }
  37. int func_2(struct emp_1 *x) {
  38. return 1 + x - x; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
  39. }
  40. int func_3(struct emp_1 *x, struct emp_1 *y) {
  41. return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
  42. }
  43. int func_4(struct emp_1 *x, const struct emp_1 *y) {
  44. return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
  45. }
  46. int func_5(volatile struct emp_1 *x, const struct emp_1 *y) {
  47. return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
  48. }
  49. int func_6() {
  50. union emp_2 v[1];
  51. return v - v; // expected-warning {{subtraction of pointers to type 'union emp_2' of zero size has undefined behavior}}
  52. }
  53. struct A; // expected-note {{forward declaration of 'struct A'}}
  54. int func_7(struct A *x, struct A *y) {
  55. return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct A'}}
  56. }
  57. int func_8(struct emp_1 (*x)[10], struct emp_1 (*y)[10]) {
  58. return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1 [10]' of zero size has undefined behavior}}
  59. }
  60. int func_9(struct emp_1 (*x)[], struct emp_1 (*y)[]) {
  61. return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct emp_1 []'}}
  62. }
  63. int func_10(int (*x)[0], int (*y)[0]) {
  64. return x - y; // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}}
  65. }