lint.h revision 1.40 1 /* $NetBSD: lint.h,v 1.40 2023/07/08 09:35: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 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 unsigned int tt_rank; /* relative size of the type; depends on
99 * portable mode, as well as on whether sizeof
100 * has type 'int' or 'long' */
101 #endif
102 tspec_t tt_signed_counterpart;
103 tspec_t tt_unsigned_counterpart;
104 bool tt_is_integer:1; /* integer type */
105 #ifdef IS_LINT1
106 bool tt_is_uinteger:1; /* unsigned integer type */
107 bool tt_is_floating:1; /* floating point type */
108 bool tt_is_arithmetic:1; /* arithmetic type */
109 bool tt_is_scalar:1; /* scalar type */
110 bool tt_is_complex:1; /* complex type */
111 #endif
112 const char *tt_name; /* name of the type */
113 } ttab_t;
114
115 extern ttab_t ttab[];
116
117 static inline const ttab_t *
118 type_properties(tspec_t t) {
119 return ttab + t;
120 }
121
122 #define size_in_bits(t) (type_properties(t)->tt_size_in_bits)
123 #define signed_type(t) (type_properties(t)->tt_signed_counterpart)
124 #define unsigned_type(t) (type_properties(t)->tt_unsigned_counterpart)
125 #define is_integer(t) (type_properties(t)->tt_is_integer)
126 #define is_uinteger(t) (type_properties(t)->tt_is_uinteger)
127 #define is_floating(t) (type_properties(t)->tt_is_floating)
128 #define is_arithmetic(t) (type_properties(t)->tt_is_arithmetic)
129 #define is_complex(t) (type_properties(t)->tt_is_complex)
130 #define is_scalar(t) (type_properties(t)->tt_is_scalar)
131
132
133 typedef enum {
134 NODECL, /* not declared until now */
135 DECL, /* declared */
136 TDEF, /* tentative defined */
137 DEF /* defined */
138 } def_t;
139
140 /* Some data used for the output buffer. */
141 typedef struct ob {
142 char *o_buf; /* buffer */
143 char *o_end; /* first byte after buffer */
144 size_t o_len; /* length of buffer */
145 char *o_next; /* next free byte in buffer */
146 } ob_t;
147
148 #if defined(IS_LINT1)
149 typedef struct lint1_type type_t;
150 #else
151 typedef struct lint2_type type_t;
152 #endif
153 #endif
154
155 #include "externs.h"
156
157 static inline bool
158 ch_isalnum(char ch) { return isalnum((unsigned char)ch) != 0; }
159 static inline bool
160 ch_isdigit(char ch) { return isdigit((unsigned char)ch) != 0; }
161 static inline bool
162 ch_isprint(char ch) { return isprint((unsigned char)ch) != 0; }
163 static inline bool
164 ch_isspace(char ch) { return isspace((unsigned char)ch) != 0; }
165 static inline bool
166 ch_isupper(char ch) { return isupper((unsigned char)ch) != 0; }
167