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