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