Home | History | Annotate | Line # | Download | only in lint1
lex.c revision 1.223
      1 /* $NetBSD: lex.c,v 1.223 2024/03/29 08:35:32 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)
     41 __RCSID("$NetBSD: lex.c,v 1.223 2024/03/29 08:35:32 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 bool in_gcc_attribute;
     68 bool in_system_header;
     69 
     70 /*
     71  * Valid values for 'since' are 78, 90, 99, 11, 23.
     72  *
     73  * The C11 keywords are all taken from the reserved namespace.  They are added
     74  * in C99 mode as well, to make the parse error messages more useful.  For
     75  * example, if the keyword '_Generic' were not defined, it would be interpreted
     76  * as an implicit function call, leading to a parse error.
     77  *
     78  * The C23 keywords are not made available in earlier modes, as they may
     79  * conflict with user-defined identifiers.
     80  */
     81 #define kwdef(name, token, detail,	since, gcc, deco) \
     82 	{ /* CONSTCOND */ \
     83 		name, token, detail, \
     84 		(since) == 90, \
     85 		(since) == 99 || (since) == 11, \
     86 		(since) == 23, \
     87 		(gcc) > 0, \
     88 		((deco) & 1) != 0, ((deco) & 2) != 0, ((deco) & 4) != 0, \
     89 	}
     90 #define kwdef_token(name, token,		since, gcc, deco) \
     91 	kwdef(name, token, {false},		since, gcc, deco)
     92 #define kwdef_sclass(name, sclass,		since, gcc, deco) \
     93 	kwdef(name, T_SCLASS, .u.kw_scl = (sclass), since, gcc, deco)
     94 #define kwdef_type(name, tspec,			since) \
     95 	kwdef(name, T_TYPE, .u.kw_tspec = (tspec), since, 0, 1)
     96 #define kwdef_tqual(name, tqual,		since, gcc, deco) \
     97 	kwdef(name, T_QUAL, .u.kw_tqual = {.tqual = true}, since, gcc, deco)
     98 #define kwdef_keyword(name, token) \
     99 	kwdef(name, token, {false},		78, 0, 1)
    100 
    101 /* During initialization, these keywords are written to the symbol table. */
    102 static const struct keyword {
    103 	const	char kw_name[20];
    104 	int	kw_token;	/* token to be returned by yylex() */
    105 	union {
    106 		bool kw_dummy;
    107 		scl_t kw_scl;		/* if kw_token is T_SCLASS */
    108 		tspec_t kw_tspec;	/* if kw_token is T_TYPE or
    109 					 * T_STRUCT_OR_UNION */
    110 		type_qualifiers kw_tqual;	/* if kw_token is T_QUAL */
    111 		function_specifier kw_fs;	/* if kw_token is
    112 						 * T_FUNCTION_SPECIFIER */
    113 	} u;
    114 	bool	kw_added_in_c90:1;
    115 	bool	kw_added_in_c99_or_c11:1;
    116 	bool	kw_added_in_c23:1;
    117 	bool	kw_gcc:1;	/* available in GCC mode */
    118 	bool	kw_plain:1;	/* 'name' */
    119 	bool	kw_leading:1;	/* '__name' */
    120 	bool	kw_both:1;	/* '__name__' */
    121 } keywords[] = {
    122 	// TODO: _Alignas is not available in C99.
    123 	kwdef_keyword(	"_Alignas",	T_ALIGNAS),
    124 	// TODO: _Alignof is not available in C99.
    125 	kwdef_keyword(	"_Alignof",	T_ALIGNOF),
    126 	// TODO: alignof is not available in C99.
    127 	kwdef_token(	"alignof",	T_ALIGNOF,		78,0,6),
    128 	kwdef_token(	"asm",		T_ASM,			78,1,7),
    129 	kwdef_token(	"_Atomic",	T_ATOMIC,		11,0,1),
    130 	kwdef_token(	"attribute",	T_ATTRIBUTE,		78,1,6),
    131 	kwdef_sclass(	"auto",		AUTO,			78,0,1),
    132 	kwdef_type(	"_Bool",	BOOL,			99),
    133 	kwdef_keyword(	"break",	T_BREAK),
    134 	kwdef_token(	"__builtin_offsetof", T_BUILTIN_OFFSETOF, 78,1,1),
    135 	kwdef_keyword(	"case",		T_CASE),
    136 	kwdef_type(	"char",		CHAR,			78),
    137 	kwdef_type(	"_Complex",	COMPLEX,		99),
    138 	kwdef_tqual(	"const",	tq_const,		90,0,7),
    139 	kwdef_keyword(	"continue",	T_CONTINUE),
    140 	kwdef_keyword(	"default",	T_DEFAULT),
    141 	kwdef_keyword(	"do",		T_DO),
    142 	kwdef_type(	"double",	DOUBLE,			78),
    143 	kwdef_keyword(	"else",		T_ELSE),
    144 	// XXX: enum is not available in traditional C.
    145 	kwdef_keyword(	"enum",		T_ENUM),
    146 	kwdef_token(	"__extension__",T_EXTENSION,		78,1,1),
    147 	kwdef_sclass(	"extern",	EXTERN,			78,0,1),
    148 	kwdef_type(	"float",	FLOAT,			78),
    149 	kwdef_keyword(	"for",		T_FOR),
    150 	kwdef_token(	"_Generic",	T_GENERIC,		11,0,1),
    151 	kwdef_keyword(	"goto",		T_GOTO),
    152 	kwdef_keyword(	"if",		T_IF),
    153 	kwdef_token(	"__imag__",	T_IMAG,			78,1,1),
    154 	kwdef("inline",	T_FUNCTION_SPECIFIER, .u.kw_fs = FS_INLINE, 99,0,7),
    155 	kwdef_type(	"int",		INT,			78),
    156 #ifdef INT128_SIZE
    157 	kwdef_type(	"__int128_t",	INT128,			99),
    158 #endif
    159 	kwdef_type(	"long",		LONG,			78),
    160 	kwdef("_Noreturn", T_FUNCTION_SPECIFIER, .u.kw_fs = FS_NORETURN, 11,0,1),
    161 	// XXX: __packed is GCC-specific.
    162 	kwdef_token(	"__packed",	T_PACKED,		78,0,1),
    163 	kwdef_token(	"__real__",	T_REAL,			78,1,1),
    164 	kwdef_sclass(	"register",	REG,			78,0,1),
    165 	kwdef_tqual(	"restrict",	tq_restrict,		99,0,7),
    166 	kwdef_keyword(	"return",	T_RETURN),
    167 	kwdef_type(	"short",	SHORT,			78),
    168 	kwdef(		"signed", T_TYPE, .u.kw_tspec = SIGNED,	90,0,3),
    169 	kwdef_keyword(	"sizeof",	T_SIZEOF),
    170 	kwdef_sclass(	"static",	STATIC,			78,0,1),
    171 	// XXX: _Static_assert was added in C11.
    172 	kwdef_keyword(	"_Static_assert",	T_STATIC_ASSERT),
    173 	kwdef("struct",	T_STRUCT_OR_UNION, .u.kw_tspec = STRUCT, 78,0,1),
    174 	kwdef_keyword(	"switch",	T_SWITCH),
    175 	kwdef_token(	"__symbolrename",	T_SYMBOLRENAME,	78,0,1),
    176 	kwdef_sclass(	"__thread",	THREAD_LOCAL,		78,1,1),
    177 	kwdef_sclass(	"_Thread_local", THREAD_LOCAL,		11,0,1),
    178 	kwdef_sclass(	"thread_local", THREAD_LOCAL,		23,0,1),
    179 	kwdef_sclass(	"typedef",	TYPEDEF,		78,0,1),
    180 	kwdef_token(	"typeof",	T_TYPEOF,		78,1,7),
    181 #ifdef INT128_SIZE
    182 	kwdef_type(	"__uint128_t",	UINT128,		99),
    183 #endif
    184 	kwdef("union",	T_STRUCT_OR_UNION, .u.kw_tspec = UNION,	78,0,1),
    185 	kwdef_type(	"unsigned",	UNSIGN,			78),
    186 	// XXX: void is not available in traditional C.
    187 	kwdef_type(	"void",		VOID,			78),
    188 	kwdef_tqual(	"volatile",	tq_volatile,		90,0,7),
    189 	kwdef_keyword(	"while",	T_WHILE),
    190 #undef kwdef
    191 #undef kwdef_token
    192 #undef kwdef_sclass
    193 #undef kwdef_type
    194 #undef kwdef_tqual
    195 #undef kwdef_keyword
    196 };
    197 
    198 /*
    199  * The symbol table containing all keywords, identifiers and labels. The hash
    200  * entries are linked via sym_t.s_symtab_next.
    201  */
    202 static sym_t *symtab[503];
    203 
    204 /*
    205  * The kind of the next expected symbol, to distinguish the namespaces of
    206  * members, labels, type tags and other identifiers.
    207  */
    208 symbol_kind sym_kind;
    209 
    210 
    211 static unsigned int
    212 hash(const char *s)
    213 {
    214 	unsigned int v = 0;
    215 	for (const char *p = s; *p != '\0'; p++) {
    216 		v = (v << 4) + (unsigned char)*p;
    217 		v ^= v >> 28;
    218 	}
    219 	return v % (sizeof(symtab) / sizeof(symtab[0]));
    220 }
    221 
    222 static void
    223 symtab_add(sym_t *sym)
    224 {
    225 	unsigned int h = hash(sym->s_name);
    226 	if ((sym->s_symtab_next = symtab[h]) != NULL)
    227 		symtab[h]->s_symtab_ref = &sym->s_symtab_next;
    228 	sym->s_symtab_ref = &symtab[h];
    229 	symtab[h] = sym;
    230 }
    231 
    232 static sym_t *
    233 symtab_search(const char *name)
    234 {
    235 
    236 	unsigned int h = hash(name);
    237 	for (sym_t *sym = symtab[h]; sym != NULL; sym = sym->s_symtab_next) {
    238 		if (strcmp(sym->s_name, name) != 0)
    239 			continue;
    240 		if (sym->s_keyword != NULL ||
    241 		    sym->s_kind == sym_kind ||
    242 		    in_gcc_attribute)
    243 			return sym;
    244 	}
    245 
    246 	return NULL;
    247 }
    248 
    249 static void
    250 symtab_remove(sym_t *sym)
    251 {
    252 
    253 	if ((*sym->s_symtab_ref = sym->s_symtab_next) != NULL)
    254 		sym->s_symtab_next->s_symtab_ref = sym->s_symtab_ref;
    255 	sym->s_symtab_next = NULL;
    256 }
    257 
    258 static void
    259 symtab_remove_locals(void)
    260 {
    261 
    262 	for (size_t i = 0; i < sizeof(symtab) / sizeof(symtab[0]); i++) {
    263 		for (sym_t *sym = symtab[i]; sym != NULL; ) {
    264 			sym_t *next = sym->s_symtab_next;
    265 			if (sym->s_block_level >= 1)
    266 				symtab_remove(sym);
    267 			sym = next;
    268 		}
    269 	}
    270 }
    271 
    272 #ifdef DEBUG
    273 static int
    274 sym_by_name(const void *va, const void *vb)
    275 {
    276 	const sym_t *a = *(const sym_t *const *)va;
    277 	const sym_t *b = *(const sym_t *const *)vb;
    278 
    279 	return strcmp(a->s_name, b->s_name);
    280 }
    281 
    282 struct syms {
    283 	const sym_t **items;
    284 	size_t len;
    285 	size_t cap;
    286 };
    287 
    288 static void
    289 syms_add(struct syms *syms, const sym_t *sym)
    290 {
    291 	if (syms->len >= syms->cap) {
    292 		syms->cap *= 2;
    293 		syms->items = xrealloc(syms->items,
    294 		    syms->cap * sizeof(syms->items[0]));
    295 	}
    296 	syms->items[syms->len++] = sym;
    297 }
    298 
    299 void
    300 debug_symtab(void)
    301 {
    302 	struct syms syms = { xcalloc(64, sizeof(syms.items[0])), 0, 64 };
    303 
    304 	debug_enter();
    305 	for (int level = -1;; level++) {
    306 		bool more = false;
    307 		size_t n = sizeof(symtab) / sizeof(symtab[0]);
    308 
    309 		syms.len = 0;
    310 		for (size_t i = 0; i < n; i++) {
    311 			for (sym_t *sym = symtab[i]; sym != NULL;) {
    312 				if (sym->s_block_level == level &&
    313 				    sym->s_keyword == NULL)
    314 					syms_add(&syms, sym);
    315 				if (sym->s_block_level > level)
    316 					more = true;
    317 				sym = sym->s_symtab_next;
    318 			}
    319 		}
    320 
    321 		if (syms.len > 0) {
    322 			debug_step("symbol table level %d", level);
    323 			debug_indent_inc();
    324 			qsort(syms.items, syms.len, sizeof(syms.items[0]),
    325 			    sym_by_name);
    326 			for (size_t i = 0; i < syms.len; i++)
    327 				debug_sym("", syms.items[i], "\n");
    328 			debug_indent_dec();
    329 
    330 			lint_assert(level != -1);
    331 		}
    332 
    333 		if (!more)
    334 			break;
    335 	}
    336 	debug_leave();
    337 
    338 	free(syms.items);
    339 }
    340 #endif
    341 
    342 static void
    343 register_keyword(const struct keyword *kw, bool leading, bool trailing)
    344 {
    345 
    346 	const char *name;
    347 	if (!leading && !trailing) {
    348 		name = kw->kw_name;
    349 	} else {
    350 		char buf[256];
    351 		(void)snprintf(buf, sizeof(buf), "%s%s%s",
    352 		    leading ? "__" : "", kw->kw_name, trailing ? "__" : "");
    353 		name = xstrdup(buf);
    354 	}
    355 
    356 	sym_t *sym = block_zero_alloc(sizeof(*sym), "sym");
    357 	sym->s_name = name;
    358 	sym->s_keyword = kw;
    359 	int tok = kw->kw_token;
    360 	sym->u.s_keyword.sk_token = tok;
    361 	if (tok == T_TYPE || tok == T_STRUCT_OR_UNION)
    362 		sym->u.s_keyword.u.sk_tspec = kw->u.kw_tspec;
    363 	if (tok == T_SCLASS)
    364 		sym->s_scl = kw->u.kw_scl;
    365 	if (tok == T_QUAL)
    366 		sym->u.s_keyword.u.sk_type_qualifier = kw->u.kw_tqual;
    367 	if (tok == T_FUNCTION_SPECIFIER)
    368 		sym->u.s_keyword.u.function_specifier = kw->u.kw_fs;
    369 
    370 	symtab_add(sym);
    371 }
    372 
    373 static bool
    374 is_keyword_known(const struct keyword *kw)
    375 {
    376 
    377 	if (kw->kw_added_in_c23 && !allow_c23)
    378 		return false;
    379 	if ((kw->kw_added_in_c90 || kw->kw_added_in_c99_or_c11) && !allow_c90)
    380 		return false;
    381 
    382 	/*
    383 	 * In the 1990s, GCC defined several keywords that were later
    384 	 * incorporated into C99, therefore in GCC mode, all C99 keywords are
    385 	 * made available.  The C11 keywords are made available as well, but
    386 	 * there are so few that they don't matter practically.
    387 	 */
    388 	if (allow_gcc)
    389 		return true;
    390 	if (kw->kw_gcc)
    391 		return false;
    392 
    393 	if (kw->kw_added_in_c99_or_c11 && !allow_c99)
    394 		return false;
    395 	return true;
    396 }
    397 
    398 /* Write all keywords to the symbol table. */
    399 void
    400 init_lex(void)
    401 {
    402 
    403 	size_t n = sizeof(keywords) / sizeof(keywords[0]);
    404 	for (size_t i = 0; i < n; i++) {
    405 		const struct keyword *kw = keywords + i;
    406 		if (!is_keyword_known(kw))
    407 			continue;
    408 		if (kw->kw_plain)
    409 			register_keyword(kw, false, false);
    410 		if (kw->kw_leading)
    411 			register_keyword(kw, true, false);
    412 		if (kw->kw_both)
    413 			register_keyword(kw, true, true);
    414 	}
    415 }
    416 
    417 /*
    418  * When scanning the remainder of a long token (see lex_input), read a byte
    419  * and return it as an unsigned char or as EOF.
    420  *
    421  * Increment the line counts if necessary.
    422  */
    423 static int
    424 read_byte(void)
    425 {
    426 	int c = lex_input();
    427 
    428 	if (c == '\n')
    429 		lex_next_line();
    430 	return c == '\0' ? EOF : c;	/* lex returns 0 on EOF. */
    431 }
    432 
    433 static int
    434 lex_keyword(sym_t *sym)
    435 {
    436 	int tok = sym->u.s_keyword.sk_token;
    437 
    438 	if (tok == T_SCLASS)
    439 		yylval.y_scl = sym->s_scl;
    440 	if (tok == T_TYPE || tok == T_STRUCT_OR_UNION)
    441 		yylval.y_tspec = sym->u.s_keyword.u.sk_tspec;
    442 	if (tok == T_QUAL)
    443 		yylval.y_type_qualifiers =
    444 		    sym->u.s_keyword.u.sk_type_qualifier;
    445 	if (tok == T_FUNCTION_SPECIFIER)
    446 		yylval.y_function_specifier =
    447 		    sym->u.s_keyword.u.function_specifier;
    448 	return tok;
    449 }
    450 
    451 /*
    452  * Look up the definition of a name in the symbol table. This symbol must
    453  * either be a keyword or a symbol of the type required by sym_kind (label,
    454  * member, tag, ...).
    455  */
    456 extern int
    457 lex_name(const char *yytext, size_t yyleng)
    458 {
    459 
    460 	sym_t *sym = symtab_search(yytext);
    461 	if (sym != NULL && sym->s_keyword != NULL)
    462 		return lex_keyword(sym);
    463 
    464 	sbuf_t *sb = xmalloc(sizeof(*sb));
    465 	sb->sb_len = yyleng;
    466 	sb->sb_sym = sym;
    467 	yylval.y_name = sb;
    468 
    469 	if (sym != NULL) {
    470 		lint_assert(block_level >= sym->s_block_level);
    471 		sb->sb_name = sym->s_name;
    472 		return sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
    473 	}
    474 
    475 	char *name = block_zero_alloc(yyleng + 1, "string");
    476 	(void)memcpy(name, yytext, yyleng + 1);
    477 	sb->sb_name = name;
    478 	return T_NAME;
    479 }
    480 
    481 static tspec_t
    482 integer_constant_type_signed(unsigned ls, uint64_t ui, int base, bool warned)
    483 {
    484 	if (ls == 0 && ui <= TARG_INT_MAX)
    485 		return INT;
    486 	if (ls == 0 && ui <= TARG_UINT_MAX && base != 10 && allow_c90)
    487 		return UINT;
    488 	if (ls == 0 && ui <= TARG_LONG_MAX)
    489 		return LONG;
    490 
    491 	if (ls <= 1 && ui <= TARG_LONG_MAX)
    492 		return LONG;
    493 	if (ls <= 1 && ui <= TARG_ULONG_MAX && base != 10)
    494 		return allow_c90 ? ULONG : LONG;
    495 	if (ls <= 1 && !allow_c99) {
    496 		if (!warned)
    497 			/* integer constant out of range */
    498 			warning(252);
    499 		return allow_c90 ? ULONG : LONG;
    500 	}
    501 
    502 	if (ui <= TARG_LLONG_MAX)
    503 		return LLONG;
    504 	if (ui <= TARG_ULLONG_MAX && base != 10)
    505 		return allow_c90 ? ULLONG : LLONG;
    506 	if (!warned)
    507 		/* integer constant out of range */
    508 		warning(252);
    509 	return allow_c90 ? ULLONG : LLONG;
    510 }
    511 
    512 static tspec_t
    513 integer_constant_type_unsigned(unsigned l, uint64_t ui, bool warned)
    514 {
    515 	if (l == 0 && ui <= TARG_UINT_MAX)
    516 		return UINT;
    517 
    518 	if (l <= 1 && ui <= TARG_ULONG_MAX)
    519 		return ULONG;
    520 	if (l <= 1 && !allow_c99) {
    521 		if (!warned)
    522 			/* integer constant out of range */
    523 			warning(252);
    524 		return ULONG;
    525 	}
    526 
    527 	if (ui <= TARG_ULLONG_MAX)
    528 		return ULLONG;
    529 	if (!warned)
    530 		/* integer constant out of range */
    531 		warning(252);
    532 	return ULLONG;
    533 }
    534 
    535 int
    536 lex_integer_constant(const char *yytext, size_t yyleng, int base)
    537 {
    538 	const char *cp = yytext;
    539 	size_t len = yyleng;
    540 
    541 	/* skip 0[xX] or 0[bB] */
    542 	if (base == 16 || base == 2) {
    543 		cp += 2;
    544 		len -= 2;
    545 	}
    546 
    547 	/* read suffixes */
    548 	unsigned l_suffix = 0, u_suffix = 0;
    549 	for (;; len--) {
    550 		char c = cp[len - 1];
    551 		if (c == 'l' || c == 'L')
    552 			l_suffix++;
    553 		else if (c == 'u' || c == 'U')
    554 			u_suffix++;
    555 		else
    556 			break;
    557 	}
    558 	if (l_suffix > 2 || u_suffix > 1) {
    559 		/* malformed integer constant */
    560 		warning(251);
    561 		if (l_suffix > 2)
    562 			l_suffix = 2;
    563 		if (u_suffix > 1)
    564 			u_suffix = 1;
    565 	}
    566 	if (!allow_c90 && u_suffix > 0)
    567 		/* suffix 'U' is illegal in traditional C */
    568 		warning(97);
    569 
    570 	bool warned = false;
    571 	errno = 0;
    572 	char *eptr;
    573 	uint64_t ui = (uint64_t)strtoull(cp, &eptr, base);
    574 	lint_assert(eptr == cp + len);
    575 	if (errno != 0) {
    576 		/* integer constant out of range */
    577 		warning(252);
    578 		warned = true;
    579 	}
    580 
    581 	if (any_query_enabled && base == 8 && ui != 0)
    582 		/* octal number '%.*s' */
    583 		query_message(8, (int)len, cp);
    584 
    585 	bool unsigned_since_c90 = allow_trad && allow_c90 && u_suffix == 0
    586 	    && ui > TARG_INT_MAX
    587 	    && ((l_suffix == 0 && base != 10 && ui <= TARG_UINT_MAX)
    588 		|| (l_suffix <= 1 && ui > TARG_LONG_MAX));
    589 
    590 	tspec_t t = u_suffix > 0
    591 	    ? integer_constant_type_unsigned(l_suffix, ui, warned)
    592 	    : integer_constant_type_signed(l_suffix, ui, base, warned);
    593 	ui = (uint64_t)convert_integer((int64_t)ui, t, size_in_bits(t));
    594 
    595 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    596 	yylval.y_val->v_tspec = t;
    597 	yylval.y_val->v_unsigned_since_c90 = unsigned_since_c90;
    598 	yylval.y_val->u.integer = (int64_t)ui;
    599 
    600 	return T_CON;
    601 }
    602 
    603 /* Extend or truncate si to match t.  If t is signed, sign-extend. */
    604 int64_t
    605 convert_integer(int64_t si, tspec_t t, unsigned int bits)
    606 {
    607 
    608 	uint64_t vbits = value_bits(bits);
    609 	uint64_t ui = (uint64_t)si;
    610 	return t == PTR || is_uinteger(t) || ((ui & bit(bits - 1)) == 0)
    611 	    ? (int64_t)(ui & vbits)
    612 	    : (int64_t)(ui | ~vbits);
    613 }
    614 
    615 int
    616 lex_floating_constant(const char *yytext, size_t yyleng)
    617 {
    618 	const char *cp = yytext;
    619 	size_t len = yyleng;
    620 
    621 	bool imaginary = cp[len - 1] == 'i';
    622 	if (imaginary)
    623 		len--;
    624 
    625 	char c = cp[len - 1];
    626 	tspec_t t;
    627 	if (c == 'f' || c == 'F') {
    628 		t = imaginary ? FCOMPLEX : FLOAT;
    629 		len--;
    630 	} else if (c == 'l' || c == 'L') {
    631 		t = imaginary ? LCOMPLEX : LDOUBLE;
    632 		len--;
    633 	} else
    634 		t = imaginary ? DCOMPLEX : DOUBLE;
    635 
    636 	if (!allow_c90 && t != DOUBLE)
    637 		/* suffixes 'F' and 'L' are illegal in traditional C */
    638 		warning(98);
    639 
    640 	errno = 0;
    641 	char *eptr;
    642 	long double ld = strtold(cp, &eptr);
    643 	lint_assert(eptr == cp + len);
    644 	if (errno != 0)
    645 		/* floating-point constant out of range */
    646 		warning(248);
    647 	else if (t == FLOAT) {
    648 		ld = (float)ld;
    649 		if (isfinite(ld) == 0) {
    650 			/* floating-point constant out of range */
    651 			warning(248);
    652 			ld = ld > 0 ? FLT_MAX : -FLT_MAX;
    653 		}
    654 	} else if (t == DOUBLE
    655 	    || /* CONSTCOND */ LDOUBLE_SIZE == DOUBLE_SIZE) {
    656 		ld = (double)ld;
    657 		if (isfinite(ld) == 0) {
    658 			/* floating-point constant out of range */
    659 			warning(248);
    660 			ld = ld > 0 ? DBL_MAX : -DBL_MAX;
    661 		}
    662 	}
    663 
    664 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    665 	yylval.y_val->v_tspec = t;
    666 	yylval.y_val->u.floating = ld;
    667 
    668 	return T_CON;
    669 }
    670 
    671 int
    672 lex_operator(int t, op_t o)
    673 {
    674 
    675 	yylval.y_op = o;
    676 	return t;
    677 }
    678 
    679 static buffer *
    680 read_quoted(bool *complete, char delim, bool wide)
    681 {
    682 	buffer *buf = xcalloc(1, sizeof(*buf));
    683 	buf_init(buf);
    684 	if (wide)
    685 		buf_add_char(buf, 'L');
    686 	buf_add_char(buf, delim);
    687 
    688 	for (;;) {
    689 		int c = read_byte();
    690 		if (c <= 0)
    691 			break;
    692 		buf_add_char(buf, (char)c);
    693 		if (c == '\n')
    694 			break;
    695 		if (c == delim) {
    696 			*complete = true;
    697 			return buf;
    698 		}
    699 		if (c == '\\') {
    700 			c = read_byte();
    701 			buf_add_char(buf, (char)(c <= 0 ? ' ' : c));
    702 			if (c <= 0)
    703 				break;
    704 		}
    705 	}
    706 	*complete = false;
    707 	buf_add_char(buf, delim);
    708 	return buf;
    709 }
    710 
    711 /*
    712  * Analyze the lexical representation of the next character in the string
    713  * literal list. At the end, only update the position information.
    714  */
    715 bool
    716 quoted_next(const buffer *lit, quoted_iterator *it)
    717 {
    718 	const char *s = lit->data;
    719 
    720 	*it = (quoted_iterator){ .start = it->end };
    721 
    722 	char delim = s[s[0] == 'L' ? 1 : 0];
    723 
    724 	bool in_the_middle = it->start > 0;
    725 	if (!in_the_middle) {
    726 		it->start = s[0] == 'L' ? 2 : 1;
    727 		it->end = it->start;
    728 	}
    729 
    730 	while (s[it->start] == delim) {
    731 		if (it->start + 1 == lit->len) {
    732 			it->end = it->start;
    733 			return false;
    734 		}
    735 		it->next_literal = in_the_middle;
    736 		it->start += 2;
    737 	}
    738 	it->end = it->start;
    739 
    740 again:
    741 	switch (s[it->end]) {
    742 	case '\\':
    743 		it->end++;
    744 		goto backslash;
    745 	case '\n':
    746 		it->unescaped_newline = true;
    747 		return false;
    748 	default:
    749 		it->value = (unsigned char)s[it->end++];
    750 		return true;
    751 	}
    752 
    753 backslash:
    754 	it->escaped = true;
    755 	if ('0' <= s[it->end] && s[it->end] <= '7')
    756 		goto octal_escape;
    757 	switch (s[it->end++]) {
    758 	case '\n':
    759 		goto again;
    760 	case 'a':
    761 		it->named_escape = true;
    762 		it->value = '\a';
    763 		it->invalid_escape = !allow_c90;
    764 		return true;
    765 	case 'b':
    766 		it->named_escape = true;
    767 		it->value = '\b';
    768 		return true;
    769 	case 'e':
    770 		it->named_escape = true;
    771 		it->value = '\033';
    772 		it->invalid_escape = !allow_gcc;
    773 		return true;
    774 	case 'f':
    775 		it->named_escape = true;
    776 		it->value = '\f';
    777 		return true;
    778 	case 'n':
    779 		it->named_escape = true;
    780 		it->value = '\n';
    781 		return true;
    782 	case 'r':
    783 		it->named_escape = true;
    784 		it->value = '\r';
    785 		return true;
    786 	case 't':
    787 		it->named_escape = true;
    788 		it->value = '\t';
    789 		return true;
    790 	case 'v':
    791 		it->named_escape = true;
    792 		it->value = '\v';
    793 		it->invalid_escape = !allow_c90;
    794 		return true;
    795 	case 'x':
    796 		goto hex_escape;
    797 	case '"':
    798 		it->literal_escape = true;
    799 		it->value = '"';
    800 		it->invalid_escape = !allow_c90 && delim == '\'';
    801 		return true;
    802 	case '?':
    803 		it->literal_escape = true;
    804 		it->value = '?';
    805 		it->invalid_escape = !allow_c90;
    806 		return true;
    807 	default:
    808 		it->invalid_escape = true;
    809 		/* FALLTHROUGH */
    810 	case '\'':
    811 	case '\\':
    812 		it->literal_escape = true;
    813 		it->value = (unsigned char)s[it->end - 1];
    814 		return true;
    815 	}
    816 
    817 octal_escape:
    818 	it->octal_digits++;
    819 	it->value = s[it->end++] - '0';
    820 	if ('0' <= s[it->end] && s[it->end] <= '7') {
    821 		it->octal_digits++;
    822 		it->value = 8 * it->value + (s[it->end++] - '0');
    823 		if ('0' <= s[it->end] && s[it->end] <= '7') {
    824 			it->octal_digits++;
    825 			it->value = 8 * it->value + (s[it->end++] - '0');
    826 			it->overflow = it->value > TARG_UCHAR_MAX
    827 			    && s[0] != 'L';
    828 		}
    829 	}
    830 	return true;
    831 
    832 hex_escape:
    833 	for (;;) {
    834 		char ch = s[it->end];
    835 		unsigned digit_value;
    836 		if ('0' <= ch && ch <= '9')
    837 			digit_value = ch - '0';
    838 		else if ('A' <= ch && ch <= 'F')
    839 			digit_value = 10 + (ch - 'A');
    840 		else if ('a' <= ch && ch <= 'f')
    841 			digit_value = 10 + (ch - 'a');
    842 		else
    843 			break;
    844 
    845 		it->end++;
    846 		it->value = 16 * it->value + digit_value;
    847 		uint64_t limit = s[0] == 'L' ? TARG_UINT_MAX : TARG_UCHAR_MAX;
    848 		if (it->value > limit)
    849 			it->overflow = true;
    850 		if (it->hex_digits < 3)
    851 			it->hex_digits++;
    852 	}
    853 	it->missing_hex_digits = it->hex_digits == 0;
    854 	return true;
    855 }
    856 
    857 static void
    858 check_quoted(const buffer *buf, bool complete, char delim)
    859 {
    860 	quoted_iterator it = { .end = 0 }, prev = it;
    861 	for (; quoted_next(buf, &it); prev = it) {
    862 		if (it.missing_hex_digits)
    863 			/* no hex digits follow \x */
    864 			error(74);
    865 		if (it.hex_digits > 0 && !allow_c90)
    866 			/* \x undefined in traditional C */
    867 			warning(82);
    868 		else if (!it.invalid_escape)
    869 			;
    870 		else if (it.value == '8' || it.value == '9')
    871 			/* bad octal digit '%c' */
    872 			warning(77, (int)it.value);
    873 		else if (it.literal_escape && it.value == '?')
    874 			/* \? undefined in traditional C */
    875 			warning(263);
    876 		else if (it.literal_escape && it.value == '"')
    877 			/* \" inside character constants undefined in ... */
    878 			warning(262);
    879 		else if (it.named_escape && it.value == '\a')
    880 			/* \a undefined in traditional C */
    881 			warning(81);
    882 		else if (it.named_escape && it.value == '\v')
    883 			/* \v undefined in traditional C */
    884 			warning(264);
    885 		else {
    886 			unsigned char ch = buf->data[it.end - 1];
    887 			if (isprint(ch))
    888 				/* dubious escape \%c */
    889 				warning(79, ch);
    890 			else
    891 				/* dubious escape \%o */
    892 				warning(80, ch);
    893 		}
    894 		if (it.overflow && it.hex_digits > 0)
    895 			/* overflow in hex escape */
    896 			warning(75);
    897 		if (it.overflow && it.octal_digits > 0)
    898 			/* character escape does not fit in character */
    899 			warning(76);
    900 		if (it.value < ' ' && !it.escaped && complete)
    901 			/* invisible character U+%04X in %s */
    902 			query_message(17, (unsigned)it.value, delim == '"'
    903 			    ? "string literal" : "character constant");
    904 		if (prev.octal_digits > 0 && prev.octal_digits < 3
    905 		    && !it.escaped && it.value >= '8' && it.value <= '9')
    906 			/* short octal escape '%.*s' followed by digit '%c' */
    907 			warning(356, (int)(prev.end - prev.start),
    908 			    buf->data + prev.start, buf->data[it.start]);
    909 	}
    910 	if (it.unescaped_newline)
    911 		/* newline in string or char constant */
    912 		error(254);
    913 	if (!complete && delim == '"')
    914 		/* unterminated string constant */
    915 		error(258);
    916 	if (!complete && delim == '\'')
    917 		/* unterminated character constant */
    918 		error(253);
    919 }
    920 
    921 static buffer *
    922 lex_quoted(char delim, bool wide)
    923 {
    924 	bool complete;
    925 	buffer *buf = read_quoted(&complete, delim, wide);
    926 	check_quoted(buf, complete, delim);
    927 	return buf;
    928 }
    929 
    930 /* Called if lex found a leading "'". */
    931 int
    932 lex_character_constant(void)
    933 {
    934 	buffer *buf = lex_quoted('\'', false);
    935 
    936 	size_t n = 0;
    937 	uint64_t val = 0;
    938 	quoted_iterator it = { .end = 0 };
    939 	while (quoted_next(buf, &it)) {
    940 		val = (val << CHAR_SIZE) + it.value;
    941 		n++;
    942 	}
    943 	if (n > sizeof(int) || (n > 1 && (pflag || hflag))) {
    944 		/*
    945 		 * XXX: ^^ should rather be sizeof(TARG_INT). Luckily,
    946 		 * sizeof(int) is the same on all supported platforms.
    947 		 */
    948 		/* too many characters in character constant */
    949 		error(71);
    950 	} else if (n > 1)
    951 		/* multi-character character constant */
    952 		warning(294);
    953 	else if (n == 0 && !it.unescaped_newline)
    954 		/* empty character constant */
    955 		error(73);
    956 
    957 	int64_t cval = n == 1
    958 	    ? convert_integer((int64_t)val, CHAR, CHAR_SIZE)
    959 	    : (int64_t)val;
    960 
    961 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    962 	yylval.y_val->v_tspec = INT;
    963 	yylval.y_val->v_char_constant = true;
    964 	yylval.y_val->u.integer = cval;
    965 
    966 	return T_CON;
    967 }
    968 
    969 /* Called if lex found a leading "L'". */
    970 int
    971 lex_wide_character_constant(void)
    972 {
    973 	buffer *buf = lex_quoted('\'', true);
    974 
    975 	static char wbuf[MB_LEN_MAX + 1];
    976 	size_t n = 0, nmax = MB_CUR_MAX;
    977 
    978 	quoted_iterator it = { .end = 0 };
    979 	while (quoted_next(buf, &it)) {
    980 		if (n < nmax)
    981 			wbuf[n] = (char)it.value;
    982 		n++;
    983 	}
    984 
    985 	wchar_t wc = 0;
    986 	if (n == 0)
    987 		/* empty character constant */
    988 		error(73);
    989 	else if (n > nmax) {
    990 		n = nmax;
    991 		/* too many characters in character constant */
    992 		error(71);
    993 	} else {
    994 		wbuf[n] = '\0';
    995 		(void)mbtowc(NULL, NULL, 0);
    996 		if (mbtowc(&wc, wbuf, nmax) < 0)
    997 			/* invalid multibyte character */
    998 			error(291);
    999 	}
   1000 
   1001 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
   1002 	yylval.y_val->v_tspec = WCHAR_TSPEC;
   1003 	yylval.y_val->v_char_constant = true;
   1004 	yylval.y_val->u.integer = wc;
   1005 
   1006 	return T_CON;
   1007 }
   1008 
   1009 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
   1010 static void
   1011 parse_line_directive_flags(const char *p,
   1012 			   bool *is_begin, bool *is_end, bool *is_system)
   1013 {
   1014 
   1015 	*is_begin = false;
   1016 	*is_end = false;
   1017 	*is_system = false;
   1018 
   1019 	while (*p != '\0') {
   1020 		while (isspace((unsigned char)*p))
   1021 			p++;
   1022 
   1023 		const char *word = p;
   1024 		while (*p != '\0' && !isspace((unsigned char)*p))
   1025 			p++;
   1026 		size_t len = (size_t)(p - word);
   1027 
   1028 		if (len == 1 && word[0] == '1')
   1029 			*is_begin = true;
   1030 		if (len == 1 && word[0] == '2')
   1031 			*is_end = true;
   1032 		if (len == 1 && word[0] == '3')
   1033 			*is_system = true;
   1034 		/* Flag '4' is only interesting for C++. */
   1035 	}
   1036 }
   1037 
   1038 /*
   1039  * The first directive of the preprocessed translation unit provides the name
   1040  * of the C source file as specified at the command line.
   1041  */
   1042 static void
   1043 set_csrc_pos(void)
   1044 {
   1045 	static bool done;
   1046 
   1047 	if (done)
   1048 		return;
   1049 	done = true;
   1050 	csrc_pos.p_file = curr_pos.p_file;
   1051 	outsrc(transform_filename(curr_pos.p_file, strlen(curr_pos.p_file)));
   1052 }
   1053 
   1054 /*
   1055  * Called for preprocessor directives. Currently implemented are:
   1056  *	# pragma [argument...]
   1057  *	# lineno
   1058  *	# lineno "filename" [GCC-flag...]
   1059  */
   1060 void
   1061 lex_directive(const char *yytext)
   1062 {
   1063 	const char *p = yytext + 1;	/* skip '#' */
   1064 
   1065 	while (*p == ' ' || *p == '\t')
   1066 		p++;
   1067 
   1068 	if (!isdigit((unsigned char)*p)) {
   1069 		if (strncmp(p, "pragma", 6) == 0
   1070 		    && isspace((unsigned char)p[6]))
   1071 			return;
   1072 		goto error;
   1073 	}
   1074 
   1075 	char *end;
   1076 	long ln = strtol(--p, &end, 10);
   1077 	if (end == p)
   1078 		goto error;
   1079 	p = end;
   1080 
   1081 	if (*p != ' ' && *p != '\t' && *p != '\0')
   1082 		goto error;
   1083 	while (*p == ' ' || *p == '\t')
   1084 		p++;
   1085 
   1086 	if (*p != '\0') {
   1087 		if (*p != '"')
   1088 			goto error;
   1089 		const char *fn = ++p;
   1090 		while (*p != '"' && *p != '\0')
   1091 			p++;
   1092 		if (*p != '"')
   1093 			goto error;
   1094 		size_t fn_len = p++ - fn;
   1095 		if (fn_len > PATH_MAX)
   1096 			goto error;
   1097 		if (fn_len == 0) {
   1098 			fn = "{standard input}";
   1099 			fn_len = strlen(fn);
   1100 		}
   1101 		curr_pos.p_file = record_filename(fn, fn_len);
   1102 		set_csrc_pos();
   1103 
   1104 		bool is_begin, is_end, is_system;
   1105 		parse_line_directive_flags(p, &is_begin, &is_end, &is_system);
   1106 		update_location(curr_pos.p_file, (int)ln, is_begin, is_end);
   1107 		in_system_header = is_system;
   1108 	}
   1109 	curr_pos.p_line = (int)ln - 1;
   1110 	curr_pos.p_uniq = 0;
   1111 	if (curr_pos.p_file == csrc_pos.p_file) {
   1112 		csrc_pos.p_line = (int)ln - 1;
   1113 		csrc_pos.p_uniq = 0;
   1114 	}
   1115 	return;
   1116 
   1117 error:
   1118 	/* undefined or invalid '#' directive */
   1119 	warning(255);
   1120 }
   1121 
   1122 /* Handle lint comments such as ARGSUSED. */
   1123 void
   1124 lex_comment(void)
   1125 {
   1126 	int c;
   1127 	static const struct {
   1128 		const	char name[18];
   1129 		bool	arg;
   1130 		lint_comment comment;
   1131 	} keywtab[] = {
   1132 		{ "ARGSUSED",		true,	LC_ARGSUSED	},
   1133 		{ "BITFIELDTYPE",	false,	LC_BITFIELDTYPE	},
   1134 		{ "CONSTCOND",		false,	LC_CONSTCOND	},
   1135 		{ "CONSTANTCOND",	false,	LC_CONSTCOND	},
   1136 		{ "CONSTANTCONDITION",	false,	LC_CONSTCOND	},
   1137 		{ "FALLTHRU",		false,	LC_FALLTHROUGH	},
   1138 		{ "FALLTHROUGH",	false,	LC_FALLTHROUGH	},
   1139 		{ "FALL THROUGH",	false,	LC_FALLTHROUGH	},
   1140 		{ "fallthrough",	false,	LC_FALLTHROUGH	},
   1141 		{ "LINTLIBRARY",	false,	LC_LINTLIBRARY	},
   1142 		{ "LINTED",		true,	LC_LINTED	},
   1143 		{ "LONGLONG",		false,	LC_LONGLONG	},
   1144 		{ "NOSTRICT",		true,	LC_LINTED	},
   1145 		{ "NOTREACHED",		false,	LC_NOTREACHED	},
   1146 		{ "PRINTFLIKE",		true,	LC_PRINTFLIKE	},
   1147 		{ "PROTOLIB",		true,	LC_PROTOLIB	},
   1148 		{ "SCANFLIKE",		true,	LC_SCANFLIKE	},
   1149 		{ "VARARGS",		true,	LC_VARARGS	},
   1150 	};
   1151 	char keywd[32];
   1152 
   1153 	bool seen_end_of_comment = false;
   1154 
   1155 	while (c = read_byte(), isspace(c))
   1156 		continue;
   1157 
   1158 	/* Read the potential keyword to keywd */
   1159 	size_t l = 0;
   1160 	while (c != EOF && l < sizeof(keywd) - 1 &&
   1161 	    (isalpha(c) || isspace(c))) {
   1162 		if (islower(c) && l > 0 && isupper((unsigned char)keywd[0]))
   1163 			break;
   1164 		keywd[l++] = (char)c;
   1165 		c = read_byte();
   1166 	}
   1167 	while (l > 0 && isspace((unsigned char)keywd[l - 1]))
   1168 		l--;
   1169 	keywd[l] = '\0';
   1170 
   1171 	/* look for the keyword */
   1172 	size_t i;
   1173 	for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++)
   1174 		if (strcmp(keywtab[i].name, keywd) == 0)
   1175 			goto found_keyword;
   1176 	goto skip_rest;
   1177 
   1178 found_keyword:
   1179 	while (isspace(c))
   1180 		c = read_byte();
   1181 
   1182 	/* read the argument, if the keyword accepts one and there is one */
   1183 	char arg[32];
   1184 	l = 0;
   1185 	if (keywtab[i].arg) {
   1186 		while (isdigit(c) && l < sizeof(arg) - 1) {
   1187 			arg[l++] = (char)c;
   1188 			c = read_byte();
   1189 		}
   1190 	}
   1191 	arg[l] = '\0';
   1192 	int a = l != 0 ? atoi(arg) : -1;
   1193 
   1194 	while (isspace(c))
   1195 		c = read_byte();
   1196 
   1197 	seen_end_of_comment = c == '*' && (c = read_byte()) == '/';
   1198 	if (!seen_end_of_comment && keywtab[i].comment != LC_LINTED)
   1199 		/* extra characters in lint comment */
   1200 		warning(257);
   1201 
   1202 	handle_lint_comment(keywtab[i].comment, a);
   1203 
   1204 skip_rest:
   1205 	while (!seen_end_of_comment) {
   1206 		int lc = c;
   1207 		if ((c = read_byte()) == EOF) {
   1208 			/* unterminated comment */
   1209 			error(256);
   1210 			break;
   1211 		}
   1212 		if (lc == '*' && c == '/')
   1213 			seen_end_of_comment = true;
   1214 	}
   1215 }
   1216 
   1217 void
   1218 lex_slash_slash_comment(void)
   1219 {
   1220 
   1221 	if (!allow_c99 && !allow_gcc)
   1222 		/* %s does not support '//' comments */
   1223 		gnuism(312, allow_c90 ? "C90" : "traditional C");
   1224 
   1225 	for (int c; c = read_byte(), c != EOF && c != '\n';)
   1226 		continue;
   1227 }
   1228 
   1229 void
   1230 reset_suppressions(void)
   1231 {
   1232 
   1233 	lwarn = LWARN_ALL;
   1234 	suppress_longlong = false;
   1235 	suppress_constcond = false;
   1236 }
   1237 
   1238 int
   1239 lex_string(void)
   1240 {
   1241 	yylval.y_string = lex_quoted('"', false);
   1242 	return T_STRING;
   1243 }
   1244 
   1245 static size_t
   1246 wide_length(const buffer *buf)
   1247 {
   1248 
   1249 	(void)mblen(NULL, 0);
   1250 	size_t len = 0, i = 0;
   1251 	while (i < buf->len) {
   1252 		int n = mblen(buf->data + i, MB_CUR_MAX);
   1253 		if (n == -1) {
   1254 			/* invalid multibyte character */
   1255 			error(291);
   1256 			break;
   1257 		}
   1258 		i += n > 1 ? n : 1;
   1259 		len++;
   1260 	}
   1261 	return len;
   1262 }
   1263 
   1264 int
   1265 lex_wide_string(void)
   1266 {
   1267 	buffer *buf = lex_quoted('"', true);
   1268 
   1269 	buffer str;
   1270 	buf_init(&str);
   1271 	quoted_iterator it = { .end = 0 };
   1272 	while (quoted_next(buf, &it))
   1273 		buf_add_char(&str, (char)it.value);
   1274 
   1275 	free(buf->data);
   1276 	*buf = (buffer) { .len = wide_length(&str) };
   1277 
   1278 	yylval.y_string = buf;
   1279 	return T_STRING;
   1280 }
   1281 
   1282 void
   1283 lex_next_line(void)
   1284 {
   1285 	curr_pos.p_line++;
   1286 	curr_pos.p_uniq = 0;
   1287 	debug_skip_indent();
   1288 	debug_printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
   1289 	if (curr_pos.p_file == csrc_pos.p_file) {
   1290 		csrc_pos.p_line++;
   1291 		csrc_pos.p_uniq = 0;
   1292 	}
   1293 }
   1294 
   1295 void
   1296 lex_unknown_character(int c)
   1297 {
   1298 
   1299 	/* unknown character \%o */
   1300 	error(250, c);
   1301 }
   1302 
   1303 /*
   1304  * The scanner does not create new symbol table entries for symbols it cannot
   1305  * find in the symbol table. This is to avoid putting undeclared symbols into
   1306  * the symbol table if a syntax error occurs.
   1307  *
   1308  * getsym is called as soon as it is probably ok to put the symbol in the
   1309  * symbol table. It is still possible that symbols are put in the symbol
   1310  * table that are not completely declared due to syntax errors. To avoid too
   1311  * many problems in this case, symbols get type 'int' in getsym.
   1312  *
   1313  * XXX calls to getsym should be delayed until declare_1_* is called.
   1314  */
   1315 sym_t *
   1316 getsym(sbuf_t *sb)
   1317 {
   1318 
   1319 	sym_t *sym = sb->sb_sym;
   1320 
   1321 	/*
   1322 	 * During member declaration it is possible that name() looked for
   1323 	 * symbols of type SK_VCFT, although it should have looked for symbols
   1324 	 * of type SK_TAG. Same can happen for labels. Both cases are
   1325 	 * compensated here.
   1326 	 */
   1327 	if (sym_kind == SK_MEMBER || sym_kind == SK_LABEL) {
   1328 		if (sym == NULL || sym->s_kind == SK_VCFT)
   1329 			sym = symtab_search(sb->sb_name);
   1330 	}
   1331 
   1332 	if (sym != NULL) {
   1333 		lint_assert(sym->s_kind == sym_kind);
   1334 		set_sym_kind(SK_VCFT);
   1335 		free(sb);
   1336 		return sym;
   1337 	}
   1338 
   1339 	/* create a new symbol table entry */
   1340 
   1341 	decl_level *dl;
   1342 	if (sym_kind == SK_LABEL) {
   1343 		sym = level_zero_alloc(1, sizeof(*sym), "sym");
   1344 		char *s = level_zero_alloc(1, sb->sb_len + 1, "string");
   1345 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1346 		sym->s_name = s;
   1347 		sym->s_block_level = 1;
   1348 		dl = dcs;
   1349 		while (dl->d_enclosing != NULL &&
   1350 		    dl->d_enclosing->d_enclosing != NULL)
   1351 			dl = dl->d_enclosing;
   1352 		lint_assert(dl->d_kind == DLK_AUTO);
   1353 	} else {
   1354 		sym = block_zero_alloc(sizeof(*sym), "sym");
   1355 		sym->s_name = sb->sb_name;
   1356 		sym->s_block_level = block_level;
   1357 		dl = dcs;
   1358 	}
   1359 
   1360 	sym->s_def_pos = unique_curr_pos();
   1361 	if ((sym->s_kind = sym_kind) != SK_LABEL)
   1362 		sym->s_type = gettyp(INT);
   1363 
   1364 	set_sym_kind(SK_VCFT);
   1365 
   1366 	if (!in_gcc_attribute) {
   1367 		debug_printf("%s: symtab_add ", __func__);
   1368 		debug_sym("", sym, "\n");
   1369 		symtab_add(sym);
   1370 
   1371 		*dl->d_last_dlsym = sym;
   1372 		dl->d_last_dlsym = &sym->s_level_next;
   1373 	}
   1374 
   1375 	free(sb);
   1376 	return sym;
   1377 }
   1378 
   1379 /*
   1380  * Construct a temporary symbol. The symbol name starts with a digit to avoid
   1381  * name clashes with other identifiers.
   1382  */
   1383 sym_t *
   1384 mktempsym(type_t *tp)
   1385 {
   1386 	static unsigned n = 0;
   1387 	char *s = level_zero_alloc((size_t)block_level, 64, "string");
   1388 	sym_t *sym = block_zero_alloc(sizeof(*sym), "sym");
   1389 	scl_t scl;
   1390 
   1391 	(void)snprintf(s, 64, "%.8u_tmp", n++);
   1392 
   1393 	scl = dcs->d_scl;
   1394 	if (scl == NO_SCL)
   1395 		scl = block_level > 0 ? AUTO : EXTERN;
   1396 
   1397 	sym->s_name = s;
   1398 	sym->s_type = tp;
   1399 	sym->s_block_level = block_level;
   1400 	sym->s_scl = scl;
   1401 	sym->s_kind = SK_VCFT;
   1402 	sym->s_used = true;
   1403 	sym->s_set = true;
   1404 
   1405 	symtab_add(sym);
   1406 
   1407 	*dcs->d_last_dlsym = sym;
   1408 	dcs->d_last_dlsym = &sym->s_level_next;
   1409 
   1410 	return sym;
   1411 }
   1412 
   1413 void
   1414 symtab_remove_forever(sym_t *sym)
   1415 {
   1416 
   1417 	debug_step("%s '%s' %s '%s'", __func__,
   1418 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1419 	    type_name(sym->s_type));
   1420 	symtab_remove(sym);
   1421 
   1422 	/* avoid that the symbol will later be put back to the symbol table */
   1423 	sym->s_block_level = -1;
   1424 }
   1425 
   1426 /*
   1427  * Remove all symbols from the symbol table that have the same level as the
   1428  * given symbol.
   1429  */
   1430 void
   1431 symtab_remove_level(sym_t *syms)
   1432 {
   1433 
   1434 	if (syms != NULL)
   1435 		debug_step("%s %d", __func__, syms->s_block_level);
   1436 
   1437 	/* Note the use of s_level_next instead of s_symtab_next. */
   1438 	for (sym_t *sym = syms; sym != NULL; sym = sym->s_level_next) {
   1439 		if (sym->s_block_level != -1) {
   1440 			debug_step("%s '%s' %s '%s' %d", __func__,
   1441 			    sym->s_name, symbol_kind_name(sym->s_kind),
   1442 			    type_name(sym->s_type), sym->s_block_level);
   1443 			symtab_remove(sym);
   1444 			sym->s_symtab_ref = NULL;
   1445 		}
   1446 	}
   1447 }
   1448 
   1449 /* Put a symbol into the symbol table. */
   1450 void
   1451 inssym(int level, sym_t *sym)
   1452 {
   1453 
   1454 	debug_step("%s '%s' %s '%s' %d", __func__,
   1455 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1456 	    type_name(sym->s_type), level);
   1457 	sym->s_block_level = level;
   1458 	symtab_add(sym);
   1459 
   1460 	const sym_t *next = sym->s_symtab_next;
   1461 	if (next != NULL)
   1462 		lint_assert(sym->s_block_level >= next->s_block_level);
   1463 }
   1464 
   1465 /* Called at level 0 after syntax errors. */
   1466 void
   1467 clean_up_after_error(void)
   1468 {
   1469 
   1470 	symtab_remove_locals();
   1471 
   1472 	while (mem_block_level > 0)
   1473 		level_free_all(mem_block_level--);
   1474 }
   1475 
   1476 /* Create a new symbol with the same name as an existing symbol. */
   1477 sym_t *
   1478 pushdown(const sym_t *sym)
   1479 {
   1480 
   1481 	debug_step("pushdown '%s' %s '%s'",
   1482 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1483 	    type_name(sym->s_type));
   1484 
   1485 	sym_t *nsym = block_zero_alloc(sizeof(*nsym), "sym");
   1486 	lint_assert(sym->s_block_level <= block_level);
   1487 	nsym->s_name = sym->s_name;
   1488 	nsym->s_def_pos = unique_curr_pos();
   1489 	nsym->s_kind = sym->s_kind;
   1490 	nsym->s_block_level = block_level;
   1491 
   1492 	symtab_add(nsym);
   1493 
   1494 	*dcs->d_last_dlsym = nsym;
   1495 	dcs->d_last_dlsym = &nsym->s_level_next;
   1496 
   1497 	return nsym;
   1498 }
   1499 
   1500 /*
   1501  * Free any dynamically allocated memory referenced by
   1502  * the value stack or yylval.
   1503  * The type of information in yylval is described by tok.
   1504  */
   1505 void
   1506 freeyyv(void *sp, int tok)
   1507 {
   1508 	if (tok == T_NAME || tok == T_TYPENAME) {
   1509 		sbuf_t *sb = *(sbuf_t **)sp;
   1510 		free(sb);
   1511 	} else if (tok == T_CON) {
   1512 		val_t *val = *(val_t **)sp;
   1513 		free(val);
   1514 	} else if (tok == T_STRING) {
   1515 		buffer *str = *(buffer **)sp;
   1516 		free(str->data);
   1517 		free(str);
   1518 	}
   1519 }
   1520