blob: 9a2fb678de664081b4395dd83e34232ebfb5bcdf [file] [log] [blame]
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00001// RUN: %clang_cc1 %s -triple %itanium_abi_triple -fsyntax-only -verify -Wc++-compat
Hans Wennborg9125b082014-01-13 19:48:13 +00002
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00003// Note: Empty C structs are 4 bytes in the Microsoft ABI.
Serge Pavlov89578fd2013-06-08 13:29:58 +00004
5struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
6};
7
8union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
9};
10
Serge Pavlovf7c1a212013-06-17 17:18:51 +000011struct emp_3 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000012 int : 0;
13};
14
Serge Pavlovf7c1a212013-06-17 17:18:51 +000015union emp_4 { // expected-warning {{union has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000016 int : 0;
17};
18
Serge Pavlovf7c1a212013-06-17 17:18:51 +000019struct emp_5 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000020 int : 0;
21 int : 0;
22};
23
Serge Pavlovf7c1a212013-06-17 17:18:51 +000024union emp_6 { // expected-warning {{union has size 0 in C, size 1 in C++}}
Serge Pavlov89578fd2013-06-08 13:29:58 +000025 int : 0;
26 int : 0;
27};
Serge Pavlovf7c1a212013-06-17 17:18:51 +000028
29struct emp_7 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
30 struct emp_1 f1;
31};
32
33union emp_8 { // expected-warning {{union has size 0 in C, size 1 in C++}}
34 struct emp_1 f1;
35};
36
37struct emp_9 { // expected-warning {{struct has size 0 in C, non-zero size in C++}}
38 struct emp_1 f1;
39 union emp_2 f2;
40};
Richard Smith84c6b3d2013-09-10 21:34:14 +000041
42// Checks for pointer subtraction (PR15683)
43struct emp_1 *func_1p(struct emp_1 *x) { return x - 5; }
44
45int func_1() {
46 struct emp_1 v[1];
47 return v - v; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
48}
49
50int func_2(struct emp_1 *x) {
51 return 1 + x - x; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
52}
53
54int func_3(struct emp_1 *x, struct emp_1 *y) {
55 return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
56}
57
58int func_4(struct emp_1 *x, const struct emp_1 *y) {
59 return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
60}
61
62int func_5(volatile struct emp_1 *x, const struct emp_1 *y) {
63 return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}
64}
65
66int func_6() {
67 union emp_2 v[1];
68 return v - v; // expected-warning {{subtraction of pointers to type 'union emp_2' of zero size has undefined behavior}}
69}
70
71struct A; // expected-note {{forward declaration of 'struct A'}}
72
73int func_7(struct A *x, struct A *y) {
74 return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct A'}}
75}
76
77int func_8(struct emp_1 (*x)[10], struct emp_1 (*y)[10]) {
78 return x - y; // expected-warning {{subtraction of pointers to type 'struct emp_1 [10]' of zero size has undefined behavior}}
79}
80
81int func_9(struct emp_1 (*x)[], struct emp_1 (*y)[]) {
82 return x - y; // expected-error {{arithmetic on a pointer to an incomplete type 'struct emp_1 []'}}
83}
84
85int func_10(int (*x)[0], int (*y)[0]) {
86 return x - y; // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}}
87}