xref: /netbsd/usr.bin/xlint/common/lint.h (revision 3a0e83fa)
1 /*	$NetBSD: lint.h,v 1.42 2023/07/13 08:40:38 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 defined(IS_LINT1) || defined(IS_LINT2)
51 /*
52  * Type specifiers, used in type structures (type_t) and elsewhere.
53  */
54 typedef enum {
55 	NO_TSPEC = 0,
56 	SIGNED,		/* keyword "signed", only used in the parser */
57 	UNSIGN,		/* keyword "unsigned", only used in the parser */
58 	BOOL,		/* _Bool */
59 	CHAR,		/* char */
60 	SCHAR,		/* signed char */
61 	UCHAR,		/* unsigned char */
62 	SHORT,		/* (signed) short */
63 	USHORT,		/* unsigned short */
64 	INT,		/* (signed) int */
65 	UINT,		/* unsigned int */
66 	LONG,		/* (signed) long */
67 	ULONG,		/* unsigned long */
68 	LLONG,		/* (signed) long long */
69 	ULLONG,		/* unsigned long long */
70 #ifdef INT128_SIZE
71 	INT128,		/* (signed) __int128_t */
72 	UINT128,	/* __uint128_t */
73 #endif
74 	FLOAT,		/* float */
75 	DOUBLE,		/* double or, with tflag, long float */
76 	LDOUBLE,	/* long double */
77 	COMPLEX,	/* keyword "_Complex", only used in the parser */
78 	FCOMPLEX,	/* float _Complex */
79 	DCOMPLEX,	/* double _Complex */
80 	LCOMPLEX,	/* long double _Complex */
81 	VOID,		/* void */
82 	STRUCT,		/* structure tag */
83 	UNION,		/* union tag */
84 	ENUM,		/* enum tag */
85 	PTR,		/* pointer */
86 	ARRAY,		/* array */
87 	FUNC,		/* function */
88 #define NTSPEC ((int)FUNC + 1)
89 } tspec_t;
90 
91 
92 /*
93  * size of types, name and classification
94  */
95 typedef	struct {
96 #ifdef IS_LINT1
97 	unsigned int tt_size_in_bits;
98 	enum rank_kind {
99 		RK_NONE,
100 		RK_INTEGER,
101 		RK_FLOATING,
102 		RK_COMPLEX,
103 	} tt_rank_kind;
104 	unsigned int tt_rank_value;	/* relative size of the type; depends
105 					 * on pflag and PTRDIFF_TSPEC */
106 #endif
107 	tspec_t	tt_signed_counterpart;
108 	tspec_t	tt_unsigned_counterpart;
109 	bool	tt_is_integer:1;	/* integer type */
110 #ifdef IS_LINT1
111 	bool	tt_is_uinteger:1;	/* unsigned integer type */
112 	bool	tt_is_floating:1;	/* floating point type */
113 	bool	tt_is_arithmetic:1;	/* arithmetic type */
114 	bool	tt_is_scalar:1;		/* scalar type */
115 	bool	tt_is_complex:1;	/* complex type */
116 #endif
117 	const char *tt_name;		/* name of the type */
118 } ttab_t;
119 
120 extern	ttab_t	ttab[];
121 
122 static inline const ttab_t *
type_properties(tspec_t t)123 type_properties(tspec_t t) {
124 	return ttab + t;
125 }
126 
127 #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
128 #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
129 #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
130 #define is_integer(t)		(type_properties(t)->tt_is_integer)
131 #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
132 #define is_floating(t)		(type_properties(t)->tt_is_floating)
133 #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
134 #define is_complex(t)		(type_properties(t)->tt_is_complex)
135 #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
136 
137 
138 typedef	enum {
139 	NODECL,			/* not declared until now */
140 	DECL,			/* declared */
141 	TDEF,			/* tentative defined */
142 	DEF			/* defined */
143 } def_t;
144 
145 /* Some data used for the output buffer. */
146 typedef	struct	ob {
147 	char	*o_buf;		/* buffer */
148 	char	*o_end;		/* first byte after buffer */
149 	size_t	o_len;		/* length of buffer */
150 	char	*o_next;	/* next free byte in buffer */
151 } ob_t;
152 
153 #if defined(IS_LINT1)
154 typedef struct lint1_type type_t;
155 #else
156 typedef struct lint2_type type_t;
157 #endif
158 #endif
159 
160 #include "externs.h"
161 
162 static inline bool
ch_isalnum(char ch)163 ch_isalnum(char ch) { return isalnum((unsigned char)ch) != 0; }
164 static inline bool
ch_isdigit(char ch)165 ch_isdigit(char ch) { return isdigit((unsigned char)ch) != 0; }
166 static inline bool
ch_isprint(char ch)167 ch_isprint(char ch) { return isprint((unsigned char)ch) != 0; }
168 static inline bool
ch_isspace(char ch)169 ch_isspace(char ch) { return isspace((unsigned char)ch) != 0; }
170 static inline bool
ch_isupper(char ch)171 ch_isupper(char ch) { return isupper((unsigned char)ch) != 0; }
172