Home | History | Annotate | Line # | Download | only in lint1
scan.l revision 1.78
      1 %{
      2 /* $NetBSD: scan.l,v 1.78 2017/01/15 21:10:24 christos Exp $ */
      3 
      4 /*
      5  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      6  * Copyright (c) 1994, 1995 Jochen Pohl
      7  * All Rights Reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Jochen Pohl for
     20  *      The NetBSD Project.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(lint)
     38 __RCSID("$NetBSD: scan.l,v 1.78 2017/01/15 21:10:24 christos Exp $");
     39 #endif
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <limits.h>
     44 #include <float.h>
     45 #include <ctype.h>
     46 #include <errno.h>
     47 #include <math.h>
     48 
     49 #include "lint1.h"
     50 #include "cgram.h"
     51 
     52 #define CHAR_MASK	((int)(~(~0U << CHAR_BIT)))
     53 
     54 /* Current position (its also updated when an included file is parsed) */
     55 pos_t	curr_pos = { 1, "", 0 };
     56 
     57 /*
     58  * Current position in C source (not updated when an included file is
     59  * parsed).
     60  */
     61 pos_t	csrc_pos = { 1, "", 0 };
     62 
     63 /* Are we parsing a gcc attribute? */
     64 int attron;
     65 
     66 static	void	incline(void);
     67 static	void	badchar(int);
     68 static	sbuf_t	*allocsb(void);
     69 static	void	freesb(sbuf_t *);
     70 static	int	inpc(void);
     71 static	int	hash(const char *);
     72 static	sym_t	*search(sbuf_t *);
     73 static	int	name(void);
     74 static	int	keyw(sym_t *);
     75 static	int	icon(int);
     76 static	int	fcon(void);
     77 static	int	operator(int, op_t);
     78 static	int	ccon(void);
     79 static	int	wccon(void);
     80 static	int	getescc(int);
     81 static	void	directive(void);
     82 static	void	comment(void);
     83 static	void	slashslashcomment(void);
     84 static	int	string(void);
     85 static	int	wcstrg(void);
     86 
     87 %}
     88 
     89 
     90 L	[_A-Za-z]
     91 D	[0-9]
     92 NZD	[1-9]
     93 BD	[0-1]
     94 OD	[0-7]
     95 HD	[0-9A-Fa-f]
     96 EX	([eE][+-]?[0-9]+)
     97 HX	(p[+-]?[0-9A-Fa-f]+)
     98 TL	([fFlL]?[i]?)
     99 
    100 %option nounput
    101 
    102 %%
    103 
    104 {L}({L}|{D})*		 	return (name());
    105 0[bB]{BD}+[lLuU]*		return (icon(2));
    106 0{OD}*[lLuU]*			return (icon(8));
    107 {NZD}{D}*[lLuU]*		return (icon(10));
    108 0[xX]{HD}+[lLuU]*		return (icon(16));
    109 {D}+\.{D}*{EX}?{TL}	|
    110 {D}+{EX}{TL}		|
    111 0[xX]{HD}+\.{HD}*{HX}{TL} |
    112 0[xX]{HD}+{HX}{TL} 	|
    113 \.{D}+{EX}?{TL}		return (fcon());
    114 "="				return (operator(T_ASSIGN, ASSIGN));
    115 "*="				return (operator(T_OPASS, MULASS));
    116 "/="				return (operator(T_OPASS, DIVASS));
    117 "%="				return (operator(T_OPASS, MODASS));
    118 "+="				return (operator(T_OPASS, ADDASS));
    119 "-="				return (operator(T_OPASS, SUBASS));
    120 "<<="				return (operator(T_OPASS, SHLASS));
    121 ">>="				return (operator(T_OPASS, SHRASS));
    122 "&="				return (operator(T_OPASS, ANDASS));
    123 "^="				return (operator(T_OPASS, XORASS));
    124 "|="				return (operator(T_OPASS, ORASS));
    125 "||"				return (operator(T_LOGOR, LOGOR));
    126 "&&"				return (operator(T_LOGAND, LOGAND));
    127 "|"				return (operator(T_OR, OR));
    128 "&"				return (operator(T_AND, AND));
    129 "^"				return (operator(T_XOR, XOR));
    130 "=="				return (operator(T_EQOP, EQ));
    131 "!="				return (operator(T_EQOP, NE));
    132 "<"				return (operator(T_RELOP, LT));
    133 ">"				return (operator(T_RELOP, GT));
    134 "<="				return (operator(T_RELOP, LE));
    135 ">="				return (operator(T_RELOP, GE));
    136 "<<"				return (operator(T_SHFTOP, SHL));
    137 ">>"				return (operator(T_SHFTOP, SHR));
    138 "++"				return (operator(T_INCDEC, INC));
    139 "--"				return (operator(T_INCDEC, DEC));
    140 "->"				return (operator(T_STROP, ARROW));
    141 "."				return (operator(T_STROP, POINT));
    142 "+"				return (operator(T_ADDOP, PLUS));
    143 "-"				return (operator(T_ADDOP, MINUS));
    144 "*"				return (operator(T_MULT, MULT));
    145 "/"				return (operator(T_DIVOP, DIV));
    146 "%"				return (operator(T_DIVOP, MOD));
    147 "!"				return (operator(T_UNOP, NOT));
    148 "~"				return (operator(T_UNOP, COMPL));
    149 "\""				return (string());
    150 "L\""				return (wcstrg());
    151 ";"				return (T_SEMI);
    152 "{"				return (T_LBRACE);
    153 "}"				return (T_RBRACE);
    154 ","				return (T_COMMA);
    155 ":"				return (T_COLON);
    156 "?"				return (T_QUEST);
    157 "["				return (T_LBRACK);
    158 "]"				return (T_RBRACK);
    159 "("				return (T_LPARN);
    160 ")"				return (T_RPARN);
    161 "..."				return (T_ELLIPSE);
    162 "'"				return (ccon());
    163 "L'"				return (wccon());
    164 ^#.*$				directive();
    165 \n				incline();
    166 \t|" "|\f|\v			;
    167 "/*"				comment();
    168 "//"				slashslashcomment();
    169 .				badchar(yytext[0]);
    170 
    171 %%
    172 
    173 static void
    174 incline(void)
    175 {
    176 	curr_pos.p_line++;
    177 	curr_pos.p_uniq = 0;
    178 	if (curr_pos.p_file == csrc_pos.p_file) {
    179 		csrc_pos.p_line++;
    180 		csrc_pos.p_uniq = 0;
    181 	}
    182 }
    183 
    184 static void
    185 badchar(int c)
    186 {
    187 
    188 	/* unknown character \%o */
    189 	error(250, c);
    190 }
    191 
    192 /*
    193  * Keywords.
    194  * During initialisation they are written to the symbol table.
    195  */
    196 static	struct	kwtab {
    197 	const	char *kw_name;	/* keyword */
    198 	int	kw_token;	/* token returned by yylex() */
    199 	scl_t	kw_scl;		/* storage class if kw_token T_SCLASS */
    200 	tspec_t	kw_tspec;	/* type spec. if kw_token T_TYPE or T_SOU */
    201 	tqual_t	kw_tqual;	/* type qual. fi kw_token T_QUAL */
    202 	u_int	kw_c89 : 1;	/* c89 keyword */
    203 	u_int	kw_c99 : 1;	/* c99 keyword */
    204 	u_int	kw_gcc : 1;	/* GCC keyword */
    205 	u_int	kw_attr : 1;	/* GCC attribute, keyword */
    206 	u_int	kw_deco : 3;	/* name[1] __name[2] __name__[4] */
    207 } kwtab[] = {
    208 	{ "_Bool",	T_TYPE,		0,	BOOL,	0,	  0,1,0,0,1 },
    209 	{ "_Complex",	T_TYPE,		0,	COMPLEX,0,	  0,1,0,0,1 },
    210 	{ "_Generic",	T_GENERIC,	0,	0,	0,	  0,1,0,0,1 },
    211 	{ "alias",	T_AT_ALIAS,	0,	0,	0,	  0,0,1,1,5 },
    212 	{ "aligned",	T_AT_ALIGNED,	0,	0,	0,	  0,0,1,1,5 },
    213 	{ "alignof",	T_ALIGNOF,	0,	0,	0,	  0,0,0,0,4 },
    214 	{ "always_inline", T_AT_ALWAYS_INLINE, 0,0,	0,	  0,0,1,1,5 },
    215 	{ "asm",	T_ASM,		0,	0,	0,	  0,0,1,0,7 },
    216 	{ "attribute",	T_ATTRIBUTE,	0,	0,	0,	  0,0,1,0,6 },
    217 	{ "auto",	T_SCLASS,	AUTO,	0,	0,	  0,0,0,0,1 },
    218 	{ "bounded",	T_AT_BOUNDED,	0,	0,	0,	  0,0,1,1,5 },
    219 	{ "break",	T_BREAK,	0,	0,	0,	  0,0,0,0,1 },
    220 	{ "buffer",	T_AT_BUFFER,	0,	0,	0,	  0,0,1,1,5 },
    221 	{ "case",	T_CASE,		0,	0,	0,	  0,0,0,0,1 },
    222 	{ "char",	T_TYPE,		0,	CHAR,	0,	  0,0,0,0,1 },
    223 	{ "cold",	T_AT_COLD,	0,	0,	0,	  0,0,1,1,5 },
    224 	{ "const",	T_QUAL,		0,	0,	CONST,	  1,0,0,0,7 },
    225 	{ "constructor",T_AT_CONSTRUCTOR,0,	0,	0,	  0,0,1,1,5 },
    226 	{ "continue",	T_CONTINUE,	0,	0,	0,	  0,0,0,0,1 },
    227 	{ "default",	T_DEFAULT,	0,	0,	0,	  0,0,0,0,1 },
    228 	{ "deprecated",	T_AT_DEPRECATED,0,	0,	0,	  0,0,1,1,5 },
    229 	{ "do",		T_DO,		0,	0,	0,	  0,0,0,0,1 },
    230 	{ "double",	T_TYPE,		0,	DOUBLE,	0,	  0,0,0,0,1 },
    231 	{ "else",	T_ELSE,		0,	0,	0,	  0,0,0,0,1 },
    232 	{ "enum",	T_ENUM,		0,	0,	0,	  0,0,0,0,1 },
    233 	{ "extension",  T_EXTENSION,	0,	0,	0,	  0,0,1,0,4 },
    234 	{ "extern",	T_SCLASS,	EXTERN,	0,	0,	  0,0,0,0,1 },
    235 	{ "float",	T_TYPE,		0,	FLOAT,	0,	  0,0,0,0,1 },
    236 	{ "for",	T_FOR,		0,	0,	0,	  0,0,0,0,1 },
    237 	{ "format",	T_AT_FORMAT,	0,	0,	0,	  0,0,1,1,5 },
    238 	{ "format_arg", T_AT_FORMAT_ARG,0,	0,	0,	  0,0,1,1,5 },
    239 	{ "gnu_inline",	T_AT_GNU_INLINE,0,	0,	0,	  0,0,1,1,5 },
    240 	{ "goto",	T_GOTO,		0,	0,	0,	  0,0,0,0,1 },
    241 	{ "if",		T_IF,		0,	0,	0,	  0,0,0,0,1 },
    242 	{ "imag",	T_IMAG,		0,	0,	0,	  0,1,0,0,4 },
    243 	{ "inline",	T_SCLASS,	INLINE,	0,	0,	  0,1,0,0,7 },
    244 	{ "int",	T_TYPE,		0,	INT,	0,	  0,0,0,0,1 },
    245 	{ "long",	T_TYPE,		0,	LONG,	0,	  0,0,0,0,1 },
    246 	{ "may_alias",	T_AT_MAY_ALIAS,	0,	0,	0,	  0,0,1,1,5 },
    247 	{ "minbytes",	T_AT_MINBYTES,	0,	0,	0,	  0,0,1,1,5 },
    248 	{ "mode",	T_AT_MODE,	0,	0,	0,	  0,0,1,1,5 },
    249 	{ "no_instrument_function", T_AT_NO_INSTRUMENT_FUNCTION,
    250 					0,	0,	0,	  0,0,1,1,5 },
    251 	{ "nonnull",	T_AT_NONNULL,	0,	0,	0,	  0,0,1,1,5 },
    252 	{ "noreturn",	T_AT_NORETURN,	0,	0,	0,	  0,0,1,1,5 },
    253 	{ "packed",	T_AT_PACKED,	0,	0,	0,	  0,0,1,1,5 },
    254 	{ "packed",	T_PACKED,	0,	0,	0,	  0,0,0,0,2 },
    255 	{ "pcs",	T_AT_PCS,	0,	0,	0,	  0,0,0,0,5 },
    256 	{ "printf",	T_AT_FORMAT_PRINTF,0,	0,	0,	  0,0,1,1,5 },
    257 	{ "pure",	T_AT_PURE,	0,	0,	0,	  0,0,1,1,5 },
    258 	{ "real",	T_REAL,		0,	0,	0,	  0,1,0,0,4 },
    259 	{ "register",	T_SCLASS,	REG,	0,	0,	  0,0,0,0,1 },
    260 	{ "restrict",	T_QUAL,		0,	0,	RESTRICT, 0,1,0,0,5 },
    261 	{ "return",	T_RETURN,	0,	0,	0,	  0,0,0,0,1 },
    262 	{ "returns_twice", T_AT_RETURNS_TWICE,0,0,	0,	  0,0,1,1,5 },
    263 	{ "scanf",	T_AT_FORMAT_SCANF,0,	0,	0,	  0,0,1,1,5 },
    264 	{ "section",	T_AT_SECTION,	0,	0,	0,	  0,0,1,1,7 },
    265 	{ "sentinel",	T_AT_SENTINEL,	0,	0,	0,	  0,0,1,1,5 },
    266 	{ "short",	T_TYPE,		0,	SHORT,	0,	  0,0,0,0,1 },
    267 	{ "signed",	T_TYPE,		0,	SIGNED,	0,	  1,0,0,0,3 },
    268 	{ "sizeof",	T_SIZEOF,	0,	0,	0,	  0,0,0,0,1 },
    269 	{ "static",	T_SCLASS,	STATIC,	0,	0,	  0,0,0,0,1 },
    270 	{ "strfmon",	T_AT_FORMAT_STRFMON,0,	0,	0,	  0,0,1,1,5 },
    271 	{ "strftime",	T_AT_FORMAT_STRFTIME,0,	0,	0,	  0,0,1,1,5 },
    272 	{ "string",	T_AT_STRING,	0,	0,	0,	  0,0,1,1,5 },
    273 	{ "struct",	T_SOU,		0,	STRUCT,	0,	  0,0,0,0,1 },
    274 	{ "switch",	T_SWITCH,	0,	0,	0,	  0,0,0,0,1 },
    275 	{ "symbolrename", T_SYMBOLRENAME, 0,	0,	0,	  0,0,0,0,2 },
    276 	{ "transparent_union",T_AT_TUNION,0,	0,	0,	  0,0,1,1,5 },
    277 	{ "typedef",	T_SCLASS,	TYPEDEF, 0,	0,	  0,0,0,0,1 },
    278 	{ "typeof",	T_TYPEOF,	0,	0,	0,	  0,0,1,0,7 },
    279 	{ "union",	T_SOU,		0,	UNION,	0,	  0,0,0,0,1 },
    280 	{ "unsigned",	T_TYPE,		0,	UNSIGN,	0,	  0,0,0,0,1 },
    281 	{ "unused",	T_AT_UNUSED,	0,	0,	0,	  0,0,1,1,5 },
    282 	{ "used",	T_AT_USED,	0,	0,	0,	  0,0,1,1,5 },
    283 	{ "visibility", T_AT_VISIBILITY,0,	0,	0,	  0,0,1,1,5 },
    284 	{ "void",	T_TYPE,		0,	VOID,	0,	  0,0,0,0,1 },
    285 	{ "volatile",	T_QUAL,		0,	0,	VOLATILE, 1,0,0,0,7 },
    286 	{ "weak",	T_AT_WEAK,	0,	0,	0,	  0,0,1,1,5 },
    287 	{ "while",	T_WHILE,	0,	0,	0,	  0,0,0,0,1 },
    288 	{ NULL,		0,		0,	0,	0,	  0,0,0,0,0 }
    289 };
    290 
    291 /* Symbol table */
    292 static	sym_t	*symtab[HSHSIZ1];
    293 
    294 /* bit i of the entry with index i is set */
    295 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
    296 
    297 /* least significant i bits are set in the entry with index i */
    298 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
    299 
    300 /* least significant i bits are not set in the entry with index i */
    301 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
    302 
    303 /* free list for sbuf structures */
    304 static	sbuf_t	 *sbfrlst;
    305 
    306 /* Typ of next expected symbol */
    307 symt_t	symtyp;
    308 
    309 
    310 static void
    311 addkw(struct kwtab *kw, int deco)
    312 {
    313 	sym_t *sym;
    314 	size_t h;
    315 	char buf[256];
    316 	const char *name;
    317 
    318 	if (!(kw->kw_deco & deco))
    319 		return;
    320 
    321 	switch (deco) {
    322 	case 1:
    323 		name = kw->kw_name;
    324 		break;
    325 	case 2:
    326 		snprintf(buf, sizeof(buf), "__%s", kw->kw_name);
    327 		name = strdup(buf);
    328 		break;
    329 	case 4:
    330 		snprintf(buf, sizeof(buf), "__%s__", kw->kw_name);
    331 		name = strdup(buf);
    332 		break;
    333 	default:
    334 		abort();
    335 	}
    336 
    337 	if (name == NULL)
    338 		err(1, "Can't init symbol table");
    339 
    340 	sym = getblk(sizeof (sym_t));
    341 	sym->s_name = name;
    342 	sym->s_keyw = kw;
    343 	sym->s_value.v_quad = kw->kw_token;
    344 	if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
    345 		sym->s_tspec = kw->kw_tspec;
    346 	} else if (kw->kw_token == T_SCLASS) {
    347 		sym->s_scl = kw->kw_scl;
    348 	} else if (kw->kw_token == T_QUAL) {
    349 		sym->s_tqual = kw->kw_tqual;
    350 	}
    351 	h = hash(sym->s_name);
    352 	if ((sym->s_link = symtab[h]) != NULL)
    353 		symtab[h]->s_rlink = &sym->s_link;
    354 	sym->s_rlink = &symtab[h];
    355 	symtab[h] = sym;
    356 }
    357 
    358 /*
    359  * All keywords are written to the symbol table. This saves us looking
    360  * in a extra table for each name we found.
    361  */
    362 void
    363 initscan(void)
    364 {
    365 	struct	kwtab *kw;
    366 	size_t	i;
    367 	uint64_t uq;
    368 
    369 	for (kw = kwtab; kw->kw_name != NULL; kw++) {
    370 		if ((kw->kw_c89 || kw->kw_c99) && tflag)
    371 			continue;
    372 		if (kw->kw_c99 && !(Sflag || gflag))
    373 			continue;
    374 		if (kw->kw_gcc && !gflag)
    375 			continue;
    376 		addkw(kw, 1);
    377 		addkw(kw, 2);
    378 		addkw(kw, 4);
    379 	}
    380 
    381 	/* initialize bit-masks for quads */
    382 	for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
    383 		qbmasks[i] = (uint64_t)1 << i;
    384 		uq = ~(uint64_t)0 << i;
    385 		qumasks[i] = uq;
    386 		qlmasks[i] = ~uq;
    387 	}
    388 	qumasks[i] = 0;
    389 	qlmasks[i] = ~(uint64_t)0;
    390 }
    391 
    392 /*
    393  * Get a free sbuf structure, if possible from the free list
    394  */
    395 static sbuf_t *
    396 allocsb(void)
    397 {
    398 	sbuf_t	*sb;
    399 
    400 	if ((sb = sbfrlst) != NULL) {
    401 		sbfrlst = sb->sb_nxt;
    402 #ifdef BLKDEBUG
    403 		(void)memset(sb, 0, sizeof (*sb));
    404 #else
    405 		sb->sb_nxt = NULL;
    406 #endif
    407 	} else {
    408 		sb = xmalloc(sizeof (sbuf_t));
    409 		(void)memset(sb, 0, sizeof (*sb));
    410 	}
    411 	return (sb);
    412 }
    413 
    414 /*
    415  * Put a sbuf structure to the free list
    416  */
    417 static void
    418 freesb(sbuf_t *sb)
    419 {
    420 
    421 	(void)memset(sb, ZERO, sizeof (*sb));
    422 	sb->sb_nxt = sbfrlst;
    423 	sbfrlst = sb;
    424 }
    425 
    426 /*
    427  * Read a character and ensure that it is positive (except EOF).
    428  * Increment line count(s) if necessary.
    429  */
    430 static int
    431 inpc(void)
    432 {
    433 	int	c;
    434 
    435 	if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
    436 		incline();
    437 	return (c);
    438 }
    439 
    440 static int
    441 hash(const char *s)
    442 {
    443 	u_int	v;
    444 	const	u_char *us;
    445 
    446 	v = 0;
    447 	for (us = (const u_char *)s; *us != '\0'; us++) {
    448 		v = (v << sizeof (v)) + *us;
    449 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
    450 	}
    451 	return (v % HSHSIZ1);
    452 }
    453 
    454 /*
    455  * Lex has found a letter followed by zero or more letters or digits.
    456  * It looks for a symbol in the symbol table with the same name. This
    457  * symbol must either be a keyword or a symbol of the type required by
    458  * symtyp (label, member, tag, ...).
    459  *
    460  * If it is a keyword, the token is returned. In some cases it is described
    461  * more deeply by data written to yylval.
    462  *
    463  * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
    464  * is stored in yylval. This struct contains the name of the symbol, its
    465  * length and hash value. If there is already a symbol of the same name
    466  * and type in the symbol table, the sbuf struct also contains a pointer
    467  * to the symbol table entry.
    468  */
    469 static int
    470 name(void)
    471 {
    472 	char	*s;
    473 	sbuf_t	*sb;
    474 	sym_t	*sym;
    475 	int	tok;
    476 
    477 	sb = allocsb();
    478 	sb->sb_name = yytext;
    479 	sb->sb_len = yyleng;
    480 	sb->sb_hash = hash(yytext);
    481 	if ((sym = search(sb)) != NULL && sym->s_keyw) {
    482 		freesb(sb);
    483 		return (keyw(sym));
    484 	}
    485 
    486 	sb->sb_sym = sym;
    487 
    488 	if (sym != NULL) {
    489 		if (blklev < sym->s_blklev)
    490 			LERROR("name()");
    491 		sb->sb_name = sym->s_name;
    492 		sb->sb_len = strlen(sym->s_name);
    493 		tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
    494 	} else {
    495 		s = getblk(yyleng + 1);
    496 		(void)memcpy(s, yytext, yyleng + 1);
    497 		sb->sb_name = s;
    498 		sb->sb_len = yyleng;
    499 		tok = T_NAME;
    500 	}
    501 
    502 	yylval.y_sb = sb;
    503 	return (tok);
    504 }
    505 
    506 static sym_t *
    507 search(sbuf_t *sb)
    508 {
    509 	sym_t	*sym;
    510 
    511 	for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
    512 		if (strcmp(sym->s_name, sb->sb_name) == 0) {
    513 			if (sym->s_keyw) {
    514 				struct kwtab *kw = sym->s_keyw;
    515 				if (!kw->kw_attr || attron)
    516 					return (sym);
    517 			} else if (!attron && sym->s_kind == symtyp)
    518 				return sym;
    519 		}
    520 	}
    521 
    522 	return (NULL);
    523 }
    524 
    525 static int
    526 keyw(sym_t *sym)
    527 {
    528 	int	t;
    529 
    530 	if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
    531 		yylval.y_scl = sym->s_scl;
    532 	} else if (t == T_TYPE || t == T_SOU) {
    533 		yylval.y_tspec = sym->s_tspec;
    534 	} else if (t == T_QUAL) {
    535 		yylval.y_tqual = sym->s_tqual;
    536 	}
    537 	return (t);
    538 }
    539 
    540 /*
    541  * Convert a string representing an integer into internal representation.
    542  * The value is returned in yylval. icon() (and yylex()) returns T_CON.
    543  */
    544 static int
    545 icon(int base)
    546 {
    547 	int	l_suffix, u_suffix;
    548 	int	len;
    549 	const	char *cp;
    550 	char	c, *eptr;
    551 	tspec_t	typ;
    552 	uint64_t uq = 0;
    553 	int	ansiu;
    554 	static	tspec_t contypes[2][3] = {
    555 		{ INT,  LONG,  QUAD },
    556 		{ UINT, ULONG, UQUAD }
    557 	};
    558 
    559 	cp = yytext;
    560 	len = yyleng;
    561 
    562 	/* skip 0[xX] or 0[bB] */
    563 	if (base == 16 || base == 2) {
    564 		cp += 2;
    565 		len -= 2;
    566 	}
    567 
    568 	/* read suffixes */
    569 	l_suffix = u_suffix = 0;
    570 	for ( ; ; ) {
    571 		if ((c = cp[len - 1]) == 'l' || c == 'L') {
    572 			l_suffix++;
    573 		} else if (c == 'u' || c == 'U') {
    574 			u_suffix++;
    575 		} else {
    576 			break;
    577 		}
    578 		len--;
    579 	}
    580 	if (l_suffix > 2 || u_suffix > 1) {
    581 		/* malformed integer constant */
    582 		warning(251);
    583 		if (l_suffix > 2)
    584 			l_suffix = 2;
    585 		if (u_suffix > 1)
    586 			u_suffix = 1;
    587 	}
    588 	if (tflag && u_suffix != 0) {
    589 		/* suffix U is illegal in traditional C */
    590 		warning(97);
    591 	}
    592 	typ = contypes[u_suffix][l_suffix];
    593 
    594 	errno = 0;
    595 
    596 	uq = strtouq(cp, &eptr, base);
    597 	if (eptr != cp + len)
    598 		LERROR("icon()");
    599 	if (errno != 0)
    600 		/* integer constant out of range */
    601 		warning(252);
    602 
    603 	/*
    604 	 * If the value is too big for the current type, we must choose
    605 	 * another type.
    606 	 */
    607 	ansiu = 0;
    608 	switch (typ) {
    609 	case INT:
    610 		if (uq <= TARG_INT_MAX) {
    611 			/* ok */
    612 		} else if (uq <= TARG_UINT_MAX && base != 10) {
    613 			typ = UINT;
    614 		} else if (uq <= TARG_LONG_MAX) {
    615 			typ = LONG;
    616 		} else {
    617 			typ = ULONG;
    618 			if (uq > TARG_ULONG_MAX) {
    619 				/* integer constant out of range */
    620 				warning(252);
    621 			}
    622 		}
    623 		if (typ == UINT || typ == ULONG) {
    624 			if (tflag) {
    625 				typ = LONG;
    626 			} else if (!sflag) {
    627 				/*
    628 				 * Remember that the constant is unsigned
    629 				 * only in ANSI C
    630 				 */
    631 				ansiu = 1;
    632 			}
    633 		}
    634 		break;
    635 	case UINT:
    636 		if (uq > TARG_UINT_MAX) {
    637 			typ = ULONG;
    638 			if (uq > TARG_ULONG_MAX) {
    639 				/* integer constant out of range */
    640 				warning(252);
    641 			}
    642 		}
    643 		break;
    644 	case LONG:
    645 		if (uq > TARG_LONG_MAX && !tflag) {
    646 			typ = ULONG;
    647 			if (!sflag)
    648 				ansiu = 1;
    649 			if (uq > TARG_ULONG_MAX) {
    650 				/* integer constant out of range */
    651 				warning(252);
    652 			}
    653 		}
    654 		break;
    655 	case ULONG:
    656 		if (uq > TARG_ULONG_MAX) {
    657 			/* integer constant out of range */
    658 			warning(252);
    659 		}
    660 		break;
    661 	case QUAD:
    662 		if (uq > TARG_QUAD_MAX && !tflag) {
    663 			typ = UQUAD;
    664 			if (!sflag)
    665 				ansiu = 1;
    666 		}
    667 		break;
    668 	case UQUAD:
    669 		if (uq > TARG_UQUAD_MAX) {
    670 			/* integer constant out of range */
    671 			warning(252);
    672 		}
    673 		break;
    674 		/* LINTED206: (enumeration values not handled in switch) */
    675 	case STRUCT:
    676 	case VOID:
    677 	case LDOUBLE:
    678 	case FUNC:
    679 	case ARRAY:
    680 	case PTR:
    681 	case ENUM:
    682 	case UNION:
    683 	case SIGNED:
    684 	case NOTSPEC:
    685 	case DOUBLE:
    686 	case FLOAT:
    687 	case USHORT:
    688 	case SHORT:
    689 	case UCHAR:
    690 	case SCHAR:
    691 	case CHAR:
    692 	case BOOL:
    693 	case UNSIGN:
    694 	case FCOMPLEX:
    695 	case DCOMPLEX:
    696 	case LCOMPLEX:
    697 	case COMPLEX:
    698 		break;
    699 
    700 	case NTSPEC:	/* this value unused */
    701 		break;
    702 	}
    703 
    704 	uq = (uint64_t)xsign((int64_t)uq, typ, -1);
    705 
    706 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    707 	yylval.y_val->v_ansiu = ansiu;
    708 	yylval.y_val->v_quad = (int64_t)uq;
    709 
    710 	return (T_CON);
    711 }
    712 
    713 /*
    714  * Returns 1 if t is a signed type and the value is negative.
    715  *
    716  * len is the number of significant bits. If len is -1, len is set
    717  * to the width of type t.
    718  */
    719 int
    720 sign(int64_t q, tspec_t t, int len)
    721 {
    722 
    723 	if (t == PTR || isutyp(t))
    724 		return (0);
    725 	return (msb(q, t, len));
    726 }
    727 
    728 int
    729 msb(int64_t q, tspec_t t, int len)
    730 {
    731 
    732 	if (len <= 0)
    733 		len = size(t);
    734 	return ((q & qbmasks[len - 1]) != 0);
    735 }
    736 
    737 /*
    738  * Extends the sign of q.
    739  */
    740 int64_t
    741 xsign(int64_t q, tspec_t t, int len)
    742 {
    743 
    744 	if (len <= 0)
    745 		len = size(t);
    746 
    747 	if (t == PTR || isutyp(t) || !sign(q, t, len)) {
    748 		q &= qlmasks[len];
    749 	} else {
    750 		q |= qumasks[len];
    751 	}
    752 	return (q);
    753 }
    754 
    755 /*
    756  * Convert a string representing a floating point value into its interal
    757  * representation. Type and value are returned in yylval. fcon()
    758  * (and yylex()) returns T_CON.
    759  * XXX Currently it is not possible to convert constants of type
    760  * long double which are greater than DBL_MAX.
    761  */
    762 static int
    763 fcon(void)
    764 {
    765 	const	char *cp;
    766 	int	len;
    767 	tspec_t typ;
    768 	char	c, *eptr;
    769 	double	d;
    770 	float	f = 0;
    771 
    772 	cp = yytext;
    773 	len = yyleng;
    774 
    775 	if (cp[len - 1] == 'i') {
    776 		/* imaginary, do nothing for now */
    777 		len--;
    778 	}
    779 	if ((c = cp[len - 1]) == 'f' || c == 'F') {
    780 		typ = FLOAT;
    781 		len--;
    782 	} else if (c == 'l' || c == 'L') {
    783 		typ = LDOUBLE;
    784 		len--;
    785 	} else {
    786 		if (c == 'd' || c == 'D')
    787 			len--;
    788 		typ = DOUBLE;
    789 	}
    790 
    791 	if (tflag && typ != DOUBLE) {
    792 		/* suffixes F and L are illegal in traditional C */
    793 		warning(98);
    794 	}
    795 
    796 	errno = 0;
    797 	d = strtod(cp, &eptr);
    798 	if (eptr != cp + len) {
    799 		switch (*eptr) {
    800 		/*
    801 		 * XXX: non-native non-current strtod() may not handle hex
    802 		 * floats, ignore the rest if we find traces of hex float
    803 		 * syntax...
    804 		 */
    805 		case 'p':
    806 		case 'P':
    807 		case 'x':
    808 		case 'X':
    809 			d = 0;
    810 			errno = 0;
    811 			break;
    812 		default:
    813 			LERROR("fcon(%s->%s)", cp, eptr);
    814 		}
    815 	}
    816 	if (errno != 0)
    817 		/* floating-point constant out of range */
    818 		warning(248);
    819 
    820 	if (typ == FLOAT) {
    821 		f = (float)d;
    822 		if (!finite(f)) {
    823 			/* floating-point constant out of range */
    824 			warning(248);
    825 			f = f > 0 ? FLT_MAX : -FLT_MAX;
    826 		}
    827 	}
    828 
    829 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    830 	if (typ == FLOAT) {
    831 		yylval.y_val->v_ldbl = f;
    832 	} else {
    833 		yylval.y_val->v_ldbl = d;
    834 	}
    835 
    836 	return (T_CON);
    837 }
    838 
    839 static int
    840 operator(int t, op_t o)
    841 {
    842 
    843 	yylval.y_op = o;
    844 	return (t);
    845 }
    846 
    847 /*
    848  * Called if lex found a leading \'.
    849  */
    850 static int
    851 ccon(void)
    852 {
    853 	size_t	n;
    854 	int val, c;
    855 	char	cv;
    856 
    857 	n = 0;
    858 	val = 0;
    859 	while ((c = getescc('\'')) >= 0) {
    860 		val = (val << CHAR_BIT) + c;
    861 		n++;
    862 	}
    863 	if (c == -2) {
    864 		/* unterminated character constant */
    865 		error(253);
    866 	} else {
    867 		if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
    868 			/* too many characters in character constant */
    869 			error(71);
    870 		} else if (n > 1) {
    871 			/* multi-character character constant */
    872 			warning(294);
    873 		} else if (n == 0) {
    874 			/* empty character constant */
    875 			error(73);
    876 		}
    877 	}
    878 	if (n == 1) {
    879 		cv = (char)val;
    880 		val = cv;
    881 	}
    882 
    883 	yylval.y_val = xcalloc(1, sizeof (val_t));
    884 	yylval.y_val->v_tspec = INT;
    885 	yylval.y_val->v_quad = val;
    886 
    887 	return (T_CON);
    888 }
    889 
    890 /*
    891  * Called if lex found a leading L\'
    892  */
    893 static int
    894 wccon(void)
    895 {
    896 	static	char buf[MB_LEN_MAX + 1];
    897 	size_t	i;
    898 	int c;
    899 	wchar_t	wc;
    900 
    901 	i = 0;
    902 	while ((c = getescc('\'')) >= 0) {
    903 		if (i < MB_CUR_MAX)
    904 			buf[i] = (char)c;
    905 		i++;
    906 	}
    907 
    908 	wc = 0;
    909 
    910 	if (c == -2) {
    911 		/* unterminated character constant */
    912 		error(253);
    913 	} else if (c == 0) {
    914 		/* empty character constant */
    915 		error(73);
    916 	} else {
    917 		if (i > MB_CUR_MAX) {
    918 			i = MB_CUR_MAX;
    919 			/* too many characters in character constant */
    920 			error(71);
    921 		} else {
    922 			buf[i] = '\0';
    923 			(void)mbtowc(NULL, NULL, 0);
    924 			if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
    925 				/* invalid multibyte character */
    926 				error(291);
    927 		}
    928 	}
    929 
    930 	yylval.y_val = xcalloc(1, sizeof (val_t));
    931 	yylval.y_val->v_tspec = WCHAR;
    932 	yylval.y_val->v_quad = wc;
    933 
    934 	return (T_CON);
    935 }
    936 
    937 /*
    938  * Read a character which is part of a character constant or of a string
    939  * and handle escapes.
    940  *
    941  * The Argument is the character which delimits the character constant or
    942  * string.
    943  *
    944  * Returns -1 if the end of the character constant or string is reached,
    945  * -2 if the EOF is reached, and the character otherwise.
    946  */
    947 static int
    948 getescc(int d)
    949 {
    950 	static	int pbc = -1;
    951 	int	n, c, v;
    952 
    953 	if (pbc == -1) {
    954 		c = inpc();
    955 	} else {
    956 		c = pbc;
    957 		pbc = -1;
    958 	}
    959 	if (c == d)
    960 		return (-1);
    961 	switch (c) {
    962 	case '\n':
    963 		if (tflag) {
    964 			/* newline in string or char constant */
    965 			error(254);
    966 			return (-2);
    967 		}
    968 		return (c);
    969 	case EOF:
    970 		return (-2);
    971 	case '\\':
    972 		switch (c = inpc()) {
    973 		case '"':
    974 			if (tflag && d == '\'')
    975 				/* \" inside character constant undef. ... */
    976 				warning(262);
    977 			return ('"');
    978 		case '\'':
    979 			return ('\'');
    980 		case '?':
    981 			if (tflag)
    982 				/* \? undefined in traditional C */
    983 				warning(263);
    984 			return ('?');
    985 		case '\\':
    986 			return ('\\');
    987 		case 'a':
    988 			if (tflag)
    989 				/* \a undefined in traditional C */
    990 				warning(81);
    991 			return ('\a');
    992 		case 'b':
    993 			return ('\b');
    994 		case 'f':
    995 			return ('\f');
    996 		case 'n':
    997 			return ('\n');
    998 		case 'r':
    999 			return ('\r');
   1000 		case 't':
   1001 			return ('\t');
   1002 		case 'v':
   1003 			if (tflag)
   1004 				/* \v undefined in traditional C */
   1005 				warning(264);
   1006 			return ('\v');
   1007 		case '8': case '9':
   1008 			/* bad octal digit %c */
   1009 			warning(77, c);
   1010 			/* FALLTHROUGH */
   1011 		case '0': case '1': case '2': case '3':
   1012 		case '4': case '5': case '6': case '7':
   1013 			n = 3;
   1014 			v = 0;
   1015 			do {
   1016 				v = (v << 3) + (c - '0');
   1017 				c = inpc();
   1018 			} while (--n && isdigit(c) && (tflag || c <= '7'));
   1019 			if (tflag && n > 0 && isdigit(c))
   1020 				/* bad octal digit %c */
   1021 				warning(77, c);
   1022 			pbc = c;
   1023 			if (v > TARG_UCHAR_MAX) {
   1024 				/* character escape does not fit in char. */
   1025 				warning(76);
   1026 				v &= CHAR_MASK;
   1027 			}
   1028 			return (v);
   1029 		case 'x':
   1030 			if (tflag)
   1031 				/* \x undefined in traditional C */
   1032 				warning(82);
   1033 			v = 0;
   1034 			n = 0;
   1035 			while ((c = inpc()) >= 0 && isxdigit(c)) {
   1036 				c = isdigit(c) ?
   1037 					c - '0' : toupper(c) - 'A' + 10;
   1038 				v = (v << 4) + c;
   1039 				if (n >= 0) {
   1040 					if ((v & ~CHAR_MASK) != 0) {
   1041 						/* overflow in hex escape */
   1042 						warning(75);
   1043 						n = -1;
   1044 					} else {
   1045 						n++;
   1046 					}
   1047 				}
   1048 			}
   1049 			pbc = c;
   1050 			if (n == 0) {
   1051 				/* no hex digits follow \x */
   1052 				error(74);
   1053 			} if (n == -1) {
   1054 				v &= CHAR_MASK;
   1055 			}
   1056 			return (v);
   1057 		case '\n':
   1058 			return (getescc(d));
   1059 		case EOF:
   1060 			return (-2);
   1061 		default:
   1062 			if (isprint(c)) {
   1063 				/* dubious escape \%c */
   1064 				warning(79, c);
   1065 			} else {
   1066 				/* dubious escape \%o */
   1067 				warning(80, c);
   1068 			}
   1069 		}
   1070 	}
   1071 	return (c);
   1072 }
   1073 
   1074 /*
   1075  * Called for preprocessor directives. Currently implemented are:
   1076  *	# lineno
   1077  *	# lineno "filename"
   1078  */
   1079 static void
   1080 directive(void)
   1081 {
   1082 	const	char *cp, *fn;
   1083 	char	c, *eptr;
   1084 	size_t	fnl;
   1085 	long	ln;
   1086 	static	int first = 1;
   1087 
   1088 	/* Go to first non-whitespace after # */
   1089 	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
   1090 		continue;
   1091 
   1092 	if (!isdigit((unsigned char)c)) {
   1093 		if (strncmp(cp, "pragma", 6) == 0
   1094 		    && isspace((unsigned char)cp[6]))
   1095 			return;
   1096 	error:
   1097 		/* undefined or invalid # directive */
   1098 		warning(255);
   1099 		return;
   1100 	}
   1101 	ln = strtol(--cp, &eptr, 10);
   1102 	if (cp == eptr)
   1103 		goto error;
   1104 	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
   1105 		goto error;
   1106 	while ((c = *cp++) == ' ' || c == '\t')
   1107 		continue;
   1108 	if (c != '\0') {
   1109 		if (c != '"')
   1110 			goto error;
   1111 		fn = cp;
   1112 		while ((c = *cp) != '"' && c != '\0')
   1113 			cp++;
   1114 		if (c != '"')
   1115 			goto error;
   1116 		if ((fnl = cp++ - fn) > PATH_MAX)
   1117 			goto error;
   1118 		while ((c = *cp++) == ' ' || c == '\t')
   1119 			continue;
   1120 #if 0
   1121 		if (c != '\0')
   1122 			warning("extra character(s) after directive");
   1123 #endif
   1124 
   1125 		/* empty string means stdin */
   1126 		if (fnl == 0) {
   1127 			fn = "{standard input}";
   1128 			fnl = 16;			/* strlen (fn) */
   1129 		}
   1130 		curr_pos.p_file = fnnalloc(fn, fnl);
   1131 		/*
   1132 		 * If this is the first directive, the name is the name
   1133 		 * of the C source file as specified at the command line.
   1134 		 * It is written to the output file.
   1135 		 */
   1136 		if (first) {
   1137 			csrc_pos.p_file = curr_pos.p_file;
   1138 			outsrc(fnxform(curr_pos.p_file,
   1139 			    strlen(curr_pos.p_file)));
   1140 			first = 0;
   1141 		}
   1142 	}
   1143 	curr_pos.p_line = (int)ln - 1;
   1144 	curr_pos.p_uniq = 0;
   1145 	if (curr_pos.p_file == csrc_pos.p_file) {
   1146 		csrc_pos.p_line = (int)ln - 1;
   1147 		csrc_pos.p_uniq = 0;
   1148 	}
   1149 }
   1150 
   1151 /*
   1152  * Handle lint comments. Following comments are currently understood:
   1153  *	ARGSUSEDn
   1154  *	BITFIELDTYPE
   1155  *	CONSTCOND CONSTANTCOND CONSTANTCONDITION
   1156  *	FALLTHRU FALLTHROUGH
   1157  *	LINTLIBRARY
   1158  *	LINTEDn NOSTRICTn
   1159  *	LONGLONG
   1160  *	NOTREACHED
   1161  *	PRINTFLIKEn
   1162  *	PROTOLIB
   1163  *	SCANFLIKEn
   1164  *	VARARGSn
   1165  * If one of this comments is recognized, the arguments, if any, are
   1166  * parsed and a function which handles this comment is called.
   1167  */
   1168 static void
   1169 comment(void)
   1170 {
   1171 	int	c, lc;
   1172 	static struct {
   1173 		const	char *keywd;
   1174 		int	arg;
   1175 		void	(*func)(int);
   1176 	} keywtab[] = {
   1177 		{ "ARGSUSED",		1,	argsused	},
   1178 		{ "BITFIELDTYPE",	0,	bitfieldtype	},
   1179 		{ "CONSTCOND",		0,	constcond	},
   1180 		{ "CONSTANTCOND",	0,	constcond	},
   1181 		{ "CONSTANTCONDITION",	0,	constcond	},
   1182 		{ "FALLTHRU",		0,	fallthru	},
   1183 		{ "FALLTHROUGH",	0,	fallthru	},
   1184 		{ "LINTLIBRARY",	0,	lintlib		},
   1185 		{ "LINTED",		1,	linted		},
   1186 		{ "LONGLONG",		0,	longlong	},
   1187 		{ "NOSTRICT",		1,	linted		},
   1188 		{ "NOTREACHED",		0,	notreach	},
   1189 		{ "PRINTFLIKE",		1,	printflike	},
   1190 		{ "PROTOLIB",		1,	protolib	},
   1191 		{ "SCANFLIKE",		1,	scanflike	},
   1192 		{ "VARARGS",		1,	varargs		},
   1193 	};
   1194 	char	keywd[32];
   1195 	char	arg[32];
   1196 	size_t	l, i;
   1197 	int	a;
   1198 	int	eoc;
   1199 
   1200 	eoc = 0;
   1201 
   1202 	/* Skip white spaces after the start of the comment */
   1203 	while ((c = inpc()) != EOF && isspace(c))
   1204 		continue;
   1205 
   1206 	/* Read the potential keyword to keywd */
   1207 	l = 0;
   1208 	while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
   1209 		keywd[l++] = (char)c;
   1210 		c = inpc();
   1211 	}
   1212 	keywd[l] = '\0';
   1213 
   1214 	/* look for the keyword */
   1215 	for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
   1216 		if (strcmp(keywtab[i].keywd, keywd) == 0)
   1217 			break;
   1218 	}
   1219 	if (i == sizeof (keywtab) / sizeof (keywtab[0]))
   1220 		goto skip_rest;
   1221 
   1222 	/* skip white spaces after the keyword */
   1223 	while (c != EOF && isspace(c))
   1224 		c = inpc();
   1225 
   1226 	/* read the argument, if the keyword accepts one and there is one */
   1227 	l = 0;
   1228 	if (keywtab[i].arg) {
   1229 		while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
   1230 			arg[l++] = (char)c;
   1231 			c = inpc();
   1232 		}
   1233 	}
   1234 	arg[l] = '\0';
   1235 	a = l != 0 ? atoi(arg) : -1;
   1236 
   1237 	/* skip white spaces after the argument */
   1238 	while (c != EOF && isspace(c))
   1239 		c = inpc();
   1240 
   1241 	if (c != '*' || (c = inpc()) != '/') {
   1242 		if (keywtab[i].func != linted)
   1243 			/* extra characters in lint comment */
   1244 			warning(257);
   1245 	} else {
   1246 		/*
   1247 		 * remember that we have already found the end of the
   1248 		 * comment
   1249 		 */
   1250 		eoc = 1;
   1251 	}
   1252 
   1253 	if (keywtab[i].func != NULL)
   1254 		(*keywtab[i].func)(a);
   1255 
   1256  skip_rest:
   1257 	while (!eoc) {
   1258 		lc = c;
   1259 		if ((c = inpc()) == EOF) {
   1260 			/* unterminated comment */
   1261 			error(256);
   1262 			break;
   1263 		}
   1264 		if (lc == '*' && c == '/')
   1265 			eoc = 1;
   1266 	}
   1267 }
   1268 
   1269 /*
   1270  * Handle // style comments
   1271  */
   1272 static void
   1273 slashslashcomment(void)
   1274 {
   1275 	int c;
   1276 
   1277 	if (!Sflag && !gflag)
   1278 		/* // comments only supported in C99 */
   1279 		(void)gnuism(312, tflag ? "traditional" : "ANSI");
   1280 
   1281 	while ((c = inpc()) != EOF && c != '\n')
   1282 		continue;
   1283 }
   1284 
   1285 /*
   1286  * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
   1287  * clrwflgs() is called after function definitions and global and
   1288  * local declarations and definitions. It is also called between
   1289  * the controlling expression and the body of control statements
   1290  * (if, switch, for, while).
   1291  */
   1292 void
   1293 clrwflgs(void)
   1294 {
   1295 
   1296 	lwarn = LWARN_ALL;
   1297 	quadflg = 0;
   1298 	ccflg = 0;
   1299 }
   1300 
   1301 /*
   1302  * Strings are stored in a dynamically alloceted buffer and passed
   1303  * in yylval.y_xstrg to the parser. The parser or the routines called
   1304  * by the parser are responsible for freeing this buffer.
   1305  */
   1306 static int
   1307 string(void)
   1308 {
   1309 	u_char	*s;
   1310 	int	c;
   1311 	size_t	len, max;
   1312 	strg_t	*strg;
   1313 
   1314 	s = xmalloc(max = 64);
   1315 
   1316 	len = 0;
   1317 	while ((c = getescc('"')) >= 0) {
   1318 		/* +1 to reserve space for a trailing NUL character */
   1319 		if (len + 1 == max)
   1320 			s = xrealloc(s, max *= 2);
   1321 		s[len++] = (char)c;
   1322 	}
   1323 	s[len] = '\0';
   1324 	if (c == -2)
   1325 		/* unterminated string constant */
   1326 		error(258);
   1327 
   1328 	strg = xcalloc(1, sizeof (strg_t));
   1329 	strg->st_tspec = CHAR;
   1330 	strg->st_len = len;
   1331 	strg->st_cp = s;
   1332 
   1333 	yylval.y_strg = strg;
   1334 	return (T_STRING);
   1335 }
   1336 
   1337 static int
   1338 wcstrg(void)
   1339 {
   1340 	char	*s;
   1341 	int	c, n;
   1342 	size_t	i, wi;
   1343 	size_t	len, max, wlen;
   1344 	wchar_t	*ws;
   1345 	strg_t	*strg;
   1346 
   1347 	s = xmalloc(max = 64);
   1348 	len = 0;
   1349 	while ((c = getescc('"')) >= 0) {
   1350 		/* +1 to save space for a trailing NUL character */
   1351 		if (len + 1 >= max)
   1352 			s = xrealloc(s, max *= 2);
   1353 		s[len++] = (char)c;
   1354 	}
   1355 	s[len] = '\0';
   1356 	if (c == -2)
   1357 		/* unterminated string constant */
   1358 		error(258);
   1359 
   1360 	/* get length of wide-character string */
   1361 	(void)mblen(NULL, 0);
   1362 	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
   1363 		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
   1364 			/* invalid multibyte character */
   1365 			error(291);
   1366 			break;
   1367 		}
   1368 		if (n == 0)
   1369 			n = 1;
   1370 	}
   1371 
   1372 	ws = xmalloc((wlen + 1) * sizeof (wchar_t));
   1373 
   1374 	/* convert from multibyte to wide char */
   1375 	(void)mbtowc(NULL, NULL, 0);
   1376 	for (i = 0, wi = 0; i < len; i += n, wi++) {
   1377 		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
   1378 			break;
   1379 		if (n == 0)
   1380 			n = 1;
   1381 	}
   1382 	ws[wi] = 0;
   1383 	free(s);
   1384 
   1385 	strg = xcalloc(1, sizeof (strg_t));
   1386 	strg->st_tspec = WCHAR;
   1387 	strg->st_len = wlen;
   1388 	strg->st_wcp = ws;
   1389 
   1390 	yylval.y_strg = strg;
   1391 	return (T_STRING);
   1392 }
   1393 
   1394 /*
   1395  * As noted above the scanner does not create new symbol table entries
   1396  * for symbols it cannot find in the symbol table. This is to avoid
   1397  * putting undeclared symbols into the symbol table if a syntax error
   1398  * occurs.
   1399  *
   1400  * getsym() is called as soon as it is probably ok to put the symbol to
   1401  * the symbol table. This does not mean that it is not possible that
   1402  * symbols are put to the symbol table which are than not completely
   1403  * declared due to syntax errors. To avoid too many problems in this
   1404  * case symbols get type int in getsym().
   1405  *
   1406  * XXX calls to getsym() should be delayed until decl1*() is called
   1407  */
   1408 sym_t *
   1409 getsym(sbuf_t *sb)
   1410 {
   1411 	dinfo_t	*di;
   1412 	char	*s;
   1413 	sym_t	*sym;
   1414 
   1415 	sym = sb->sb_sym;
   1416 
   1417 	/*
   1418 	 * During member declaration it is possible that name() looked
   1419 	 * for symbols of type FVFT, although it should have looked for
   1420 	 * symbols of type FTAG. Same can happen for labels. Both cases
   1421 	 * are compensated here.
   1422 	 */
   1423 	if (symtyp == FMOS || symtyp == FLAB) {
   1424 		if (sym == NULL || sym->s_kind == FVFT)
   1425 			sym = search(sb);
   1426 	}
   1427 
   1428 	if (sym != NULL) {
   1429 		if (sym->s_kind != symtyp)
   1430 			LERROR("storesym(%d, %d)", sym->s_kind, symtyp);
   1431 		symtyp = FVFT;
   1432 		freesb(sb);
   1433 		return (sym);
   1434 	}
   1435 
   1436 	/* create a new symbol table entry */
   1437 
   1438 	/* labels must always be allocated at level 1 (outhermost block) */
   1439 	if (symtyp == FLAB) {
   1440 		sym = getlblk(1, sizeof (sym_t));
   1441 		s = getlblk(1, sb->sb_len + 1);
   1442 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1443 		sym->s_name = s;
   1444 		sym->s_blklev = 1;
   1445 		di = dcs;
   1446 		while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
   1447 			di = di->d_nxt;
   1448 		if (di->d_ctx != AUTO)
   1449 			LERROR("storesym()");
   1450 	} else {
   1451 		sym = getblk(sizeof (sym_t));
   1452 		sym->s_name = sb->sb_name;
   1453 		sym->s_blklev = blklev;
   1454 		di = dcs;
   1455 	}
   1456 
   1457 	UNIQUE_CURR_POS(sym->s_dpos);
   1458 	if ((sym->s_kind = symtyp) != FLAB)
   1459 		sym->s_type = gettyp(INT);
   1460 
   1461 	symtyp = FVFT;
   1462 
   1463 	if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
   1464 		symtab[sb->sb_hash]->s_rlink = &sym->s_link;
   1465 	sym->s_rlink = &symtab[sb->sb_hash];
   1466 	symtab[sb->sb_hash] = sym;
   1467 
   1468 	*di->d_ldlsym = sym;
   1469 	di->d_ldlsym = &sym->s_dlnxt;
   1470 
   1471 	freesb(sb);
   1472 	return (sym);
   1473 }
   1474 
   1475 /*
   1476  * Construct a temporary symbol. The symbol starts with a digit, so that
   1477  * it is illegal.
   1478  */
   1479 sym_t *
   1480 mktempsym(type_t *t)
   1481 {
   1482 	static int n = 0;
   1483 	int h;
   1484 	char *s = getlblk(blklev, 64);
   1485 	sym_t *sym = getblk(sizeof (sym_t));
   1486 
   1487 	(void)snprintf(s, 64, "%.8d_tmp", n++);
   1488 	h = hash(s);
   1489 
   1490 	sym->s_name = s;
   1491 	sym->s_type = t;
   1492 	sym->s_blklev = blklev;
   1493 	sym->s_scl = AUTO;
   1494 	sym->s_kind = FVFT;
   1495 	sym->s_used = 1;
   1496 	sym->s_set = 1;
   1497 
   1498 	if ((sym->s_link = symtab[h]) != NULL)
   1499 		symtab[h]->s_rlink = &sym->s_link;
   1500 	sym->s_rlink = &symtab[h];
   1501 	symtab[h] = sym;
   1502 
   1503 	*dcs->d_ldlsym = sym;
   1504 	dcs->d_ldlsym = &sym->s_dlnxt;
   1505 
   1506 	return sym;
   1507 }
   1508 
   1509 /*
   1510  * Remove a symbol forever from the symbol table. s_blklev
   1511  * is set to -1 to avoid that the symbol will later be put
   1512  * back to the symbol table.
   1513  */
   1514 void
   1515 rmsym(sym_t *sym)
   1516 {
   1517 
   1518 	if ((*sym->s_rlink = sym->s_link) != NULL)
   1519 		sym->s_link->s_rlink = sym->s_rlink;
   1520 	sym->s_blklev = -1;
   1521 	sym->s_link = NULL;
   1522 }
   1523 
   1524 /*
   1525  * Remove a list of symbols declared at one level from the symbol
   1526  * table.
   1527  */
   1528 void
   1529 rmsyms(sym_t *syms)
   1530 {
   1531 	sym_t	*sym;
   1532 
   1533 	for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
   1534 		if (sym->s_blklev != -1) {
   1535 			if ((*sym->s_rlink = sym->s_link) != NULL)
   1536 				sym->s_link->s_rlink = sym->s_rlink;
   1537 			sym->s_link = NULL;
   1538 			sym->s_rlink = NULL;
   1539 		}
   1540 	}
   1541 }
   1542 
   1543 /*
   1544  * Put a symbol into the symbol table
   1545  */
   1546 void
   1547 inssym(int bl, sym_t *sym)
   1548 {
   1549 	int	h;
   1550 
   1551 	h = hash(sym->s_name);
   1552 	if ((sym->s_link = symtab[h]) != NULL)
   1553 		symtab[h]->s_rlink = &sym->s_link;
   1554 	sym->s_rlink = &symtab[h];
   1555 	symtab[h] = sym;
   1556 	sym->s_blklev = bl;
   1557 	if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
   1558 		LERROR("inssym()");
   1559 }
   1560 
   1561 /*
   1562  * Called at level 0 after syntax errors
   1563  * Removes all symbols which are not declared at level 0 from the
   1564  * symbol table. Also frees all memory which is not associated with
   1565  * level 0.
   1566  */
   1567 void
   1568 cleanup(void)
   1569 {
   1570 	sym_t	*sym, *nsym;
   1571 	int	i;
   1572 
   1573 	for (i = 0; i < HSHSIZ1; i++) {
   1574 		for (sym = symtab[i]; sym != NULL; sym = nsym) {
   1575 			nsym = sym->s_link;
   1576 			if (sym->s_blklev >= 1) {
   1577 				if ((*sym->s_rlink = nsym) != NULL)
   1578 					nsym->s_rlink = sym->s_rlink;
   1579 			}
   1580 		}
   1581 	}
   1582 
   1583 	for (i = mblklev; i > 0; i--)
   1584 		freelblk(i);
   1585 }
   1586 
   1587 /*
   1588  * Create a new symbol with the name of an existing symbol.
   1589  */
   1590 sym_t *
   1591 pushdown(sym_t *sym)
   1592 {
   1593 	int	h;
   1594 	sym_t	*nsym;
   1595 
   1596 	h = hash(sym->s_name);
   1597 	nsym = getblk(sizeof (sym_t));
   1598 	if (sym->s_blklev > blklev)
   1599 		LERROR("pushdown()");
   1600 	nsym->s_name = sym->s_name;
   1601 	UNIQUE_CURR_POS(nsym->s_dpos);
   1602 	nsym->s_kind = sym->s_kind;
   1603 	nsym->s_blklev = blklev;
   1604 
   1605 	if ((nsym->s_link = symtab[h]) != NULL)
   1606 		symtab[h]->s_rlink = &nsym->s_link;
   1607 	nsym->s_rlink = &symtab[h];
   1608 	symtab[h] = nsym;
   1609 
   1610 	*dcs->d_ldlsym = nsym;
   1611 	dcs->d_ldlsym = &nsym->s_dlnxt;
   1612 
   1613 	return (nsym);
   1614 }
   1615 
   1616 /*
   1617  * Free any dynamically allocated memory referenced by
   1618  * the value stack or yylval.
   1619  * The type of information in yylval is described by tok.
   1620  */
   1621 void
   1622 freeyyv(void *sp, int tok)
   1623 {
   1624 	if (tok == T_NAME || tok == T_TYPENAME) {
   1625 		sbuf_t *sb = *(sbuf_t **)sp;
   1626 		freesb(sb);
   1627 	} else if (tok == T_CON) {
   1628 		val_t *val = *(val_t **)sp;
   1629 		free(val);
   1630 	} else if (tok == T_STRING) {
   1631 		strg_t *strg = *(strg_t **)sp;
   1632 		if (strg->st_tspec == CHAR) {
   1633 			free(strg->st_cp);
   1634 		} else if (strg->st_tspec == WCHAR) {
   1635 			free(strg->st_wcp);
   1636 		} else {
   1637 			LERROR("fryylv()");
   1638 		}
   1639 		free(strg);
   1640 	}
   1641 }
   1642