Hans Wennborg | 9125b08 | 2014-01-13 19:48:13 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -cxx-abi itanium -fsyntax-only -verify -Wc++-compat |
| 2 | |
| 3 | // FIXME: Empty C structs are 4 bytes in MSVC, but the -cxx-abi flag probably |
| 4 | // shouldn't affect this since it's not C++. PR18263. |
Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 5 | |
| 6 | struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}} |
| 7 | }; |
| 8 | |
| 9 | union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}} |
| 10 | }; |
| 11 | |
Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 12 | struct emp_3 { // expected-warning {{struct has size 0 in C, size 1 in C++}} |
Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 13 | int : 0; |
| 14 | }; |
| 15 | |
Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 16 | union emp_4 { // expected-warning {{union has size 0 in C, size 1 in C++}} |
Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 17 | int : 0; |
| 18 | }; |
| 19 | |
Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 20 | struct emp_5 { // expected-warning {{struct has size 0 in C, size 1 in C++}} |
Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 21 | int : 0; |
| 22 | int : 0; |
| 23 | }; |
| 24 | |
Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 25 | union emp_6 { // expected-warning {{union has size 0 in C, size 1 in C++}} |
Serge Pavlov | 89578fd | 2013-06-08 13:29:58 +0000 | [diff] [blame] | 26 | int : 0; |
| 27 | int : 0; |
| 28 | }; |
Serge Pavlov | f7c1a21 | 2013-06-17 17:18:51 +0000 | [diff] [blame] | 29 | |
| 30 | struct emp_7 { // expected-warning {{struct has size 0 in C, size 1 in C++}} |
| 31 | struct emp_1 f1; |
| 32 | }; |
| 33 | |
| 34 | union emp_8 { // expected-warning {{union has size 0 in C, size 1 in C++}} |
| 35 | struct emp_1 f1; |
| 36 | }; |
| 37 | |
| 38 | struct emp_9 { // expected-warning {{struct has size 0 in C, non-zero size in C++}} |
| 39 | struct emp_1 f1; |
| 40 | union emp_2 f2; |
| 41 | }; |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 42 | |
| 43 | // Checks for pointer subtraction (PR15683) |
| 44 | struct emp_1 *func_1p(struct emp_1 *x) { return x - 5; } |
| 45 | |
| 46 | int func_1() { |
| 47 | struct emp_1 v[1]; |
| 48 | return v - v; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}} |
| 49 | } |
| 50 | |
| 51 | int func_2(struct emp_1 *x) { |
| 52 | return 1 + x - x; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}} |
| 53 | } |
| 54 | |
| 55 | int func_3(struct emp_1 *x, struct emp_1 *y) { |
| 56 | return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}} |
| 57 | } |
| 58 | |
| 59 | int func_4(struct emp_1 *x, const struct emp_1 *y) { |
| 60 | return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}} |
| 61 | } |
| 62 | |
| 63 | int func_5(volatile struct emp_1 *x, const struct emp_1 *y) { |
| 64 | return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}} |
| 65 | } |
| 66 | |
| 67 | int func_6() { |
| 68 | union emp_2 v[1]; |
| 69 | return v - v; // expected-warning {{subtraction of pointers to type 'union emp_2' of zero size has undefined behavior}} |
| 70 | } |
| 71 | |
| 72 | struct A; // expected-note {{forward declaration of 'struct A'}} |
| 73 | |
| 74 | int func_7(struct A *x, struct A *y) { |
| 75 | return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct A'}} |
| 76 | } |
| 77 | |
| 78 | int func_8(struct emp_1 (*x)[10], struct emp_1 (*y)[10]) { |
| 79 | return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1 [10]' of zero size has undefined behavior}} |
| 80 | } |
| 81 | |
| 82 | int func_9(struct emp_1 (*x)[], struct emp_1 (*y)[]) { |
| 83 | return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct emp_1 []'}} |
| 84 | } |
| 85 | |
| 86 | int func_10(int (*x)[0], int (*y)[0]) { |
| 87 | return x - y; // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}} |
| 88 | } |