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