xref: /netbsd-src/usr.bin/xlint/lint1/init.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: init.c,v 1.21 2006/10/15 18:18:54 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Jochen Pohl for
18  *	The NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37 
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: init.c,v 1.21 2006/10/15 18:18:54 christos Exp $");
41 #endif
42 
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 
47 #include "lint1.h"
48 
49 /*
50  * initerr is set as soon as a fatal error occurred in an initialisation.
51  * The effect is that the rest of the initialisation is ignored (parsed
52  * by yacc, expression trees built, but no initialisation takes place).
53  */
54 int	initerr;
55 
56 /* Pointer to the symbol which is to be initialized. */
57 sym_t	*initsym;
58 
59 /* Points to the top element of the initialisation stack. */
60 istk_t	*initstk;
61 
62 typedef struct namlist {
63 	const char *n_name;
64 	struct namlist *n_prev;
65 	struct namlist *n_next;
66 } namlist_t;
67 
68 /* Points to a c9x named member; */
69 namlist_t	*namedmem = NULL;
70 
71 
72 static	void	popi2(void);
73 static	void	popinit(int);
74 static	void	pushinit(void);
75 static	void	testinit(void);
76 static	void	nextinit(int);
77 static	int	strginit(tnode_t *);
78 static	void	memberpop(void);
79 
80 #ifndef DEBUG
81 #define DPRINTF(a)
82 #else
83 #define DPRINTF(a) printf a
84 #endif
85 
86 void
87 memberpush(sb)
88 	sbuf_t *sb;
89 {
90 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
91 	nam->n_name = sb->sb_name;
92 	DPRINTF(("memberpush = %s\n", nam->n_name));
93 	if (namedmem == NULL) {
94 		nam->n_prev = nam->n_next = nam;
95 		namedmem = nam;
96 	} else {
97 		namedmem->n_prev->n_next = nam;
98 		nam->n_prev = namedmem->n_prev;
99 		nam->n_next = namedmem;
100 		namedmem->n_prev = nam;
101 	}
102 #if 0
103 	nam->n_next = namedmem;
104 	namedmem = nam;
105 #endif
106 }
107 
108 static void
109 memberpop()
110 {
111 	DPRINTF(("memberpop = %s\n", namedmem->n_name));
112 	if (namedmem->n_next == namedmem) {
113 		free(namedmem);
114 		namedmem = NULL;
115 	} else {
116 		namlist_t *nam = namedmem;
117 		namedmem = namedmem->n_next;
118 		free(nam);
119 	}
120 #if 0
121 	namedmem = namedmem->n_next;
122 	free(nam);
123 #endif
124 }
125 
126 
127 /*
128  * Initialize the initialisation stack by putting an entry for the variable
129  * which is to be initialized on it.
130  */
131 void
132 prepinit(void)
133 {
134 	istk_t	*istk;
135 
136 	if (initerr)
137 		return;
138 
139 	/* free memory used in last initialisation */
140 	while ((istk = initstk) != NULL) {
141 		initstk = istk->i_nxt;
142 		free(istk);
143 	}
144 
145 	/*
146 	 * If the type which is to be initialized is an incomplete type,
147 	 * it must be duplicated.
148 	 */
149 	if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
150 		initsym->s_type = duptyp(initsym->s_type);
151 
152 	istk = initstk = xcalloc(1, sizeof (istk_t));
153 	istk->i_subt = initsym->s_type;
154 	istk->i_cnt = 1;
155 
156 }
157 
158 static void
159 popi2(void)
160 {
161 #ifdef DEBUG
162 	char	buf[64];
163 #endif
164 	istk_t	*istk;
165 	sym_t	*m;
166 
167 	DPRINTF(("popi2(%s): brace=%d count=%d namedmem %d\n",
168 	    tyname(buf, sizeof(buf),
169 	    initstk->i_type ? initstk->i_type : initstk->i_subt),
170 	    initstk->i_brace, initstk->i_cnt, initstk->i_namedmem));
171 	initstk = (istk = initstk)->i_nxt;
172 	if (initstk == NULL)
173 		LERROR("popi2()");
174 	free(istk);
175 
176 	istk = initstk;
177 
178 	istk->i_cnt--;
179 	if (istk->i_cnt < 0)
180 		LERROR("popi2()");
181 
182 	DPRINTF(("popi2(): %d %s\n", istk->i_cnt,
183 	    namedmem ? namedmem->n_name : "*null*"));
184 	if (istk->i_cnt >= 0 && namedmem != NULL) {
185 		DPRINTF(("popi2(): %d %s %s\n", istk->i_cnt,
186 		    tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
187 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
188 			if (m->s_field && m->s_name == unnamed)
189 				continue;
190 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
191 				istk->i_subt = m->s_type;
192 				istk->i_cnt++;
193 				memberpop();
194 				return;
195 			}
196 		}
197 		error(101, namedmem->n_name);
198 		memberpop();
199 		istk->i_namedmem = 1;
200 		return;
201 	}
202 	/*
203 	 * If the removed element was a structure member, we must go
204 	 * to the next structure member.
205 	 */
206 	if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
207 	    !istk->i_namedmem) {
208 		do {
209 			m = istk->i_mem = istk->i_mem->s_nxt;
210 			if (m == NULL)
211 				LERROR("popi2()");
212 		} while (m->s_field && m->s_name == unnamed);
213 		istk->i_subt = m->s_type;
214 	}
215 }
216 
217 static void
218 popinit(int brace)
219 {
220 	DPRINTF(("popinit(%d)\n", brace));
221 
222 	if (brace) {
223 		/*
224 		 * Take all entries, including the first which requires
225 		 * a closing brace, from the stack.
226 		 */
227 		do {
228 			brace = initstk->i_brace;
229 			popi2();
230 		} while (!brace);
231 	} else {
232 		/*
233 		 * Take all entries which cannot be used for further
234 		 * initializers from the stack, but do this only if
235 		 * they do not require a closing brace.
236 		 */
237 		while (!initstk->i_brace &&
238 		       initstk->i_cnt == 0 && !initstk->i_nolimit) {
239 			popi2();
240 		}
241 	}
242 }
243 
244 static void
245 pushinit(void)
246 {
247 #ifdef DEBUG
248 	char	buf[64];
249 #endif
250 	istk_t	*istk;
251 	int	cnt;
252 	sym_t	*m;
253 
254 	istk = initstk;
255 
256 	/* Extend an incomplete array type by one element */
257 	if (istk->i_cnt == 0) {
258 		DPRINTF(("pushinit(extend) %s\n", tyname(buf, sizeof(buf),
259 		    istk->i_type)));
260 		/*
261 		 * Inside of other aggregate types must not be an incomplete
262 		 * type.
263 		 */
264 		if (istk->i_nxt->i_nxt != NULL)
265 			LERROR("pushinit()");
266 		istk->i_cnt = 1;
267 		if (istk->i_type->t_tspec != ARRAY)
268 			LERROR("pushinit()");
269 		istk->i_type->t_dim++;
270 		/* from now its an complete type */
271 		setcompl(istk->i_type, 0);
272 	}
273 
274 	if (istk->i_cnt <= 0)
275 		LERROR("pushinit()");
276 	if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
277 		LERROR("pushinit()");
278 
279 	initstk = xcalloc(1, sizeof (istk_t));
280 	initstk->i_nxt = istk;
281 	initstk->i_type = istk->i_subt;
282 	if (initstk->i_type->t_tspec == FUNC)
283 		LERROR("pushinit()");
284 
285 again:
286 	istk = initstk;
287 
288 	DPRINTF(("pushinit(%s)\n", tyname(buf, sizeof(buf), istk->i_type)));
289 	switch (istk->i_type->t_tspec) {
290 	case ARRAY:
291 		if (namedmem) {
292 			DPRINTF(("pushinit ARRAY %s brace=%d\n",
293 			    namedmem->n_name, istk->i_brace));
294 			goto pop;
295 		} else if (istk->i_nxt->i_namedmem) {
296 			DPRINTF(("pushinit ARRAY brace=%d, namedmem=%d\n",
297 			    istk->i_brace, istk->i_nxt->i_namedmem));
298 			istk->i_brace = 1;
299 		}
300 
301 		if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
302 			/* initialisation of an incomplete type */
303 			error(175);
304 			initerr = 1;
305 			return;
306 		}
307 		istk->i_subt = istk->i_type->t_subt;
308 		istk->i_nolimit = incompl(istk->i_type);
309 		istk->i_cnt = istk->i_type->t_dim;
310 		DPRINTF(("elements array %s[%d] %s\n",
311 		    tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
312 		    namedmem ? namedmem->n_name : "*none*"));
313 		break;
314 	case UNION:
315 		if (tflag)
316 			/* initialisation of union is illegal in trad. C */
317 			warning(238);
318 		/* FALLTHROUGH */
319 	case STRUCT:
320 		if (incompl(istk->i_type)) {
321 			/* initialisation of an incomplete type */
322 			error(175);
323 			initerr = 1;
324 			return;
325 		}
326 		cnt = 0;
327 		DPRINTF(("2. member lookup %s %s\n",
328 		    tyname(buf, sizeof(buf), istk->i_type),
329 		    namedmem ? namedmem->n_name : "*none*"));
330 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
331 			if (m->s_field && m->s_name == unnamed)
332 				continue;
333 			if (namedmem != NULL) {
334 				DPRINTF(("pushinit():[member:%s, looking:%s]\n",
335 				    m->s_name, namedmem->n_name));
336 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
337 					cnt++;
338 					break;
339 				} else
340 					continue;
341 			}
342 			if (++cnt == 1) {
343 				istk->i_mem = m;
344 				istk->i_subt = m->s_type;
345 			}
346 		}
347 		if (namedmem != NULL) {
348 			if (m == NULL) {
349 				DPRINTF(("pushinit(): struct pop\n"));
350 				goto pop;
351 			} else {
352 				istk->i_mem = m;
353 				istk->i_subt = m->s_type;
354 			}
355 			istk->i_namedmem = 1;
356 			istk->i_brace = 1;
357 			memberpop();
358 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
359 		}
360 		if (cnt == 0) {
361 			/* cannot init. struct/union with no named member */
362 			error(179);
363 			initerr = 1;
364 			return;
365 		}
366 		istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
367 		break;
368 	default:
369 		if (namedmem) {
370 			DPRINTF(("pushinit(): pop\n"));
371 	pop:
372 			free(istk);
373 			initstk = initstk->i_nxt;
374 			goto again;
375 		}
376 		istk->i_cnt = 1;
377 		break;
378 	}
379 }
380 
381 static void
382 testinit(void)
383 {
384 	istk_t	*istk;
385 
386 	istk = initstk;
387 
388 	/*
389 	 * If a closing brace is expected we have at least one initializer
390 	 * too much.
391 	 */
392 	if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
393 		switch (istk->i_type->t_tspec) {
394 		case ARRAY:
395 			/* too many array initializers */
396 			error(173);
397 			break;
398 		case STRUCT:
399 		case UNION:
400 			/* too many struct/union initializers */
401 			error(172);
402 			break;
403 		default:
404 			/* too many initializers */
405 			error(174);
406 			break;
407 		}
408 		initerr = 1;
409 	}
410 }
411 
412 static void
413 nextinit(int brace)
414 {
415 	char buf[64];
416 
417 	DPRINTF(("nextinit(%d)\n", brace));
418 	if (!brace) {
419 		if (initstk->i_type == NULL &&
420 		    !issclt(initstk->i_subt->t_tspec)) {
421 			/* {}-enclosed initializer required */
422 			error(181);
423 		}
424 		/*
425 		 * Make sure an entry with a scalar type is at the top
426 		 * of the stack.
427 		 */
428 		if (!initerr)
429 			testinit();
430 		while (!initerr && (initstk->i_type == NULL ||
431 				    !issclt(initstk->i_type->t_tspec))) {
432 			if (!initerr)
433 				pushinit();
434 		}
435 	} else {
436 		if (initstk->i_type != NULL &&
437 		    issclt(initstk->i_type->t_tspec)) {
438 			/* invalid initializer */
439 			error(176, tyname(buf, sizeof(buf), initstk->i_type));
440 			initerr = 1;
441 		}
442 		if (!initerr)
443 			testinit();
444 		if (!initerr)
445 			pushinit();
446 		if (!initerr)
447 			initstk->i_brace = 1;
448 	}
449 }
450 
451 void
452 initlbr(void)
453 {
454 	DPRINTF(("initlbr\n"));
455 
456 	if (initerr)
457 		return;
458 
459 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
460 	    initstk->i_nxt == NULL) {
461 		if (tflag && !issclt(initstk->i_subt->t_tspec))
462 			/* no automatic aggregate initialization in trad. C*/
463 			warning(188);
464 	}
465 
466 	/*
467 	 * Remove all entries which cannot be used for further initializers
468 	 * and do not expect a closing brace.
469 	 */
470 	popinit(0);
471 
472 	nextinit(1);
473 }
474 
475 void
476 initrbr(void)
477 {
478 	DPRINTF(("initrbr\n"));
479 
480 	if (initerr)
481 		return;
482 
483 	popinit(1);
484 }
485 
486 void
487 mkinit(tnode_t *tn)
488 {
489 	ptrdiff_t offs;
490 	sym_t	*sym;
491 	tspec_t	lt, rt;
492 	tnode_t	*ln;
493 	struct	mbl *tmem;
494 	scl_t	sc;
495 #ifdef DEBUG
496 	char	buf[64], sbuf[64];
497 #endif
498 
499 	DPRINTF(("mkinit(%s %s)\n", tyname(buf, sizeof(buf), tn->tn_type),
500 	   prtnode(sbuf, sizeof(sbuf), tn)));
501 	if (initerr || tn == NULL)
502 		goto end;
503 
504 	sc = initsym->s_scl;
505 
506 	/*
507 	 * Do not test for automatic aggregate initialisation. If the
508 	 * initializer starts with a brace we have the warning already.
509 	 * If not, an error will be printed that the initializer must
510 	 * be enclosed by braces.
511 	 */
512 
513 	/*
514 	 * Local initialisation of non-array-types with only one expression
515 	 * without braces is done by ASSIGN
516 	 */
517 	if ((sc == AUTO || sc == REG) &&
518 	    initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
519 		ln = getnnode(initsym, 0);
520 		ln->tn_type = tduptyp(ln->tn_type);
521 		ln->tn_type->t_const = 0;
522 		tn = build(ASSIGN, ln, tn);
523 		expr(tn, 0, 0, 1);
524 		goto end;
525 	}
526 
527 	/*
528 	 * Remove all entries which cannot be used for further initializers
529 	 * and do not require a closing brace.
530 	 */
531 	popinit(0);
532 
533 	/* Initialisations by strings are done in strginit(). */
534 	if (strginit(tn))
535 		goto end;
536 
537 	nextinit(0);
538 	if (initerr || tn == NULL)
539 		goto end;
540 
541 	initstk->i_cnt--;
542 	DPRINTF(("mkinit() cnt=%d tn=%p\n", initstk->i_cnt, tn));
543 	/* Create a temporary node for the left side. */
544 	ln = tgetblk(sizeof (tnode_t));
545 	ln->tn_op = NAME;
546 	ln->tn_type = tduptyp(initstk->i_type);
547 	ln->tn_type->t_const = 0;
548 	ln->tn_lvalue = 1;
549 	ln->tn_sym = initsym;		/* better than nothing */
550 
551 	tn = cconv(tn);
552 
553 	lt = ln->tn_type->t_tspec;
554 	rt = tn->tn_type->t_tspec;
555 
556 	if (!issclt(lt))
557 		LERROR("mkinit()");
558 
559 	if (!typeok(INIT, 0, ln, tn))
560 		goto end;
561 
562 	/*
563 	 * Store the tree memory. This is nessesary because otherwise
564 	 * expr() would free it.
565 	 */
566 	tmem = tsave();
567 	expr(tn, 1, 0, 1);
568 	trestor(tmem);
569 
570 	if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
571 		/*
572 		 * Bit-fields can be initialized in trad. C only by integer
573 		 * constants.
574 		 */
575 		if (tflag)
576 			/* bit-field initialisation is illegal in trad. C */
577 			warning(186);
578 	}
579 
580 	if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
581 		tn = convert(INIT, 0, initstk->i_type, tn);
582 
583 	if (tn != NULL && tn->tn_op != CON) {
584 		sym = NULL;
585 		offs = 0;
586 		if (conaddr(tn, &sym, &offs) == -1) {
587 			if (sc == AUTO || sc == REG) {
588 				/* non-constant initializer */
589 				(void)gnuism(177);
590 			} else {
591 				/* non-constant initializer */
592 				error(177);
593 			}
594 		}
595 	}
596 
597  end:
598 	/*
599 	 * We only free the block, if we are not a compound declaration
600 	 * We know that the only symbols that start with a digit are the
601 	 * ones we allocate with mktempsym() for compound declarations
602 	 */
603 	if (!isdigit((unsigned char)initsym->s_name[0]))
604 		tfreeblk();
605 }
606 
607 
608 static int
609 strginit(tnode_t *tn)
610 {
611 	tspec_t	t;
612 	istk_t	*istk;
613 	int	len;
614 	strg_t	*strg;
615 
616 	if (tn->tn_op != STRING)
617 		return (0);
618 
619 	istk = initstk;
620 	strg = tn->tn_strg;
621 
622 	/*
623 	 * Check if we have an array type which can be initialized by
624 	 * the string.
625 	 */
626 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
627 		DPRINTF(("strginit subt array\n"));
628 		t = istk->i_subt->t_subt->t_tspec;
629 		if (!((strg->st_tspec == CHAR &&
630 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
631 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
632 			return (0);
633 		}
634 		/* Put the array at top of stack */
635 		pushinit();
636 		istk = initstk;
637 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
638 		DPRINTF(("strginit type array\n"));
639 		t = istk->i_type->t_subt->t_tspec;
640 		if (!((strg->st_tspec == CHAR &&
641 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
642 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
643 			return (0);
644 		}
645 		/*
646 		 * If the array is already partly initialized, we are
647 		 * wrong here.
648 		 */
649 		if (istk->i_cnt != istk->i_type->t_dim)
650 			return (0);
651 	} else {
652 		return (0);
653 	}
654 
655 	/* Get length without trailing NUL character. */
656 	len = strg->st_len;
657 
658 	if (istk->i_nolimit) {
659 		istk->i_nolimit = 0;
660 		istk->i_type->t_dim = len + 1;
661 		/* from now complete type */
662 		setcompl(istk->i_type, 0);
663 	} else {
664 		if (istk->i_type->t_dim < len) {
665 			/* non-null byte ignored in string initializer */
666 			warning(187);
667 		}
668 	}
669 
670 	/* In every case the array is initialized completely. */
671 	istk->i_cnt = 0;
672 
673 	return (1);
674 }
675