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