aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validation/sizeof-void.c
blob: 0fd917a21fb296a95da0fcd6119618e8e3111417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define is_constexpr(x) \
	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

static int test(void)
{
	unsigned int s = 0, i = 0;
	void *ptr = &i;

	// OK
	s += sizeof i;
	s += sizeof &i;
	s += sizeof ptr;
	s += sizeof &ptr;

	// KO
	s += sizeof(void);
	s += sizeof *ptr;
	s += is_constexpr(ptr++);
	s += is_constexpr((i++, 1));
	s += is_constexpr(sizeof *ptr);
	s += is_constexpr(ptr + 1);
	s += is_constexpr(&ptr + 1);
	s += is_constexpr(*(((char *)&ptr) + 1));

	return s;
}

/*
 * check-name: sizeof(void) is valid
 * check-description: sizeof(void) was rejected because void is an incomplete
 * type
 * check-command: sparse -Wpointer-arith $file
 *
 * check-error-start
sizeof-void.c:16:14: warning: expression using sizeof(void)
sizeof-void.c:17:14: warning: expression using sizeof(void)
sizeof-void.c:18:14: warning: expression using sizeof(void)
sizeof-void.c:19:14: warning: expression using sizeof(void)
sizeof-void.c:20:14: warning: expression using sizeof(void)
sizeof-void.c:21:14: warning: expression using sizeof(void)
sizeof-void.c:22:14: warning: expression using sizeof(void)
sizeof-void.c:23:14: warning: expression using sizeof(void)
 * check-error-end
 */