emit1.c revision 1.58 1 1.58 rillig /* $NetBSD: emit1.c,v 1.58 2021/09/04 18:58:57 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.58 rillig __RCSID("$NetBSD: emit1.c,v 1.58 2021/09/04 18:58:57 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.53 rillig /* Available letters: ------GH--K-MNO--R--U-W-YZ */
96 1.53 rillig #ifdef INT128_SIZE
97 1.53 rillig static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDDVTTTPAF?XXX";
98 1.53 rillig static const char ss[NTSPEC] = "??? su u u u u us l sue ?s l";
99 1.53 rillig #else
100 1.53 rillig static const char tt[NTSPEC] = "???BCCCSSIILLQQDDDVTTTPAF?XXX";
101 1.53 rillig static const char ss[NTSPEC] = "??? su u u u us l sue ?s l";
102 1.53 rillig #endif
103 1.53 rillig int na;
104 1.1 cgd sym_t *arg;
105 1.1 cgd tspec_t ts;
106 1.1 cgd
107 1.1 cgd while (tp != NULL) {
108 1.42 rillig if ((ts = tp->t_tspec) == INT && tp->t_is_enum)
109 1.1 cgd ts = ENUM;
110 1.58 rillig lint_assert(tt[ts] != '?' && ss[ts] != '?');
111 1.1 cgd if (tp->t_const)
112 1.1 cgd outchar('c');
113 1.1 cgd if (tp->t_volatile)
114 1.1 cgd outchar('v');
115 1.58 rillig if (ss[ts] != ' ')
116 1.58 rillig outchar(ss[ts]);
117 1.58 rillig outchar(tt[ts]);
118 1.58 rillig
119 1.1 cgd if (ts == ARRAY) {
120 1.1 cgd outint(tp->t_dim);
121 1.1 cgd } else if (ts == ENUM) {
122 1.41 rillig outtt(tp->t_enum->en_tag, tp->t_enum->en_first_typedef);
123 1.1 cgd } else if (ts == STRUCT || ts == UNION) {
124 1.40 rillig outtt(tp->t_str->sou_tag, tp->t_str->sou_first_typedef);
125 1.1 cgd } else if (ts == FUNC && tp->t_proto) {
126 1.1 cgd na = 0;
127 1.26 rillig for (arg = tp->t_args; arg != NULL; arg = arg->s_next)
128 1.45 rillig na++;
129 1.1 cgd if (tp->t_vararg)
130 1.1 cgd na++;
131 1.1 cgd outint(na);
132 1.26 rillig for (arg = tp->t_args; arg != NULL; arg = arg->s_next)
133 1.1 cgd outtype(arg->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.1 cgd * file. Compatibility of function declarations (for both static
184 1.1 cgd * and extern functions) must be checked in lint2. Lint1 can't do
185 1.23 rillig * this, especially not if functions are declared at block level
186 1.1 cgd * before their 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.1 cgd
191 1.1 cgd /* reset buffer */
192 1.1 cgd outclr();
193 1.1 cgd
194 1.1 cgd /*
195 1.1 cgd * line number of .c source, 'd' for declaration, Id of current
196 1.1 cgd * source (.c or .h), and line in current source.
197 1.1 cgd */
198 1.1 cgd outint(csrc_pos.p_line);
199 1.1 cgd outchar('d');
200 1.43 rillig outint(get_filename_id(sym->s_def_pos.p_file));
201 1.1 cgd outchar('.');
202 1.29 rillig outint(sym->s_def_pos.p_line);
203 1.1 cgd
204 1.1 cgd /* flags */
205 1.1 cgd
206 1.48 rillig if (def == DEF)
207 1.48 rillig outchar('d'); /* defined */
208 1.48 rillig else if (def == TDEF)
209 1.48 rillig outchar('t'); /* tentative defined */
210 1.48 rillig else {
211 1.48 rillig lint_assert(def == DECL);
212 1.48 rillig outchar('e'); /* declared */
213 1.1 cgd }
214 1.48 rillig
215 1.3 jpo if (llibflg && def != DECL) {
216 1.1 cgd /*
217 1.48 rillig * mark it as used so lint2 does not complain about
218 1.48 rillig * unused symbols in libraries
219 1.1 cgd */
220 1.1 cgd outchar('u');
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd if (sc == STATIC)
224 1.1 cgd outchar('s');
225 1.1 cgd
226 1.1 cgd /* name of the symbol */
227 1.1 cgd outname(sym->s_name);
228 1.1 cgd
229 1.6 cgd /* renamed name of symbol, if necessary */
230 1.37 rillig if (sym->s_rename != NULL) {
231 1.6 cgd outchar('r');
232 1.6 cgd outname(sym->s_rename);
233 1.6 cgd }
234 1.6 cgd
235 1.1 cgd /* type of the symbol */
236 1.1 cgd outtype(sym->s_type);
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd /*
240 1.1 cgd * write information about function definition
241 1.1 cgd *
242 1.1 cgd * this is also done for static functions so we are able to check if
243 1.1 cgd * they are called with proper argument types
244 1.1 cgd */
245 1.1 cgd void
246 1.37 rillig outfdef(const sym_t *fsym, const pos_t *posp, bool rval, bool osdef,
247 1.36 rillig const sym_t *args)
248 1.1 cgd {
249 1.36 rillig int narg;
250 1.36 rillig const sym_t *arg;
251 1.1 cgd
252 1.1 cgd /* reset the buffer */
253 1.1 cgd outclr();
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * line number of .c source, 'd' for declaration, Id of current
257 1.1 cgd * source (.c or .h), and line in current source
258 1.1 cgd *
259 1.1 cgd * we are already at the end of the function. If we are in the
260 1.1 cgd * .c source, posp->p_line is correct, otherwise csrc_pos.p_line
261 1.1 cgd * (for functions defined in header files).
262 1.1 cgd */
263 1.1 cgd if (posp->p_file == csrc_pos.p_file) {
264 1.1 cgd outint(posp->p_line);
265 1.1 cgd } else {
266 1.1 cgd outint(csrc_pos.p_line);
267 1.1 cgd }
268 1.1 cgd outchar('d');
269 1.43 rillig outint(get_filename_id(posp->p_file));
270 1.1 cgd outchar('.');
271 1.1 cgd outint(posp->p_line);
272 1.1 cgd
273 1.1 cgd /* flags */
274 1.1 cgd
275 1.1 cgd /* both SCANFLIKE and PRINTFLIKE imply VARARGS */
276 1.33 rillig if (printflike_argnum != -1) {
277 1.33 rillig nvararg = printflike_argnum;
278 1.33 rillig } else if (scanflike_argnum != -1) {
279 1.33 rillig nvararg = scanflike_argnum;
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd if (nvararg != -1) {
283 1.1 cgd outchar('v');
284 1.1 cgd outint(nvararg);
285 1.1 cgd }
286 1.33 rillig if (scanflike_argnum != -1) {
287 1.1 cgd outchar('S');
288 1.33 rillig outint(scanflike_argnum);
289 1.1 cgd }
290 1.33 rillig if (printflike_argnum != -1) {
291 1.1 cgd outchar('P');
292 1.33 rillig outint(printflike_argnum);
293 1.1 cgd }
294 1.33 rillig nvararg = printflike_argnum = scanflike_argnum = -1;
295 1.1 cgd
296 1.1 cgd outchar('d');
297 1.1 cgd
298 1.1 cgd if (rval)
299 1.48 rillig outchar('r'); /* has return value */
300 1.1 cgd
301 1.1 cgd if (llibflg)
302 1.1 cgd /*
303 1.1 cgd * mark it as used so lint2 does not complain about
304 1.1 cgd * unused symbols in libraries
305 1.1 cgd */
306 1.1 cgd outchar('u');
307 1.1 cgd
308 1.1 cgd if (osdef)
309 1.48 rillig outchar('o'); /* old style function definition */
310 1.1 cgd
311 1.20 christos if (fsym->s_inline)
312 1.20 christos outchar('i');
313 1.20 christos
314 1.1 cgd if (fsym->s_scl == STATIC)
315 1.1 cgd outchar('s');
316 1.1 cgd
317 1.1 cgd /* name of function */
318 1.1 cgd outname(fsym->s_name);
319 1.6 cgd
320 1.6 cgd /* renamed name of function, if necessary */
321 1.37 rillig if (fsym->s_rename != NULL) {
322 1.6 cgd outchar('r');
323 1.6 cgd outname(fsym->s_rename);
324 1.6 cgd }
325 1.1 cgd
326 1.1 cgd /* argument types and return value */
327 1.1 cgd if (osdef) {
328 1.1 cgd narg = 0;
329 1.26 rillig for (arg = args; arg != NULL; arg = arg->s_next)
330 1.1 cgd narg++;
331 1.1 cgd outchar('f');
332 1.1 cgd outint(narg);
333 1.26 rillig for (arg = args; arg != NULL; arg = arg->s_next)
334 1.1 cgd outtype(arg->s_type);
335 1.4 jpo outtype(fsym->s_type->t_subt);
336 1.1 cgd } else {
337 1.4 jpo outtype(fsym->s_type);
338 1.1 cgd }
339 1.1 cgd }
340 1.1 cgd
341 1.1 cgd /*
342 1.1 cgd * write out all information necessary for lint2 to check function
343 1.1 cgd * calls
344 1.1 cgd *
345 1.34 rillig * rvused is set if the return value is used (assigned to a variable)
346 1.1 cgd * rvdisc is set if the return value is not used and not ignored
347 1.1 cgd * (casted to void)
348 1.1 cgd */
349 1.1 cgd void
350 1.37 rillig outcall(const tnode_t *tn, bool rvused, bool rvdisc)
351 1.1 cgd {
352 1.1 cgd tnode_t *args, *arg;
353 1.1 cgd int narg, n, i;
354 1.12 thorpej int64_t q;
355 1.1 cgd tspec_t t;
356 1.1 cgd
357 1.1 cgd /* reset buffer */
358 1.1 cgd outclr();
359 1.1 cgd
360 1.1 cgd /*
361 1.1 cgd * line number of .c source, 'c' for function call, Id of current
362 1.1 cgd * source (.c or .h), and line in current source
363 1.1 cgd */
364 1.1 cgd outint(csrc_pos.p_line);
365 1.1 cgd outchar('c');
366 1.43 rillig outint(get_filename_id(curr_pos.p_file));
367 1.1 cgd outchar('.');
368 1.1 cgd outint(curr_pos.p_line);
369 1.1 cgd
370 1.1 cgd /*
371 1.1 cgd * flags; 'u' and 'i' must be last to make sure a letter
372 1.1 cgd * is between the numeric argument of a flag and the name of
373 1.1 cgd * the function
374 1.1 cgd */
375 1.1 cgd narg = 0;
376 1.1 cgd args = tn->tn_right;
377 1.1 cgd for (arg = args; arg != NULL; arg = arg->tn_right)
378 1.1 cgd narg++;
379 1.25 rillig /* information about arguments */
380 1.1 cgd for (n = 1; n <= narg; n++) {
381 1.1 cgd /* the last argument is the top one in the tree */
382 1.10 lukem for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
383 1.10 lukem continue;
384 1.1 cgd arg = arg->tn_left;
385 1.1 cgd if (arg->tn_op == CON) {
386 1.35 rillig if (is_integer(t = arg->tn_type->t_tspec)) {
387 1.1 cgd /*
388 1.1 cgd * XXX it would probably be better to
389 1.16 perry * explicitly test the sign
390 1.1 cgd */
391 1.1 cgd if ((q = arg->tn_val->v_quad) == 0) {
392 1.1 cgd /* zero constant */
393 1.1 cgd outchar('z');
394 1.50 rillig } else if (!msb(q, t)) {
395 1.49 rillig /* positive if cast to signed */
396 1.1 cgd outchar('p');
397 1.1 cgd } else {
398 1.49 rillig /* negative if cast to signed */
399 1.1 cgd outchar('n');
400 1.1 cgd }
401 1.1 cgd outint(n);
402 1.1 cgd }
403 1.39 rillig } else if (arg->tn_op == ADDR &&
404 1.1 cgd arg->tn_left->tn_op == STRING &&
405 1.28 rillig arg->tn_left->tn_string->st_tspec == CHAR) {
406 1.1 cgd /* constant string, write all format specifiers */
407 1.1 cgd outchar('s');
408 1.1 cgd outint(n);
409 1.28 rillig outfstrg(arg->tn_left->tn_string);
410 1.1 cgd }
411 1.1 cgd
412 1.1 cgd }
413 1.1 cgd /* return value discarded/used/ignored */
414 1.55 rillig outchar((char)(rvdisc ? 'd' : (rvused ? 'u' : 'i')));
415 1.1 cgd
416 1.1 cgd /* name of the called function */
417 1.1 cgd outname(tn->tn_left->tn_left->tn_sym->s_name);
418 1.1 cgd
419 1.1 cgd /* types of arguments */
420 1.1 cgd outchar('f');
421 1.1 cgd outint(narg);
422 1.1 cgd for (n = 1; n <= narg; n++) {
423 1.1 cgd /* the last argument is the top one in the tree */
424 1.10 lukem for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
425 1.10 lukem continue;
426 1.1 cgd outtype(arg->tn_left->tn_type);
427 1.1 cgd }
428 1.1 cgd /* expected type of return value */
429 1.1 cgd outtype(tn->tn_type);
430 1.1 cgd }
431 1.1 cgd
432 1.56 rillig /* write a character to the output buffer, quoted if necessary */
433 1.56 rillig static void
434 1.56 rillig outqchar(char c)
435 1.56 rillig {
436 1.56 rillig
437 1.56 rillig if (ch_isprint(c) && c != '\\' && c != '"' && c != '\'') {
438 1.56 rillig outchar(c);
439 1.56 rillig return;
440 1.56 rillig }
441 1.56 rillig
442 1.56 rillig outchar('\\');
443 1.56 rillig switch (c) {
444 1.56 rillig case '\\':
445 1.56 rillig outchar('\\');
446 1.56 rillig break;
447 1.56 rillig case '"':
448 1.56 rillig outchar('"');
449 1.56 rillig break;
450 1.56 rillig case '\'':
451 1.56 rillig outchar('\'');
452 1.56 rillig break;
453 1.56 rillig case '\b':
454 1.56 rillig outchar('b');
455 1.56 rillig break;
456 1.56 rillig case '\t':
457 1.56 rillig outchar('t');
458 1.56 rillig break;
459 1.56 rillig case '\n':
460 1.56 rillig outchar('n');
461 1.56 rillig break;
462 1.56 rillig case '\f':
463 1.56 rillig outchar('f');
464 1.56 rillig break;
465 1.56 rillig case '\r':
466 1.56 rillig outchar('r');
467 1.56 rillig break;
468 1.56 rillig case '\v':
469 1.56 rillig outchar('v');
470 1.56 rillig break;
471 1.56 rillig case '\a':
472 1.56 rillig outchar('a');
473 1.56 rillig break;
474 1.56 rillig default:
475 1.56 rillig outchar((char)((((unsigned char)c >> 6) & 07) + '0'));
476 1.56 rillig outchar((char)((((unsigned char)c >> 3) & 07) + '0'));
477 1.56 rillig outchar((char)((c & 07) + '0'));
478 1.56 rillig break;
479 1.56 rillig }
480 1.56 rillig }
481 1.56 rillig
482 1.1 cgd /*
483 1.1 cgd * extracts potential format specifiers for printf() and scanf() and
484 1.34 rillig * writes them, enclosed in "" and quoted if necessary, to the output buffer
485 1.1 cgd */
486 1.1 cgd static void
487 1.10 lukem outfstrg(strg_t *strg)
488 1.1 cgd {
489 1.57 rillig char c, oc;
490 1.37 rillig bool first;
491 1.57 rillig const char *cp;
492 1.1 cgd
493 1.30 rillig lint_assert(strg->st_tspec == CHAR);
494 1.1 cgd
495 1.57 rillig cp = (const char *)strg->st_cp;
496 1.1 cgd
497 1.1 cgd outchar('"');
498 1.1 cgd
499 1.1 cgd c = *cp++;
500 1.1 cgd
501 1.1 cgd while (c != '\0') {
502 1.1 cgd
503 1.1 cgd if (c != '%') {
504 1.1 cgd c = *cp++;
505 1.1 cgd continue;
506 1.1 cgd }
507 1.1 cgd
508 1.57 rillig outchar('%');
509 1.1 cgd c = *cp++;
510 1.1 cgd
511 1.1 cgd /* flags for printf and scanf and *-fieldwidth for printf */
512 1.57 rillig while (c == '-' || c == '+' || c == ' ' ||
513 1.57 rillig c == '#' || c == '0' || c == '*') {
514 1.57 rillig outchar(c);
515 1.1 cgd c = *cp++;
516 1.1 cgd }
517 1.1 cgd
518 1.1 cgd /* numeric field width */
519 1.57 rillig while (ch_isdigit(c)) {
520 1.57 rillig outchar(c);
521 1.1 cgd c = *cp++;
522 1.1 cgd }
523 1.1 cgd
524 1.1 cgd /* precision for printf */
525 1.1 cgd if (c == '.') {
526 1.57 rillig outchar(c);
527 1.57 rillig c = *cp++;
528 1.57 rillig if (c == '*') {
529 1.57 rillig outchar(c);
530 1.1 cgd c = *cp++;
531 1.1 cgd } else {
532 1.57 rillig while (ch_isdigit(c)) {
533 1.57 rillig outchar(c);
534 1.1 cgd c = *cp++;
535 1.1 cgd }
536 1.1 cgd }
537 1.1 cgd }
538 1.1 cgd
539 1.57 rillig /* h, l, L and q flags for printf and scanf */
540 1.1 cgd if (c == 'h' || c == 'l' || c == 'L' || c == 'q') {
541 1.57 rillig outchar(c);
542 1.1 cgd c = *cp++;
543 1.1 cgd }
544 1.1 cgd
545 1.1 cgd /*
546 1.57 rillig * The last character. It is always written, so we can detect
547 1.1 cgd * invalid format specifiers.
548 1.1 cgd */
549 1.1 cgd if (c != '\0') {
550 1.1 cgd outqchar(c);
551 1.1 cgd oc = c;
552 1.1 cgd c = *cp++;
553 1.1 cgd /*
554 1.1 cgd * handle [ for scanf. [-] means that a minus sign
555 1.1 cgd * was found at an undefined position.
556 1.1 cgd */
557 1.1 cgd if (oc == '[') {
558 1.1 cgd if (c == '^')
559 1.1 cgd c = *cp++;
560 1.1 cgd if (c == ']')
561 1.1 cgd c = *cp++;
562 1.37 rillig first = true;
563 1.1 cgd while (c != '\0' && c != ']') {
564 1.1 cgd if (c == '-') {
565 1.1 cgd if (!first && *cp != ']')
566 1.57 rillig outchar(c);
567 1.1 cgd }
568 1.37 rillig first = false;
569 1.1 cgd c = *cp++;
570 1.1 cgd }
571 1.1 cgd if (c == ']') {
572 1.57 rillig outchar(c);
573 1.1 cgd c = *cp++;
574 1.1 cgd }
575 1.1 cgd }
576 1.1 cgd }
577 1.1 cgd
578 1.1 cgd }
579 1.1 cgd
580 1.1 cgd outchar('"');
581 1.1 cgd }
582 1.1 cgd
583 1.1 cgd /*
584 1.1 cgd * writes a record if sym was used
585 1.1 cgd */
586 1.1 cgd void
587 1.36 rillig outusg(const sym_t *sym)
588 1.1 cgd {
589 1.1 cgd /* reset buffer */
590 1.1 cgd outclr();
591 1.1 cgd
592 1.1 cgd /*
593 1.1 cgd * line number of .c source, 'u' for used, Id of current
594 1.1 cgd * source (.c or .h), and line in current source
595 1.1 cgd */
596 1.1 cgd outint(csrc_pos.p_line);
597 1.1 cgd outchar('u');
598 1.43 rillig outint(get_filename_id(curr_pos.p_file));
599 1.1 cgd outchar('.');
600 1.1 cgd outint(curr_pos.p_line);
601 1.1 cgd
602 1.1 cgd /* necessary to delimit both numbers */
603 1.1 cgd outchar('x');
604 1.1 cgd
605 1.1 cgd outname(sym->s_name);
606 1.1 cgd }
607