lint2.h revision 1.21
1/* $NetBSD: lint2.h,v 1.21 2021/12/22 14:49:11 rillig Exp $ */
2
3/*
4 * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *      This product includes software developed by Jochen Pohl for
19 *	The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include "lint.h"
36
37/*
38 * Types are described by structures of type type_t.
39 */
40struct lint2_type {
41	tspec_t	t_tspec;	/* type specifier */
42	bool	t_const:1;	/* constant */
43	bool	t_volatile:1;	/* volatile */
44	bool	t_vararg:1;	/* function has variable number of arguments */
45	bool	t_is_enum:1;
46	bool	t_proto:1;	/* this is a prototype */
47	bool	t_istag:1;	/* tag with _t_tag valid */
48	bool	t_istynam:1;	/* tag with _t_tynam valid */
49	bool	t_isuniqpos:1;	/* tag with _t_uniqpos valid */
50	union {
51		int	_t_dim;		/* if the type is an ARRAY than this
52					   is the dimension of the array. */
53		struct	hte *_t_tag;	/* hash table entry of tag if
54					   t_is_enum, STRUCT or UNION */
55		struct	hte *_t_tynam;	/* hash table entry of typename if
56					   t_is_enum, STRUCT or UNION */
57		struct {
58			int p_line;
59			short p_file;
60			int p_uniq;
61		} _t_uniqpos;		/* unique position, for untagged
62					   untyped STRUCTs, UNIONS, and ENUMs,
63					   if t_isuniqpos */
64		struct	lint2_type **_t_args; /* list of argument types if
65					   this is a prototype */
66	} t_u;
67	struct	lint2_type *t_subt;	/* element type (if ARRAY),
68					   return type (if FUNC),
69					   target type (if PTR) */
70};
71
72#define	t_dim		t_u._t_dim
73#define	t_tag		t_u._t_tag
74#define	t_tynam		t_u._t_tynam
75#define	t_uniqpos	t_u._t_uniqpos
76#define	t_args		t_u._t_args
77
78/*
79 * argument information
80 *
81 * Such a structure is created for each argument of a function call
82 * which is an integer constant or a constant string.
83 */
84typedef	struct arginf {
85	int	a_num;		/* # of argument (1..) */
86	bool	a_zero:1;	/* argument is 0 */
87	bool	a_pcon:1;	/* msb of argument is not set */
88	bool	a_ncon:1;	/* msb of argument is set */
89	bool	a_fmt:1;	/* a_fstrg points to format string */
90	char	*a_fstrg;	/* format string */
91	struct	arginf *a_next;	/* information for next const. argument */
92} arginf_t;
93
94/*
95 * Keeps information about position in source file.
96 */
97typedef	struct {
98	unsigned short p_src;	/* index of name of translation unit
99				   (the name which was specified at the
100				   command line) */
101	unsigned short p_line;	/* line number in p_src */
102	unsigned short p_isrc;	/* index of (included) file */
103	unsigned short p_iline;	/* line number in p_iline */
104} pos_t;
105
106/* Used for definitions and declarations. */
107typedef	struct sym {
108	struct {
109		pos_t	s_pos;		/* pos of def./decl. */
110#if !defined(lint) && !defined(DEBUG)
111		unsigned char s_def;	/* DECL, TDEF or DEF */
112#else
113		def_t	s_def;
114#endif
115		bool	s_function_has_return_value:1;
116		bool	s_inline:1;
117		bool	s_old_style_function:1;
118		bool	s_static:1;
119		bool	s_check_only_first_args:1;
120		bool	s_printflike:1;
121		bool	s_scanflike:1;
122		unsigned short s_type;
123		/* XXX: gap of 4 bytes on LP64 platforms */
124		struct	sym *s_next;	/* next symbol with same name */
125	} s_s;
126	/*
127	 * To save memory, the remaining members are only allocated if one of
128	 * s_check_only_first_args, s_printflike and s_scanflike is set.
129	 */
130	short	s_check_num_args;	/* if s_check_only_first_args */
131	short	s_printflike_arg;	/* if s_printflike */
132	short	s_scanflike_arg;	/* if s_scanflike */
133} sym_t;
134
135#define s_pos		s_s.s_pos
136#define s_function_has_return_value s_s.s_function_has_return_value
137#define s_old_style_function s_s.s_old_style_function
138#define s_inline	s_s.s_inline
139#define s_static	s_s.s_static
140#define s_def		s_s.s_def
141#define s_check_only_first_args	s_s.s_check_only_first_args
142#define s_printflike	s_s.s_printflike
143#define s_scanflike	s_s.s_scanflike
144#define s_type		s_s.s_type
145#define s_next		s_s.s_next
146
147/*
148 * Used to store information about function calls.
149 */
150typedef	struct fcall {
151	pos_t	f_pos;		/* position of call */
152	bool	f_rused:1;	/* return value used */
153	bool	f_rdisc:1;	/* return value discarded (casted to void) */
154	unsigned short f_type;	/* types of expected return value and args */
155	arginf_t *f_args;	/* information about constant arguments */
156	struct	fcall *f_next;	/* next call of same function */
157} fcall_t;
158
159/*
160 * Used to store information about usage of symbols other
161 * than for function calls.
162 */
163typedef	struct usym {
164	pos_t	u_pos;		/* position */
165	struct	usym *u_next;	/* next usage */
166} usym_t;
167
168/*
169 * hash table entry
170 */
171typedef	struct hte {
172	const	char *h_name;	/* name */
173	bool	h_used:1;	/* symbol is used */
174	bool	h_def:1;	/* symbol is defined */
175	bool	h_static:1;	/* static symbol */
176	sym_t	*h_syms;	/* declarations and definitions */
177	sym_t	**h_lsym;	/* points to s_next of last decl./def. */
178	fcall_t	*h_calls;	/* function calls */
179	fcall_t	**h_lcall;	/* points to f_next of last call */
180	usym_t	*h_usyms;	/* usage info */
181	usym_t	**h_lusym;	/* points to u_next of last usage info */
182	struct	hte *h_link;	/* next hte with same hash function */
183	struct  hte *h_hte;	/* pointer to other htes (for renames) */
184} hte_t;
185
186#include "externs2.h"
187
188/* maps type indices into pointers to type structs */
189INLINE_FUNC type_t *
190TP(unsigned short type_id) {
191	/* force sequence point for newly parsed type_id */
192	return tlst[type_id];
193}
194