Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.18
      1 /*	$NetBSD: init.c,v 1.18 2004/06/20 22:20:17 jmc 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.18 2004/06/20 22:20:17 jmc 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 	initstk = (istk = initstk)->i_nxt;
    168 	if (initstk == NULL)
    169 		LERROR("popi2()");
    170 	free(istk);
    171 
    172 	istk = initstk;
    173 
    174 	istk->i_cnt--;
    175 	if (istk->i_cnt < 0)
    176 		LERROR("popi2()");
    177 
    178 	DPRINTF(("popi2(): %d %s\n", istk->i_cnt,
    179 	    namedmem ? namedmem->n_name : "*null*"));
    180 	if (istk->i_cnt >= 0 && namedmem != NULL) {
    181 		DPRINTF(("popi2(): %d %s %s\n", istk->i_cnt,
    182 		    tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
    183 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
    184 			if (m->s_field && m->s_name == unnamed)
    185 				continue;
    186 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
    187 				istk->i_subt = m->s_type;
    188 				istk->i_cnt++;
    189 				memberpop();
    190 				return;
    191 			}
    192 		}
    193 		error(101, namedmem->n_name);
    194 		memberpop();
    195 		istk->i_namedmem = 1;
    196 		return;
    197 	}
    198 	/*
    199 	 * If the removed element was a structure member, we must go
    200 	 * to the next structure member.
    201 	 */
    202 	if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
    203 	    !istk->i_namedmem) {
    204 		do {
    205 			m = istk->i_mem = istk->i_mem->s_nxt;
    206 			if (m == NULL)
    207 				LERROR("popi2()");
    208 		} while (m->s_field && m->s_name == unnamed);
    209 		istk->i_subt = m->s_type;
    210 	}
    211 }
    212 
    213 static void
    214 popinit(int brace)
    215 {
    216 	DPRINTF(("popinit(%d)\n", brace));
    217 
    218 	if (brace) {
    219 		/*
    220 		 * Take all entries, including the first which requires
    221 		 * a closing brace, from the stack.
    222 		 */
    223 		do {
    224 			brace = initstk->i_brace;
    225 			popi2();
    226 		} while (!brace);
    227 	} else {
    228 		/*
    229 		 * Take all entries which cannot be used for further
    230 		 * initializers from the stack, but do this only if
    231 		 * they do not require a closing brace.
    232 		 */
    233 		while (!initstk->i_brace &&
    234 		       initstk->i_cnt == 0 && !initstk->i_nolimit) {
    235 			popi2();
    236 		}
    237 	}
    238 }
    239 
    240 static void
    241 pushinit(void)
    242 {
    243 #ifdef DEBUG
    244 	char	buf[64];
    245 #endif
    246 	istk_t	*istk;
    247 	int	cnt;
    248 	sym_t	*m;
    249 
    250 	istk = initstk;
    251 
    252 	/* Extend an incomplete array type by one element */
    253 	if (istk->i_cnt == 0) {
    254 		DPRINTF(("pushinit(extend) %s\n", tyname(buf, sizeof(buf),
    255 		    istk->i_type)));
    256 		/*
    257 		 * Inside of other aggregate types must not be an incomplete
    258 		 * type.
    259 		 */
    260 		if (istk->i_nxt->i_nxt != NULL)
    261 			LERROR("pushinit()");
    262 		istk->i_cnt = 1;
    263 		if (istk->i_type->t_tspec != ARRAY)
    264 			LERROR("pushinit()");
    265 		istk->i_type->t_dim++;
    266 		/* from now its an complete type */
    267 		setcompl(istk->i_type, 0);
    268 	}
    269 
    270 	if (istk->i_cnt <= 0)
    271 		LERROR("pushinit()");
    272 	if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
    273 		LERROR("pushinit()");
    274 
    275 	initstk = xcalloc(1, sizeof (istk_t));
    276 	initstk->i_nxt = istk;
    277 	initstk->i_type = istk->i_subt;
    278 	if (initstk->i_type->t_tspec == FUNC)
    279 		LERROR("pushinit()");
    280 
    281 again:
    282 	istk = initstk;
    283 
    284 	DPRINTF(("pushinit(%s)\n", tyname(buf, sizeof(buf), istk->i_type)));
    285 	switch (istk->i_type->t_tspec) {
    286 	case ARRAY:
    287 		if (namedmem) {
    288 			DPRINTF(("pushinit ARRAY %s\n", namedmem->n_name));
    289 			free(istk);
    290 			initstk = initstk->i_nxt;
    291 			goto again;
    292 		}
    293 		if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
    294 			/* initialisation of an incomplete type */
    295 			error(175);
    296 			initerr = 1;
    297 			return;
    298 		}
    299 		istk->i_subt = istk->i_type->t_subt;
    300 		istk->i_nolimit = incompl(istk->i_type);
    301 		istk->i_cnt = istk->i_type->t_dim;
    302 		DPRINTF(("elements array %s[%d] %s\n",
    303 		    tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
    304 		    namedmem ? namedmem->n_name : "*none*"));
    305 		break;
    306 	case UNION:
    307 		if (tflag)
    308 			/* initialisation of union is illegal in trad. C */
    309 			warning(238);
    310 		/* FALLTHROUGH */
    311 	case STRUCT:
    312 		if (incompl(istk->i_type)) {
    313 			/* initialisation of an incomplete type */
    314 			error(175);
    315 			initerr = 1;
    316 			return;
    317 		}
    318 		cnt = 0;
    319 		DPRINTF(("2. member lookup %s %s\n",
    320 		    tyname(buf, sizeof(buf), istk->i_type),
    321 		    namedmem ? namedmem->n_name : "*none*"));
    322 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
    323 			if (m->s_field && m->s_name == unnamed)
    324 				continue;
    325 			if (namedmem != NULL) {
    326 				DPRINTF(("pushinit():[member:%s, looking:%s]\n",
    327 				    m->s_name, namedmem->n_name));
    328 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
    329 					cnt++;
    330 					break;
    331 				} else
    332 					continue;
    333 			}
    334 			if (++cnt == 1) {
    335 				istk->i_mem = m;
    336 				istk->i_subt = m->s_type;
    337 			}
    338 		}
    339 		if (namedmem != NULL) {
    340 			istk->i_namedmem = 1;
    341 			if (m == NULL) {
    342 				error(101, namedmem->n_name);
    343 				initerr = 1;
    344 			} else {
    345 				istk->i_mem = m;
    346 				istk->i_subt = m->s_type;
    347 			}
    348 			memberpop();
    349 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    350 		}
    351 		if (cnt == 0) {
    352 			/* cannot init. struct/union with no named member */
    353 			error(179);
    354 			initerr = 1;
    355 			return;
    356 		}
    357 		istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    358 		break;
    359 	default:
    360 		if (namedmem) {
    361 			DPRINTF(("pushinit(): pop\n"));
    362 			free(istk);
    363 			initstk = initstk->i_nxt;
    364 			goto again;
    365 		}
    366 		istk->i_cnt = 1;
    367 		break;
    368 	}
    369 }
    370 
    371 static void
    372 testinit(void)
    373 {
    374 	istk_t	*istk;
    375 
    376 	istk = initstk;
    377 
    378 	/*
    379 	 * If a closing brace is expected we have at least one initializer
    380 	 * too much.
    381 	 */
    382 	if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
    383 		switch (istk->i_type->t_tspec) {
    384 		case ARRAY:
    385 			/* too many array initializers */
    386 			error(173);
    387 			break;
    388 		case STRUCT:
    389 		case UNION:
    390 			/* too many struct/union initializers */
    391 			error(172);
    392 			break;
    393 		default:
    394 			/* too many initializers */
    395 			error(174);
    396 			break;
    397 		}
    398 		initerr = 1;
    399 	}
    400 }
    401 
    402 static void
    403 nextinit(int brace)
    404 {
    405 	char buf[64];
    406 
    407 	DPRINTF(("nextinit(%d)\n", brace));
    408 	if (!brace) {
    409 		if (initstk->i_type == NULL &&
    410 		    !issclt(initstk->i_subt->t_tspec)) {
    411 			/* {}-enclosed initializer required */
    412 			error(181);
    413 		}
    414 		/*
    415 		 * Make sure an entry with a scalar type is at the top
    416 		 * of the stack.
    417 		 */
    418 		if (!initerr)
    419 			testinit();
    420 		while (!initerr && (initstk->i_type == NULL ||
    421 				    !issclt(initstk->i_type->t_tspec))) {
    422 			if (!initerr)
    423 				pushinit();
    424 		}
    425 	} else {
    426 		if (initstk->i_type != NULL &&
    427 		    issclt(initstk->i_type->t_tspec)) {
    428 			/* invalid initializer */
    429 			error(176, tyname(buf, sizeof(buf), initstk->i_type));
    430 			initerr = 1;
    431 		}
    432 		if (!initerr)
    433 			testinit();
    434 		if (!initerr)
    435 			pushinit();
    436 		if (!initerr)
    437 			initstk->i_brace = 1;
    438 	}
    439 }
    440 
    441 void
    442 initlbr(void)
    443 {
    444 
    445 	if (initerr)
    446 		return;
    447 
    448 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
    449 	    initstk->i_nxt == NULL) {
    450 		if (tflag && !issclt(initstk->i_subt->t_tspec))
    451 			/* no automatic aggregate initialization in trad. C*/
    452 			warning(188);
    453 	}
    454 
    455 	/*
    456 	 * Remove all entries which cannot be used for further initializers
    457 	 * and do not expect a closing brace.
    458 	 */
    459 	popinit(0);
    460 
    461 	nextinit(1);
    462 }
    463 
    464 void
    465 initrbr(void)
    466 {
    467 
    468 	if (initerr)
    469 		return;
    470 
    471 	popinit(1);
    472 }
    473 
    474 void
    475 mkinit(tnode_t *tn)
    476 {
    477 	ptrdiff_t offs;
    478 	sym_t	*sym;
    479 	tspec_t	lt, rt;
    480 	tnode_t	*ln;
    481 	struct	mbl *tmem;
    482 	scl_t	sc;
    483 #ifdef DEBUG
    484 	char	buf[64];
    485 #endif
    486 
    487 	DPRINTF(("mkinit(%s)\n", tyname(buf, sizeof(buf), tn->tn_type)));
    488 	if (initerr || tn == NULL)
    489 		goto end;
    490 
    491 	sc = initsym->s_scl;
    492 
    493 	/*
    494 	 * Do not test for automatic aggregate initialisation. If the
    495 	 * initializer starts with a brace we have the warning already.
    496 	 * If not, an error will be printed that the initializer must
    497 	 * be enclosed by braces.
    498 	 */
    499 
    500 	/*
    501 	 * Local initialisation of non-array-types with only one expression
    502 	 * without braces is done by ASSIGN
    503 	 */
    504 	if ((sc == AUTO || sc == REG) &&
    505 	    initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
    506 		ln = getnnode(initsym, 0);
    507 		ln->tn_type = tduptyp(ln->tn_type);
    508 		ln->tn_type->t_const = 0;
    509 		tn = build(ASSIGN, ln, tn);
    510 		expr(tn, 0, 0, 1);
    511 		goto end;
    512 	}
    513 
    514 	/*
    515 	 * Remove all entries which cannot be used for further initializers
    516 	 * and do not require a closing brace.
    517 	 */
    518 	popinit(0);
    519 
    520 	/* Initialisations by strings are done in strginit(). */
    521 	if (strginit(tn))
    522 		goto end;
    523 
    524 	nextinit(0);
    525 	if (initerr || tn == NULL)
    526 		goto end;
    527 
    528 	initstk->i_cnt--;
    529 	DPRINTF(("mkinit() cnt=%d tn=%p\n", initstk->i_cnt, tn));
    530 	/* Create a temporary node for the left side. */
    531 	ln = tgetblk(sizeof (tnode_t));
    532 	ln->tn_op = NAME;
    533 	ln->tn_type = tduptyp(initstk->i_type);
    534 	ln->tn_type->t_const = 0;
    535 	ln->tn_lvalue = 1;
    536 	ln->tn_sym = initsym;		/* better than nothing */
    537 
    538 	tn = cconv(tn);
    539 
    540 	lt = ln->tn_type->t_tspec;
    541 	rt = tn->tn_type->t_tspec;
    542 
    543 	if (!issclt(lt))
    544 		LERROR("mkinit()");
    545 
    546 	if (!typeok(INIT, 0, ln, tn))
    547 		goto end;
    548 
    549 	/*
    550 	 * Store the tree memory. This is nessesary because otherwise
    551 	 * expr() would free it.
    552 	 */
    553 	tmem = tsave();
    554 	expr(tn, 1, 0, 1);
    555 	trestor(tmem);
    556 
    557 	if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
    558 		/*
    559 		 * Bit-fields can be initialized in trad. C only by integer
    560 		 * constants.
    561 		 */
    562 		if (tflag)
    563 			/* bit-field initialisation is illegal in trad. C */
    564 			warning(186);
    565 	}
    566 
    567 	if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
    568 		tn = convert(INIT, 0, initstk->i_type, tn);
    569 
    570 	if (tn != NULL && tn->tn_op != CON) {
    571 		sym = NULL;
    572 		offs = 0;
    573 		if (conaddr(tn, &sym, &offs) == -1) {
    574 			if (sc == AUTO || sc == REG) {
    575 				/* non-constant initializer */
    576 				(void)gnuism(177);
    577 			} else {
    578 				/* non-constant initializer */
    579 				error(177);
    580 			}
    581 		}
    582 	}
    583 
    584  end:
    585 	/*
    586 	 * We only free the block, if we are not a compound declaration
    587 	 * We know that the only symbols that start with a digit are the
    588 	 * ones we allocate with mktempsym() for compound declarations
    589 	 */
    590 	if (!isdigit((unsigned char)initsym->s_name[0]))
    591 		tfreeblk();
    592 }
    593 
    594 
    595 static int
    596 strginit(tnode_t *tn)
    597 {
    598 	tspec_t	t;
    599 	istk_t	*istk;
    600 	int	len;
    601 	strg_t	*strg;
    602 
    603 	if (tn->tn_op != STRING)
    604 		return (0);
    605 
    606 	istk = initstk;
    607 	strg = tn->tn_strg;
    608 
    609 	/*
    610 	 * Check if we have an array type which can be initialized by
    611 	 * the string.
    612 	 */
    613 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
    614 		t = istk->i_subt->t_subt->t_tspec;
    615 		if (!((strg->st_tspec == CHAR &&
    616 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    617 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    618 			return (0);
    619 		}
    620 		/* Put the array at top of stack */
    621 		pushinit();
    622 		istk = initstk;
    623 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
    624 		t = istk->i_type->t_subt->t_tspec;
    625 		if (!((strg->st_tspec == CHAR &&
    626 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    627 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    628 			return (0);
    629 		}
    630 		/*
    631 		 * If the array is already partly initialized, we are
    632 		 * wrong here.
    633 		 */
    634 		if (istk->i_cnt != istk->i_type->t_dim)
    635 			return (0);
    636 	} else {
    637 		return (0);
    638 	}
    639 
    640 	/* Get length without trailing NUL character. */
    641 	len = strg->st_len;
    642 
    643 	if (istk->i_nolimit) {
    644 		istk->i_nolimit = 0;
    645 		istk->i_type->t_dim = len + 1;
    646 		/* from now complete type */
    647 		setcompl(istk->i_type, 0);
    648 	} else {
    649 		if (istk->i_type->t_dim < len) {
    650 			/* non-null byte ignored in string initializer */
    651 			warning(187);
    652 		}
    653 	}
    654 
    655 	/* In every case the array is initialized completely. */
    656 	istk->i_cnt = 0;
    657 
    658 	return (1);
    659 }
    660