xref: /netbsd-src/usr.bin/xlint/common/lint.h (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: lint.h,v 1.50 2024/05/12 18:49:35 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 	STRUCT,		/* structure tag */
91 	UNION,		/* union tag */
92 	ENUM,		/* enum tag */
93 	PTR,		/* pointer */
94 	ARRAY,		/* array */
95 	FUNC,		/* function */
96 #define NTSPEC ((int)FUNC + 1)
97 } tspec_t;
98 
99 
100 /*
101  * size of types, name and classification
102  */
103 typedef struct {
104 #if IS_LINT1
105 	unsigned int tt_size_in_bits;
106 	enum rank_kind {
107 		RK_NONE,
108 		RK_INTEGER,
109 		RK_FLOATING,
110 		RK_COMPLEX,
111 	} tt_rank_kind;
112 	unsigned int tt_rank_value;	/* relative size of the type; depends
113 					 * on pflag and PTRDIFF_TSPEC */
114 #endif
115 	tspec_t	tt_signed_counterpart;
116 	tspec_t	tt_unsigned_counterpart;
117 	bool	tt_is_integer:1;	/* integer type */
118 #if IS_LINT1
119 	bool	tt_is_uinteger:1;	/* unsigned integer type */
120 	bool	tt_is_floating:1;	/* floating point type */
121 	bool	tt_is_arithmetic:1;	/* arithmetic type */
122 	bool	tt_is_scalar:1;		/* scalar type */
123 	bool	tt_is_complex:1;	/* complex type */
124 #endif
125 	const char *tt_name;		/* name of the type */
126 } ttab_t;
127 
128 extern ttab_t ttab[];
129 
130 static inline const ttab_t *
131 type_properties(tspec_t t)
132 {
133 	return ttab + t;
134 }
135 
136 #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
137 #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
138 #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
139 #define is_integer(t)		(type_properties(t)->tt_is_integer)
140 #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
141 #define is_floating(t)		(type_properties(t)->tt_is_floating)
142 #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
143 #define is_complex(t)		(type_properties(t)->tt_is_complex)
144 #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
145 
146 #define has_operands(tn)	(modtab[(tn)->tn_op].m_has_operands)
147 
148 
149 typedef enum {
150 	NODECL,			/* not declared until now */
151 	DECL,			/* declared */
152 	TDEF,			/* tentative defined */
153 	DEF			/* defined */
154 } def_t;
155 
156 #if IS_LINT1
157 typedef struct lint1_type type_t;
158 #else
159 typedef struct lint2_type type_t;
160 #endif
161 #endif
162 
163 static inline bool
164 ch_isalnum(char ch)
165 {
166 	return isalnum((unsigned char)ch) != 0;
167 }
168 
169 static inline bool
170 ch_isalpha(char ch)
171 {
172 	return isalpha((unsigned char)ch) != 0;
173 }
174 
175 static inline bool
176 ch_isdigit(char ch)
177 {
178 	return isdigit((unsigned char)ch) != 0;
179 }
180 
181 static inline bool
182 ch_islower(char ch)
183 {
184 	return islower((unsigned char)ch) != 0;
185 }
186 
187 static inline bool
188 ch_isprint(char ch)
189 {
190 	return isprint((unsigned char)ch) != 0;
191 }
192 
193 static inline bool
194 ch_isspace(char ch)
195 {
196 	return isspace((unsigned char)ch) != 0;
197 }
198 
199 static inline bool
200 ch_isupper(char ch)
201 {
202 	return isupper((unsigned char)ch) != 0;
203 }
204 
205 #include "externs.h"
206