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