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