xref: /illumos-gate/usr/src/tools/smatch/src/validation/compound-sizes.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1 // This tests sparse "-vcompound" output.
2 #define NULL ((void*)0)
3 typedef unsigned int       uint32_t;
4 typedef unsigned long long uint64_t;
5 
6 // Do not list functions.
7 static int do_nothing(void)
8 {}
9 
10 // no:
11 static inline int zero(void)
12 {
13 	return 0 / 1;
14 }
15 
16 // no:
17 struct inventory {
18 	unsigned char	description[64];
19 	unsigned char	department[64];
20 	uint32_t	dept_number;
21 	uint32_t	item_cost;
22 	uint64_t	stock_number;
23 	uint32_t	tally[12];	// per month
24 };
25 
26 // no
27 static struct inventory *get_inv(uint64_t stocknum)
28 {
29 	return NULL;
30 }
31 
32 // no
33 union un {
34 	struct inventory inv;
35 	unsigned char	bytes[0];
36 };
37 
38 // yes
39 static union un un;
40 
41 // yes
42 static struct inventory	inven[100];
43 
44 // no
45 typedef struct inventory	inventory_t;
46 
47 // no
48 static struct inventory	*invptr;
49 
50 // yes
51 static inventory_t		invent[10];
52 
53 // no
54 static float		floater;
55 static double		double_float;
56 
57 // yes
58 static float		floats[42];
59 static double		doubles[84];
60 
61 // no
62 int main(void)
63 {
64 	// no, these are not global.
65 	struct inventory inv[10];
66 	inventory_t	invt[10];
67 	// what about statics?
68 	static struct inventory invtop;
69 	static inventory_t inv_top;
70 	static uint64_t stocknums[100];
71 
72 	invptr = get_inv(42000);
73 	return 0;
74 }
75 
76 /*
77  * check-name: compound-sizes
78  * check-command: sparse -vcompound $file
79  * check-assert: _Alignof(long long) == 8
80  *
81  * check-error-start
82 compound-sizes.c:39:17: union un static [toplevel] un: compound size 192, alignment 8
83 compound-sizes.c:42:25: struct inventory static [toplevel] inven[100]: compound size 19200, alignment 8
84 compound-sizes.c:51:33: struct inventory static [toplevel] [usertype] invent[10]: compound size 1920, alignment 8
85 compound-sizes.c:58:25: float static [toplevel] floats[42]: compound size 168, alignment 4
86 compound-sizes.c:59:25: double static [toplevel] doubles[84]: compound size 672, alignment 8
87  * check-error-end
88  */
89