xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_376.c (revision 465ab238788e2dd3555edb0497148fa4c5f47dc7)
1 /*	$NetBSD: msg_376.c,v 1.4 2024/08/31 06:57:31 rillig Exp $	*/
2 # 3 "msg_376.c"
3 
4 // Test for message: '%.*s' overlaps earlier '%.*s' on bit %u [376]
5 
6 /*
7  * When bits and fields overlap, it's often due to typos or off-by-one errors.
8  */
9 
10 /* lint1-extra-flags: -X 351 */
11 
12 typedef typeof(sizeof(0)) size_t;
13 typedef unsigned long long uint64_t;
14 
15 int snprintb(char *, size_t, const char *, uint64_t);
16 
17 void
18 example(unsigned u32, uint64_t u64)
19 {
20 	char buf[64];
21 
22 	// In the old-style format, bit positions are 1-based.
23 	snprintb(buf, sizeof(buf),
24 	    "\020"
25 	    "\001lsb"
26 	    "\x01lsb"
27 	    "\040msb"
28 	    "\x20msb"
29 	    "\041oob"
30 	    "\x21oob",
31 	    /* expect+4: warning: '\x01lsb' overlaps earlier '\001lsb' on bit 1 [376] */
32 	    /* expect+3: warning: escaped character '\041' in description of conversion '\x20msb""\041' [363] */
33 	    /* expect+2: warning: escaped character '\x21' in description of conversion '\x20msb""\041oob""\x21' [363] */
34 	    /* expect+1: warning: '\x20msb""\041oob""\x21oob' overlaps earlier '\040msb' on bit 32 [376] */
35 	    u32);
36 
37 	// In the new-style format, bit positions are 0-based.
38 	/* expect+10: warning: 'b\x00lsb\0' overlaps earlier 'b\000lsb\0' on bit 0 [376] */
39 	/* expect+9: warning: 'b\x3fmsb\0' overlaps earlier 'b\077msb\0' on bit 63 [376] */
40 	/* expect+8: warning: bit position '\x40' (64) in 'b\x40oob\0' out of range 0..63 [371] */
41 	snprintb(buf, sizeof(buf),
42 	    "\177\020"
43 	    "b\000lsb\0"
44 	    "b\x00lsb\0"
45 	    "b\077msb\0"
46 	    "b\x3fmsb\0"
47 	    "b\x40oob\0",
48 	    u64);
49 
50 	/* expect+7: warning: 'F\014\010f2\0' overlaps earlier 'f\010\010f1\0' on bit 12 [376] */
51 	/* expect+6: warning: 'f\020\010f3\0' overlaps earlier 'F\014\010f2\0' on bit 16 [376] */
52 	snprintb(buf, sizeof(buf),
53 	    "\177\020"
54 	    "f\010\010f1\0"
55 	    "F\014\010f2\0"
56 	    "f\020\010f3\0",
57 	    u64);
58 }
59