aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validation/varargs-format-tests.c
blob: 4d80a9ce13d3bae0a56df00d997690c0b505aa34 (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
45
46
47
48
49
50
51
52
53
54
55
56
extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2)));

static int test(void)
{
	pf("%*d\n", 5, 10);		/* value 10, print width is 5 */
	pf("%2$*1$d\n", 5, 10);		/* value 10, print width is 5 */
	pf("%3$*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$-*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$*2$-d\n", 1, 5, 10);	/* bad, the "-" shouldn't be before the 'd' */
	pf("%3$ *2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$+*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$0+*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$+0*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$+#*2$d\n", 1, 5, 10);	/* ok, skipping the '1' */
	pf("%3$+#*2$.5d\n", 1, 5, 10);	/* ok, skipping the '1' */

	/* go with some precision as well as width strings */
	pf("%2$+*1$.6d\n", 5, 10);	/* ok */
	pf("%2$+*1$.*3$d\n", 5, 10, 6);	/* ok */
	pf("%2$+*3$.*1$d\n", 6, 10, 5);	/* ok */
	pf("%2$+*1$.*d\n", 5, 10, 6);	/* not ok */

	pf("%s", "msg");
	return 0;
}

static void test2(int x, int y, const void *p)
{
	pf("%02x%02x %8p\n", x, y, p);
}

static inline void fn(int x) { pf("%08x\n", x); }
static void test3(int x)
{
	fn;
	fn(x);
}

static void test4(int i, unsigned int u)
{
	pf("%d\n", i);
	pf("%x\n", u);
}

/*
 * check-name: variadic formatting tests for width/precisions
 * check-command: sparse -Wformat $file
 * check-known-to-fail
 *
 * check-error-start
varargs-format-tests.c:10:12: warning: cannot evaluate type '%3$*2$-d'
varargs-format-tests.c:10:12: warning: cannot evaluate format string
varargs-format-tests.c:22:12: warning: format 3: position: no position specified
 * check-error-end
 */