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