Home | History | Annotate | Line # | Download | only in lint2
emit2.c revision 1.25
      1 /* $NetBSD: emit2.c,v 1.25 2021/09/04 18:49:33 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 <sys/cdefs.h>
     36 #if defined(__RCSID) && !defined(lint)
     37 __RCSID("$NetBSD: emit2.c,v 1.25 2021/09/04 18:49:33 rillig Exp $");
     38 #endif
     39 
     40 #include "lint2.h"
     41 
     42 static	void	outtype(type_t *);
     43 static	void	outdef(hte_t *, sym_t *);
     44 static	void	dumpname(hte_t *);
     45 static	void	outfiles(void);
     46 
     47 /*
     48  * Write type into the output buffer.
     49  */
     50 static void
     51 outtype(type_t *tp)
     52 {
     53 	char	t, s;
     54 	int	na;
     55 	tspec_t	ts;
     56 	type_t	**ap;
     57 
     58 #ifdef INT128_SIZE
     59 	static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDDVTTTPAF?XXX";
     60 	static const char ss[NTSPEC] = "???  su u u u u us l sue   ?s l";
     61 #else
     62 	static const char tt[NTSPEC] = "???BCCCSSIILLQQDDDVTTTPAF?XXX";
     63 	static const char ss[NTSPEC] = "???  su u u u us l sue   ?s l";
     64 #endif
     65 
     66 	while (tp != NULL) {
     67 		if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
     68 			ts = ENUM;
     69 		t = tt[ts];
     70 		s = ss[ts];
     71 		if (!ch_isupper(t))
     72 			errx(1, "internal error: outtype(%d)", ts);
     73 		if (ts == FUNC && tp->t_args != NULL && !tp->t_proto)
     74 			t = 'f';
     75 		if (tp->t_const)
     76 			outchar('c');
     77 		if (tp->t_volatile)
     78 			outchar('v');
     79 		if (s != ' ')
     80 			outchar(s);
     81 		outchar(t);
     82 		if (ts == ARRAY) {
     83 			outint(tp->t_dim);
     84 		} else if (ts == ENUM || ts == STRUCT || ts == UNION) {
     85 			if (tp->t_istag) {
     86 				outint(1);
     87 				outname(tp->t_tag->h_name);
     88 			} else if (tp->t_istynam) {
     89 				outint(2);
     90 				outname(tp->t_tynam->h_name);
     91 			} else if (tp->t_isuniqpos) {
     92 				outint(3);
     93 				outint(tp->t_uniqpos.p_line);
     94 				outchar('.');
     95 				outint(tp->t_uniqpos.p_file);
     96 				outchar('.');
     97 				outint(tp->t_uniqpos.p_uniq);
     98 			} else
     99 				errx(1, "internal error: outtype() 2");
    100 		} else if (ts == FUNC && tp->t_args != NULL) {
    101 			na = 0;
    102 			for (ap = tp->t_args; *ap != NULL; ap++)
    103 				na++;
    104 			if (tp->t_vararg)
    105 				na++;
    106 			outint(na);
    107 			for (ap = tp->t_args; *ap != NULL; ap++)
    108 				outtype(*ap);
    109 			if (tp->t_vararg)
    110 				outchar('E');
    111 		}
    112 		tp = tp->t_subt;
    113 	}
    114 }
    115 
    116 /*
    117  * Write a definition.
    118  */
    119 static void
    120 outdef(hte_t *hte, sym_t *sym)
    121 {
    122 
    123 	/* reset output buffer */
    124 	outclr();
    125 
    126 	/* line number in C source file */
    127 	outint(0);
    128 
    129 	/* this is a definition */
    130 	outchar('d');
    131 
    132 	/* index of file where symbol was defined and line number of def. */
    133 	outint(0);
    134 	outchar('.');
    135 	outint(0);
    136 
    137 	/* flags */
    138 	if (sym->s_check_only_first_args) {
    139 		outchar('v');
    140 		outint(sym->s_check_num_args);
    141 	}
    142 	if (sym->s_scanflike) {
    143 		outchar('S');
    144 		outint(sym->s_scanflike_arg);
    145 	}
    146 	if (sym->s_printflike) {
    147 		outchar('P');
    148 		outint(sym->s_printflike_arg);
    149 	}
    150 	/* definition or tentative definition */
    151 	outchar(sym->s_def == DEF ? 'd' : 't');
    152 	if (TP(sym->s_type)->t_tspec == FUNC) {
    153 		if (sym->s_function_has_return_value)
    154 			outchar('r');
    155 		if (sym->s_old_style_function)
    156 			outchar('o');
    157 	}
    158 	outchar('u');			/* used (no warning if not used) */
    159 
    160 	/* name */
    161 	outname(hte->h_name);
    162 
    163 	/* type */
    164 	outtype(TP(sym->s_type));
    165 }
    166 
    167 /*
    168  * Write the first definition of a name into the lint library.
    169  */
    170 static void
    171 dumpname(hte_t *hte)
    172 {
    173 	sym_t	*sym, *def;
    174 
    175 	/* static and undefined symbols are not written */
    176 	if (hte->h_static || !hte->h_def)
    177 		return;
    178 
    179 	/*
    180 	 * If there is a definition, write it. Otherwise write a tentative
    181 	 * definition. This is necessary because more than one tentative
    182 	 * definition is allowed (except with sflag).
    183 	 */
    184 	def = NULL;
    185 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
    186 		if (sym->s_def == DEF) {
    187 			def = sym;
    188 			break;
    189 		}
    190 		if (sym->s_def == TDEF && def == NULL)
    191 			def = sym;
    192 	}
    193 	if (def == NULL)
    194 		errx(1, "internal error: dumpname() %s", hte->h_name);
    195 
    196 	outdef(hte, def);
    197 }
    198 
    199 /*
    200  * Write a new lint library.
    201  */
    202 void
    203 outlib(const char *name)
    204 {
    205 	/* Open of output file and initialization of the output buffer */
    206 	outopen(name);
    207 
    208 	/* write name of lint library */
    209 	outsrc(name);
    210 
    211 	/* name of lint lib has index 0 */
    212 	outclr();
    213 	outint(0);
    214 	outchar('s');
    215 	outstrg(name);
    216 
    217 	/*
    218 	 * print the names of all files references by unnamed
    219 	 * struct/union/enum declarations.
    220 	 */
    221 	outfiles();
    222 
    223 	/* write all definitions with external linkage */
    224 	symtab_forall_sorted(dumpname);
    225 
    226 	/* close the output */
    227 	outclose();
    228 }
    229 
    230 /*
    231  * Write out the name of a file referenced by a type.
    232  */
    233 struct outflist {
    234 	short		ofl_num;
    235 	struct outflist *ofl_next;
    236 };
    237 static struct outflist *outflist;
    238 
    239 int
    240 addoutfile(short num)
    241 {
    242 	struct outflist *ofl, **pofl;
    243 	int i;
    244 
    245 	ofl = outflist;
    246 	pofl = &outflist;
    247 	i = 1;				/* library is 0 */
    248 
    249 	while (ofl != NULL) {
    250 		if (ofl->ofl_num == num)
    251 			break;
    252 
    253 		pofl = &ofl->ofl_next;
    254 		ofl = ofl->ofl_next;
    255 		i++;
    256 	}
    257 
    258 	if (ofl == NULL) {
    259 		ofl = *pofl = xmalloc(sizeof(**pofl));
    260 		ofl->ofl_num = num;
    261 		ofl->ofl_next = NULL;
    262 	}
    263 	return i;
    264 }
    265 
    266 static void
    267 outfiles(void)
    268 {
    269 	struct outflist *ofl;
    270 	int i;
    271 
    272 	for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) {
    273 		/* reset output buffer */
    274 		outclr();
    275 
    276 		outint(i);
    277 		outchar('s');
    278 		outstrg(fnames[ofl->ofl_num]);
    279 	}
    280 }
    281