xref: /netbsd-src/tests/usr.bin/xlint/lint1/lex_integer.c (revision aef5eb5f59cdfe8314f1b5f78ac04eb144e44010)
1 /*	$NetBSD: lex_integer.c,v 1.10 2022/06/17 18:54:53 rillig Exp $	*/
2 # 3 "lex_integer.c"
3 
4 /*
5  * Tests for lexical analysis of integer constants.
6  *
7  * C99 6.4.4.1 "Integer constants"
8  */
9 
10 /* lint1-only-if: lp64 */
11 
12 long signed_long;
13 unsigned long long unsigned_long_long_var;
14 
15 struct s {
16 	int member;
17 };
18 /*
19  * When lint tries to convert the argument to 'struct s', it prints the
20  * actual type of the argument as a side effect.
21  */
22 void print_type(struct s);
23 
24 void
25 no_suffix(void)
26 {
27 	/* expect+1: ... passing 'int' ... */
28 	print_type(0);
29 	/* The '-' is not part of the constant, it is a unary operator. */
30 	/* expect+1: ... passing 'int' ... */
31 	print_type(-1);
32 
33 	/* expect+1: ... passing 'int' ... */
34 	print_type(2147483647);
35 	/* expect+1: ... passing 'int' ... */
36 	print_type(0x7fffffff);
37 	/* expect+1: ... passing 'int' ... */
38 	print_type(017777777777);
39 
40 	/* expect+1: ... passing 'unsigned int' ... */
41 	print_type(0x80000000);
42 	/* expect+1: ... passing 'unsigned int' ... */
43 	print_type(020000000000);
44 	/* expect+1: ... passing 'unsigned int' ... */
45 	print_type(0xffffffff);
46 
47 	/* expect+1: ... passing 'long' ... */
48 	print_type(2147483648);
49 	/* expect+1: ... passing 'long' ... */
50 	print_type(0x0000000100000000);
51 	/* expect+1: ... passing 'long' ... */
52 	print_type(0x7fffffffffffffff);
53 
54 	/* expect+1: ... passing 'unsigned long' ... */
55 	print_type(0x8000000000000000);
56 	/* expect+1: ... passing 'unsigned long' ... */
57 	print_type(0xffffffffffffffff);
58 
59 	/* expect+2: warning: integer constant out of range [252] */
60 	/* expect+1: ... passing 'unsigned long' ... */
61 	print_type(0x00010000000000000000);
62 }
63 
64 void
65 suffix_u(void)
66 {
67 	/* expect+1: ... passing 'unsigned int' ... */
68 	print_type(3U);
69 	/* expect+1: ... passing 'unsigned int' ... */
70 	print_type(3u);
71 
72 	/* expect+1: ... passing 'unsigned int' ... */
73 	print_type(4294967295U);
74 	/* expect+1: ... passing 'unsigned long' ... */
75 	print_type(4294967296U);
76 }
77 
78 void
79 suffix_l(void)
80 {
81 	/* expect+1: ... passing 'long' ... */
82 	print_type(3L);
83 
84 	/* expect+1: ... passing 'long' ... */
85 	print_type(3l);
86 }
87 
88 void
89 suffix_ul(void)
90 {
91 	/* expect+1: ... passing 'unsigned long' ... */
92 	print_type(3UL);
93 	/* expect+1: ... passing 'unsigned long' ... */
94 	print_type(3LU);
95 }
96 
97 void
98 suffix_ll(void)
99 {
100 	/* expect+1: ... passing 'long long' ... */
101 	print_type(3LL);
102 
103 	/* The 'Ll' must not use mixed case. Checked by the compiler. */
104 	/* expect+1: ... passing 'long long' ... */
105 	print_type(3Ll);
106 
107 	/* expect+1: ... passing 'long long' ... */
108 	print_type(3ll);
109 }
110 
111 void
112 suffix_ull(void)
113 {
114 	/* expect+1: ... passing 'unsigned long long' ... */
115 	print_type(3llu);
116 	/* expect+1: ... passing 'unsigned long long' ... */
117 	print_type(3Ull);
118 
119 	/* The 'LL' must not be split. Checked by the compiler. */
120 	/* expect+1: ... passing 'unsigned long long' ... */
121 	print_type(3lul);
122 
123 	/* The 'Ll' must not use mixed case. Checked by the compiler. */
124 	/* expect+1: ... passing 'unsigned long long' ... */
125 	print_type(3ULl);
126 }
127 
128 void
129 suffix_too_many(void)
130 {
131 	/* expect+2: warning: malformed integer constant [251] */
132 	/* expect+1: ... passing 'long long' ... */
133 	print_type(3LLL);
134 
135 	/* expect+2: warning: malformed integer constant [251] */
136 	/* expect+1: ... passing 'unsigned int' ... */
137 	print_type(3uu);
138 }
139 
140 /* https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html */
141 void
142 binary_literal(void)
143 {
144 	/* This is a GCC extension, but lint doesn't know that. */
145 	/* expect+1: ... passing 'int' ... */
146 	print_type(0b1111000001011010);
147 
148 	/* expect+1: ... passing 'unsigned int' ... */
149 	print_type(0b11110000111100001111000011110000);
150 }
151