Home | History | Annotate | Line # | Download | only in common
      1  1.51    rillig /*	$NetBSD: lint.h,v 1.51 2024/08/29 20:35:18 rillig Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.1   thorpej  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1   thorpej  * All Rights Reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     16   1.1   thorpej  *    must display the following acknowledgement:
     17  1.42    rillig  *	This product includes software developed by Jochen Pohl for
     18   1.1   thorpej  *	The NetBSD Project.
     19   1.1   thorpej  * 4. The name of the author may not be used to endorse or promote products
     20   1.1   thorpej  *    derived from this software without specific prior written permission.
     21   1.1   thorpej  *
     22   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1   thorpej  */
     33   1.1   thorpej 
     34   1.7     lukem #if HAVE_NBTOOL_CONFIG_H
     35   1.7     lukem #include "nbtool_config.h"
     36   1.2        tv #else
     37   1.2        tv #define HAVE_DECL_SYS_SIGNAME 1
     38   1.2        tv #endif
     39   1.2        tv 
     40   1.1   thorpej #include <sys/types.h>
     41  1.25    rillig #include <ctype.h>
     42   1.3        tv #include <err.h>
     43   1.6        tv #include <inttypes.h>
     44  1.21    rillig #include <stdbool.h>
     45  1.21    rillig #include <stddef.h>
     46   1.5        tv #include <stdio.h>
     47   1.1   thorpej 
     48   1.1   thorpej #include "param.h"
     49   1.1   thorpej 
     50  1.47    rillig #if IS_LINT1 || IS_LINT2
     51  1.48    rillig 
     52  1.48    rillig // Null-terminated character buffer, may contain null characters.
     53  1.48    rillig typedef struct {
     54  1.48    rillig 	size_t	len;		/* excluding the terminating '\0' */
     55  1.48    rillig 	size_t	cap;
     56  1.48    rillig 	char	*data;
     57  1.48    rillig } buffer;
     58  1.48    rillig 
     59   1.1   thorpej /*
     60  1.17    rillig  * Type specifiers, used in type structures (type_t) and elsewhere.
     61   1.1   thorpej  */
     62   1.1   thorpej typedef enum {
     63  1.36    rillig 	NO_TSPEC = 0,
     64   1.1   thorpej 	SIGNED,		/* keyword "signed", only used in the parser */
     65   1.1   thorpej 	UNSIGN,		/* keyword "unsigned", only used in the parser */
     66   1.8      yamt 	BOOL,		/* _Bool */
     67   1.1   thorpej 	CHAR,		/* char */
     68   1.1   thorpej 	SCHAR,		/* signed char */
     69   1.1   thorpej 	UCHAR,		/* unsigned char */
     70   1.1   thorpej 	SHORT,		/* (signed) short */
     71   1.1   thorpej 	USHORT,		/* unsigned short */
     72   1.1   thorpej 	INT,		/* (signed) int */
     73   1.1   thorpej 	UINT,		/* unsigned int */
     74   1.1   thorpej 	LONG,		/* (signed) long */
     75   1.1   thorpej 	ULONG,		/* unsigned long */
     76  1.38    rillig 	LLONG,		/* (signed) long long */
     77  1.38    rillig 	ULLONG,		/* unsigned long long */
     78  1.14  christos #ifdef INT128_SIZE
     79  1.14  christos 	INT128,		/* (signed) __int128_t */
     80  1.14  christos 	UINT128,	/* __uint128_t */
     81  1.14  christos #endif
     82   1.1   thorpej 	FLOAT,		/* float */
     83   1.1   thorpej 	DOUBLE,		/* double or, with tflag, long float */
     84   1.1   thorpej 	LDOUBLE,	/* long double */
     85  1.37    rillig 	COMPLEX,	/* keyword "_Complex", only used in the parser */
     86  1.37    rillig 	FCOMPLEX,	/* float _Complex */
     87  1.37    rillig 	DCOMPLEX,	/* double _Complex */
     88  1.37    rillig 	LCOMPLEX,	/* long double _Complex */
     89   1.1   thorpej 	VOID,		/* void */
     90  1.51    rillig 	AUTO_TYPE,	/* GCC's __auto_type */
     91   1.1   thorpej 	STRUCT,		/* structure tag */
     92   1.1   thorpej 	UNION,		/* union tag */
     93   1.1   thorpej 	ENUM,		/* enum tag */
     94   1.1   thorpej 	PTR,		/* pointer */
     95   1.1   thorpej 	ARRAY,		/* array */
     96   1.1   thorpej 	FUNC,		/* function */
     97  1.37    rillig #define NTSPEC ((int)FUNC + 1)
     98   1.1   thorpej } tspec_t;
     99   1.1   thorpej 
    100  1.19    rillig 
    101   1.1   thorpej /*
    102   1.1   thorpej  * size of types, name and classification
    103   1.1   thorpej  */
    104  1.46    rillig typedef struct {
    105  1.47    rillig #if IS_LINT1
    106  1.30    rillig 	unsigned int tt_size_in_bits;
    107  1.41    rillig 	enum rank_kind {
    108  1.41    rillig 		RK_NONE,
    109  1.41    rillig 		RK_INTEGER,
    110  1.41    rillig 		RK_FLOATING,
    111  1.41    rillig 		RK_COMPLEX,
    112  1.41    rillig 	} tt_rank_kind;
    113  1.41    rillig 	unsigned int tt_rank_value;	/* relative size of the type; depends
    114  1.41    rillig 					 * on pflag and PTRDIFF_TSPEC */
    115  1.32    rillig #endif
    116  1.20    rillig 	tspec_t	tt_signed_counterpart;
    117  1.20    rillig 	tspec_t	tt_unsigned_counterpart;
    118  1.34    rillig 	bool	tt_is_integer:1;	/* integer type */
    119  1.47    rillig #if IS_LINT1
    120  1.34    rillig 	bool	tt_is_uinteger:1;	/* unsigned integer type */
    121  1.34    rillig 	bool	tt_is_floating:1;	/* floating point type */
    122  1.34    rillig 	bool	tt_is_arithmetic:1;	/* arithmetic type */
    123  1.34    rillig 	bool	tt_is_scalar:1;		/* scalar type */
    124  1.34    rillig 	bool	tt_is_complex:1;	/* complex type */
    125  1.32    rillig #endif
    126  1.15    rillig 	const char *tt_name;		/* name of the type */
    127   1.1   thorpej } ttab_t;
    128   1.1   thorpej 
    129  1.46    rillig extern ttab_t ttab[];
    130   1.1   thorpej 
    131  1.39    rillig static inline const ttab_t *
    132  1.45    rillig type_properties(tspec_t t)
    133  1.45    rillig {
    134  1.39    rillig 	return ttab + t;
    135  1.39    rillig }
    136  1.39    rillig 
    137  1.39    rillig #define size_in_bits(t)		(type_properties(t)->tt_size_in_bits)
    138  1.39    rillig #define signed_type(t)		(type_properties(t)->tt_signed_counterpart)
    139  1.39    rillig #define unsigned_type(t)	(type_properties(t)->tt_unsigned_counterpart)
    140  1.39    rillig #define is_integer(t)		(type_properties(t)->tt_is_integer)
    141  1.39    rillig #define is_uinteger(t)		(type_properties(t)->tt_is_uinteger)
    142  1.39    rillig #define is_floating(t)		(type_properties(t)->tt_is_floating)
    143  1.39    rillig #define is_arithmetic(t)	(type_properties(t)->tt_is_arithmetic)
    144  1.39    rillig #define is_complex(t)		(type_properties(t)->tt_is_complex)
    145  1.39    rillig #define is_scalar(t)		(type_properties(t)->tt_is_scalar)
    146   1.1   thorpej 
    147  1.44    rillig #define has_operands(tn)	(modtab[(tn)->tn_op].m_has_operands)
    148  1.44    rillig 
    149   1.1   thorpej 
    150  1.46    rillig typedef enum {
    151  1.19    rillig 	NODECL,			/* not declared until now */
    152   1.1   thorpej 	DECL,			/* declared */
    153   1.1   thorpej 	TDEF,			/* tentative defined */
    154   1.1   thorpej 	DEF			/* defined */
    155   1.1   thorpej } def_t;
    156   1.1   thorpej 
    157  1.47    rillig #if IS_LINT1
    158  1.27    rillig typedef struct lint1_type type_t;
    159  1.27    rillig #else
    160  1.27    rillig typedef struct lint2_type type_t;
    161  1.27    rillig #endif
    162  1.39    rillig #endif
    163   1.9  christos 
    164  1.50    rillig static inline bool
    165  1.50    rillig ch_isalnum(char ch)
    166  1.50    rillig {
    167  1.50    rillig 	return isalnum((unsigned char)ch) != 0;
    168  1.50    rillig }
    169  1.50    rillig 
    170  1.50    rillig static inline bool
    171  1.50    rillig ch_isalpha(char ch)
    172  1.50    rillig {
    173  1.50    rillig 	return isalpha((unsigned char)ch) != 0;
    174  1.50    rillig }
    175  1.50    rillig 
    176  1.50    rillig static inline bool
    177  1.50    rillig ch_isdigit(char ch)
    178  1.50    rillig {
    179  1.50    rillig 	return isdigit((unsigned char)ch) != 0;
    180  1.50    rillig }
    181  1.50    rillig 
    182  1.50    rillig static inline bool
    183  1.50    rillig ch_islower(char ch)
    184  1.50    rillig {
    185  1.50    rillig 	return islower((unsigned char)ch) != 0;
    186  1.50    rillig }
    187  1.50    rillig 
    188  1.50    rillig static inline bool
    189  1.50    rillig ch_isprint(char ch)
    190  1.50    rillig {
    191  1.50    rillig 	return isprint((unsigned char)ch) != 0;
    192  1.50    rillig }
    193  1.50    rillig 
    194  1.50    rillig static inline bool
    195  1.50    rillig ch_isspace(char ch)
    196  1.50    rillig {
    197  1.50    rillig 	return isspace((unsigned char)ch) != 0;
    198  1.50    rillig }
    199  1.50    rillig 
    200  1.50    rillig static inline bool
    201  1.50    rillig ch_isupper(char ch)
    202  1.50    rillig {
    203  1.50    rillig 	return isupper((unsigned char)ch) != 0;
    204  1.50    rillig }
    205  1.50    rillig 
    206   1.1   thorpej #include "externs.h"
    207