Home | History | Annotate | Line # | Download | only in common
lint.h revision 1.47
      1 /*	$NetBSD: lint.h,v 1.47 2024/01/20 10:25:57 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  * 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 #if 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 #if 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 *
    123 type_properties(tspec_t t)
    124 {
    125 	return ttab + t;
    126 }
    127 
    128 #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
    129 #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
    130 #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
    131 #define is_integer(t)		(type_properties(t)->tt_is_integer)
    132 #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
    133 #define is_floating(t)		(type_properties(t)->tt_is_floating)
    134 #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
    135 #define is_complex(t)		(type_properties(t)->tt_is_complex)
    136 #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
    137 
    138 #define has_operands(tn)	(modtab[(tn)->tn_op].m_has_operands)
    139 
    140 
    141 typedef enum {
    142 	NODECL,			/* not declared until now */
    143 	DECL,			/* declared */
    144 	TDEF,			/* tentative defined */
    145 	DEF			/* defined */
    146 } def_t;
    147 
    148 #if 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)
    159 {
    160 	return isalnum((unsigned char)ch) != 0;
    161 }
    162 
    163 static inline bool
    164 ch_isdigit(char ch)
    165 {
    166 	return isdigit((unsigned char)ch) != 0;
    167 }
    168 
    169 static inline bool
    170 ch_isprint(char ch)
    171 {
    172 	return isprint((unsigned char)ch) != 0;
    173 }
    174 
    175 static inline bool
    176 ch_isspace(char ch)
    177 {
    178 	return isspace((unsigned char)ch) != 0;
    179 }
    180 
    181 static inline bool
    182 ch_isupper(char ch)
    183 {
    184 	return isupper((unsigned char)ch) != 0;
    185 }
    186