xref: /netbsd-src/usr.bin/xlint/common/lint.h (revision a644e605c1637fe174dffe39af8df4c1c51acbe0)
1 /*	$NetBSD: lint.h,v 1.51 2024/08/29 20:35:18 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Jochen Pohl for
18  *	The NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #else
37 #define HAVE_DECL_SYS_SIGNAME 1
38 #endif
39 
40 #include <sys/types.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <inttypes.h>
44 #include <stdbool.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 
48 #include "param.h"
49 
50 #if IS_LINT1 || IS_LINT2
51 
52 // Null-terminated character buffer, may contain null characters.
53 typedef struct {
54 	size_t	len;		/* excluding the terminating '\0' */
55 	size_t	cap;
56 	char	*data;
57 } buffer;
58 
59 /*
60  * Type specifiers, used in type structures (type_t) and elsewhere.
61  */
62 typedef enum {
63 	NO_TSPEC = 0,
64 	SIGNED,		/* keyword "signed", only used in the parser */
65 	UNSIGN,		/* keyword "unsigned", only used in the parser */
66 	BOOL,		/* _Bool */
67 	CHAR,		/* char */
68 	SCHAR,		/* signed char */
69 	UCHAR,		/* unsigned char */
70 	SHORT,		/* (signed) short */
71 	USHORT,		/* unsigned short */
72 	INT,		/* (signed) int */
73 	UINT,		/* unsigned int */
74 	LONG,		/* (signed) long */
75 	ULONG,		/* unsigned long */
76 	LLONG,		/* (signed) long long */
77 	ULLONG,		/* unsigned long long */
78 #ifdef INT128_SIZE
79 	INT128,		/* (signed) __int128_t */
80 	UINT128,	/* __uint128_t */
81 #endif
82 	FLOAT,		/* float */
83 	DOUBLE,		/* double or, with tflag, long float */
84 	LDOUBLE,	/* long double */
85 	COMPLEX,	/* keyword "_Complex", only used in the parser */
86 	FCOMPLEX,	/* float _Complex */
87 	DCOMPLEX,	/* double _Complex */
88 	LCOMPLEX,	/* long double _Complex */
89 	VOID,		/* void */
90 	AUTO_TYPE,	/* GCC's __auto_type */
91 	STRUCT,		/* structure tag */
92 	UNION,		/* union tag */
93 	ENUM,		/* enum tag */
94 	PTR,		/* pointer */
95 	ARRAY,		/* array */
96 	FUNC,		/* function */
97 #define NTSPEC ((int)FUNC + 1)
98 } tspec_t;
99 
100 
101 /*
102  * size of types, name and classification
103  */
104 typedef struct {
105 #if IS_LINT1
106 	unsigned int tt_size_in_bits;
107 	enum rank_kind {
108 		RK_NONE,
109 		RK_INTEGER,
110 		RK_FLOATING,
111 		RK_COMPLEX,
112 	} tt_rank_kind;
113 	unsigned int tt_rank_value;	/* relative size of the type; depends
114 					 * on pflag and PTRDIFF_TSPEC */
115 #endif
116 	tspec_t	tt_signed_counterpart;
117 	tspec_t	tt_unsigned_counterpart;
118 	bool	tt_is_integer:1;	/* integer type */
119 #if IS_LINT1
120 	bool	tt_is_uinteger:1;	/* unsigned integer type */
121 	bool	tt_is_floating:1;	/* floating point type */
122 	bool	tt_is_arithmetic:1;	/* arithmetic type */
123 	bool	tt_is_scalar:1;		/* scalar type */
124 	bool	tt_is_complex:1;	/* complex type */
125 #endif
126 	const char *tt_name;		/* name of the type */
127 } ttab_t;
128 
129 extern ttab_t ttab[];
130 
131 static inline const ttab_t *
132 type_properties(tspec_t t)
133 {
134 	return ttab + t;
135 }
136 
137 #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
138 #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
139 #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
140 #define is_integer(t)		(type_properties(t)->tt_is_integer)
141 #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
142 #define is_floating(t)		(type_properties(t)->tt_is_floating)
143 #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
144 #define is_complex(t)		(type_properties(t)->tt_is_complex)
145 #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
146 
147 #define has_operands(tn)	(modtab[(tn)->tn_op].m_has_operands)
148 
149 
150 typedef enum {
151 	NODECL,			/* not declared until now */
152 	DECL,			/* declared */
153 	TDEF,			/* tentative defined */
154 	DEF			/* defined */
155 } def_t;
156 
157 #if IS_LINT1
158 typedef struct lint1_type type_t;
159 #else
160 typedef struct lint2_type type_t;
161 #endif
162 #endif
163 
164 static inline bool
165 ch_isalnum(char ch)
166 {
167 	return isalnum((unsigned char)ch) != 0;
168 }
169 
170 static inline bool
171 ch_isalpha(char ch)
172 {
173 	return isalpha((unsigned char)ch) != 0;
174 }
175 
176 static inline bool
177 ch_isdigit(char ch)
178 {
179 	return isdigit((unsigned char)ch) != 0;
180 }
181 
182 static inline bool
183 ch_islower(char ch)
184 {
185 	return islower((unsigned char)ch) != 0;
186 }
187 
188 static inline bool
189 ch_isprint(char ch)
190 {
191 	return isprint((unsigned char)ch) != 0;
192 }
193 
194 static inline bool
195 ch_isspace(char ch)
196 {
197 	return isspace((unsigned char)ch) != 0;
198 }
199 
200 static inline bool
201 ch_isupper(char ch)
202 {
203 	return isupper((unsigned char)ch) != 0;
204 }
205 
206 #include "externs.h"
207