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