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