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