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