Home | History | Annotate | Line # | Download | only in lint1
emit1.c revision 1.95.2.1
      1  1.95.2.1  perseant /* $NetBSD: emit1.c,v 1.95.2.1 2025/08/02 05:58:45 perseant 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.95.2.1  perseant __RCSID("$NetBSD: emit1.c,v 1.95.2.1 2025/08/02 05:58:45 perseant Exp $");
     42       1.1       cgd #endif
     43       1.1       cgd 
     44      1.85    rillig #include <stdlib.h>
     45      1.85    rillig 
     46       1.1       cgd #include "lint1.h"
     47       1.1       cgd 
     48      1.81    rillig static void outtt(sym_t *, sym_t *);
     49      1.83    rillig static void outfstrg(const char *);
     50       1.1       cgd 
     51       1.1       cgd /*
     52      1.75    rillig  * Write type into the output file, encoded as follows:
     53      1.75    rillig  *	const			c
     54      1.75    rillig  *	volatile		v
     55      1.15      yamt  *	_Bool			B
     56      1.18  christos  *	_Complex float		s X
     57      1.21    rillig  *	_Complex double		X
     58      1.21    rillig  *	_Complex long double	l X
     59       1.1       cgd  *	char			C
     60       1.1       cgd  *	signed char		s C
     61       1.1       cgd  *	unsigned char		u C
     62       1.1       cgd  *	short			S
     63       1.1       cgd  *	unsigned short		u S
     64       1.1       cgd  *	int			I
     65       1.1       cgd  *	unsigned int		u I
     66       1.1       cgd  *	long			L
     67       1.1       cgd  *	unsigned long		u L
     68       1.1       cgd  *	long long		Q
     69       1.1       cgd  *	unsigned long long	u Q
     70       1.1       cgd  *	float			s D
     71       1.1       cgd  *	double			D
     72       1.1       cgd  *	long double		l D
     73       1.1       cgd  *	void			V
     74       1.1       cgd  *	*			P
     75       1.1       cgd  *	[n]			A n
     76       1.1       cgd  *	()			F
     77       1.1       cgd  *	(void)			F 0
     78      1.44    rillig  *	(n parameters)		F n arg1 arg2 ... argn
     79      1.65    rillig  *	(n parameters, ...)	F n arg1 arg2 ... argn E
     80       1.1       cgd  *	enum tag		e T tag_or_typename
     81       1.1       cgd  *	struct tag		s T tag_or_typename
     82       1.1       cgd  *	union tag		u T tag_or_typename
     83       1.1       cgd  *
     84      1.44    rillig  *	tag_or_typename		0 (obsolete)		no tag or type name
     85      1.44    rillig  *				1 n tag			tagged type
     86      1.65    rillig  *				2 n typename		only typedef name
     87      1.44    rillig  *				3 line.file.uniq	anonymous types
     88       1.1       cgd  */
     89       1.1       cgd void
     90      1.36    rillig outtype(const type_t *tp)
     91       1.1       cgd {
     92      1.53    rillig 	/* Available letters: ------GH--K-MNO--R--U-W-YZ */
     93      1.53    rillig #ifdef INT128_SIZE
     94  1.95.2.1  perseant 	static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXV?TTTPAF";
     95  1.95.2.1  perseant 	static const char ss[NTSPEC] = "???  su u u u u us l?s l ?sue   ";
     96      1.53    rillig #else
     97  1.95.2.1  perseant 	static const char tt[NTSPEC] = "???BCCCSSIILLQQDDD?XXXV?TTTPAF";
     98  1.95.2.1  perseant 	static const char ss[NTSPEC] = "???  su u u u us l?s l ?sue   ";
     99      1.53    rillig #endif
    100      1.67    rillig 	int na;
    101      1.67    rillig 	tspec_t ts;
    102       1.1       cgd 
    103       1.1       cgd 	while (tp != NULL) {
    104      1.42    rillig 		if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
    105       1.1       cgd 			ts = ENUM;
    106      1.58    rillig 		lint_assert(tt[ts] != '?' && ss[ts] != '?');
    107       1.1       cgd 		if (tp->t_const)
    108       1.1       cgd 			outchar('c');
    109       1.1       cgd 		if (tp->t_volatile)
    110       1.1       cgd 			outchar('v');
    111      1.58    rillig 		if (ss[ts] != ' ')
    112      1.58    rillig 			outchar(ss[ts]);
    113      1.58    rillig 		outchar(tt[ts]);
    114      1.58    rillig 
    115       1.1       cgd 		if (ts == ARRAY) {
    116      1.91    rillig 			outint(tp->u.dimension);
    117       1.1       cgd 		} else if (ts == ENUM) {
    118      1.91    rillig 			outtt(tp->u.enumer->en_tag,
    119      1.91    rillig 			    tp->u.enumer->en_first_typedef);
    120      1.63    rillig 		} else if (is_struct_or_union(ts)) {
    121      1.91    rillig 			outtt(tp->u.sou->sou_tag,
    122      1.91    rillig 			    tp->u.sou->sou_first_typedef);
    123       1.1       cgd 		} else if (ts == FUNC && tp->t_proto) {
    124       1.1       cgd 			na = 0;
    125      1.91    rillig 			for (const sym_t *param = tp->u.params;
    126      1.79    rillig 			    param != NULL; param = param->s_next)
    127      1.45    rillig 				na++;
    128       1.1       cgd 			if (tp->t_vararg)
    129       1.1       cgd 				na++;
    130       1.1       cgd 			outint(na);
    131      1.91    rillig 			for (const sym_t *param = tp->u.params;
    132      1.79    rillig 			    param != NULL; param = param->s_next)
    133      1.73    rillig 				outtype(param->s_type);
    134       1.1       cgd 			if (tp->t_vararg)
    135       1.1       cgd 				outchar('E');
    136       1.1       cgd 		}
    137       1.1       cgd 		tp = tp->t_subt;
    138       1.1       cgd 	}
    139       1.1       cgd }
    140       1.1       cgd 
    141       1.1       cgd /*
    142       1.1       cgd  * write the name of a tag or typename
    143       1.1       cgd  *
    144      1.44    rillig  * if the tag is named, the name of the tag is written,
    145      1.44    rillig  * otherwise, if a typename exists which refers to this tag,
    146      1.44    rillig  * this typename is written
    147       1.1       cgd  */
    148       1.1       cgd static void
    149      1.10     lukem outtt(sym_t *tag, sym_t *tdef)
    150       1.1       cgd {
    151       1.5       cgd 
    152      1.44    rillig 	/* 0 is no longer used. */
    153      1.44    rillig 
    154       1.1       cgd 	if (tag->s_name != unnamed) {
    155       1.1       cgd 		outint(1);
    156       1.1       cgd 		outname(tag->s_name);
    157       1.1       cgd 	} else if (tdef != NULL) {
    158       1.1       cgd 		outint(2);
    159       1.1       cgd 		outname(tdef->s_name);
    160       1.1       cgd 	} else {
    161       1.5       cgd 		outint(3);
    162      1.29    rillig 		outint(tag->s_def_pos.p_line);
    163       1.5       cgd 		outchar('.');
    164      1.43    rillig 		outint(get_filename_id(tag->s_def_pos.p_file));
    165       1.5       cgd 		outchar('.');
    166      1.29    rillig 		outint(tag->s_def_pos.p_uniq);
    167       1.1       cgd 	}
    168       1.1       cgd }
    169       1.1       cgd 
    170       1.1       cgd /*
    171      1.23    rillig  * write information about a globally declared/defined symbol
    172       1.3       jpo  * with storage class extern
    173       1.1       cgd  *
    174      1.23    rillig  * information about function definitions are written in outfdef(),
    175       1.1       cgd  * not here
    176       1.1       cgd  */
    177       1.1       cgd void
    178      1.36    rillig outsym(const sym_t *sym, scl_t sc, def_t def)
    179       1.1       cgd {
    180      1.10     lukem 
    181       1.1       cgd 	/*
    182       1.1       cgd 	 * Static function declarations must also be written to the output
    183      1.80    rillig 	 * file. Compatibility of function declarations (for both static and
    184      1.80    rillig 	 * extern functions) must be checked in lint2. Lint1 can't do this,
    185      1.80    rillig 	 * especially not if functions are declared at block level before their
    186      1.80    rillig 	 * first declaration at level 0.
    187       1.1       cgd 	 */
    188       1.1       cgd 	if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
    189       1.1       cgd 		return;
    190      1.95    rillig 	if (ch_isdigit(sym->s_name[0]))	/* see mktempsym */
    191      1.60    rillig 		return;
    192       1.1       cgd 
    193       1.1       cgd 	outint(csrc_pos.p_line);
    194      1.68    rillig 	outchar('d');		/* declaration */
    195      1.43    rillig 	outint(get_filename_id(sym->s_def_pos.p_file));
    196       1.1       cgd 	outchar('.');
    197      1.29    rillig 	outint(sym->s_def_pos.p_line);
    198       1.1       cgd 
    199       1.1       cgd 	/* flags */
    200       1.1       cgd 
    201      1.48    rillig 	if (def == DEF)
    202      1.48    rillig 		outchar('d');	/* defined */
    203      1.48    rillig 	else if (def == TDEF)
    204      1.48    rillig 		outchar('t');	/* tentative defined */
    205      1.48    rillig 	else {
    206      1.48    rillig 		lint_assert(def == DECL);
    207      1.48    rillig 		outchar('e');	/* declared */
    208       1.1       cgd 	}
    209      1.48    rillig 
    210       1.3       jpo 	if (llibflg && def != DECL) {
    211       1.1       cgd 		/*
    212      1.80    rillig 		 * mark it as used so lint2 does not complain about unused
    213      1.80    rillig 		 * symbols in libraries
    214       1.1       cgd 		 */
    215       1.1       cgd 		outchar('u');
    216       1.1       cgd 	}
    217       1.1       cgd 
    218       1.1       cgd 	if (sc == STATIC)
    219       1.1       cgd 		outchar('s');
    220       1.1       cgd 
    221       1.1       cgd 	outname(sym->s_name);
    222       1.1       cgd 
    223      1.37    rillig 	if (sym->s_rename != NULL) {
    224       1.6       cgd 		outchar('r');
    225       1.6       cgd 		outname(sym->s_rename);
    226       1.6       cgd 	}
    227       1.6       cgd 
    228       1.1       cgd 	outtype(sym->s_type);
    229      1.74    rillig 	outchar('\n');
    230       1.1       cgd }
    231       1.1       cgd 
    232       1.1       cgd /*
    233      1.68    rillig  * Write information about a function definition. This is also done for static
    234      1.68    rillig  * functions, to later check if they are called with proper argument types.
    235       1.1       cgd  */
    236       1.1       cgd void
    237      1.37    rillig outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
    238      1.36    rillig 	const sym_t *args)
    239       1.1       cgd {
    240      1.36    rillig 	int narg;
    241       1.1       cgd 
    242       1.1       cgd 	if (posp->p_file == csrc_pos.p_file) {
    243       1.1       cgd 		outint(posp->p_line);
    244       1.1       cgd 	} else {
    245       1.1       cgd 		outint(csrc_pos.p_line);
    246       1.1       cgd 	}
    247      1.68    rillig 	outchar('d');		/* declaration */
    248      1.43    rillig 	outint(get_filename_id(posp->p_file));
    249       1.1       cgd 	outchar('.');
    250       1.1       cgd 	outint(posp->p_line);
    251       1.1       cgd 
    252       1.1       cgd 	/* both SCANFLIKE and PRINTFLIKE imply VARARGS */
    253      1.33    rillig 	if (printflike_argnum != -1) {
    254      1.33    rillig 		nvararg = printflike_argnum;
    255      1.33    rillig 	} else if (scanflike_argnum != -1) {
    256      1.33    rillig 		nvararg = scanflike_argnum;
    257       1.1       cgd 	}
    258       1.1       cgd 
    259       1.1       cgd 	if (nvararg != -1) {
    260       1.1       cgd 		outchar('v');
    261       1.1       cgd 		outint(nvararg);
    262       1.1       cgd 	}
    263      1.33    rillig 	if (scanflike_argnum != -1) {
    264       1.1       cgd 		outchar('S');
    265      1.33    rillig 		outint(scanflike_argnum);
    266       1.1       cgd 	}
    267      1.33    rillig 	if (printflike_argnum != -1) {
    268       1.1       cgd 		outchar('P');
    269      1.33    rillig 		outint(printflike_argnum);
    270       1.1       cgd 	}
    271      1.33    rillig 	nvararg = printflike_argnum = scanflike_argnum = -1;
    272       1.1       cgd 
    273       1.1       cgd 	outchar('d');
    274       1.1       cgd 
    275       1.1       cgd 	if (rval)
    276      1.48    rillig 		outchar('r');	/* has return value */
    277       1.1       cgd 
    278       1.1       cgd 	if (llibflg)
    279       1.1       cgd 		/*
    280      1.80    rillig 		 * mark it as used so lint2 does not complain about unused
    281      1.80    rillig 		 * symbols in libraries
    282       1.1       cgd 		 */
    283       1.1       cgd 		outchar('u');
    284       1.1       cgd 
    285       1.1       cgd 	if (osdef)
    286      1.64    rillig 		outchar('o');	/* old-style function definition */
    287       1.1       cgd 
    288      1.20  christos 	if (fsym->s_inline)
    289      1.20  christos 		outchar('i');
    290      1.20  christos 
    291       1.1       cgd 	if (fsym->s_scl == STATIC)
    292       1.1       cgd 		outchar('s');
    293       1.1       cgd 
    294       1.1       cgd 	outname(fsym->s_name);
    295       1.6       cgd 
    296      1.37    rillig 	if (fsym->s_rename != NULL) {
    297       1.6       cgd 		outchar('r');
    298       1.6       cgd 		outname(fsym->s_rename);
    299       1.6       cgd 	}
    300       1.1       cgd 
    301      1.73    rillig 	/* parameter types and return value */
    302       1.1       cgd 	if (osdef) {
    303       1.1       cgd 		narg = 0;
    304      1.84    rillig 		for (const sym_t *arg = args; arg != NULL; arg = arg->s_next)
    305       1.1       cgd 			narg++;
    306       1.1       cgd 		outchar('f');
    307       1.1       cgd 		outint(narg);
    308      1.84    rillig 		for (const sym_t *arg = args; arg != NULL; arg = arg->s_next)
    309       1.1       cgd 			outtype(arg->s_type);
    310       1.4       jpo 		outtype(fsym->s_type->t_subt);
    311       1.1       cgd 	} else {
    312       1.4       jpo 		outtype(fsym->s_type);
    313       1.1       cgd 	}
    314      1.74    rillig 	outchar('\n');
    315       1.1       cgd }
    316       1.1       cgd 
    317       1.1       cgd /*
    318       1.1       cgd  * write out all information necessary for lint2 to check function
    319       1.1       cgd  * calls
    320       1.1       cgd  *
    321      1.59    rillig  * retval_used is set if the return value is used (assigned to a variable)
    322      1.59    rillig  * retval_discarded is set if the return value is neither used nor ignored
    323      1.59    rillig  * (that is, cast to void)
    324       1.1       cgd  */
    325       1.1       cgd void
    326      1.59    rillig outcall(const tnode_t *tn, bool retval_used, bool retval_discarded)
    327       1.1       cgd {
    328       1.1       cgd 	outint(csrc_pos.p_line);
    329      1.68    rillig 	outchar('c');		/* function call */
    330      1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    331       1.1       cgd 	outchar('.');
    332       1.1       cgd 	outint(curr_pos.p_line);
    333       1.1       cgd 
    334       1.1       cgd 	/*
    335      1.80    rillig 	 * flags; 'u' and 'i' must be last to make sure a letter is between the
    336      1.80    rillig 	 * numeric argument of a flag and the name of the function
    337       1.1       cgd 	 */
    338      1.92    rillig 	const function_call *call = tn->u.call;
    339      1.86    rillig 
    340      1.93    rillig 	for (size_t i = 0, n = call->args_len; i < n; i++) {
    341      1.93    rillig 		const tnode_t *arg = call->args[i];
    342       1.1       cgd 		if (arg->tn_op == CON) {
    343      1.84    rillig 			tspec_t t = arg->tn_type->t_tspec;
    344      1.84    rillig 			if (is_integer(t)) {
    345       1.1       cgd 				/*
    346       1.1       cgd 				 * XXX it would probably be better to
    347      1.16     perry 				 * explicitly test the sign
    348       1.1       cgd 				 */
    349      1.92    rillig 				int64_t si = arg->u.value.u.integer;
    350      1.87    rillig 				if (si == 0)
    351       1.1       cgd 					/* zero constant */
    352       1.1       cgd 					outchar('z');
    353      1.87    rillig 				else if (!msb(si, t))
    354      1.49    rillig 					/* positive if cast to signed */
    355       1.1       cgd 					outchar('p');
    356      1.87    rillig 				else
    357      1.49    rillig 					/* negative if cast to signed */
    358       1.1       cgd 					outchar('n');
    359      1.93    rillig 				outint((int)i + 1);
    360       1.1       cgd 			}
    361      1.39    rillig 		} else if (arg->tn_op == ADDR &&
    362      1.92    rillig 		    arg->u.ops.left->tn_op == STRING &&
    363      1.92    rillig 		    arg->u.ops.left->u.str_literals->data != NULL) {
    364      1.85    rillig 			buffer buf;
    365      1.85    rillig 			buf_init(&buf);
    366      1.90    rillig 			quoted_iterator it = { .end = 0 };
    367      1.92    rillig 			while (quoted_next(arg->u.ops.left->u.str_literals, &it))
    368      1.85    rillig 				buf_add_char(&buf, (char)it.value);
    369      1.85    rillig 
    370      1.85    rillig 			/* string literal, write all format specifiers */
    371       1.1       cgd 			outchar('s');
    372      1.93    rillig 			outint((int)i + 1);
    373      1.85    rillig 			outfstrg(buf.data);
    374      1.85    rillig 			free(buf.data);
    375       1.1       cgd 		}
    376       1.1       cgd 	}
    377      1.75    rillig 	outchar((char)(retval_discarded ? 'd' : retval_used ? 'u' : 'i'));
    378       1.1       cgd 
    379      1.92    rillig 	outname(call->func->u.ops.left->u.sym->s_name);
    380       1.1       cgd 
    381       1.1       cgd 	/* types of arguments */
    382       1.1       cgd 	outchar('f');
    383      1.86    rillig 	outint((int)call->args_len);
    384      1.93    rillig 	for (size_t i = 0, n = call->args_len; i < n; i++)
    385      1.86    rillig 		outtype(call->args[i]->tn_type);
    386       1.1       cgd 	/* expected type of return value */
    387       1.1       cgd 	outtype(tn->tn_type);
    388      1.74    rillig 	outchar('\n');
    389       1.1       cgd }
    390       1.1       cgd 
    391      1.76    rillig /* write a character to the output file, quoted if necessary */
    392      1.56    rillig static void
    393      1.56    rillig outqchar(char c)
    394      1.56    rillig {
    395      1.56    rillig 
    396      1.95    rillig 	if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
    397      1.56    rillig 		outchar(c);
    398      1.56    rillig 		return;
    399      1.56    rillig 	}
    400      1.56    rillig 
    401      1.56    rillig 	outchar('\\');
    402      1.56    rillig 	switch (c) {
    403      1.56    rillig 	case '\\':
    404      1.56    rillig 		outchar('\\');
    405      1.56    rillig 		break;
    406      1.56    rillig 	case '"':
    407      1.56    rillig 		outchar('"');
    408      1.56    rillig 		break;
    409      1.56    rillig 	case '\'':
    410      1.56    rillig 		outchar('\'');
    411      1.56    rillig 		break;
    412      1.56    rillig 	case '\b':
    413      1.56    rillig 		outchar('b');
    414      1.56    rillig 		break;
    415      1.56    rillig 	case '\t':
    416      1.56    rillig 		outchar('t');
    417      1.56    rillig 		break;
    418      1.56    rillig 	case '\n':
    419      1.56    rillig 		outchar('n');
    420      1.56    rillig 		break;
    421      1.56    rillig 	case '\f':
    422      1.56    rillig 		outchar('f');
    423      1.56    rillig 		break;
    424      1.56    rillig 	case '\r':
    425      1.56    rillig 		outchar('r');
    426      1.56    rillig 		break;
    427      1.56    rillig 	case '\v':
    428      1.56    rillig 		outchar('v');
    429      1.56    rillig 		break;
    430      1.56    rillig 	case '\a':
    431      1.56    rillig 		outchar('a');
    432      1.56    rillig 		break;
    433      1.56    rillig 	default:
    434      1.56    rillig 		outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
    435      1.56    rillig 		outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
    436      1.56    rillig 		outchar((char)((c & 07) + '0'));
    437      1.56    rillig 		break;
    438      1.56    rillig 	}
    439      1.56    rillig }
    440      1.56    rillig 
    441       1.1       cgd /*
    442       1.1       cgd  * extracts potential format specifiers for printf() and scanf() and
    443      1.76    rillig  * writes them, enclosed in "" and quoted if necessary, to the output file
    444       1.1       cgd  */
    445       1.1       cgd static void
    446      1.83    rillig outfstrg(const char *cp)
    447       1.1       cgd {
    448       1.1       cgd 
    449       1.1       cgd 	outchar('"');
    450       1.1       cgd 
    451      1.82    rillig 	char c = *cp++;
    452       1.1       cgd 	while (c != '\0') {
    453       1.1       cgd 
    454       1.1       cgd 		if (c != '%') {
    455       1.1       cgd 			c = *cp++;
    456       1.1       cgd 			continue;
    457       1.1       cgd 		}
    458       1.1       cgd 
    459      1.57    rillig 		outchar('%');
    460       1.1       cgd 		c = *cp++;
    461       1.1       cgd 
    462       1.1       cgd 		/* flags for printf and scanf and *-fieldwidth for printf */
    463      1.57    rillig 		while (c == '-' || c == '+' || c == ' ' ||
    464      1.79    rillig 		    c == '#' || c == '0' || c == '*') {
    465      1.57    rillig 			outchar(c);
    466       1.1       cgd 			c = *cp++;
    467       1.1       cgd 		}
    468       1.1       cgd 
    469       1.1       cgd 		/* numeric field width */
    470      1.95    rillig 		while (ch_isdigit(c)) {
    471      1.57    rillig 			outchar(c);
    472       1.1       cgd 			c = *cp++;
    473       1.1       cgd 		}
    474       1.1       cgd 
    475       1.1       cgd 		/* precision for printf */
    476       1.1       cgd 		if (c == '.') {
    477      1.57    rillig 			outchar(c);
    478      1.57    rillig 			c = *cp++;
    479      1.57    rillig 			if (c == '*') {
    480      1.57    rillig 				outchar(c);
    481       1.1       cgd 				c = *cp++;
    482       1.1       cgd 			} else {
    483      1.95    rillig 				while (ch_isdigit(c)) {
    484      1.57    rillig 					outchar(c);
    485       1.1       cgd 					c = *cp++;
    486       1.1       cgd 				}
    487       1.1       cgd 			}
    488       1.1       cgd 		}
    489       1.1       cgd 
    490      1.57    rillig 		/* h, l, L and q flags for printf and scanf */
    491       1.1       cgd 		if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
    492      1.57    rillig 			outchar(c);
    493       1.1       cgd 			c = *cp++;
    494       1.1       cgd 		}
    495       1.1       cgd 
    496       1.1       cgd 		/*
    497      1.57    rillig 		 * The last character. It is always written, so we can detect
    498       1.1       cgd 		 * invalid format specifiers.
    499       1.1       cgd 		 */
    500       1.1       cgd 		if (c != '\0') {
    501       1.1       cgd 			outqchar(c);
    502      1.82    rillig 			char oc = c;
    503       1.1       cgd 			c = *cp++;
    504       1.1       cgd 			/*
    505      1.80    rillig 			 * handle [ for scanf. [-] means that a minus sign was
    506      1.80    rillig 			 * found at an undefined position.
    507       1.1       cgd 			 */
    508       1.1       cgd 			if (oc == '[') {
    509       1.1       cgd 				if (c == '^')
    510       1.1       cgd 					c = *cp++;
    511       1.1       cgd 				if (c == ']')
    512       1.1       cgd 					c = *cp++;
    513      1.82    rillig 				bool first = true;
    514       1.1       cgd 				while (c != '\0' && c != ']') {
    515       1.1       cgd 					if (c == '-') {
    516       1.1       cgd 						if (!first && *cp != ']')
    517      1.57    rillig 							outchar(c);
    518       1.1       cgd 					}
    519      1.37    rillig 					first = false;
    520       1.1       cgd 					c = *cp++;
    521       1.1       cgd 				}
    522       1.1       cgd 				if (c == ']') {
    523      1.57    rillig 					outchar(c);
    524       1.1       cgd 					c = *cp++;
    525       1.1       cgd 				}
    526       1.1       cgd 			}
    527       1.1       cgd 		}
    528       1.1       cgd 	}
    529       1.1       cgd 
    530       1.1       cgd 	outchar('"');
    531       1.1       cgd }
    532       1.1       cgd 
    533      1.76    rillig /* writes a record if sym was used */
    534       1.1       cgd void
    535      1.36    rillig outusg(const sym_t *sym)
    536       1.1       cgd {
    537      1.95    rillig 	if (ch_isdigit(sym->s_name[0]))	/* see mktempsym */
    538      1.60    rillig 		return;
    539      1.60    rillig 
    540       1.1       cgd 	outint(csrc_pos.p_line);
    541      1.68    rillig 	outchar('u');		/* used */
    542      1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    543       1.1       cgd 	outchar('.');
    544       1.1       cgd 	outint(curr_pos.p_line);
    545      1.75    rillig 	outchar('x');		/* separate the two numbers */
    546       1.1       cgd 	outname(sym->s_name);
    547      1.74    rillig 	outchar('\n');
    548       1.1       cgd }
    549