blob: 115f9381bc0c30236fb8418616c48316df04f9a1 [file] [log] [blame]
Hans Wennborg9125b082014-01-13 19:48:13 +00001// 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 Pavlov89578fd2013-06-08 13:29:58 +00005
6struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
7};
8
9union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
10};
11
Serge Pavlovf7c1a212013-06-17 17:18:51 +000012struct emp_3 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000013 int : 0;
14};
15
Serge Pavlovf7c1a212013-06-17 17:18:51 +000016union emp_4 { // expected-warning {{union has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000017 int : 0;
18};
19
Serge Pavlovf7c1a212013-06-17 17:18:51 +000020struct emp_5 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000021 int : 0;
22 int : 0;
23};
24
Serge Pavlovf7c1a212013-06-17 17:18:51 +000025union emp_6 { // expected-warning {{union has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000026 int : 0;
27 int : 0;
28};
Serge Pavlovf7c1a212013-06-17 17:18:51 +000029
30struct emp_7 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
31 struct emp_1 f1;
32};
33
34union emp_8 { // expected-warning {{union has size 0 in C, size 1 in C++}}
35 struct emp_1 f1;
36};
37
38struct 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 Smith84c6b3d2013-09-10 21:34:14 +000042
43// Checks for pointer subtraction (PR15683)
44struct emp_1 *func_1p(struct emp_1 *x) { return x - 5; }
45
46int 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
51int 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
55int 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
59int 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
63int 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
67int 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
72struct A; // expected-note {{forward declaration of 'struct A'}}
73
74int 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
78int 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
82int 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
86int 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}