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