read.c revision 1.86 1 1.86 rillig /* $NetBSD: read.c,v 1.86 2023/07/10 14:13:19 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.3 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.5 christos #include <sys/cdefs.h>
40 1.76 rillig #if defined(__RCSID)
41 1.86 rillig __RCSID("$NetBSD: read.c,v 1.86 2023/07/10 14:13:19 rillig Exp $");
42 1.1 cgd #endif
43 1.1 cgd
44 1.12 tv #include <ctype.h>
45 1.12 tv #include <limits.h>
46 1.29 rillig #include <stdarg.h>
47 1.1 cgd #include <stdio.h>
48 1.1 cgd #include <stdlib.h>
49 1.1 cgd #include <string.h>
50 1.1 cgd
51 1.1 cgd #include "lint2.h"
52 1.1 cgd
53 1.1 cgd
54 1.1 cgd /* index of current (included) source file */
55 1.1 cgd static int srcfile;
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * The array pointed to by inpfns maps the file name indices of input files
59 1.1 cgd * to the file name indices used in lint2
60 1.1 cgd */
61 1.1 cgd static short *inpfns;
62 1.1 cgd static size_t ninpfns;
63 1.1 cgd
64 1.1 cgd /*
65 1.36 rillig * The array pointed to by *fnames maps file name indices to file names.
66 1.1 cgd * Indices of type short are used instead of pointers to save memory.
67 1.1 cgd */
68 1.1 cgd const char **fnames;
69 1.20 christos static size_t *flines;
70 1.1 cgd static size_t nfnames;
71 1.1 cgd
72 1.1 cgd /*
73 1.1 cgd * Types are shared (to save memory for the types itself) and accessed
74 1.1 cgd * via indices (to save memory for references to types (indices are short)).
75 1.67 rillig * To share types, an equal type must be located fast. This is done by a
76 1.1 cgd * hash table. Access by indices is done via an array of pointers to the
77 1.1 cgd * types.
78 1.1 cgd */
79 1.1 cgd typedef struct thtab {
80 1.56 rillig const char *th_name;
81 1.56 rillig unsigned short th_idx;
82 1.56 rillig struct thtab *th_next;
83 1.1 cgd } thtab_t;
84 1.84 rillig static thtab_t *thtab[1009]; /* hash table */
85 1.1 cgd type_t **tlst; /* array for indexed access */
86 1.1 cgd static size_t tlstlen; /* length of tlst */
87 1.1 cgd
88 1.4 cgd static hte_t **renametab;
89 1.4 cgd
90 1.36 rillig /* index of current C source file (as specified at the command line) */
91 1.1 cgd static int csrcfile;
92 1.1 cgd
93 1.71 rillig static const char *readfile_line;
94 1.1 cgd
95 1.67 rillig static void inperr(const char *, ...)
96 1.79 rillig __printflike(1, 2) __attribute__((noreturn));
97 1.9 lukem static void setsrc(const char *);
98 1.9 lukem static void setfnid(int, const char *);
99 1.74 rillig static void funccall(pos_t, const char *);
100 1.74 rillig static void decldef(pos_t, const char *);
101 1.74 rillig static void usedsym(pos_t, const char *);
102 1.56 rillig static unsigned short inptype(const char *, const char **);
103 1.65 rillig static size_t gettlen(const char *, const char **);
104 1.56 rillig static unsigned short findtype(const char *, size_t, int);
105 1.56 rillig static unsigned short storetyp(type_t *, const char *, size_t, int);
106 1.69 rillig static unsigned int thash(const char *, size_t);
107 1.9 lukem static char *inpqstrg(const char *, const char **);
108 1.9 lukem static const char *inpname(const char *, const char **);
109 1.9 lukem static int getfnidx(const char *);
110 1.1 cgd
111 1.77 rillig /* Allocate zero-initialized memory that doesn't need to be freed. */
112 1.77 rillig static void *
113 1.77 rillig xalloc(size_t sz)
114 1.77 rillig {
115 1.77 rillig
116 1.77 rillig void *ptr = xmalloc(sz);
117 1.77 rillig (void)memset(ptr, 0, sz);
118 1.77 rillig return ptr;
119 1.77 rillig }
120 1.77 rillig
121 1.44 rillig static bool
122 1.44 rillig try_parse_int(const char **p, int *num)
123 1.44 rillig {
124 1.44 rillig char *end;
125 1.44 rillig
126 1.44 rillig *num = (int)strtol(*p, &end, 10);
127 1.44 rillig if (end == *p)
128 1.44 rillig return false;
129 1.44 rillig *p = end;
130 1.44 rillig return true;
131 1.44 rillig }
132 1.44 rillig
133 1.44 rillig static int
134 1.44 rillig parse_int(const char **p)
135 1.44 rillig {
136 1.44 rillig char *end;
137 1.44 rillig int n;
138 1.44 rillig
139 1.44 rillig n = (int)strtol(*p, &end, 10);
140 1.44 rillig if (end == *p)
141 1.44 rillig inperr("not a number: %s", *p);
142 1.44 rillig *p = end;
143 1.44 rillig return n;
144 1.44 rillig }
145 1.44 rillig
146 1.44 rillig static short
147 1.44 rillig parse_short(const char **p)
148 1.44 rillig {
149 1.61 rillig return (short)parse_int(p);
150 1.44 rillig }
151 1.44 rillig
152 1.66 rillig static void
153 1.73 rillig read_ln_line(const char *line)
154 1.66 rillig {
155 1.66 rillig const char *cp;
156 1.66 rillig int cline, isrc, iline;
157 1.66 rillig char rt;
158 1.66 rillig pos_t pos;
159 1.66 rillig
160 1.66 rillig cp = line;
161 1.66 rillig
162 1.66 rillig /* line number in csrcfile */
163 1.66 rillig if (!try_parse_int(&cp, &cline))
164 1.66 rillig cline = -1;
165 1.66 rillig
166 1.66 rillig /* record type */
167 1.66 rillig if (*cp == '\0')
168 1.66 rillig inperr("missing record type");
169 1.66 rillig rt = *cp++;
170 1.66 rillig
171 1.66 rillig if (rt == 'S') {
172 1.66 rillig setsrc(cp);
173 1.66 rillig return;
174 1.66 rillig }
175 1.66 rillig if (rt == 's') {
176 1.66 rillig setfnid(cline, cp);
177 1.66 rillig return;
178 1.66 rillig }
179 1.66 rillig
180 1.66 rillig /*
181 1.66 rillig * Index of (included) source file. If this index is
182 1.66 rillig * different from csrcfile, it refers to an included
183 1.66 rillig * file.
184 1.66 rillig */
185 1.66 rillig isrc = parse_int(&cp);
186 1.66 rillig isrc = inpfns[isrc];
187 1.66 rillig
188 1.66 rillig /* line number in isrc */
189 1.66 rillig if (*cp++ != '.')
190 1.66 rillig inperr("bad line number");
191 1.66 rillig iline = parse_int(&cp);
192 1.66 rillig
193 1.66 rillig pos.p_src = (unsigned short)csrcfile;
194 1.66 rillig pos.p_line = (unsigned short)cline;
195 1.66 rillig pos.p_isrc = (unsigned short)isrc;
196 1.66 rillig pos.p_iline = (unsigned short)iline;
197 1.66 rillig
198 1.66 rillig /* process rest of this record */
199 1.66 rillig switch (rt) {
200 1.66 rillig case 'c':
201 1.74 rillig funccall(pos, cp);
202 1.66 rillig break;
203 1.66 rillig case 'd':
204 1.74 rillig decldef(pos, cp);
205 1.66 rillig break;
206 1.66 rillig case 'u':
207 1.74 rillig usedsym(pos, cp);
208 1.66 rillig break;
209 1.66 rillig default:
210 1.66 rillig inperr("bad record type %c", rt);
211 1.66 rillig }
212 1.66 rillig }
213 1.66 rillig
214 1.1 cgd void
215 1.9 lukem readfile(const char *name)
216 1.1 cgd {
217 1.81 rillig FILE *inp;
218 1.81 rillig size_t len;
219 1.81 rillig char *line;
220 1.1 cgd
221 1.1 cgd if (inpfns == NULL)
222 1.42 rillig inpfns = xcalloc(ninpfns = 128, sizeof(*inpfns));
223 1.1 cgd if (fnames == NULL)
224 1.42 rillig fnames = xcalloc(nfnames = 256, sizeof(*fnames));
225 1.20 christos if (flines == NULL)
226 1.42 rillig flines = xcalloc(nfnames, sizeof(*flines));
227 1.1 cgd if (tlstlen == 0)
228 1.42 rillig tlst = xcalloc(tlstlen = 256, sizeof(*tlst));
229 1.1 cgd
230 1.68 rillig renametab = htab_new();
231 1.4 cgd
232 1.1 cgd srcfile = getfnidx(name);
233 1.1 cgd
234 1.1 cgd if ((inp = fopen(name, "r")) == NULL)
235 1.1 cgd err(1, "cannot open %s", name);
236 1.1 cgd
237 1.70 rillig while ((line = fgetln(inp, &len)) != NULL) {
238 1.72 rillig flines[srcfile]++;
239 1.72 rillig
240 1.71 rillig readfile_line = line;
241 1.70 rillig if (len == 0 || line[len - 1] != '\n')
242 1.72 rillig inperr("missing newline after '%s'", &line[len - 1]);
243 1.70 rillig line[len - 1] = '\0';
244 1.70 rillig
245 1.73 rillig read_ln_line(line);
246 1.71 rillig readfile_line = NULL;
247 1.70 rillig }
248 1.1 cgd
249 1.85 rillig hash_free(renametab);
250 1.4 cgd
251 1.37 rillig if (ferror(inp) != 0)
252 1.1 cgd err(1, "read error on %s", name);
253 1.1 cgd
254 1.1 cgd (void)fclose(inp);
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd
258 1.67 rillig static void
259 1.67 rillig inperr(const char *fmt, ...)
260 1.1 cgd {
261 1.20 christos va_list ap;
262 1.20 christos char buf[1024];
263 1.9 lukem
264 1.20 christos va_start(ap, fmt);
265 1.42 rillig (void)vsnprintf(buf, sizeof(buf), fmt, ap);
266 1.20 christos va_end(ap);
267 1.20 christos
268 1.71 rillig errx(1, "error: %s:%zu: %s (for '%s')",
269 1.71 rillig fnames[srcfile], flines[srcfile], buf, readfile_line);
270 1.1 cgd }
271 1.1 cgd
272 1.1 cgd /*
273 1.1 cgd * Set the name of the C source file of the .ln file which is
274 1.1 cgd * currently read.
275 1.1 cgd */
276 1.1 cgd static void
277 1.9 lukem setsrc(const char *cp)
278 1.1 cgd {
279 1.9 lukem
280 1.1 cgd csrcfile = getfnidx(cp);
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd /*
284 1.1 cgd * setfnid() gets as input an index as used in an input file and the
285 1.10 wiz * associated file name. If necessary, it creates a new lint2 file
286 1.1 cgd * name index for this file name and creates the mapping of the index
287 1.1 cgd * as used in the input file to the index used in lint2.
288 1.1 cgd */
289 1.1 cgd static void
290 1.9 lukem setfnid(int fid, const char *cp)
291 1.1 cgd {
292 1.9 lukem
293 1.25 christos if (fid < 0)
294 1.20 christos inperr("bad fid");
295 1.1 cgd
296 1.25 christos if ((size_t)fid >= ninpfns) {
297 1.42 rillig inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof(*inpfns));
298 1.42 rillig (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof(*inpfns));
299 1.1 cgd ninpfns *= 2;
300 1.1 cgd }
301 1.1 cgd /*
302 1.1 cgd * Should always be true because indices written in the output
303 1.1 cgd * file by lint1 are always the previous index + 1.
304 1.1 cgd */
305 1.25 christos if ((size_t)fid >= ninpfns)
306 1.78 rillig errx(1, "internal error: setfnid");
307 1.56 rillig inpfns[fid] = (unsigned short)getfnidx(cp);
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd /*
311 1.1 cgd * Process a function call record (c-record).
312 1.1 cgd */
313 1.1 cgd static void
314 1.74 rillig funccall(pos_t pos, const char *cp)
315 1.1 cgd {
316 1.1 cgd arginf_t *ai, **lai;
317 1.81 rillig char c;
318 1.81 rillig bool rused, rdisc;
319 1.81 rillig hte_t *hte;
320 1.81 rillig fcall_t *fcall;
321 1.4 cgd const char *name;
322 1.1 cgd
323 1.42 rillig fcall = xalloc(sizeof(*fcall));
324 1.74 rillig fcall->f_pos = pos;
325 1.1 cgd
326 1.1 cgd /* read flags */
327 1.37 rillig rused = rdisc = false;
328 1.1 cgd lai = &fcall->f_args;
329 1.64 rillig
330 1.64 rillig again:
331 1.64 rillig c = *cp++;
332 1.64 rillig switch (c) {
333 1.64 rillig case 'u':
334 1.64 rillig if (rused || rdisc)
335 1.64 rillig inperr("used or discovered: %c", c);
336 1.64 rillig rused = true;
337 1.64 rillig goto again;
338 1.64 rillig case 'i':
339 1.64 rillig if (rused || rdisc)
340 1.64 rillig inperr("used or discovered: %c", c);
341 1.64 rillig goto again;
342 1.64 rillig case 'd':
343 1.64 rillig if (rused || rdisc)
344 1.64 rillig inperr("used or discovered: %c", c);
345 1.64 rillig rdisc = true;
346 1.64 rillig goto again;
347 1.64 rillig case 'z':
348 1.64 rillig case 'p':
349 1.64 rillig case 'n':
350 1.64 rillig case 's':
351 1.64 rillig ai = xalloc(sizeof(*ai));
352 1.64 rillig ai->a_num = parse_int(&cp);
353 1.75 rillig if (c == 'z')
354 1.64 rillig ai->a_pcon = ai->a_zero = true;
355 1.75 rillig else if (c == 'p')
356 1.64 rillig ai->a_pcon = true;
357 1.75 rillig else if (c == 'n')
358 1.64 rillig ai->a_ncon = true;
359 1.75 rillig else {
360 1.64 rillig ai->a_fmt = true;
361 1.64 rillig ai->a_fstrg = inpqstrg(cp, &cp);
362 1.1 cgd }
363 1.64 rillig *lai = ai;
364 1.64 rillig lai = &ai->a_next;
365 1.64 rillig goto again;
366 1.64 rillig default:
367 1.64 rillig cp--;
368 1.1 cgd }
369 1.64 rillig
370 1.1 cgd fcall->f_rused = rused;
371 1.1 cgd fcall->f_rdisc = rdisc;
372 1.1 cgd
373 1.1 cgd /* read name of function */
374 1.4 cgd name = inpname(cp, &cp);
375 1.4 cgd
376 1.4 cgd /* first look it up in the renaming table, then in the normal table */
377 1.85 rillig hte = hash_search(renametab, name, false);
378 1.4 cgd if (hte != NULL)
379 1.4 cgd hte = hte->h_hte;
380 1.4 cgd else
381 1.85 rillig hte = htab_search(name, true);
382 1.37 rillig hte->h_used = true;
383 1.1 cgd
384 1.1 cgd fcall->f_type = inptype(cp, &cp);
385 1.1 cgd
386 1.1 cgd *hte->h_lcall = fcall;
387 1.33 rillig hte->h_lcall = &fcall->f_next;
388 1.1 cgd
389 1.1 cgd if (*cp != '\0')
390 1.20 christos inperr("trailing line data: %s", cp);
391 1.1 cgd }
392 1.1 cgd
393 1.63 rillig static bool
394 1.63 rillig parse_function_attribute(const char **pp, sym_t *sym, bool *used)
395 1.63 rillig {
396 1.63 rillig
397 1.63 rillig switch (*(*pp)++) {
398 1.63 rillig case 'd':
399 1.63 rillig if (sym->s_def != NODECL)
400 1.63 rillig inperr("def");
401 1.63 rillig sym->s_def = DEF;
402 1.63 rillig break;
403 1.63 rillig case 'e':
404 1.63 rillig if (sym->s_def != NODECL)
405 1.63 rillig inperr("decl");
406 1.63 rillig sym->s_def = DECL;
407 1.63 rillig break;
408 1.63 rillig case 'i':
409 1.63 rillig if (sym->s_inline)
410 1.63 rillig inperr("inline");
411 1.63 rillig sym->s_inline = true;
412 1.63 rillig break;
413 1.63 rillig case 'o':
414 1.63 rillig if (sym->s_old_style_function)
415 1.63 rillig inperr("osdef");
416 1.63 rillig sym->s_old_style_function = true;
417 1.63 rillig break;
418 1.63 rillig case 'r':
419 1.63 rillig if (sym->s_function_has_return_value)
420 1.63 rillig inperr("r");
421 1.63 rillig sym->s_function_has_return_value = true;
422 1.63 rillig break;
423 1.63 rillig case 's':
424 1.63 rillig if (sym->s_static)
425 1.63 rillig inperr("static");
426 1.63 rillig sym->s_static = true;
427 1.63 rillig break;
428 1.63 rillig case 't':
429 1.63 rillig if (sym->s_def != NODECL)
430 1.63 rillig inperr("tdef");
431 1.63 rillig sym->s_def = TDEF;
432 1.63 rillig break;
433 1.63 rillig case 'u':
434 1.63 rillig if (*used)
435 1.63 rillig inperr("used");
436 1.63 rillig *used = true;
437 1.63 rillig break;
438 1.63 rillig case 'v':
439 1.63 rillig if (sym->s_check_only_first_args)
440 1.63 rillig inperr("v");
441 1.63 rillig sym->s_check_only_first_args = true;
442 1.63 rillig sym->s_check_num_args = parse_short(pp);
443 1.63 rillig break;
444 1.63 rillig case 'P':
445 1.63 rillig if (sym->s_printflike)
446 1.63 rillig inperr("P");
447 1.63 rillig sym->s_printflike = true;
448 1.63 rillig sym->s_printflike_arg = parse_short(pp);
449 1.63 rillig break;
450 1.63 rillig case 'S':
451 1.63 rillig if (sym->s_scanflike)
452 1.63 rillig inperr("S");
453 1.63 rillig sym->s_scanflike = true;
454 1.63 rillig sym->s_scanflike_arg = parse_short(pp);
455 1.63 rillig break;
456 1.63 rillig default:
457 1.63 rillig (*pp)--;
458 1.63 rillig return false;
459 1.63 rillig }
460 1.63 rillig return true;
461 1.63 rillig }
462 1.63 rillig
463 1.1 cgd /*
464 1.1 cgd * Process a declaration or definition (d-record).
465 1.1 cgd */
466 1.1 cgd static void
467 1.74 rillig decldef(pos_t pos, const char *cp)
468 1.1 cgd {
469 1.81 rillig sym_t *symp, sym;
470 1.86 rillig char *tname;
471 1.81 rillig bool used, renamed;
472 1.81 rillig hte_t *hte, *renamehte = NULL;
473 1.24 lukem const char *name, *newname;
474 1.1 cgd
475 1.42 rillig (void)memset(&sym, 0, sizeof(sym));
476 1.74 rillig sym.s_pos = pos;
477 1.1 cgd sym.s_def = NODECL;
478 1.1 cgd
479 1.37 rillig used = false;
480 1.1 cgd
481 1.63 rillig while (parse_function_attribute(&cp, &sym, &used))
482 1.63 rillig continue;
483 1.1 cgd
484 1.4 cgd /* read symbol name, doing renaming if necessary */
485 1.4 cgd name = inpname(cp, &cp);
486 1.37 rillig renamed = false;
487 1.4 cgd if (*cp == 'r') {
488 1.4 cgd cp++;
489 1.24 lukem tname = xstrdup(name);
490 1.24 lukem newname = inpname(cp, &cp);
491 1.4 cgd
492 1.4 cgd /* enter it and see if it's already been renamed */
493 1.85 rillig renamehte = hash_search(renametab, tname, true);
494 1.4 cgd if (renamehte->h_hte == NULL) {
495 1.85 rillig hte = htab_search(newname, true);
496 1.4 cgd renamehte->h_hte = hte;
497 1.37 rillig renamed = true;
498 1.37 rillig } else if (hte = renamehte->h_hte,
499 1.37 rillig strcmp(hte->h_name, newname) != 0) {
500 1.40 rillig /* %s renamed multiple times \t%s :: %s */
501 1.86 rillig msg(18, tname, mkpos(&renamehte->h_syms->s_pos),
502 1.86 rillig mkpos(&sym.s_pos));
503 1.4 cgd }
504 1.24 lukem free(tname);
505 1.4 cgd } else {
506 1.4 cgd /* it might be a previously-done rename */
507 1.85 rillig hte = hash_search(renametab, name, false);
508 1.4 cgd if (hte != NULL)
509 1.4 cgd hte = hte->h_hte;
510 1.4 cgd else
511 1.85 rillig hte = htab_search(name, true);
512 1.4 cgd }
513 1.1 cgd hte->h_used |= used;
514 1.1 cgd if (sym.s_def == DEF || sym.s_def == TDEF)
515 1.37 rillig hte->h_def = true;
516 1.1 cgd
517 1.1 cgd sym.s_type = inptype(cp, &cp);
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Allocate memory for this symbol only if it was not already
521 1.1 cgd * declared or tentatively defined at the same location with
522 1.1 cgd * the same type. Works only for symbols with external linkage,
523 1.1 cgd * because static symbols, tentatively defined at the same location
524 1.1 cgd * but in different translation units are really different symbols.
525 1.1 cgd */
526 1.32 rillig for (symp = hte->h_syms; symp != NULL; symp = symp->s_next) {
527 1.1 cgd if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
528 1.1 cgd symp->s_pos.p_iline == sym.s_pos.p_iline &&
529 1.1 cgd symp->s_type == sym.s_type &&
530 1.1 cgd ((symp->s_def == DECL && sym.s_def == DECL) ||
531 1.1 cgd (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
532 1.75 rillig !symp->s_static && !sym.s_static)
533 1.1 cgd break;
534 1.1 cgd }
535 1.1 cgd
536 1.1 cgd if (symp == NULL) {
537 1.57 rillig if (sym.s_check_only_first_args ||
538 1.57 rillig sym.s_printflike || sym.s_scanflike) {
539 1.42 rillig symp = xalloc(sizeof(*symp));
540 1.34 rillig *symp = sym;
541 1.1 cgd } else {
542 1.57 rillig /* no need to allocate memory for unused members */
543 1.42 rillig symp = xalloc(sizeof(symp->s_s));
544 1.34 rillig symp->s_s = sym.s_s;
545 1.1 cgd }
546 1.1 cgd *hte->h_lsym = symp;
547 1.32 rillig hte->h_lsym = &symp->s_next;
548 1.4 cgd
549 1.4 cgd /* XXX hack so we can remember where a symbol was renamed */
550 1.4 cgd if (renamed)
551 1.4 cgd renamehte->h_syms = symp;
552 1.1 cgd }
553 1.1 cgd
554 1.1 cgd if (*cp != '\0')
555 1.20 christos inperr("trailing line: %s", cp);
556 1.1 cgd }
557 1.1 cgd
558 1.1 cgd /*
559 1.36 rillig * Read an u-record (emitted by lint1 if a symbol was used).
560 1.1 cgd */
561 1.1 cgd static void
562 1.74 rillig usedsym(pos_t pos, const char *cp)
563 1.1 cgd {
564 1.81 rillig usym_t *usym;
565 1.81 rillig hte_t *hte;
566 1.4 cgd const char *name;
567 1.1 cgd
568 1.42 rillig usym = xalloc(sizeof(*usym));
569 1.74 rillig usym->u_pos = pos;
570 1.1 cgd
571 1.1 cgd /* needed as delimiter between two numbers */
572 1.1 cgd if (*cp++ != 'x')
573 1.20 christos inperr("bad delim %c", cp[-1]);
574 1.1 cgd
575 1.4 cgd name = inpname(cp, &cp);
576 1.85 rillig hte = hash_search(renametab, name, false);
577 1.4 cgd if (hte != NULL)
578 1.4 cgd hte = hte->h_hte;
579 1.4 cgd else
580 1.85 rillig hte = htab_search(name, true);
581 1.37 rillig hte->h_used = true;
582 1.1 cgd
583 1.1 cgd *hte->h_lusym = usym;
584 1.33 rillig hte->h_lusym = &usym->u_next;
585 1.1 cgd }
586 1.1 cgd
587 1.50 rillig static tspec_t
588 1.50 rillig parse_tspec(const char **pp, char c, bool *osdef)
589 1.50 rillig {
590 1.50 rillig char s;
591 1.50 rillig
592 1.50 rillig switch (c) {
593 1.50 rillig case 's': /* 'signed' or 'struct' or 'float' */
594 1.50 rillig case 'u': /* 'unsigned' or 'union' */
595 1.50 rillig case 'l': /* 'long double' */
596 1.50 rillig case 'e': /* 'enum' */
597 1.50 rillig s = c;
598 1.50 rillig c = *(*pp)++;
599 1.50 rillig break;
600 1.50 rillig default:
601 1.50 rillig s = '\0';
602 1.50 rillig break;
603 1.50 rillig }
604 1.50 rillig
605 1.50 rillig switch (c) {
606 1.50 rillig case 'B':
607 1.50 rillig return BOOL;
608 1.50 rillig case 'C':
609 1.50 rillig return s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
610 1.50 rillig case 'S':
611 1.50 rillig return s == 'u' ? USHORT : SHORT;
612 1.50 rillig case 'I':
613 1.50 rillig return s == 'u' ? UINT : INT;
614 1.50 rillig case 'L':
615 1.50 rillig return s == 'u' ? ULONG : LONG;
616 1.50 rillig case 'Q':
617 1.82 rillig return s == 'u' ? ULLONG : LLONG;
618 1.50 rillig #ifdef INT128_SIZE
619 1.50 rillig case 'J':
620 1.50 rillig return s == 'u' ? UINT128 : INT128;
621 1.50 rillig #endif
622 1.50 rillig case 'D':
623 1.50 rillig return s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
624 1.50 rillig case 'V':
625 1.50 rillig return VOID;
626 1.50 rillig case 'P':
627 1.50 rillig return PTR;
628 1.50 rillig case 'A':
629 1.50 rillig return ARRAY;
630 1.50 rillig case 'F':
631 1.50 rillig case 'f':
632 1.50 rillig *osdef = c == 'f';
633 1.50 rillig return FUNC;
634 1.50 rillig case 'T':
635 1.50 rillig return s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
636 1.50 rillig case 'X':
637 1.50 rillig return s == 's' ? FCOMPLEX
638 1.50 rillig : (s == 'l' ? LCOMPLEX : DCOMPLEX);
639 1.50 rillig default:
640 1.50 rillig inperr("tspec '%c'", c);
641 1.65 rillig /* NOTREACHED */
642 1.50 rillig }
643 1.50 rillig }
644 1.50 rillig
645 1.1 cgd /*
646 1.1 cgd * Read a type and return the index of this type.
647 1.1 cgd */
648 1.56 rillig static unsigned short
649 1.9 lukem inptype(const char *cp, const char **epp)
650 1.1 cgd {
651 1.81 rillig char c;
652 1.81 rillig const char *ep;
653 1.81 rillig type_t *tp;
654 1.81 rillig int narg, i;
655 1.81 rillig bool osdef = false;
656 1.81 rillig size_t tlen;
657 1.56 rillig unsigned short tidx;
658 1.81 rillig int h;
659 1.1 cgd
660 1.26 snj /* If we have this type already, return its index. */
661 1.1 cgd tlen = gettlen(cp, &ep);
662 1.1 cgd h = thash(cp, tlen);
663 1.1 cgd if ((tidx = findtype(cp, tlen, h)) != 0) {
664 1.1 cgd *epp = ep;
665 1.30 rillig return tidx;
666 1.1 cgd }
667 1.1 cgd
668 1.1 cgd /* No, we must create a new type. */
669 1.42 rillig tp = xalloc(sizeof(*tp));
670 1.1 cgd
671 1.1 cgd tidx = storetyp(tp, cp, tlen, h);
672 1.1 cgd
673 1.1 cgd c = *cp++;
674 1.1 cgd
675 1.53 rillig if (c == 'c') {
676 1.53 rillig tp->t_const = true;
677 1.53 rillig c = *cp++;
678 1.53 rillig }
679 1.53 rillig if (c == 'v') {
680 1.53 rillig tp->t_volatile = true;
681 1.1 cgd c = *cp++;
682 1.1 cgd }
683 1.1 cgd
684 1.50 rillig tp->t_tspec = parse_tspec(&cp, c, &osdef);
685 1.1 cgd
686 1.1 cgd switch (tp->t_tspec) {
687 1.1 cgd case ARRAY:
688 1.44 rillig tp->t_dim = parse_int(&cp);
689 1.51 rillig tp->t_subt = TP(inptype(cp, &cp));
690 1.1 cgd break;
691 1.1 cgd case PTR:
692 1.51 rillig tp->t_subt = TP(inptype(cp, &cp));
693 1.1 cgd break;
694 1.1 cgd case FUNC:
695 1.1 cgd c = *cp;
696 1.37 rillig if (ch_isdigit(c)) {
697 1.1 cgd if (!osdef)
698 1.37 rillig tp->t_proto = true;
699 1.44 rillig narg = parse_int(&cp);
700 1.51 rillig tp->t_args = xcalloc((size_t)narg + 1,
701 1.42 rillig sizeof(*tp->t_args));
702 1.1 cgd for (i = 0; i < narg; i++) {
703 1.1 cgd if (i == narg - 1 && *cp == 'E') {
704 1.37 rillig tp->t_vararg = true;
705 1.1 cgd cp++;
706 1.75 rillig } else
707 1.51 rillig tp->t_args[i] = TP(inptype(cp, &cp));
708 1.1 cgd }
709 1.1 cgd }
710 1.51 rillig tp->t_subt = TP(inptype(cp, &cp));
711 1.1 cgd break;
712 1.1 cgd case ENUM:
713 1.1 cgd tp->t_tspec = INT;
714 1.39 rillig tp->t_is_enum = true;
715 1.1 cgd /* FALLTHROUGH */
716 1.1 cgd case STRUCT:
717 1.1 cgd case UNION:
718 1.1 cgd switch (*cp++) {
719 1.1 cgd case '1':
720 1.37 rillig tp->t_istag = true;
721 1.85 rillig tp->t_tag = htab_search(inpname(cp, &cp), true);
722 1.1 cgd break;
723 1.1 cgd case '2':
724 1.37 rillig tp->t_istynam = true;
725 1.85 rillig tp->t_tynam = htab_search(inpname(cp, &cp), true);
726 1.1 cgd break;
727 1.3 cgd case '3':
728 1.37 rillig tp->t_isuniqpos = true;
729 1.44 rillig tp->t_uniqpos.p_line = parse_int(&cp);
730 1.3 cgd cp++;
731 1.3 cgd /* xlate to 'global' file name. */
732 1.83 rillig tp->t_uniqpos.p_file = (short)
733 1.44 rillig addoutfile(inpfns[parse_int(&cp)]);
734 1.3 cgd cp++;
735 1.44 rillig tp->t_uniqpos.p_uniq = parse_int(&cp);
736 1.3 cgd break;
737 1.1 cgd }
738 1.1 cgd break;
739 1.52 rillig default:
740 1.5 christos break;
741 1.1 cgd }
742 1.1 cgd
743 1.1 cgd *epp = cp;
744 1.30 rillig return tidx;
745 1.1 cgd }
746 1.1 cgd
747 1.1 cgd /*
748 1.1 cgd * Get the length of a type string.
749 1.1 cgd */
750 1.65 rillig static size_t
751 1.9 lukem gettlen(const char *cp, const char **epp)
752 1.1 cgd {
753 1.81 rillig const char *cp1;
754 1.81 rillig char c, s;
755 1.81 rillig tspec_t t;
756 1.81 rillig int narg, i;
757 1.1 cgd
758 1.1 cgd cp1 = cp;
759 1.1 cgd
760 1.1 cgd c = *cp++;
761 1.1 cgd
762 1.53 rillig if (c == 'c')
763 1.53 rillig c = *cp++;
764 1.53 rillig if (c == 'v')
765 1.1 cgd c = *cp++;
766 1.1 cgd
767 1.22 christos switch (c) {
768 1.22 christos case 's':
769 1.22 christos case 'u':
770 1.22 christos case 'l':
771 1.22 christos case 'e':
772 1.1 cgd s = c;
773 1.1 cgd c = *cp++;
774 1.22 christos break;
775 1.22 christos default:
776 1.1 cgd s = '\0';
777 1.22 christos break;
778 1.1 cgd }
779 1.1 cgd
780 1.80 rillig t = NO_TSPEC;
781 1.1 cgd
782 1.1 cgd switch (c) {
783 1.15 yamt case 'B':
784 1.15 yamt if (s == '\0')
785 1.15 yamt t = BOOL;
786 1.15 yamt break;
787 1.1 cgd case 'C':
788 1.75 rillig if (s == 's')
789 1.1 cgd t = SCHAR;
790 1.75 rillig else if (s == 'u')
791 1.1 cgd t = UCHAR;
792 1.75 rillig else if (s == '\0')
793 1.1 cgd t = CHAR;
794 1.1 cgd break;
795 1.1 cgd case 'S':
796 1.75 rillig if (s == 'u')
797 1.1 cgd t = USHORT;
798 1.75 rillig else if (s == '\0')
799 1.1 cgd t = SHORT;
800 1.1 cgd break;
801 1.1 cgd case 'I':
802 1.75 rillig if (s == 'u')
803 1.1 cgd t = UINT;
804 1.75 rillig else if (s == '\0')
805 1.1 cgd t = INT;
806 1.1 cgd break;
807 1.1 cgd case 'L':
808 1.75 rillig if (s == 'u')
809 1.1 cgd t = ULONG;
810 1.75 rillig else if (s == '\0')
811 1.1 cgd t = LONG;
812 1.1 cgd break;
813 1.1 cgd case 'Q':
814 1.75 rillig if (s == 'u')
815 1.82 rillig t = ULLONG;
816 1.75 rillig else if (s == '\0')
817 1.82 rillig t = LLONG;
818 1.1 cgd break;
819 1.47 rillig #ifdef INT128_SIZE
820 1.47 rillig case 'J':
821 1.75 rillig if (s == 'u')
822 1.47 rillig t = UINT128;
823 1.75 rillig else if (s == '\0')
824 1.47 rillig t = INT128;
825 1.47 rillig break;
826 1.47 rillig #endif
827 1.1 cgd case 'D':
828 1.75 rillig if (s == 's')
829 1.1 cgd t = FLOAT;
830 1.75 rillig else if (s == 'l')
831 1.1 cgd t = LDOUBLE;
832 1.75 rillig else if (s == '\0')
833 1.1 cgd t = DOUBLE;
834 1.1 cgd break;
835 1.1 cgd case 'V':
836 1.1 cgd if (s == '\0')
837 1.1 cgd t = VOID;
838 1.1 cgd break;
839 1.1 cgd case 'P':
840 1.1 cgd if (s == '\0')
841 1.1 cgd t = PTR;
842 1.1 cgd break;
843 1.1 cgd case 'A':
844 1.1 cgd if (s == '\0')
845 1.1 cgd t = ARRAY;
846 1.1 cgd break;
847 1.1 cgd case 'F':
848 1.1 cgd case 'f':
849 1.1 cgd if (s == '\0')
850 1.1 cgd t = FUNC;
851 1.1 cgd break;
852 1.1 cgd case 'T':
853 1.75 rillig if (s == 'e')
854 1.1 cgd t = ENUM;
855 1.75 rillig else if (s == 's')
856 1.1 cgd t = STRUCT;
857 1.75 rillig else if (s == 'u')
858 1.1 cgd t = UNION;
859 1.1 cgd break;
860 1.20 christos case 'X':
861 1.75 rillig if (s == 's')
862 1.22 christos t = FCOMPLEX;
863 1.75 rillig else if (s == 'l')
864 1.23 matt t = LCOMPLEX;
865 1.75 rillig else if (s == '\0')
866 1.20 christos t = DCOMPLEX;
867 1.20 christos break;
868 1.1 cgd default:
869 1.52 rillig break;
870 1.1 cgd }
871 1.1 cgd
872 1.80 rillig if (t == NO_TSPEC)
873 1.52 rillig inperr("bad type: %c %c", c, s);
874 1.1 cgd
875 1.1 cgd switch (t) {
876 1.1 cgd case ARRAY:
877 1.44 rillig (void)parse_int(&cp);
878 1.1 cgd (void)gettlen(cp, &cp);
879 1.1 cgd break;
880 1.1 cgd case PTR:
881 1.1 cgd (void)gettlen(cp, &cp);
882 1.1 cgd break;
883 1.1 cgd case FUNC:
884 1.1 cgd c = *cp;
885 1.37 rillig if (ch_isdigit(c)) {
886 1.44 rillig narg = parse_int(&cp);
887 1.1 cgd for (i = 0; i < narg; i++) {
888 1.75 rillig if (i == narg - 1 && *cp == 'E')
889 1.1 cgd cp++;
890 1.75 rillig else
891 1.1 cgd (void)gettlen(cp, &cp);
892 1.1 cgd }
893 1.1 cgd }
894 1.1 cgd (void)gettlen(cp, &cp);
895 1.1 cgd break;
896 1.1 cgd case ENUM:
897 1.1 cgd case STRUCT:
898 1.1 cgd case UNION:
899 1.1 cgd switch (*cp++) {
900 1.1 cgd case '1':
901 1.1 cgd case '2':
902 1.1 cgd (void)inpname(cp, &cp);
903 1.3 cgd break;
904 1.3 cgd case '3':
905 1.3 cgd /* unique position: line.file.uniquifier */
906 1.44 rillig (void)parse_int(&cp);
907 1.3 cgd if (*cp++ != '.')
908 1.20 christos inperr("not dot: %c", cp[-1]);
909 1.44 rillig (void)parse_int(&cp);
910 1.3 cgd if (*cp++ != '.')
911 1.20 christos inperr("not dot: %c", cp[-1]);
912 1.44 rillig (void)parse_int(&cp);
913 1.1 cgd break;
914 1.1 cgd default:
915 1.55 rillig inperr("bad value: %c", cp[-1]);
916 1.1 cgd }
917 1.1 cgd break;
918 1.52 rillig default:
919 1.5 christos break;
920 1.1 cgd }
921 1.1 cgd
922 1.1 cgd *epp = cp;
923 1.65 rillig return (size_t)(cp - cp1);
924 1.1 cgd }
925 1.1 cgd
926 1.1 cgd /*
927 1.26 snj * Search a type by its type string.
928 1.1 cgd */
929 1.56 rillig static unsigned short
930 1.9 lukem findtype(const char *cp, size_t len, int h)
931 1.1 cgd {
932 1.81 rillig thtab_t *thte;
933 1.1 cgd
934 1.33 rillig for (thte = thtab[h]; thte != NULL; thte = thte->th_next) {
935 1.1 cgd if (strncmp(thte->th_name, cp, len) != 0)
936 1.1 cgd continue;
937 1.1 cgd if (thte->th_name[len] == '\0')
938 1.30 rillig return thte->th_idx;
939 1.1 cgd }
940 1.1 cgd
941 1.30 rillig return 0;
942 1.1 cgd }
943 1.1 cgd
944 1.1 cgd /*
945 1.51 rillig * Store a type and its type string, so we can later share this type
946 1.1 cgd * if we read the same type string from the input file.
947 1.1 cgd */
948 1.56 rillig static unsigned short
949 1.9 lukem storetyp(type_t *tp, const char *cp, size_t len, int h)
950 1.1 cgd {
951 1.56 rillig static unsigned int tidx = 1; /* 0 is reserved */
952 1.81 rillig thtab_t *thte;
953 1.81 rillig char *name;
954 1.1 cgd
955 1.1 cgd if (tidx >= USHRT_MAX)
956 1.1 cgd errx(1, "sorry, too many types");
957 1.1 cgd
958 1.1 cgd if (tidx == tlstlen - 1) {
959 1.42 rillig tlst = xrealloc(tlst, (tlstlen * 2) * sizeof(*tlst));
960 1.42 rillig (void)memset(tlst + tlstlen, 0, tlstlen * sizeof(*tlst));
961 1.1 cgd tlstlen *= 2;
962 1.1 cgd }
963 1.1 cgd
964 1.1 cgd tlst[tidx] = tp;
965 1.1 cgd
966 1.1 cgd /* create a hash table entry */
967 1.1 cgd name = xalloc(len + 1);
968 1.1 cgd (void)memcpy(name, cp, len);
969 1.1 cgd name[len] = '\0';
970 1.1 cgd
971 1.42 rillig thte = xalloc(sizeof(*thte));
972 1.1 cgd thte->th_name = name;
973 1.83 rillig thte->th_idx = (unsigned short)tidx;
974 1.33 rillig thte->th_next = thtab[h];
975 1.1 cgd thtab[h] = thte;
976 1.1 cgd
977 1.56 rillig return (unsigned short)tidx++;
978 1.1 cgd }
979 1.1 cgd
980 1.1 cgd /*
981 1.1 cgd * Hash function for types
982 1.1 cgd */
983 1.69 rillig static unsigned int
984 1.9 lukem thash(const char *s, size_t len)
985 1.1 cgd {
986 1.56 rillig unsigned int v;
987 1.1 cgd
988 1.1 cgd v = 0;
989 1.1 cgd while (len-- != 0) {
990 1.56 rillig v = (v << sizeof(v)) + (unsigned char)*s++;
991 1.42 rillig v ^= v >> (sizeof(v) * CHAR_BIT - sizeof(v));
992 1.1 cgd }
993 1.84 rillig return v % (sizeof(thtab) / sizeof(thtab[0]));
994 1.1 cgd }
995 1.1 cgd
996 1.1 cgd /*
997 1.1 cgd * Read a string enclosed by "". This string may contain quoted chars.
998 1.1 cgd */
999 1.1 cgd static char *
1000 1.9 lukem inpqstrg(const char *src, const char **epp)
1001 1.1 cgd {
1002 1.81 rillig char *strg, *dst;
1003 1.81 rillig size_t slen;
1004 1.81 rillig char c;
1005 1.81 rillig int v;
1006 1.1 cgd
1007 1.1 cgd dst = strg = xmalloc(slen = 32);
1008 1.1 cgd
1009 1.1 cgd if ((c = *src++) != '"')
1010 1.20 christos inperr("not quote: %c", c);
1011 1.1 cgd if ((c = *src++) == '\0')
1012 1.20 christos inperr("trailing data: %c", c);
1013 1.1 cgd
1014 1.1 cgd while (c != '"') {
1015 1.1 cgd if (c == '\\') {
1016 1.1 cgd if ((c = *src++) == '\0')
1017 1.20 christos inperr("missing after \\");
1018 1.1 cgd switch (c) {
1019 1.1 cgd case 'n':
1020 1.1 cgd c = '\n';
1021 1.1 cgd break;
1022 1.1 cgd case 't':
1023 1.1 cgd c = '\t';
1024 1.1 cgd break;
1025 1.1 cgd case 'v':
1026 1.1 cgd c = '\v';
1027 1.1 cgd break;
1028 1.1 cgd case 'b':
1029 1.1 cgd c = '\b';
1030 1.1 cgd break;
1031 1.1 cgd case 'r':
1032 1.1 cgd c = '\r';
1033 1.1 cgd break;
1034 1.1 cgd case 'f':
1035 1.1 cgd c = '\f';
1036 1.1 cgd break;
1037 1.1 cgd case 'a':
1038 1.1 cgd c = '\a';
1039 1.1 cgd break;
1040 1.1 cgd case '\\':
1041 1.1 cgd c = '\\';
1042 1.1 cgd break;
1043 1.1 cgd case '"':
1044 1.1 cgd c = '"';
1045 1.1 cgd break;
1046 1.1 cgd case '\'':
1047 1.1 cgd c = '\'';
1048 1.1 cgd break;
1049 1.1 cgd case '0': case '1': case '2': case '3':
1050 1.1 cgd v = (c - '0') << 6;
1051 1.1 cgd if ((c = *src++) < '0' || c > '7')
1052 1.20 christos inperr("not octal: %c", c);
1053 1.1 cgd v |= (c - '0') << 3;
1054 1.1 cgd if ((c = *src++) < '0' || c > '7')
1055 1.20 christos inperr("not octal: %c", c);
1056 1.1 cgd v |= c - '0';
1057 1.48 rillig c = (char)v;
1058 1.1 cgd break;
1059 1.1 cgd default:
1060 1.20 christos inperr("bad \\ escape: %c", c);
1061 1.1 cgd }
1062 1.1 cgd }
1063 1.1 cgd /* keep space for trailing '\0' */
1064 1.25 christos if ((size_t)(dst - strg) == slen - 1) {
1065 1.1 cgd strg = xrealloc(strg, slen * 2);
1066 1.1 cgd dst = strg + (slen - 1);
1067 1.1 cgd slen *= 2;
1068 1.1 cgd }
1069 1.48 rillig *dst++ = c;
1070 1.1 cgd if ((c = *src++) == '\0')
1071 1.20 christos inperr("missing closing quote");
1072 1.1 cgd }
1073 1.1 cgd *dst = '\0';
1074 1.1 cgd
1075 1.1 cgd *epp = src;
1076 1.30 rillig return strg;
1077 1.1 cgd }
1078 1.1 cgd
1079 1.1 cgd /*
1080 1.1 cgd * Read the name of a symbol in static memory.
1081 1.1 cgd */
1082 1.1 cgd static const char *
1083 1.9 lukem inpname(const char *cp, const char **epp)
1084 1.1 cgd {
1085 1.81 rillig static char *buf;
1086 1.81 rillig static size_t blen = 0;
1087 1.81 rillig size_t len, i;
1088 1.81 rillig char c;
1089 1.1 cgd
1090 1.44 rillig len = parse_int(&cp);
1091 1.1 cgd if (len + 1 > blen)
1092 1.1 cgd buf = xrealloc(buf, blen = len + 1);
1093 1.1 cgd for (i = 0; i < len; i++) {
1094 1.1 cgd c = *cp++;
1095 1.37 rillig if (!ch_isalnum(c) && c != '_')
1096 1.20 christos inperr("not alnum or _: %c", c);
1097 1.1 cgd buf[i] = c;
1098 1.1 cgd }
1099 1.1 cgd buf[i] = '\0';
1100 1.1 cgd
1101 1.1 cgd *epp = cp;
1102 1.30 rillig return buf;
1103 1.1 cgd }
1104 1.1 cgd
1105 1.1 cgd /*
1106 1.1 cgd * Return the index of a file name. If the name cannot be found, create
1107 1.1 cgd * a new entry and return the index of the newly created entry.
1108 1.1 cgd */
1109 1.1 cgd static int
1110 1.9 lukem getfnidx(const char *fn)
1111 1.1 cgd {
1112 1.81 rillig size_t i;
1113 1.1 cgd
1114 1.21 christos /* 0 is reserved */
1115 1.1 cgd for (i = 1; fnames[i] != NULL; i++) {
1116 1.1 cgd if (strcmp(fnames[i], fn) == 0)
1117 1.65 rillig return (int)i;
1118 1.1 cgd }
1119 1.1 cgd
1120 1.1 cgd if (i == nfnames - 1) {
1121 1.21 christos size_t nlen = nfnames * 2;
1122 1.42 rillig fnames = xrealloc(fnames, nlen * sizeof(*fnames));
1123 1.42 rillig (void)memset(fnames + nfnames, 0, nfnames * sizeof(*fnames));
1124 1.42 rillig flines = xrealloc(flines, nlen * sizeof(*flines));
1125 1.42 rillig (void)memset(flines + nfnames, 0, nfnames * sizeof(*flines));
1126 1.21 christos nfnames = nlen;
1127 1.1 cgd }
1128 1.1 cgd
1129 1.1 cgd fnames[i] = xstrdup(fn);
1130 1.20 christos flines[i] = 0;
1131 1.65 rillig return (int)i;
1132 1.1 cgd }
1133 1.1 cgd
1134 1.1 cgd /*
1135 1.1 cgd * Separate symbols with static and external linkage.
1136 1.1 cgd */
1137 1.1 cgd void
1138 1.9 lukem mkstatic(hte_t *hte)
1139 1.1 cgd {
1140 1.81 rillig sym_t *sym1, **symp, *sym;
1141 1.81 rillig fcall_t **callp, *call;
1142 1.81 rillig usym_t **usymp, *usym;
1143 1.81 rillig hte_t *nhte;
1144 1.81 rillig bool ofnd;
1145 1.1 cgd
1146 1.1 cgd /* Look for first static definition */
1147 1.32 rillig for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_next) {
1148 1.1 cgd if (sym1->s_static)
1149 1.1 cgd break;
1150 1.1 cgd }
1151 1.1 cgd if (sym1 == NULL)
1152 1.1 cgd return;
1153 1.1 cgd
1154 1.1 cgd /* Do nothing if this name is used only in one translation unit. */
1155 1.37 rillig ofnd = false;
1156 1.32 rillig for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_next) {
1157 1.1 cgd if (sym->s_pos.p_src != sym1->s_pos.p_src)
1158 1.37 rillig ofnd = true;
1159 1.1 cgd }
1160 1.33 rillig for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_next) {
1161 1.1 cgd if (call->f_pos.p_src != sym1->s_pos.p_src)
1162 1.37 rillig ofnd = true;
1163 1.1 cgd }
1164 1.33 rillig for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_next) {
1165 1.1 cgd if (usym->u_pos.p_src != sym1->s_pos.p_src)
1166 1.37 rillig ofnd = true;
1167 1.1 cgd }
1168 1.1 cgd if (!ofnd) {
1169 1.37 rillig hte->h_used = true;
1170 1.1 cgd /* errors about undef. static symbols are printed in lint1 */
1171 1.37 rillig hte->h_def = true;
1172 1.37 rillig hte->h_static = true;
1173 1.1 cgd return;
1174 1.1 cgd }
1175 1.1 cgd
1176 1.1 cgd /*
1177 1.1 cgd * Create a new hash table entry
1178 1.1 cgd *
1179 1.1 cgd * XXX this entry should be put at the beginning of the list to
1180 1.54 rillig * avoid processing the same symbol twice.
1181 1.1 cgd */
1182 1.9 lukem for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1183 1.9 lukem continue;
1184 1.42 rillig nhte->h_link = xmalloc(sizeof(*nhte->h_link));
1185 1.1 cgd nhte = nhte->h_link;
1186 1.1 cgd nhte->h_name = hte->h_name;
1187 1.37 rillig nhte->h_used = true;
1188 1.37 rillig nhte->h_def = true; /* error in lint1 */
1189 1.37 rillig nhte->h_static = true;
1190 1.6 mycroft nhte->h_syms = NULL;
1191 1.1 cgd nhte->h_lsym = &nhte->h_syms;
1192 1.6 mycroft nhte->h_calls = NULL;
1193 1.1 cgd nhte->h_lcall = &nhte->h_calls;
1194 1.6 mycroft nhte->h_usyms = NULL;
1195 1.1 cgd nhte->h_lusym = &nhte->h_usyms;
1196 1.6 mycroft nhte->h_link = NULL;
1197 1.6 mycroft nhte->h_hte = NULL;
1198 1.1 cgd
1199 1.1 cgd /*
1200 1.1 cgd * move all symbols used in this translation unit into the new
1201 1.1 cgd * hash table entry.
1202 1.1 cgd */
1203 1.1 cgd for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1204 1.1 cgd if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1205 1.37 rillig sym->s_static = true;
1206 1.69 rillig *symp = sym->s_next;
1207 1.32 rillig if (hte->h_lsym == &sym->s_next)
1208 1.1 cgd hte->h_lsym = symp;
1209 1.32 rillig sym->s_next = NULL;
1210 1.1 cgd *nhte->h_lsym = sym;
1211 1.32 rillig nhte->h_lsym = &sym->s_next;
1212 1.1 cgd } else {
1213 1.32 rillig symp = &sym->s_next;
1214 1.1 cgd }
1215 1.1 cgd }
1216 1.1 cgd for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1217 1.1 cgd if (call->f_pos.p_src == sym1->s_pos.p_src) {
1218 1.69 rillig *callp = call->f_next;
1219 1.33 rillig if (hte->h_lcall == &call->f_next)
1220 1.1 cgd hte->h_lcall = callp;
1221 1.33 rillig call->f_next = NULL;
1222 1.1 cgd *nhte->h_lcall = call;
1223 1.33 rillig nhte->h_lcall = &call->f_next;
1224 1.1 cgd } else {
1225 1.33 rillig callp = &call->f_next;
1226 1.1 cgd }
1227 1.1 cgd }
1228 1.1 cgd for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1229 1.1 cgd if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1230 1.69 rillig *usymp = usym->u_next;
1231 1.33 rillig if (hte->h_lusym == &usym->u_next)
1232 1.1 cgd hte->h_lusym = usymp;
1233 1.33 rillig usym->u_next = NULL;
1234 1.1 cgd *nhte->h_lusym = usym;
1235 1.33 rillig nhte->h_lusym = &usym->u_next;
1236 1.1 cgd } else {
1237 1.33 rillig usymp = &usym->u_next;
1238 1.1 cgd }
1239 1.1 cgd }
1240 1.1 cgd
1241 1.1 cgd /* h_def must be recalculated for old hte */
1242 1.37 rillig hte->h_def = nhte->h_def = false;
1243 1.32 rillig for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) {
1244 1.1 cgd if (sym->s_def == DEF || sym->s_def == TDEF) {
1245 1.37 rillig hte->h_def = true;
1246 1.1 cgd break;
1247 1.1 cgd }
1248 1.1 cgd }
1249 1.1 cgd
1250 1.1 cgd mkstatic(hte);
1251 1.1 cgd }
1252