Home | History | Annotate | Line # | Download | only in lint1
emit1.c revision 1.75
      1  1.75    rillig /* $NetBSD: emit1.c,v 1.75 2023/08/12 21:08:37 rillig Exp $ */
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.5       cgd  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      6   1.1       cgd  * All Rights Reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18  1.72    rillig  *	This product includes software developed by Jochen Pohl for
     19   1.1       cgd  *	The NetBSD Project.
     20   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     21   1.1       cgd  *    derived from this software without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.14       jmc #if HAVE_NBTOOL_CONFIG_H
     36  1.14       jmc #include "nbtool_config.h"
     37  1.14       jmc #endif
     38  1.14       jmc 
     39   1.7  christos #include <sys/cdefs.h>
     40  1.62    rillig #if defined(__RCSID)
     41  1.75    rillig __RCSID("$NetBSD: emit1.c,v 1.75 2023/08/12 21:08:37 rillig Exp $");
     42   1.1       cgd #endif
     43   1.1       cgd 
     44   1.1       cgd #include "lint1.h"
     45   1.1       cgd 
     46  1.10     lukem static	void	outtt(sym_t *, sym_t *);
     47  1.10     lukem static	void	outfstrg(strg_t *);
     48   1.1       cgd 
     49   1.1       cgd /*
     50  1.75    rillig  * Write type into the output file, encoded as follows:
     51  1.75    rillig  *	const			c
     52  1.75    rillig  *	volatile		v
     53  1.15      yamt  *	_Bool			B
     54  1.18  christos  *	_Complex float		s X
     55  1.21    rillig  *	_Complex double		X
     56  1.21    rillig  *	_Complex long double	l X
     57   1.1       cgd  *	char			C
     58   1.1       cgd  *	signed char		s C
     59   1.1       cgd  *	unsigned char		u C
     60   1.1       cgd  *	short			S
     61   1.1       cgd  *	unsigned short		u S
     62   1.1       cgd  *	int			I
     63   1.1       cgd  *	unsigned int		u I
     64   1.1       cgd  *	long			L
     65   1.1       cgd  *	unsigned long		u L
     66   1.1       cgd  *	long long		Q
     67   1.1       cgd  *	unsigned long long	u Q
     68   1.1       cgd  *	float			s D
     69   1.1       cgd  *	double			D
     70   1.1       cgd  *	long double		l D
     71   1.1       cgd  *	void			V
     72   1.1       cgd  *	*			P
     73   1.1       cgd  *	[n]			A n
     74   1.1       cgd  *	()			F
     75   1.1       cgd  *	(void)			F 0
     76  1.44    rillig  *	(n parameters)		F n arg1 arg2 ... argn
     77  1.65    rillig  *	(n parameters, ...)	F n arg1 arg2 ... argn E
     78   1.1       cgd  *	enum tag		e T tag_or_typename
     79   1.1       cgd  *	struct tag		s T tag_or_typename
     80   1.1       cgd  *	union tag		u T tag_or_typename
     81   1.1       cgd  *
     82  1.44    rillig  *	tag_or_typename		0 (obsolete)		no tag or type name
     83  1.44    rillig  *				1 n tag			tagged type
     84  1.65    rillig  *				2 n typename		only typedef name
     85  1.44    rillig  *				3 line.file.uniq	anonymous types
     86   1.1       cgd  */
     87   1.1       cgd void
     88  1.36    rillig outtype(const type_t *tp)
     89   1.1       cgd {
     90  1.53    rillig 	/* Available letters: ------GH--K-MNO--R--U-W-YZ */
     91  1.53    rillig #ifdef INT128_SIZE
     92  1.70    rillig 	static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXVTTTPAF";
     93  1.70    rillig 	static const char ss[NTSPEC] = "???  su u u u u us l?s l sue   ";
     94  1.53    rillig #else
     95  1.70    rillig 	static const char tt[NTSPEC] = "???BCCCSSIILLQQDDD?XXXVTTTPAF";
     96  1.70    rillig 	static const char ss[NTSPEC] = "???  su u u u us l?s l sue   ";
     97  1.53    rillig #endif
     98  1.67    rillig 	int na;
     99  1.67    rillig 	tspec_t ts;
    100   1.1       cgd 
    101   1.1       cgd 	while (tp != NULL) {
    102  1.42    rillig 		if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
    103   1.1       cgd 			ts = ENUM;
    104  1.58    rillig 		lint_assert(tt[ts] != '?' && ss[ts] != '?');
    105   1.1       cgd 		if (tp->t_const)
    106   1.1       cgd 			outchar('c');
    107   1.1       cgd 		if (tp->t_volatile)
    108   1.1       cgd 			outchar('v');
    109  1.58    rillig 		if (ss[ts] != ' ')
    110  1.58    rillig 			outchar(ss[ts]);
    111  1.58    rillig 		outchar(tt[ts]);
    112  1.58    rillig 
    113   1.1       cgd 		if (ts == ARRAY) {
    114   1.1       cgd 			outint(tp->t_dim);
    115   1.1       cgd 		} else if (ts == ENUM) {
    116  1.41    rillig 			outtt(tp->t_enum->en_tag, tp->t_enum->en_first_typedef);
    117  1.63    rillig 		} else if (is_struct_or_union(ts)) {
    118  1.66    rillig 			outtt(tp->t_sou->sou_tag, tp->t_sou->sou_first_typedef);
    119   1.1       cgd 		} else if (ts == FUNC && tp->t_proto) {
    120   1.1       cgd 			na = 0;
    121  1.73    rillig 			for (const sym_t *param = tp->t_params;
    122  1.73    rillig 			     param != NULL; param = param->s_next)
    123  1.45    rillig 				na++;
    124   1.1       cgd 			if (tp->t_vararg)
    125   1.1       cgd 				na++;
    126   1.1       cgd 			outint(na);
    127  1.73    rillig 			for (const sym_t *param = tp->t_params;
    128  1.73    rillig 			     param != NULL; param = param->s_next)
    129  1.73    rillig 				outtype(param->s_type);
    130   1.1       cgd 			if (tp->t_vararg)
    131   1.1       cgd 				outchar('E');
    132   1.1       cgd 		}
    133   1.1       cgd 		tp = tp->t_subt;
    134   1.1       cgd 	}
    135   1.1       cgd }
    136   1.1       cgd 
    137   1.1       cgd /*
    138   1.1       cgd  * write the name of a tag or typename
    139   1.1       cgd  *
    140  1.44    rillig  * if the tag is named, the name of the tag is written,
    141  1.44    rillig  * otherwise, if a typename exists which refers to this tag,
    142  1.44    rillig  * this typename is written
    143   1.1       cgd  */
    144   1.1       cgd static void
    145  1.10     lukem outtt(sym_t *tag, sym_t *tdef)
    146   1.1       cgd {
    147   1.5       cgd 
    148  1.44    rillig 	/* 0 is no longer used. */
    149  1.44    rillig 
    150   1.1       cgd 	if (tag->s_name != unnamed) {
    151   1.1       cgd 		outint(1);
    152   1.1       cgd 		outname(tag->s_name);
    153   1.1       cgd 	} else if (tdef != NULL) {
    154   1.1       cgd 		outint(2);
    155   1.1       cgd 		outname(tdef->s_name);
    156   1.1       cgd 	} else {
    157   1.5       cgd 		outint(3);
    158  1.29    rillig 		outint(tag->s_def_pos.p_line);
    159   1.5       cgd 		outchar('.');
    160  1.43    rillig 		outint(get_filename_id(tag->s_def_pos.p_file));
    161   1.5       cgd 		outchar('.');
    162  1.29    rillig 		outint(tag->s_def_pos.p_uniq);
    163   1.1       cgd 	}
    164   1.1       cgd }
    165   1.1       cgd 
    166   1.1       cgd /*
    167  1.23    rillig  * write information about a globally declared/defined symbol
    168   1.3       jpo  * with storage class extern
    169   1.1       cgd  *
    170  1.23    rillig  * information about function definitions are written in outfdef(),
    171   1.1       cgd  * not here
    172   1.1       cgd  */
    173   1.1       cgd void
    174  1.36    rillig outsym(const sym_t *sym, scl_t sc, def_t def)
    175   1.1       cgd {
    176  1.10     lukem 
    177   1.1       cgd 	/*
    178   1.1       cgd 	 * Static function declarations must also be written to the output
    179   1.1       cgd 	 * file. Compatibility of function declarations (for both static
    180   1.1       cgd 	 * and extern functions) must be checked in lint2. Lint1 can't do
    181  1.23    rillig 	 * this, especially not if functions are declared at block level
    182   1.1       cgd 	 * before their first declaration at level 0.
    183   1.1       cgd 	 */
    184   1.1       cgd 	if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
    185   1.1       cgd 		return;
    186  1.60    rillig 	if (ch_isdigit(sym->s_name[0]))	/* 00000000_tmp */
    187  1.60    rillig 		return;
    188   1.1       cgd 
    189   1.1       cgd 	outint(csrc_pos.p_line);
    190  1.68    rillig 	outchar('d');		/* declaration */
    191  1.43    rillig 	outint(get_filename_id(sym->s_def_pos.p_file));
    192   1.1       cgd 	outchar('.');
    193  1.29    rillig 	outint(sym->s_def_pos.p_line);
    194   1.1       cgd 
    195   1.1       cgd 	/* flags */
    196   1.1       cgd 
    197  1.48    rillig 	if (def == DEF)
    198  1.48    rillig 		outchar('d');	/* defined */
    199  1.48    rillig 	else if (def == TDEF)
    200  1.48    rillig 		outchar('t');	/* tentative defined */
    201  1.48    rillig 	else {
    202  1.48    rillig 		lint_assert(def == DECL);
    203  1.48    rillig 		outchar('e');	/* declared */
    204   1.1       cgd 	}
    205  1.48    rillig 
    206   1.3       jpo 	if (llibflg && def != DECL) {
    207   1.1       cgd 		/*
    208  1.48    rillig 		 * mark it as used so lint2 does not complain about
    209  1.48    rillig 		 * unused symbols in libraries
    210   1.1       cgd 		 */
    211   1.1       cgd 		outchar('u');
    212   1.1       cgd 	}
    213   1.1       cgd 
    214   1.1       cgd 	if (sc == STATIC)
    215   1.1       cgd 		outchar('s');
    216   1.1       cgd 
    217   1.1       cgd 	outname(sym->s_name);
    218   1.1       cgd 
    219  1.37    rillig 	if (sym->s_rename != NULL) {
    220   1.6       cgd 		outchar('r');
    221   1.6       cgd 		outname(sym->s_rename);
    222   1.6       cgd 	}
    223   1.6       cgd 
    224   1.1       cgd 	outtype(sym->s_type);
    225  1.74    rillig 	outchar('\n');
    226   1.1       cgd }
    227   1.1       cgd 
    228   1.1       cgd /*
    229  1.68    rillig  * Write information about a function definition. This is also done for static
    230  1.68    rillig  * functions, to later check if they are called with proper argument types.
    231   1.1       cgd  */
    232   1.1       cgd void
    233  1.37    rillig outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
    234  1.36    rillig 	const sym_t *args)
    235   1.1       cgd {
    236  1.36    rillig 	int narg;
    237  1.36    rillig 	const sym_t *arg;
    238   1.1       cgd 
    239   1.1       cgd 	if (posp->p_file == csrc_pos.p_file) {
    240   1.1       cgd 		outint(posp->p_line);
    241   1.1       cgd 	} else {
    242   1.1       cgd 		outint(csrc_pos.p_line);
    243   1.1       cgd 	}
    244  1.68    rillig 	outchar('d');		/* declaration */
    245  1.43    rillig 	outint(get_filename_id(posp->p_file));
    246   1.1       cgd 	outchar('.');
    247   1.1       cgd 	outint(posp->p_line);
    248   1.1       cgd 
    249   1.1       cgd 	/* both SCANFLIKE and PRINTFLIKE imply VARARGS */
    250  1.33    rillig 	if (printflike_argnum != -1) {
    251  1.33    rillig 		nvararg = printflike_argnum;
    252  1.33    rillig 	} else if (scanflike_argnum != -1) {
    253  1.33    rillig 		nvararg = scanflike_argnum;
    254   1.1       cgd 	}
    255   1.1       cgd 
    256   1.1       cgd 	if (nvararg != -1) {
    257   1.1       cgd 		outchar('v');
    258   1.1       cgd 		outint(nvararg);
    259   1.1       cgd 	}
    260  1.33    rillig 	if (scanflike_argnum != -1) {
    261   1.1       cgd 		outchar('S');
    262  1.33    rillig 		outint(scanflike_argnum);
    263   1.1       cgd 	}
    264  1.33    rillig 	if (printflike_argnum != -1) {
    265   1.1       cgd 		outchar('P');
    266  1.33    rillig 		outint(printflike_argnum);
    267   1.1       cgd 	}
    268  1.33    rillig 	nvararg = printflike_argnum = scanflike_argnum = -1;
    269   1.1       cgd 
    270   1.1       cgd 	outchar('d');
    271   1.1       cgd 
    272   1.1       cgd 	if (rval)
    273  1.48    rillig 		outchar('r');	/* has return value */
    274   1.1       cgd 
    275   1.1       cgd 	if (llibflg)
    276   1.1       cgd 		/*
    277   1.1       cgd 		 * mark it as used so lint2 does not complain about
    278   1.1       cgd 		 * unused symbols in libraries
    279   1.1       cgd 		 */
    280   1.1       cgd 		outchar('u');
    281   1.1       cgd 
    282   1.1       cgd 	if (osdef)
    283  1.64    rillig 		outchar('o');	/* old-style function definition */
    284   1.1       cgd 
    285  1.20  christos 	if (fsym->s_inline)
    286  1.20  christos 		outchar('i');
    287  1.20  christos 
    288   1.1       cgd 	if (fsym->s_scl == STATIC)
    289   1.1       cgd 		outchar('s');
    290   1.1       cgd 
    291   1.1       cgd 	/* name of function */
    292   1.1       cgd 	outname(fsym->s_name);
    293   1.6       cgd 
    294  1.37    rillig 	if (fsym->s_rename != NULL) {
    295   1.6       cgd 		outchar('r');
    296   1.6       cgd 		outname(fsym->s_rename);
    297   1.6       cgd 	}
    298   1.1       cgd 
    299  1.73    rillig 	/* parameter types and return value */
    300   1.1       cgd 	if (osdef) {
    301   1.1       cgd 		narg = 0;
    302  1.26    rillig 		for (arg = args; arg != NULL; arg = arg->s_next)
    303   1.1       cgd 			narg++;
    304   1.1       cgd 		outchar('f');
    305   1.1       cgd 		outint(narg);
    306  1.26    rillig 		for (arg = args; arg != NULL; arg = arg->s_next)
    307   1.1       cgd 			outtype(arg->s_type);
    308   1.4       jpo 		outtype(fsym->s_type->t_subt);
    309   1.1       cgd 	} else {
    310   1.4       jpo 		outtype(fsym->s_type);
    311   1.1       cgd 	}
    312  1.74    rillig 	outchar('\n');
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.1       cgd /*
    316   1.1       cgd  * write out all information necessary for lint2 to check function
    317   1.1       cgd  * calls
    318   1.1       cgd  *
    319  1.59    rillig  * retval_used is set if the return value is used (assigned to a variable)
    320  1.59    rillig  * retval_discarded is set if the return value is neither used nor ignored
    321  1.59    rillig  * (that is, cast to void)
    322   1.1       cgd  */
    323   1.1       cgd void
    324  1.59    rillig outcall(const tnode_t *tn, bool retval_used, bool retval_discarded)
    325   1.1       cgd {
    326  1.67    rillig 	tnode_t *args, *arg;
    327  1.67    rillig 	int narg, n, i;
    328  1.67    rillig 	tspec_t t;
    329   1.1       cgd 
    330   1.1       cgd 	outint(csrc_pos.p_line);
    331  1.68    rillig 	outchar('c');		/* function call */
    332  1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    333   1.1       cgd 	outchar('.');
    334   1.1       cgd 	outint(curr_pos.p_line);
    335   1.1       cgd 
    336   1.1       cgd 	/*
    337   1.1       cgd 	 * flags; 'u' and 'i' must be last to make sure a letter
    338   1.1       cgd 	 * is between the numeric argument of a flag and the name of
    339   1.1       cgd 	 * the function
    340   1.1       cgd 	 */
    341   1.1       cgd 	narg = 0;
    342   1.1       cgd 	args = tn->tn_right;
    343   1.1       cgd 	for (arg = args; arg != NULL; arg = arg->tn_right)
    344   1.1       cgd 		narg++;
    345  1.25    rillig 	/* information about arguments */
    346   1.1       cgd 	for (n = 1; n <= narg; n++) {
    347   1.1       cgd 		/* the last argument is the top one in the tree */
    348  1.10     lukem 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    349  1.10     lukem 			continue;
    350   1.1       cgd 		arg = arg->tn_left;
    351   1.1       cgd 		if (arg->tn_op == CON) {
    352  1.35    rillig 			if (is_integer(t = arg->tn_type->t_tspec)) {
    353   1.1       cgd 				/*
    354   1.1       cgd 				 * XXX it would probably be better to
    355  1.16     perry 				 * explicitly test the sign
    356   1.1       cgd 				 */
    357  1.71    rillig 				int64_t si = arg->tn_val.u.integer;
    358  1.71    rillig 				if (si == 0) {
    359   1.1       cgd 					/* zero constant */
    360   1.1       cgd 					outchar('z');
    361  1.71    rillig 				} else if (!msb(si, t)) {
    362  1.49    rillig 					/* positive if cast to signed */
    363   1.1       cgd 					outchar('p');
    364   1.1       cgd 				} else {
    365  1.49    rillig 					/* negative if cast to signed */
    366   1.1       cgd 					outchar('n');
    367   1.1       cgd 				}
    368   1.1       cgd 				outint(n);
    369   1.1       cgd 			}
    370  1.39    rillig 		} else if (arg->tn_op == ADDR &&
    371   1.1       cgd 			   arg->tn_left->tn_op == STRING &&
    372  1.61    rillig 			   arg->tn_left->tn_string->st_char) {
    373   1.1       cgd 			/* constant string, write all format specifiers */
    374   1.1       cgd 			outchar('s');
    375   1.1       cgd 			outint(n);
    376  1.28    rillig 			outfstrg(arg->tn_left->tn_string);
    377   1.1       cgd 		}
    378   1.1       cgd 
    379   1.1       cgd 	}
    380  1.75    rillig 	outchar((char)(retval_discarded ? 'd' : retval_used ? 'u' : 'i'));
    381   1.1       cgd 
    382   1.1       cgd 	/* name of the called function */
    383   1.1       cgd 	outname(tn->tn_left->tn_left->tn_sym->s_name);
    384   1.1       cgd 
    385   1.1       cgd 	/* types of arguments */
    386   1.1       cgd 	outchar('f');
    387   1.1       cgd 	outint(narg);
    388   1.1       cgd 	for (n = 1; n <= narg; n++) {
    389   1.1       cgd 		/* the last argument is the top one in the tree */
    390  1.10     lukem 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    391  1.10     lukem 			continue;
    392   1.1       cgd 		outtype(arg->tn_left->tn_type);
    393   1.1       cgd 	}
    394   1.1       cgd 	/* expected type of return value */
    395   1.1       cgd 	outtype(tn->tn_type);
    396  1.74    rillig 	outchar('\n');
    397   1.1       cgd }
    398   1.1       cgd 
    399  1.56    rillig /* write a character to the output buffer, quoted if necessary */
    400  1.56    rillig static void
    401  1.56    rillig outqchar(char c)
    402  1.56    rillig {
    403  1.56    rillig 
    404  1.56    rillig 	if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
    405  1.56    rillig 		outchar(c);
    406  1.56    rillig 		return;
    407  1.56    rillig 	}
    408  1.56    rillig 
    409  1.56    rillig 	outchar('\\');
    410  1.56    rillig 	switch (c) {
    411  1.56    rillig 	case '\\':
    412  1.56    rillig 		outchar('\\');
    413  1.56    rillig 		break;
    414  1.56    rillig 	case '"':
    415  1.56    rillig 		outchar('"');
    416  1.56    rillig 		break;
    417  1.56    rillig 	case '\'':
    418  1.56    rillig 		outchar('\'');
    419  1.56    rillig 		break;
    420  1.56    rillig 	case '\b':
    421  1.56    rillig 		outchar('b');
    422  1.56    rillig 		break;
    423  1.56    rillig 	case '\t':
    424  1.56    rillig 		outchar('t');
    425  1.56    rillig 		break;
    426  1.56    rillig 	case '\n':
    427  1.56    rillig 		outchar('n');
    428  1.56    rillig 		break;
    429  1.56    rillig 	case '\f':
    430  1.56    rillig 		outchar('f');
    431  1.56    rillig 		break;
    432  1.56    rillig 	case '\r':
    433  1.56    rillig 		outchar('r');
    434  1.56    rillig 		break;
    435  1.56    rillig 	case '\v':
    436  1.56    rillig 		outchar('v');
    437  1.56    rillig 		break;
    438  1.56    rillig 	case '\a':
    439  1.56    rillig 		outchar('a');
    440  1.56    rillig 		break;
    441  1.56    rillig 	default:
    442  1.56    rillig 		outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
    443  1.56    rillig 		outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
    444  1.56    rillig 		outchar((char)((c & 07) + '0'));
    445  1.56    rillig 		break;
    446  1.56    rillig 	}
    447  1.56    rillig }
    448  1.56    rillig 
    449   1.1       cgd /*
    450   1.1       cgd  * extracts potential format specifiers for printf() and scanf() and
    451  1.34    rillig  * writes them, enclosed in "" and quoted if necessary, to the output buffer
    452   1.1       cgd  */
    453   1.1       cgd static void
    454  1.10     lukem outfstrg(strg_t *strg)
    455   1.1       cgd {
    456  1.57    rillig 	char c, oc;
    457  1.67    rillig 	bool first;
    458  1.57    rillig 	const char *cp;
    459   1.1       cgd 
    460  1.61    rillig 	lint_assert(strg->st_char);
    461  1.61    rillig 	cp = strg->st_mem;
    462   1.1       cgd 
    463   1.1       cgd 	outchar('"');
    464   1.1       cgd 
    465   1.1       cgd 	c = *cp++;
    466   1.1       cgd 
    467   1.1       cgd 	while (c != '\0') {
    468   1.1       cgd 
    469   1.1       cgd 		if (c != '%') {
    470   1.1       cgd 			c = *cp++;
    471   1.1       cgd 			continue;
    472   1.1       cgd 		}
    473   1.1       cgd 
    474  1.57    rillig 		outchar('%');
    475   1.1       cgd 		c = *cp++;
    476   1.1       cgd 
    477   1.1       cgd 		/* flags for printf and scanf and *-fieldwidth for printf */
    478  1.57    rillig 		while (c == '-' || c == '+' || c == ' ' ||
    479  1.57    rillig 		       c == '#' || c == '0' || c == '*') {
    480  1.57    rillig 			outchar(c);
    481   1.1       cgd 			c = *cp++;
    482   1.1       cgd 		}
    483   1.1       cgd 
    484   1.1       cgd 		/* numeric field width */
    485  1.57    rillig 		while (ch_isdigit(c)) {
    486  1.57    rillig 			outchar(c);
    487   1.1       cgd 			c = *cp++;
    488   1.1       cgd 		}
    489   1.1       cgd 
    490   1.1       cgd 		/* precision for printf */
    491   1.1       cgd 		if (c == '.') {
    492  1.57    rillig 			outchar(c);
    493  1.57    rillig 			c = *cp++;
    494  1.57    rillig 			if (c == '*') {
    495  1.57    rillig 				outchar(c);
    496   1.1       cgd 				c = *cp++;
    497   1.1       cgd 			} else {
    498  1.57    rillig 				while (ch_isdigit(c)) {
    499  1.57    rillig 					outchar(c);
    500   1.1       cgd 					c = *cp++;
    501   1.1       cgd 				}
    502   1.1       cgd 			}
    503   1.1       cgd 		}
    504   1.1       cgd 
    505  1.57    rillig 		/* h, l, L and q flags for printf and scanf */
    506   1.1       cgd 		if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
    507  1.57    rillig 			outchar(c);
    508   1.1       cgd 			c = *cp++;
    509   1.1       cgd 		}
    510   1.1       cgd 
    511   1.1       cgd 		/*
    512  1.57    rillig 		 * The last character. It is always written, so we can detect
    513   1.1       cgd 		 * invalid format specifiers.
    514   1.1       cgd 		 */
    515   1.1       cgd 		if (c != '\0') {
    516   1.1       cgd 			outqchar(c);
    517   1.1       cgd 			oc = c;
    518   1.1       cgd 			c = *cp++;
    519   1.1       cgd 			/*
    520   1.1       cgd 			 * handle [ for scanf. [-] means that a minus sign
    521   1.1       cgd 			 * was found at an undefined position.
    522   1.1       cgd 			 */
    523   1.1       cgd 			if (oc == '[') {
    524   1.1       cgd 				if (c == '^')
    525   1.1       cgd 					c = *cp++;
    526   1.1       cgd 				if (c == ']')
    527   1.1       cgd 					c = *cp++;
    528  1.37    rillig 				first = true;
    529   1.1       cgd 				while (c != '\0' && c != ']') {
    530   1.1       cgd 					if (c == '-') {
    531   1.1       cgd 						if (!first && *cp != ']')
    532  1.57    rillig 							outchar(c);
    533   1.1       cgd 					}
    534  1.37    rillig 					first = false;
    535   1.1       cgd 					c = *cp++;
    536   1.1       cgd 				}
    537   1.1       cgd 				if (c == ']') {
    538  1.57    rillig 					outchar(c);
    539   1.1       cgd 					c = *cp++;
    540   1.1       cgd 				}
    541   1.1       cgd 			}
    542   1.1       cgd 		}
    543   1.1       cgd 
    544   1.1       cgd 	}
    545   1.1       cgd 
    546   1.1       cgd 	outchar('"');
    547   1.1       cgd }
    548   1.1       cgd 
    549   1.1       cgd /*
    550   1.1       cgd  * writes a record if sym was used
    551   1.1       cgd  */
    552   1.1       cgd void
    553  1.36    rillig outusg(const sym_t *sym)
    554   1.1       cgd {
    555  1.68    rillig 	if (ch_isdigit(sym->s_name[0]))	/* 00000000_tmp, from mktempsym */
    556  1.60    rillig 		return;
    557  1.60    rillig 
    558   1.1       cgd 	outint(csrc_pos.p_line);
    559  1.68    rillig 	outchar('u');		/* used */
    560  1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    561   1.1       cgd 	outchar('.');
    562   1.1       cgd 	outint(curr_pos.p_line);
    563  1.75    rillig 	outchar('x');		/* separate the two numbers */
    564   1.1       cgd 	outname(sym->s_name);
    565  1.74    rillig 	outchar('\n');
    566   1.1       cgd }
    567