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