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