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