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