xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/empty1.c (revision 0b98e8aad89f2bd4ba80b523d73cf29e9dd82ce1)
1 // RUN: %clang_cc1 %s -fsyntax-only -verify -Wc++-compat
2 
3 struct emp_1 { // expected-warning {{empty struct has size 0 in C, size 1 in C++}}
4 };
5 
6 union emp_2 { // expected-warning {{empty union has size 0 in C, size 1 in C++}}
7 };
8 
9 struct emp_3 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
10   int : 0;
11 };
12 
13 union emp_4 { // expected-warning {{union has size 0 in C, size 1 in C++}}
14   int : 0;
15 };
16 
17 struct emp_5 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
18   int : 0;
19   int : 0;
20 };
21 
22 union emp_6 { // expected-warning {{union has size 0 in C, size 1 in C++}}
23   int : 0;
24   int : 0;
25 };
26 
27 struct emp_7 { // expected-warning {{struct has size 0 in C, size 1 in C++}}
28   struct emp_1 f1;
29 };
30 
31 union emp_8 { // expected-warning {{union has size 0 in C, size 1 in C++}}
32   struct emp_1 f1;
33 };
34 
35 struct 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)
41 struct emp_1 *func_1p(struct emp_1 *x) { return x - 5; }
42 
43 int 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 
48 int 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 
52 int 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 
56 int 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 
60 int 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 
64 int 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 
69 struct A; // expected-note {{forward declaration of 'struct A'}}
70 
71 int 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 
75 int 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 
79 int 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 
83 int 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