Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: lint.h,v 1.52 2026/01/11 18:11: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 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 	INT128,		/* (signed) __int128_t */
     79 	UINT128,	/* __uint128_t */
     80 	FLOAT,		/* float */
     81 	DOUBLE,		/* double or, with tflag, long float */
     82 	LDOUBLE,	/* long double */
     83 	COMPLEX,	/* keyword "_Complex", only used in the parser */
     84 	FCOMPLEX,	/* float _Complex */
     85 	DCOMPLEX,	/* double _Complex */
     86 	LCOMPLEX,	/* long double _Complex */
     87 	VOID,		/* void */
     88 	AUTO_TYPE,	/* GCC's __auto_type */
     89 	STRUCT,		/* structure tag */
     90 	UNION,		/* union tag */
     91 	ENUM,		/* enum tag */
     92 	PTR,		/* pointer */
     93 	ARRAY,		/* array */
     94 	FUNC,		/* function */
     95 #define NTSPEC ((int)FUNC + 1)
     96 } tspec_t;
     97 
     98 
     99 /*
    100  * size of types, name and classification
    101  */
    102 typedef struct {
    103 #if IS_LINT1
    104 	unsigned int tt_size_in_bits;
    105 	enum rank_kind {
    106 		RK_NONE,
    107 		RK_INTEGER,
    108 		RK_FLOATING,
    109 		RK_COMPLEX,
    110 	} tt_rank_kind;
    111 	unsigned int tt_rank_value;	/* relative size of the type; depends
    112 					 * on pflag and PTRDIFF_TSPEC */
    113 #endif
    114 	tspec_t	tt_signed_counterpart;
    115 	tspec_t	tt_unsigned_counterpart;
    116 	bool	tt_is_integer:1;	/* integer type */
    117 #if IS_LINT1
    118 	bool	tt_is_uinteger:1;	/* unsigned integer type */
    119 	bool	tt_is_floating:1;	/* floating point type */
    120 	bool	tt_is_arithmetic:1;	/* arithmetic type */
    121 	bool	tt_is_scalar:1;		/* scalar type */
    122 	bool	tt_is_complex:1;	/* complex type */
    123 #endif
    124 	const char *tt_name;		/* name of the type */
    125 } ttab_t;
    126 
    127 extern ttab_t ttab[];
    128 
    129 static inline const ttab_t *
    130 type_properties(tspec_t t)
    131 {
    132 	return ttab + t;
    133 }
    134 
    135 #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
    136 #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
    137 #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
    138 #define is_integer(t)		(type_properties(t)->tt_is_integer)
    139 #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
    140 #define is_floating(t)		(type_properties(t)->tt_is_floating)
    141 #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
    142 #define is_complex(t)		(type_properties(t)->tt_is_complex)
    143 #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
    144 
    145 #define has_operands(tn)	(modtab[(tn)->tn_op].m_has_operands)
    146 
    147 
    148 typedef enum {
    149 	NODECL,			/* not declared until now */
    150 	DECL,			/* declared */
    151 	TDEF,			/* tentative defined */
    152 	DEF			/* defined */
    153 } def_t;
    154 
    155 #if IS_LINT1
    156 typedef struct lint1_type type_t;
    157 #else
    158 typedef struct lint2_type type_t;
    159 #endif
    160 #endif
    161 
    162 static inline bool
    163 ch_isalnum(char ch)
    164 {
    165 	return isalnum((unsigned char)ch) != 0;
    166 }
    167 
    168 static inline bool
    169 ch_isalpha(char ch)
    170 {
    171 	return isalpha((unsigned char)ch) != 0;
    172 }
    173 
    174 static inline bool
    175 ch_isdigit(char ch)
    176 {
    177 	return isdigit((unsigned char)ch) != 0;
    178 }
    179 
    180 static inline bool
    181 ch_islower(char ch)
    182 {
    183 	return islower((unsigned char)ch) != 0;
    184 }
    185 
    186 static inline bool
    187 ch_isprint(char ch)
    188 {
    189 	return isprint((unsigned char)ch) != 0;
    190 }
    191 
    192 static inline bool
    193 ch_isspace(char ch)
    194 {
    195 	return isspace((unsigned char)ch) != 0;
    196 }
    197 
    198 static inline bool
    199 ch_isupper(char ch)
    200 {
    201 	return isupper((unsigned char)ch) != 0;
    202 }
    203 
    204 #include "externs.h"
    205