Home | History | Annotate | Line # | Download | only in lint1
emit1.c revision 1.46
      1  1.46    rillig /* $NetBSD: emit1.c,v 1.46 2021/07/15 17:03:50 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.1       cgd  *      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.11        tv #if defined(__RCSID) && !defined(lint)
     41  1.46    rillig __RCSID("$NetBSD: emit1.c,v 1.46 2021/07/15 17:03:50 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.1       cgd  * Write type into the output buffer.
     51   1.1       cgd  * The type is written as a sequence of substrings, each of which describes a
     52   1.1       cgd  * node of type type_t
     53  1.23    rillig  * a node is encoded as follows:
     54  1.15      yamt  *	_Bool			B
     55  1.18  christos  *	_Complex float		s X
     56  1.21    rillig  *	_Complex double		X
     57  1.21    rillig  *	_Complex long double	l X
     58   1.1       cgd  *	char			C
     59   1.1       cgd  *	signed char		s C
     60   1.1       cgd  *	unsigned char		u C
     61   1.1       cgd  *	short			S
     62   1.1       cgd  *	unsigned short		u S
     63   1.1       cgd  *	int			I
     64   1.1       cgd  *	unsigned int		u I
     65   1.1       cgd  *	long			L
     66   1.1       cgd  *	unsigned long		u L
     67   1.1       cgd  *	long long		Q
     68   1.1       cgd  *	unsigned long long	u Q
     69   1.1       cgd  *	float			s D
     70   1.1       cgd  *	double			D
     71   1.1       cgd  *	long double		l D
     72   1.1       cgd  *	void			V
     73   1.1       cgd  *	*			P
     74   1.1       cgd  *	[n]			A n
     75   1.1       cgd  *	()			F
     76   1.1       cgd  *	(void)			F 0
     77  1.44    rillig  *	(n parameters)		F n arg1 arg2 ... argn
     78  1.44    rillig  *	(n parameters, ...)	F n arg1 arg2 ... argn-1 E
     79   1.1       cgd  *	enum tag		e T tag_or_typename
     80   1.1       cgd  *	struct tag		s T tag_or_typename
     81   1.1       cgd  *	union tag		u T tag_or_typename
     82   1.1       cgd  *
     83  1.44    rillig  *	tag_or_typename		0 (obsolete)		no tag or type name
     84  1.44    rillig  *				1 n tag			tagged type
     85   1.1       cgd  *				2 n typename		only type name
     86  1.44    rillig  *				3 line.file.uniq	anonymous types
     87   1.1       cgd  *
     88   1.1       cgd  * spaces are only for better readability
     89  1.34    rillig  * additionally it is possible to prepend the characters 'c' (for const)
     90   1.1       cgd  * and 'v' (for volatile)
     91   1.1       cgd  */
     92   1.1       cgd void
     93  1.36    rillig outtype(const type_t *tp)
     94   1.1       cgd {
     95   1.1       cgd 	int	t, s, na;
     96   1.1       cgd 	sym_t	*arg;
     97   1.1       cgd 	tspec_t	ts;
     98   1.1       cgd 
     99   1.1       cgd 	while (tp != NULL) {
    100  1.42    rillig 		if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
    101   1.1       cgd 			ts = ENUM;
    102   1.1       cgd 		switch (ts) {
    103  1.15      yamt 		case BOOL:	t = 'B';	s = '\0';	break;
    104   1.1       cgd 		case CHAR:	t = 'C';	s = '\0';	break;
    105   1.1       cgd 		case SCHAR:	t = 'C';	s = 's';	break;
    106   1.1       cgd 		case UCHAR:	t = 'C';	s = 'u';	break;
    107   1.1       cgd 		case SHORT:	t = 'S';	s = '\0';	break;
    108   1.1       cgd 		case USHORT:	t = 'S';	s = 'u';	break;
    109   1.1       cgd 		case INT:	t = 'I';	s = '\0';	break;
    110   1.1       cgd 		case UINT:	t = 'I';	s = 'u';	break;
    111   1.1       cgd 		case LONG:	t = 'L';	s = '\0';	break;
    112   1.1       cgd 		case ULONG:	t = 'L';	s = 'u';	break;
    113   1.1       cgd 		case QUAD:	t = 'Q';	s = '\0';	break;
    114   1.1       cgd 		case UQUAD:	t = 'Q';	s = 'u';	break;
    115   1.1       cgd 		case FLOAT:	t = 'D';	s = 's';	break;
    116   1.1       cgd 		case DOUBLE:	t = 'D';	s = '\0';	break;
    117   1.1       cgd 		case LDOUBLE:	t = 'D';	s = 'l';	break;
    118   1.1       cgd 		case VOID:	t = 'V';	s = '\0';	break;
    119   1.1       cgd 		case PTR:	t = 'P';	s = '\0';	break;
    120   1.1       cgd 		case ARRAY:	t = 'A';	s = '\0';	break;
    121   1.1       cgd 		case FUNC:	t = 'F';	s = '\0';	break;
    122   1.1       cgd 		case ENUM:	t = 'T';	s = 'e';	break;
    123   1.1       cgd 		case STRUCT:	t = 'T';	s = 's';	break;
    124   1.1       cgd 		case UNION:	t = 'T';	s = 'u';	break;
    125  1.17  christos 		case FCOMPLEX:	t = 'X';	s = 's';	break;
    126  1.18  christos 		case DCOMPLEX:	t = 'X';	s = '\0';	break;
    127  1.19      matt 		case LCOMPLEX:	t = 'X';	s = 'l';	break;
    128   1.1       cgd 		default:
    129  1.38    rillig 			lint_assert(/*CONSTCOND*/false);
    130   1.1       cgd 		}
    131   1.1       cgd 		if (tp->t_const)
    132   1.1       cgd 			outchar('c');
    133   1.1       cgd 		if (tp->t_volatile)
    134   1.1       cgd 			outchar('v');
    135   1.1       cgd 		if (s != '\0')
    136   1.1       cgd 			outchar(s);
    137   1.1       cgd 		outchar(t);
    138   1.1       cgd 		if (ts == ARRAY) {
    139   1.1       cgd 			outint(tp->t_dim);
    140   1.1       cgd 		} else if (ts == ENUM) {
    141  1.41    rillig 			outtt(tp->t_enum->en_tag, tp->t_enum->en_first_typedef);
    142   1.1       cgd 		} else if (ts == STRUCT || ts == UNION) {
    143  1.40    rillig 			outtt(tp->t_str->sou_tag, tp->t_str->sou_first_typedef);
    144   1.1       cgd 		} else if (ts == FUNC && tp->t_proto) {
    145   1.1       cgd 			na = 0;
    146  1.26    rillig 			for (arg = tp->t_args; arg != NULL; arg = arg->s_next)
    147  1.45    rillig 				na++;
    148   1.1       cgd 			if (tp->t_vararg)
    149   1.1       cgd 				na++;
    150   1.1       cgd 			outint(na);
    151  1.26    rillig 			for (arg = tp->t_args; arg != NULL; arg = arg->s_next)
    152   1.1       cgd 				outtype(arg->s_type);
    153   1.1       cgd 			if (tp->t_vararg)
    154   1.1       cgd 				outchar('E');
    155   1.1       cgd 		}
    156   1.1       cgd 		tp = tp->t_subt;
    157   1.1       cgd 	}
    158   1.1       cgd }
    159   1.1       cgd 
    160   1.1       cgd /*
    161   1.1       cgd  * write the name of a tag or typename
    162   1.1       cgd  *
    163  1.44    rillig  * if the tag is named, the name of the tag is written,
    164  1.44    rillig  * otherwise, if a typename exists which refers to this tag,
    165  1.44    rillig  * this typename is written
    166   1.1       cgd  */
    167   1.1       cgd static void
    168  1.10     lukem outtt(sym_t *tag, sym_t *tdef)
    169   1.1       cgd {
    170   1.5       cgd 
    171  1.44    rillig 	/* 0 is no longer used. */
    172  1.44    rillig 
    173   1.1       cgd 	if (tag->s_name != unnamed) {
    174   1.1       cgd 		outint(1);
    175   1.1       cgd 		outname(tag->s_name);
    176   1.1       cgd 	} else if (tdef != NULL) {
    177   1.1       cgd 		outint(2);
    178   1.1       cgd 		outname(tdef->s_name);
    179   1.1       cgd 	} else {
    180   1.5       cgd 		outint(3);
    181  1.29    rillig 		outint(tag->s_def_pos.p_line);
    182   1.5       cgd 		outchar('.');
    183  1.43    rillig 		outint(get_filename_id(tag->s_def_pos.p_file));
    184   1.5       cgd 		outchar('.');
    185  1.29    rillig 		outint(tag->s_def_pos.p_uniq);
    186   1.1       cgd 	}
    187   1.1       cgd }
    188   1.1       cgd 
    189   1.1       cgd /*
    190  1.23    rillig  * write information about a globally declared/defined symbol
    191   1.3       jpo  * with storage class extern
    192   1.1       cgd  *
    193  1.23    rillig  * information about function definitions are written in outfdef(),
    194   1.1       cgd  * not here
    195   1.1       cgd  */
    196   1.1       cgd void
    197  1.36    rillig outsym(const sym_t *sym, scl_t sc, def_t def)
    198   1.1       cgd {
    199  1.10     lukem 
    200   1.1       cgd 	/*
    201   1.1       cgd 	 * Static function declarations must also be written to the output
    202   1.1       cgd 	 * file. Compatibility of function declarations (for both static
    203   1.1       cgd 	 * and extern functions) must be checked in lint2. Lint1 can't do
    204  1.23    rillig 	 * this, especially not if functions are declared at block level
    205   1.1       cgd 	 * before their first declaration at level 0.
    206   1.1       cgd 	 */
    207   1.1       cgd 	if (sc != EXTERN && !(sc == STATIC && sym->s_type->t_tspec == FUNC))
    208   1.1       cgd 		return;
    209   1.1       cgd 
    210   1.1       cgd 	/* reset buffer */
    211   1.1       cgd 	outclr();
    212   1.1       cgd 
    213   1.1       cgd 	/*
    214   1.1       cgd 	 * line number of .c source, 'd' for declaration, Id of current
    215   1.1       cgd 	 * source (.c or .h), and line in current source.
    216   1.1       cgd 	 */
    217   1.1       cgd 	outint(csrc_pos.p_line);
    218   1.1       cgd 	outchar('d');
    219  1.43    rillig 	outint(get_filename_id(sym->s_def_pos.p_file));
    220   1.1       cgd 	outchar('.');
    221  1.29    rillig 	outint(sym->s_def_pos.p_line);
    222   1.1       cgd 
    223   1.1       cgd 	/* flags */
    224   1.1       cgd 
    225   1.3       jpo 	switch (def) {
    226   1.1       cgd 	case DEF:
    227   1.1       cgd 		/* defined */
    228   1.1       cgd 		outchar('d');
    229   1.1       cgd 		break;
    230   1.1       cgd 	case TDEF:
    231   1.1       cgd 		/* tentative defined */
    232   1.1       cgd 		outchar('t');
    233   1.1       cgd 		break;
    234   1.1       cgd 	case DECL:
    235   1.1       cgd 		/* declared */
    236   1.1       cgd 		outchar('e');
    237   1.1       cgd 		break;
    238   1.1       cgd 	default:
    239  1.38    rillig 		lint_assert(/*CONSTCOND*/false);
    240   1.1       cgd 	}
    241   1.3       jpo 	if (llibflg && def != DECL) {
    242   1.1       cgd 		/*
    243   1.1       cgd 		 * mark it as used so we get no warnings from lint2 about
    244   1.1       cgd 		 * unused symbols in libraries.
    245   1.1       cgd 		 */
    246   1.1       cgd 		outchar('u');
    247   1.1       cgd 	}
    248   1.1       cgd 
    249   1.1       cgd 	if (sc == STATIC)
    250   1.1       cgd 		outchar('s');
    251   1.1       cgd 
    252   1.1       cgd 	/* name of the symbol */
    253   1.1       cgd 	outname(sym->s_name);
    254   1.1       cgd 
    255   1.6       cgd 	/* renamed name of symbol, if necessary */
    256  1.37    rillig 	if (sym->s_rename != NULL) {
    257   1.6       cgd 		outchar('r');
    258   1.6       cgd 		outname(sym->s_rename);
    259   1.6       cgd 	}
    260   1.6       cgd 
    261   1.1       cgd 	/* type of the symbol */
    262   1.1       cgd 	outtype(sym->s_type);
    263   1.1       cgd }
    264   1.1       cgd 
    265   1.1       cgd /*
    266   1.1       cgd  * write information about function definition
    267   1.1       cgd  *
    268   1.1       cgd  * this is also done for static functions so we are able to check if
    269   1.1       cgd  * they are called with proper argument types
    270   1.1       cgd  */
    271   1.1       cgd void
    272  1.37    rillig outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
    273  1.36    rillig 	const sym_t *args)
    274   1.1       cgd {
    275  1.36    rillig 	int narg;
    276  1.36    rillig 	const sym_t *arg;
    277   1.1       cgd 
    278   1.1       cgd 	/* reset the buffer */
    279   1.1       cgd 	outclr();
    280   1.1       cgd 
    281   1.1       cgd 	/*
    282   1.1       cgd 	 * line number of .c source, 'd' for declaration, Id of current
    283   1.1       cgd 	 * source (.c or .h), and line in current source
    284   1.1       cgd 	 *
    285   1.1       cgd 	 * we are already at the end of the function. If we are in the
    286   1.1       cgd 	 * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
    287   1.1       cgd 	 * (for functions defined in header files).
    288   1.1       cgd 	 */
    289   1.1       cgd 	if (posp->p_file == csrc_pos.p_file) {
    290   1.1       cgd 		outint(posp->p_line);
    291   1.1       cgd 	} else {
    292   1.1       cgd 		outint(csrc_pos.p_line);
    293   1.1       cgd 	}
    294   1.1       cgd 	outchar('d');
    295  1.43    rillig 	outint(get_filename_id(posp->p_file));
    296   1.1       cgd 	outchar('.');
    297   1.1       cgd 	outint(posp->p_line);
    298   1.1       cgd 
    299   1.1       cgd 	/* flags */
    300   1.1       cgd 
    301   1.1       cgd 	/* both SCANFLIKE and PRINTFLIKE imply VARARGS */
    302  1.33    rillig 	if (printflike_argnum != -1) {
    303  1.33    rillig 		nvararg = printflike_argnum;
    304  1.33    rillig 	} else if (scanflike_argnum != -1) {
    305  1.33    rillig 		nvararg = scanflike_argnum;
    306   1.1       cgd 	}
    307   1.1       cgd 
    308   1.1       cgd 	if (nvararg != -1) {
    309   1.1       cgd 		outchar('v');
    310   1.1       cgd 		outint(nvararg);
    311   1.1       cgd 	}
    312  1.33    rillig 	if (scanflike_argnum != -1) {
    313   1.1       cgd 		outchar('S');
    314  1.33    rillig 		outint(scanflike_argnum);
    315   1.1       cgd 	}
    316  1.33    rillig 	if (printflike_argnum != -1) {
    317   1.1       cgd 		outchar('P');
    318  1.33    rillig 		outint(printflike_argnum);
    319   1.1       cgd 	}
    320  1.33    rillig 	nvararg = printflike_argnum = scanflike_argnum = -1;
    321   1.1       cgd 
    322   1.1       cgd 	outchar('d');
    323   1.1       cgd 
    324   1.1       cgd 	if (rval)
    325   1.1       cgd 		/* has return value */
    326   1.1       cgd 		outchar('r');
    327   1.1       cgd 
    328   1.1       cgd 	if (llibflg)
    329   1.1       cgd 		/*
    330   1.1       cgd 		 * mark it as used so lint2 does not complain about
    331   1.1       cgd 		 * unused symbols in libraries
    332   1.1       cgd 		 */
    333   1.1       cgd 		outchar('u');
    334   1.1       cgd 
    335   1.1       cgd 	if (osdef)
    336   1.1       cgd 		/* old style function definition */
    337   1.1       cgd 		outchar('o');
    338   1.1       cgd 
    339  1.20  christos 	if (fsym->s_inline)
    340  1.20  christos 		outchar('i');
    341  1.20  christos 
    342   1.1       cgd 	if (fsym->s_scl == STATIC)
    343   1.1       cgd 		outchar('s');
    344   1.1       cgd 
    345   1.1       cgd 	/* name of function */
    346   1.1       cgd 	outname(fsym->s_name);
    347   1.6       cgd 
    348   1.6       cgd 	/* renamed name of function, if necessary */
    349  1.37    rillig 	if (fsym->s_rename != NULL) {
    350   1.6       cgd 		outchar('r');
    351   1.6       cgd 		outname(fsym->s_rename);
    352   1.6       cgd 	}
    353   1.1       cgd 
    354   1.1       cgd 	/* argument types and return value */
    355   1.1       cgd 	if (osdef) {
    356   1.1       cgd 		narg = 0;
    357  1.26    rillig 		for (arg = args; arg != NULL; arg = arg->s_next)
    358   1.1       cgd 			narg++;
    359   1.1       cgd 		outchar('f');
    360   1.1       cgd 		outint(narg);
    361  1.26    rillig 		for (arg = args; arg != NULL; arg = arg->s_next)
    362   1.1       cgd 			outtype(arg->s_type);
    363   1.4       jpo 		outtype(fsym->s_type->t_subt);
    364   1.1       cgd 	} else {
    365   1.4       jpo 		outtype(fsym->s_type);
    366   1.1       cgd 	}
    367   1.1       cgd }
    368   1.1       cgd 
    369   1.1       cgd /*
    370   1.1       cgd  * write out all information necessary for lint2 to check function
    371   1.1       cgd  * calls
    372   1.1       cgd  *
    373  1.34    rillig  * rvused is set if the return value is used (assigned to a variable)
    374   1.1       cgd  * rvdisc is set if the return value is not used and not ignored
    375   1.1       cgd  * (casted to void)
    376   1.1       cgd  */
    377   1.1       cgd void
    378  1.37    rillig outcall(const tnode_t *tn, bool rvused, bool rvdisc)
    379   1.1       cgd {
    380   1.1       cgd 	tnode_t	*args, *arg;
    381   1.1       cgd 	int	narg, n, i;
    382  1.12   thorpej 	int64_t	q;
    383   1.1       cgd 	tspec_t	t;
    384   1.1       cgd 
    385   1.1       cgd 	/* reset buffer */
    386   1.1       cgd 	outclr();
    387   1.1       cgd 
    388   1.1       cgd 	/*
    389   1.1       cgd 	 * line number of .c source, 'c' for function call, Id of current
    390   1.1       cgd 	 * source (.c or .h), and line in current source
    391   1.1       cgd 	 */
    392   1.1       cgd 	outint(csrc_pos.p_line);
    393   1.1       cgd 	outchar('c');
    394  1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    395   1.1       cgd 	outchar('.');
    396   1.1       cgd 	outint(curr_pos.p_line);
    397   1.1       cgd 
    398   1.1       cgd 	/*
    399   1.1       cgd 	 * flags; 'u' and 'i' must be last to make sure a letter
    400   1.1       cgd 	 * is between the numeric argument of a flag and the name of
    401   1.1       cgd 	 * the function
    402   1.1       cgd 	 */
    403   1.1       cgd 	narg = 0;
    404   1.1       cgd 	args = tn->tn_right;
    405   1.1       cgd 	for (arg = args; arg != NULL; arg = arg->tn_right)
    406   1.1       cgd 		narg++;
    407  1.25    rillig 	/* information about arguments */
    408   1.1       cgd 	for (n = 1; n <= narg; n++) {
    409   1.1       cgd 		/* the last argument is the top one in the tree */
    410  1.10     lukem 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    411  1.10     lukem 			continue;
    412   1.1       cgd 		arg = arg->tn_left;
    413   1.1       cgd 		if (arg->tn_op == CON) {
    414  1.35    rillig 			if (is_integer(t = arg->tn_type->t_tspec)) {
    415   1.1       cgd 				/*
    416   1.1       cgd 				 * XXX it would probably be better to
    417  1.16     perry 				 * explicitly test the sign
    418   1.1       cgd 				 */
    419   1.1       cgd 				if ((q = arg->tn_val->v_quad) == 0) {
    420   1.1       cgd 					/* zero constant */
    421   1.1       cgd 					outchar('z');
    422   1.1       cgd 				} else if (msb(q, t, 0) == 0) {
    423   1.1       cgd 					/* positive if casted to signed */
    424   1.1       cgd 					outchar('p');
    425   1.1       cgd 				} else {
    426   1.1       cgd 					/* negative if casted to signed */
    427   1.1       cgd 					outchar('n');
    428   1.1       cgd 				}
    429   1.1       cgd 				outint(n);
    430   1.1       cgd 			}
    431  1.39    rillig 		} else if (arg->tn_op == ADDR &&
    432   1.1       cgd 			   arg->tn_left->tn_op == STRING &&
    433  1.28    rillig 			   arg->tn_left->tn_string->st_tspec == CHAR) {
    434   1.1       cgd 			/* constant string, write all format specifiers */
    435   1.1       cgd 			outchar('s');
    436   1.1       cgd 			outint(n);
    437  1.28    rillig 			outfstrg(arg->tn_left->tn_string);
    438   1.1       cgd 		}
    439   1.1       cgd 
    440   1.1       cgd 	}
    441   1.1       cgd 	/* return value discarded/used/ignored */
    442   1.1       cgd 	outchar(rvdisc ? 'd' : (rvused ? 'u' : 'i'));
    443   1.1       cgd 
    444   1.1       cgd 	/* name of the called function */
    445   1.1       cgd 	outname(tn->tn_left->tn_left->tn_sym->s_name);
    446   1.1       cgd 
    447   1.1       cgd 	/* types of arguments */
    448   1.1       cgd 	outchar('f');
    449   1.1       cgd 	outint(narg);
    450   1.1       cgd 	for (n = 1; n <= narg; n++) {
    451   1.1       cgd 		/* the last argument is the top one in the tree */
    452  1.10     lukem 		for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
    453  1.10     lukem 			continue;
    454   1.1       cgd 		outtype(arg->tn_left->tn_type);
    455   1.1       cgd 	}
    456   1.1       cgd 	/* expected type of return value */
    457   1.1       cgd 	outtype(tn->tn_type);
    458   1.1       cgd }
    459   1.1       cgd 
    460   1.1       cgd /*
    461   1.1       cgd  * extracts potential format specifiers for printf() and scanf() and
    462  1.34    rillig  * writes them, enclosed in "" and quoted if necessary, to the output buffer
    463   1.1       cgd  */
    464   1.1       cgd static void
    465  1.10     lukem outfstrg(strg_t *strg)
    466   1.1       cgd {
    467  1.45    rillig 	unsigned char c, oc;
    468  1.37    rillig 	bool	first;
    469   1.1       cgd 	u_char	*cp;
    470   1.1       cgd 
    471  1.30    rillig 	lint_assert(strg->st_tspec == CHAR);
    472   1.1       cgd 
    473   1.1       cgd 	cp = strg->st_cp;
    474   1.1       cgd 
    475   1.1       cgd 	outchar('"');
    476   1.1       cgd 
    477   1.1       cgd 	c = *cp++;
    478   1.1       cgd 
    479   1.1       cgd 	while (c != '\0') {
    480   1.1       cgd 
    481   1.1       cgd 		if (c != '%') {
    482   1.1       cgd 			c = *cp++;
    483   1.1       cgd 			continue;
    484   1.1       cgd 		}
    485   1.1       cgd 
    486   1.1       cgd 		outqchar('%');
    487   1.1       cgd 		c = *cp++;
    488   1.1       cgd 
    489   1.1       cgd 		/* flags for printf and scanf and *-fieldwidth for printf */
    490   1.1       cgd 		while (c != '\0' && (c == '-' || c == '+' || c == ' ' ||
    491   1.1       cgd 				     c == '#' || c == '0' || c == '*')) {
    492   1.1       cgd 			outqchar(c);
    493   1.1       cgd 			c = *cp++;
    494   1.1       cgd 		}
    495   1.1       cgd 
    496   1.1       cgd 		/* numeric field width */
    497  1.45    rillig 		while (c != '\0' && ch_isdigit((char)c)) {
    498   1.1       cgd 			outqchar(c);
    499   1.1       cgd 			c = *cp++;
    500   1.1       cgd 		}
    501   1.1       cgd 
    502   1.1       cgd 		/* precision for printf */
    503   1.1       cgd 		if (c == '.') {
    504   1.1       cgd 			outqchar(c);
    505   1.1       cgd 			if ((c = *cp++) == '*') {
    506   1.1       cgd 				outqchar(c);
    507   1.1       cgd 				c = *cp++;
    508   1.1       cgd 			} else {
    509  1.45    rillig 				while (c != '\0' && ch_isdigit((char)c)) {
    510   1.1       cgd 					outqchar(c);
    511   1.1       cgd 					c = *cp++;
    512   1.1       cgd 				}
    513   1.1       cgd 			}
    514   1.1       cgd 		}
    515   1.1       cgd 
    516   1.1       cgd 		/* h, l, L and q flags fpr printf and scanf */
    517   1.1       cgd 		if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
    518   1.1       cgd 			outqchar(c);
    519   1.1       cgd 			c = *cp++;
    520   1.1       cgd 		}
    521   1.1       cgd 
    522   1.1       cgd 		/*
    523   1.1       cgd 		 * The last character. It is always written so we can detect
    524   1.1       cgd 		 * invalid format specifiers.
    525   1.1       cgd 		 */
    526   1.1       cgd 		if (c != '\0') {
    527   1.1       cgd 			outqchar(c);
    528   1.1       cgd 			oc = c;
    529   1.1       cgd 			c = *cp++;
    530   1.1       cgd 			/*
    531   1.1       cgd 			 * handle [ for scanf. [-] means that a minus sign
    532   1.1       cgd 			 * was found at an undefined position.
    533   1.1       cgd 			 */
    534   1.1       cgd 			if (oc == '[') {
    535   1.1       cgd 				if (c == '^')
    536   1.1       cgd 					c = *cp++;
    537   1.1       cgd 				if (c == ']')
    538   1.1       cgd 					c = *cp++;
    539  1.37    rillig 				first = true;
    540   1.1       cgd 				while (c != '\0' && c != ']') {
    541   1.1       cgd 					if (c == '-') {
    542   1.1       cgd 						if (!first && *cp != ']')
    543   1.1       cgd 							outqchar(c);
    544   1.1       cgd 					}
    545  1.37    rillig 					first = false;
    546   1.1       cgd 					c = *cp++;
    547   1.1       cgd 				}
    548   1.1       cgd 				if (c == ']') {
    549   1.1       cgd 					outqchar(c);
    550   1.1       cgd 					c = *cp++;
    551   1.1       cgd 				}
    552   1.1       cgd 			}
    553   1.1       cgd 		}
    554   1.1       cgd 
    555   1.1       cgd 	}
    556   1.1       cgd 
    557   1.1       cgd 	outchar('"');
    558   1.1       cgd }
    559   1.1       cgd 
    560   1.1       cgd /*
    561   1.1       cgd  * writes a record if sym was used
    562   1.1       cgd  */
    563   1.1       cgd void
    564  1.36    rillig outusg(const sym_t *sym)
    565   1.1       cgd {
    566   1.1       cgd 	/* reset buffer */
    567   1.1       cgd 	outclr();
    568   1.1       cgd 
    569   1.1       cgd 	/*
    570   1.1       cgd 	 * line number of .c source, 'u' for used, Id of current
    571   1.1       cgd 	 * source (.c or .h), and line in current source
    572   1.1       cgd 	 */
    573   1.1       cgd 	outint(csrc_pos.p_line);
    574   1.1       cgd 	outchar('u');
    575  1.43    rillig 	outint(get_filename_id(curr_pos.p_file));
    576   1.1       cgd 	outchar('.');
    577   1.1       cgd 	outint(curr_pos.p_line);
    578   1.1       cgd 
    579   1.1       cgd 	/* necessary to delimit both numbers */
    580   1.1       cgd 	outchar('x');
    581   1.1       cgd 
    582   1.1       cgd 	outname(sym->s_name);
    583   1.1       cgd }
    584