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