Home | History | Annotate | Line # | Download | only in lint1
lex.c revision 1.213
      1 /* $NetBSD: lex.c,v 1.213 2024/02/03 20:10:10 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.213 2024/02/03 20:10:10 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;
    427 
    428 	if ((c = lex_input()) == EOF)
    429 		return c;
    430 	if (c == '\0')
    431 		return EOF;	/* lex returns 0 on EOF. */
    432 	if (c == '\n')
    433 		lex_next_line();
    434 	return c;
    435 }
    436 
    437 static int
    438 lex_keyword(sym_t *sym)
    439 {
    440 	int tok = sym->u.s_keyword.sk_token;
    441 
    442 	if (tok == T_SCLASS)
    443 		yylval.y_scl = sym->s_scl;
    444 	if (tok == T_TYPE || tok == T_STRUCT_OR_UNION)
    445 		yylval.y_tspec = sym->u.s_keyword.u.sk_tspec;
    446 	if (tok == T_QUAL)
    447 		yylval.y_type_qualifiers =
    448 		    sym->u.s_keyword.u.sk_type_qualifier;
    449 	if (tok == T_FUNCTION_SPECIFIER)
    450 		yylval.y_function_specifier =
    451 		    sym->u.s_keyword.u.function_specifier;
    452 	return tok;
    453 }
    454 
    455 /*
    456  * Look up the definition of a name in the symbol table. This symbol must
    457  * either be a keyword or a symbol of the type required by sym_kind (label,
    458  * member, tag, ...).
    459  */
    460 extern int
    461 lex_name(const char *yytext, size_t yyleng)
    462 {
    463 
    464 	sym_t *sym = symtab_search(yytext);
    465 	if (sym != NULL && sym->s_keyword != NULL)
    466 		return lex_keyword(sym);
    467 
    468 	sbuf_t *sb = xmalloc(sizeof(*sb));
    469 	sb->sb_len = yyleng;
    470 	sb->sb_sym = sym;
    471 	yylval.y_name = sb;
    472 
    473 	if (sym != NULL) {
    474 		lint_assert(block_level >= sym->s_block_level);
    475 		sb->sb_name = sym->s_name;
    476 		return sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
    477 	}
    478 
    479 	char *name = block_zero_alloc(yyleng + 1, "string");
    480 	(void)memcpy(name, yytext, yyleng + 1);
    481 	sb->sb_name = name;
    482 	return T_NAME;
    483 }
    484 
    485 // Determines whether the constant is signed in traditional C but unsigned in
    486 // C90 and later.
    487 static bool
    488 is_unsigned_since_c90(tspec_t typ, uint64_t ui, int base)
    489 {
    490 	if (!(allow_trad && allow_c90))
    491 		return false;
    492 	if (typ == INT) {
    493 		if (ui > TARG_INT_MAX && ui <= TARG_UINT_MAX && base != 10)
    494 			return true;
    495 		return ui > TARG_LONG_MAX;
    496 	}
    497 	return typ == LONG && ui > TARG_LONG_MAX;
    498 }
    499 
    500 static tspec_t
    501 integer_constant_type(tspec_t t, uint64_t ui, int base, bool warned)
    502 {
    503 	switch (t) {
    504 	case INT:
    505 		if (ui <= TARG_INT_MAX)
    506 			return INT;
    507 		if (ui <= TARG_UINT_MAX && base != 10 && allow_c90)
    508 			return UINT;
    509 		if (ui <= TARG_LONG_MAX)
    510 			return LONG;
    511 		if (ui <= TARG_ULONG_MAX && base != 10 && allow_c90)
    512 			return ULONG;
    513 		if (ui <= TARG_ULONG_MAX && !allow_c90)
    514 			return LONG;
    515 		if (!allow_c99) {
    516 			if (!warned)
    517 				/* integer constant out of range */
    518 				warning(252);
    519 			return allow_c90 ? ULONG : LONG;
    520 		}
    521 		if (ui <= TARG_LLONG_MAX)
    522 			return LLONG;
    523 		if (ui <= TARG_ULLONG_MAX && base != 10)
    524 			return ULLONG;
    525 		if (!warned)
    526 			/* integer constant out of range */
    527 			warning(252);
    528 		return ULLONG;
    529 	case UINT:
    530 		if (ui <= TARG_UINT_MAX)
    531 			return UINT;
    532 		if (ui <= TARG_ULONG_MAX)
    533 			return ULONG;
    534 		if (!allow_c99) {
    535 			if (!warned)
    536 				/* integer constant out of range */
    537 				warning(252);
    538 			return ULONG;
    539 		}
    540 		if (ui <= TARG_ULLONG_MAX)
    541 			return ULLONG;
    542 		if (!warned)
    543 			/* integer constant out of range */
    544 			warning(252);
    545 		return ULLONG;
    546 	case LONG:
    547 		if (ui <= TARG_LONG_MAX)
    548 			return LONG;
    549 		if (ui <= TARG_ULONG_MAX && base != 10)
    550 			return allow_c90 ? ULONG : LONG;
    551 		if (!allow_c99) {
    552 			if (!warned)
    553 				/* integer constant out of range */
    554 				warning(252);
    555 			return allow_c90 ? ULONG : LONG;
    556 		}
    557 		if (ui <= TARG_LLONG_MAX)
    558 			return LLONG;
    559 		if (ui <= TARG_ULLONG_MAX && base != 10)
    560 			return ULLONG;
    561 		if (!warned)
    562 			/* integer constant out of range */
    563 			warning(252);
    564 		return ULLONG;
    565 	case ULONG:
    566 		if (ui <= TARG_ULONG_MAX)
    567 			return ULONG;
    568 		if (!allow_c99) {
    569 			if (!warned)
    570 				/* integer constant out of range */
    571 				warning(252);
    572 			return ULONG;
    573 		}
    574 		if (ui <= TARG_ULLONG_MAX)
    575 			return ULLONG;
    576 		if (!warned)
    577 			/* integer constant out of range */
    578 			warning(252);
    579 		return ULLONG;
    580 	case LLONG:
    581 		if (ui <= TARG_LLONG_MAX)
    582 			return LLONG;
    583 		if (ui <= TARG_ULLONG_MAX && base != 10)
    584 			return allow_c90 ? ULLONG : LLONG;
    585 		if (!warned)
    586 			/* integer constant out of range */
    587 			warning(252);
    588 		return allow_c90 ? ULLONG : LLONG;
    589 	default:
    590 		if (ui <= TARG_ULLONG_MAX)
    591 			return ULLONG;
    592 		if (!warned)
    593 			/* integer constant out of range */
    594 			warning(252);
    595 		return ULLONG;
    596 	}
    597 }
    598 
    599 int
    600 lex_integer_constant(const char *yytext, size_t yyleng, int base)
    601 {
    602 	/* C11 6.4.4.1p5 */
    603 	static const tspec_t suffix_type[2][3] = {
    604 		{ INT,  LONG,  LLONG, },
    605 		{ UINT, ULONG, ULLONG, }
    606 	};
    607 
    608 	const char *cp = yytext;
    609 	size_t len = yyleng;
    610 
    611 	/* skip 0[xX] or 0[bB] */
    612 	if (base == 16 || base == 2) {
    613 		cp += 2;
    614 		len -= 2;
    615 	}
    616 
    617 	/* read suffixes */
    618 	unsigned l_suffix = 0, u_suffix = 0;
    619 	for (;; len--) {
    620 		char c = cp[len - 1];
    621 		if (c == 'l' || c == 'L')
    622 			l_suffix++;
    623 		else if (c == 'u' || c == 'U')
    624 			u_suffix++;
    625 		else
    626 			break;
    627 	}
    628 	if (l_suffix > 2 || u_suffix > 1) {
    629 		/* malformed integer constant */
    630 		warning(251);
    631 		if (l_suffix > 2)
    632 			l_suffix = 2;
    633 		if (u_suffix > 1)
    634 			u_suffix = 1;
    635 	}
    636 	if (!allow_c90 && u_suffix > 0) {
    637 		/* suffix 'U' is illegal in traditional C */
    638 		warning(97);
    639 	}
    640 	tspec_t ct = suffix_type[u_suffix][l_suffix];
    641 
    642 	bool warned = false;
    643 	errno = 0;
    644 	char *eptr;
    645 	uint64_t ui = (uint64_t)strtoull(cp, &eptr, base);
    646 	lint_assert(eptr == cp + len);
    647 	if (errno != 0) {
    648 		/* integer constant out of range */
    649 		warning(252);
    650 		warned = true;
    651 	}
    652 
    653 	if (any_query_enabled && base == 8 && ui != 0) {
    654 		/* octal number '%.*s' */
    655 		query_message(8, (int)len, cp);
    656 	}
    657 
    658 	bool ansiu = is_unsigned_since_c90(ct, ui, base);
    659 
    660 	tspec_t t = integer_constant_type(ct, ui, base, warned);
    661 	ui = (uint64_t)convert_integer((int64_t)ui, t, 0);
    662 
    663 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    664 	yylval.y_val->v_tspec = t;
    665 	yylval.y_val->v_unsigned_since_c90 = ansiu;
    666 	yylval.y_val->u.integer = (int64_t)ui;
    667 
    668 	return T_CON;
    669 }
    670 
    671 /*
    672  * Extend or truncate si to match t.  If t is signed, sign-extend.
    673  *
    674  * len is the number of significant bits. If len is 0, len is set
    675  * to the width of type t.
    676  */
    677 int64_t
    678 convert_integer(int64_t si, tspec_t t, unsigned int len)
    679 {
    680 
    681 	if (len == 0)
    682 		len = size_in_bits(t);
    683 
    684 	uint64_t vbits = value_bits(len);
    685 	uint64_t ui = (uint64_t)si;
    686 	return t == PTR || is_uinteger(t) || ((ui & bit(len - 1)) == 0)
    687 	    ? (int64_t)(ui & vbits)
    688 	    : (int64_t)(ui | ~vbits);
    689 }
    690 
    691 int
    692 lex_floating_constant(const char *yytext, size_t yyleng)
    693 {
    694 	const char *cp = yytext;
    695 	size_t len = yyleng;
    696 
    697 	bool imaginary = cp[len - 1] == 'i';
    698 	if (imaginary)
    699 		len--;
    700 
    701 	char c = cp[len - 1];
    702 	tspec_t typ;
    703 	if (c == 'f' || c == 'F') {
    704 		typ = imaginary ? FCOMPLEX : FLOAT;
    705 		len--;
    706 	} else if (c == 'l' || c == 'L') {
    707 		typ = imaginary ? LCOMPLEX : LDOUBLE;
    708 		len--;
    709 	} else
    710 		typ = imaginary ? DCOMPLEX : DOUBLE;
    711 
    712 	if (!allow_c90 && typ != DOUBLE) {
    713 		/* suffixes 'F' and 'L' are illegal in traditional C */
    714 		warning(98);
    715 	}
    716 
    717 	errno = 0;
    718 	char *eptr;
    719 	long double ld = strtold(cp, &eptr);
    720 	lint_assert(eptr == cp + len);
    721 	if (errno != 0) {
    722 		/* floating-point constant out of range */
    723 		warning(248);
    724 	} else if (typ == FLOAT) {
    725 		ld = (float)ld;
    726 		if (isfinite(ld) == 0) {
    727 			/* floating-point constant out of range */
    728 			warning(248);
    729 			ld = ld > 0 ? FLT_MAX : -FLT_MAX;
    730 		}
    731 	} else if (typ == DOUBLE
    732 	    || /* CONSTCOND */ LDOUBLE_SIZE == DOUBLE_SIZE) {
    733 		ld = (double)ld;
    734 		if (isfinite(ld) == 0) {
    735 			/* floating-point constant out of range */
    736 			warning(248);
    737 			ld = ld > 0 ? DBL_MAX : -DBL_MAX;
    738 		}
    739 	}
    740 
    741 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    742 	yylval.y_val->v_tspec = typ;
    743 	yylval.y_val->u.floating = ld;
    744 
    745 	return T_CON;
    746 }
    747 
    748 int
    749 lex_operator(int t, op_t o)
    750 {
    751 
    752 	yylval.y_op = o;
    753 	return t;
    754 }
    755 
    756 static buffer *
    757 read_quoted(bool *complete, bool wide, char delim)
    758 {
    759 	buffer *buf = xcalloc(1, sizeof(*buf));
    760 	buf_init(buf);
    761 	if (wide)
    762 		buf_add_char(buf, 'L');
    763 	buf_add_char(buf, delim);
    764 
    765 	for (;;) {
    766 		int c = read_byte();
    767 		if (c <= 0)
    768 			break;
    769 		buf_add_char(buf, (char)c);
    770 		if (c == '\n')
    771 			break;
    772 		if (c == delim) {
    773 			*complete = true;
    774 			return buf;
    775 		}
    776 		if (c == '\\') {
    777 			c = read_byte();
    778 			buf_add_char(buf, (char)(c <= 0 ? ' ' : c));
    779 			if (c <= 0)
    780 				break;
    781 		}
    782 	}
    783 	*complete = false;
    784 	buf_add_char(buf, delim);
    785 	return buf;
    786 }
    787 
    788 bool
    789 quoted_next(const buffer *lit, quoted_iterator *it)
    790 {
    791 	const char *s = lit->data;
    792 	size_t len = lit->len;
    793 
    794 	*it = (quoted_iterator){ .i = it->i, .start = it->i };
    795 
    796 	char delim = s[s[0] == 'L' ? 1 : 0];
    797 
    798 	bool in_the_middle = it->i > 0;
    799 	if (it->i == 0) {
    800 		it->start = s[0] == 'L' ? 2 : 1;
    801 		it->i = it->start;
    802 	}
    803 
    804 	for (;;) {
    805 		if (s[it->i] != delim)
    806 			break;
    807 		if (it->i + 1 == len)
    808 			return false;
    809 		it->next_literal = in_the_middle;
    810 		it->start += 2;
    811 		it->i += 2;
    812 	}
    813 
    814 again:
    815 	switch (s[it->i]) {
    816 	case '\\':
    817 		it->i++;
    818 		goto backslash;
    819 	case '\n':
    820 		it->unescaped_newline = true;
    821 		return false;
    822 	default:
    823 		it->value = (unsigned char)s[it->i++];
    824 		return true;
    825 	}
    826 
    827 backslash:
    828 	it->escaped = true;
    829 	if ('0' <= s[it->i] && s[it->i] <= '7')
    830 		goto octal_escape;
    831 	switch (s[it->i++]) {
    832 	case '\n':
    833 		goto again;
    834 	case 'a':
    835 		it->named_escape = true;
    836 		it->value = '\a';
    837 		it->invalid_escape = !allow_c90;
    838 		return true;
    839 	case 'b':
    840 		it->named_escape = true;
    841 		it->value = '\b';
    842 		return true;
    843 	case 'e':
    844 		it->named_escape = true;
    845 		it->value = '\033';
    846 		it->invalid_escape = !allow_gcc;
    847 		return true;
    848 	case 'f':
    849 		it->named_escape = true;
    850 		it->value = '\f';
    851 		return true;
    852 	case 'n':
    853 		it->named_escape = true;
    854 		it->value = '\n';
    855 		return true;
    856 	case 'r':
    857 		it->named_escape = true;
    858 		it->value = '\r';
    859 		return true;
    860 	case 't':
    861 		it->named_escape = true;
    862 		it->value = '\t';
    863 		return true;
    864 	case 'v':
    865 		it->named_escape = true;
    866 		it->value = '\v';
    867 		it->invalid_escape = !allow_c90;
    868 		return true;
    869 	case 'x':
    870 		goto hex_escape;
    871 	case '"':
    872 		it->literal_escape = true;
    873 		it->value = '"';
    874 		it->invalid_escape = !allow_c90 && delim == '\'';
    875 		return true;
    876 	case '?':
    877 		it->literal_escape = true;
    878 		it->value = '?';
    879 		it->invalid_escape = !allow_c90;
    880 		return true;
    881 	default:
    882 		it->invalid_escape = true;
    883 		/* FALLTHROUGH */
    884 	case '\'':
    885 	case '\\':
    886 		it->literal_escape = true;
    887 		it->value = (unsigned char)s[it->i - 1];
    888 		return true;
    889 	}
    890 
    891 octal_escape:
    892 	it->octal_digits++;
    893 	it->value = s[it->i++] - '0';
    894 	if ('0' <= s[it->i] && s[it->i] <= '7') {
    895 		it->octal_digits++;
    896 		it->value = 8 * it->value + (s[it->i++] - '0');
    897 		if ('0' <= s[it->i] && s[it->i] <= '7') {
    898 			it->octal_digits++;
    899 			it->value = 8 * it->value + (s[it->i++] - '0');
    900 			it->overflow = it->value > TARG_UCHAR_MAX
    901 			    && s[0] != 'L';
    902 		}
    903 	}
    904 	return true;
    905 
    906 hex_escape:
    907 	for (;;) {
    908 		char ch = s[it->i];
    909 		unsigned digit_value;
    910 		if ('0' <= ch && ch <= '9')
    911 			digit_value = ch - '0';
    912 		else if ('A' <= ch && ch <= 'F')
    913 			digit_value = 10 + (ch - 'A');
    914 		else if ('a' <= ch && ch <= 'f')
    915 			digit_value = 10 + (ch - 'a');
    916 		else
    917 			break;
    918 
    919 		it->i++;
    920 		it->value = 16 * it->value + digit_value;
    921 		uint64_t limit = s[0] == 'L' ? TARG_UINT_MAX : TARG_UCHAR_MAX;
    922 		if (it->value > limit)
    923 			it->overflow = true;
    924 		if (it->hex_digits < 3)
    925 			it->hex_digits++;
    926 	}
    927 	it->missing_hex_digits = it->hex_digits == 0;
    928 	return true;
    929 }
    930 
    931 static void
    932 check_quoted(const buffer *buf, bool complete, char delim)
    933 {
    934 	quoted_iterator it = { .start = 0 }, prev = it;
    935 	for (; quoted_next(buf, &it); prev = it) {
    936 		if (it.missing_hex_digits)
    937 			/* no hex digits follow \x */
    938 			error(74);
    939 		if (it.hex_digits > 0 && !allow_c90)
    940 			/* \x undefined in traditional C */
    941 			warning(82);
    942 		else if (!it.invalid_escape)
    943 			;
    944 		else if (it.value == '8' || it.value == '9')
    945 			/* bad octal digit '%c' */
    946 			warning(77, (int)it.value);
    947 		else if (it.literal_escape && it.value == '?')
    948 			/* \? undefined in traditional C */
    949 			warning(263);
    950 		else if (it.literal_escape && it.value == '"')
    951 			/* \" inside character constants undefined in ... */
    952 			warning(262);
    953 		else if (it.named_escape && it.value == '\a')
    954 			/* \a undefined in traditional C */
    955 			warning(81);
    956 		else if (it.named_escape && it.value == '\v')
    957 			/* \v undefined in traditional C */
    958 			warning(264);
    959 		else {
    960 			unsigned char ch = buf->data[it.i - 1];
    961 			if (isprint(ch))
    962 				/* dubious escape \%c */
    963 				warning(79, ch);
    964 			else
    965 				/* dubious escape \%o */
    966 				warning(80, ch);
    967 		}
    968 		if (it.overflow && it.hex_digits > 0)
    969 			/* overflow in hex escape */
    970 			warning(75);
    971 		if (it.overflow && it.octal_digits > 0)
    972 			/* character escape does not fit in character */
    973 			warning(76);
    974 		if (it.value < ' ' && !it.escaped && complete)
    975 			/* invisible character U+%04X in %s */
    976 			query_message(17, (unsigned)it.value, delim == '"'
    977 			    ? "string literal" : "character constant");
    978 		if (prev.octal_digits > 0 && prev.octal_digits < 3
    979 		    && !it.escaped && it.value >= '8' && it.value <= '9')
    980 			/* short octal escape '%.*s' followed by digit '%c' */
    981 			warning(356, (int)(prev.i - prev.start),
    982 			    buf->data + prev.start, buf->data[it.start]);
    983 	}
    984 	if (it.unescaped_newline)
    985 		/* newline in string or char constant */
    986 		error(254);
    987 	if (!complete && delim == '"')
    988 		/* unterminated string constant */
    989 		error(258);
    990 	if (!complete && delim == '\'')
    991 		/* unterminated character constant */
    992 		error(253);
    993 }
    994 
    995 static buffer *
    996 lex_quoted(char delim, bool wide)
    997 {
    998 	bool complete;
    999 	buffer *buf = read_quoted(&complete, wide, delim);
   1000 	check_quoted(buf, complete, delim);
   1001 	return buf;
   1002 }
   1003 
   1004 /* Called if lex found a leading "'". */
   1005 int
   1006 lex_character_constant(void)
   1007 {
   1008 	buffer *buf = lex_quoted('\'', false);
   1009 
   1010 	size_t n = 0;
   1011 	uint64_t val = 0;
   1012 	quoted_iterator it = { .start = 0 };
   1013 	while (quoted_next(buf, &it)) {
   1014 		val = (val << CHAR_SIZE) + it.value;
   1015 		n++;
   1016 	}
   1017 	if (n > sizeof(int) || (n > 1 && (pflag || hflag))) {
   1018 		/*
   1019 		 * XXX: ^^ should rather be sizeof(TARG_INT). Luckily,
   1020 		 * sizeof(int) is the same on all supported platforms.
   1021 		 */
   1022 		/* too many characters in character constant */
   1023 		error(71);
   1024 	} else if (n > 1) {
   1025 		/* multi-character character constant */
   1026 		warning(294);
   1027 	} else if (n == 0 && !it.unescaped_newline) {
   1028 		/* empty character constant */
   1029 		error(73);
   1030 	}
   1031 
   1032 	int64_t cval = n == 1
   1033 	    ? convert_integer((int64_t)val, CHAR, CHAR_SIZE)
   1034 	    : (int64_t)val;
   1035 
   1036 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
   1037 	yylval.y_val->v_tspec = INT;
   1038 	yylval.y_val->v_char_constant = true;
   1039 	yylval.y_val->u.integer = cval;
   1040 
   1041 	return T_CON;
   1042 }
   1043 
   1044 /*
   1045  * Called if lex found a leading L\'
   1046  */
   1047 int
   1048 lex_wide_character_constant(void)
   1049 {
   1050 	buffer *buf = lex_quoted('\'', true);
   1051 
   1052 	static char wbuf[MB_LEN_MAX + 1];
   1053 	size_t n = 0, nmax = MB_CUR_MAX;
   1054 
   1055 	quoted_iterator it = { .start = 0 };
   1056 	while (quoted_next(buf, &it)) {
   1057 		if (n < nmax)
   1058 			wbuf[n] = (char)it.value;
   1059 		n++;
   1060 	}
   1061 
   1062 	wchar_t wc = 0;
   1063 	if (n == 0) {
   1064 		/* empty character constant */
   1065 		error(73);
   1066 	} else if (n > nmax) {
   1067 		n = nmax;
   1068 		/* too many characters in character constant */
   1069 		error(71);
   1070 	} else {
   1071 		wbuf[n] = '\0';
   1072 		(void)mbtowc(NULL, NULL, 0);
   1073 		if (mbtowc(&wc, wbuf, nmax) < 0)
   1074 			/* invalid multibyte character */
   1075 			error(291);
   1076 	}
   1077 
   1078 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
   1079 	yylval.y_val->v_tspec = WCHAR_TSPEC;
   1080 	yylval.y_val->v_char_constant = true;
   1081 	yylval.y_val->u.integer = wc;
   1082 
   1083 	return T_CON;
   1084 }
   1085 
   1086 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
   1087 static void
   1088 parse_line_directive_flags(const char *p,
   1089 			   bool *is_begin, bool *is_end, bool *is_system)
   1090 {
   1091 
   1092 	*is_begin = false;
   1093 	*is_end = false;
   1094 	*is_system = false;
   1095 
   1096 	while (*p != '\0') {
   1097 		while (ch_isspace(*p))
   1098 			p++;
   1099 
   1100 		const char *word = p;
   1101 		while (*p != '\0' && !ch_isspace(*p))
   1102 			p++;
   1103 		size_t len = (size_t)(p - word);
   1104 
   1105 		if (len == 1 && word[0] == '1')
   1106 			*is_begin = true;
   1107 		if (len == 1 && word[0] == '2')
   1108 			*is_end = true;
   1109 		if (len == 1 && word[0] == '3')
   1110 			*is_system = true;
   1111 		/* Flag '4' is only interesting for C++. */
   1112 	}
   1113 }
   1114 
   1115 /*
   1116  * The first directive of the preprocessed translation unit provides the name
   1117  * of the C source file as specified at the command line.
   1118  */
   1119 static void
   1120 set_csrc_pos(void)
   1121 {
   1122 	static bool done;
   1123 
   1124 	if (done)
   1125 		return;
   1126 	done = true;
   1127 	csrc_pos.p_file = curr_pos.p_file;
   1128 	outsrc(transform_filename(curr_pos.p_file, strlen(curr_pos.p_file)));
   1129 }
   1130 
   1131 /*
   1132  * Called for preprocessor directives. Currently implemented are:
   1133  *	# pragma [argument...]
   1134  *	# lineno
   1135  *	# lineno "filename" [GCC-flag...]
   1136  */
   1137 void
   1138 lex_directive(const char *yytext)
   1139 {
   1140 	const char *p = yytext + 1;	/* skip '#' */
   1141 
   1142 	while (*p == ' ' || *p == '\t')
   1143 		p++;
   1144 
   1145 	if (!ch_isdigit(*p)) {
   1146 		if (strncmp(p, "pragma", 6) == 0 && ch_isspace(p[6]))
   1147 			return;
   1148 		goto error;
   1149 	}
   1150 
   1151 	char *end;
   1152 	long ln = strtol(--p, &end, 10);
   1153 	if (end == p)
   1154 		goto error;
   1155 	p = end;
   1156 
   1157 	if (*p != ' ' && *p != '\t' && *p != '\0')
   1158 		goto error;
   1159 	while (*p == ' ' || *p == '\t')
   1160 		p++;
   1161 
   1162 	if (*p != '\0') {
   1163 		if (*p != '"')
   1164 			goto error;
   1165 		const char *fn = ++p;
   1166 		while (*p != '"' && *p != '\0')
   1167 			p++;
   1168 		if (*p != '"')
   1169 			goto error;
   1170 		size_t fn_len = p++ - fn;
   1171 		if (fn_len > PATH_MAX)
   1172 			goto error;
   1173 		if (fn_len == 0) {
   1174 			fn = "{standard input}";
   1175 			fn_len = strlen(fn);
   1176 		}
   1177 		curr_pos.p_file = record_filename(fn, fn_len);
   1178 		set_csrc_pos();
   1179 
   1180 		bool is_begin, is_end, is_system;
   1181 		parse_line_directive_flags(p, &is_begin, &is_end, &is_system);
   1182 		update_location(curr_pos.p_file, (int)ln, is_begin, is_end);
   1183 		in_system_header = is_system;
   1184 	}
   1185 	curr_pos.p_line = (int)ln - 1;
   1186 	curr_pos.p_uniq = 0;
   1187 	if (curr_pos.p_file == csrc_pos.p_file) {
   1188 		csrc_pos.p_line = (int)ln - 1;
   1189 		csrc_pos.p_uniq = 0;
   1190 	}
   1191 	return;
   1192 
   1193 error:
   1194 	/* undefined or invalid '#' directive */
   1195 	warning(255);
   1196 }
   1197 
   1198 /* Handle lint comments such as ARGSUSED. */
   1199 void
   1200 lex_comment(void)
   1201 {
   1202 	int c;
   1203 	static const struct {
   1204 		const	char name[18];
   1205 		bool	arg;
   1206 		lint_comment comment;
   1207 	} keywtab[] = {
   1208 		{ "ARGSUSED",		true,	LC_ARGSUSED	},
   1209 		{ "BITFIELDTYPE",	false,	LC_BITFIELDTYPE	},
   1210 		{ "CONSTCOND",		false,	LC_CONSTCOND	},
   1211 		{ "CONSTANTCOND",	false,	LC_CONSTCOND	},
   1212 		{ "CONSTANTCONDITION",	false,	LC_CONSTCOND	},
   1213 		{ "FALLTHRU",		false,	LC_FALLTHROUGH	},
   1214 		{ "FALLTHROUGH",	false,	LC_FALLTHROUGH	},
   1215 		{ "FALL THROUGH",	false,	LC_FALLTHROUGH	},
   1216 		{ "fallthrough",	false,	LC_FALLTHROUGH	},
   1217 		{ "LINTLIBRARY",	false,	LC_LINTLIBRARY	},
   1218 		{ "LINTED",		true,	LC_LINTED	},
   1219 		{ "LONGLONG",		false,	LC_LONGLONG	},
   1220 		{ "NOSTRICT",		true,	LC_LINTED	},
   1221 		{ "NOTREACHED",		false,	LC_NOTREACHED	},
   1222 		{ "PRINTFLIKE",		true,	LC_PRINTFLIKE	},
   1223 		{ "PROTOLIB",		true,	LC_PROTOLIB	},
   1224 		{ "SCANFLIKE",		true,	LC_SCANFLIKE	},
   1225 		{ "VARARGS",		true,	LC_VARARGS	},
   1226 	};
   1227 	char keywd[32];
   1228 	char arg[32];
   1229 	size_t l, i;
   1230 	int a;
   1231 
   1232 	bool seen_end_of_comment = false;
   1233 
   1234 	while (c = read_byte(), isspace(c))
   1235 		continue;
   1236 
   1237 	/* Read the potential keyword to keywd */
   1238 	l = 0;
   1239 	while (c != EOF && l < sizeof(keywd) - 1 &&
   1240 	    (isalpha(c) || isspace(c))) {
   1241 		if (islower(c) && l > 0 && ch_isupper(keywd[0]))
   1242 			break;
   1243 		keywd[l++] = (char)c;
   1244 		c = read_byte();
   1245 	}
   1246 	while (l > 0 && ch_isspace(keywd[l - 1]))
   1247 		l--;
   1248 	keywd[l] = '\0';
   1249 
   1250 	/* look for the keyword */
   1251 	for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++)
   1252 		if (strcmp(keywtab[i].name, keywd) == 0)
   1253 			goto found_keyword;
   1254 	goto skip_rest;
   1255 
   1256 found_keyword:
   1257 	while (isspace(c))
   1258 		c = read_byte();
   1259 
   1260 	/* read the argument, if the keyword accepts one and there is one */
   1261 	l = 0;
   1262 	if (keywtab[i].arg) {
   1263 		while (isdigit(c) && l < sizeof(arg) - 1) {
   1264 			arg[l++] = (char)c;
   1265 			c = read_byte();
   1266 		}
   1267 	}
   1268 	arg[l] = '\0';
   1269 	a = l != 0 ? atoi(arg) : -1;
   1270 
   1271 	while (isspace(c))
   1272 		c = read_byte();
   1273 
   1274 	seen_end_of_comment = c == '*' && (c = read_byte()) == '/';
   1275 	if (!seen_end_of_comment && keywtab[i].comment != LC_LINTED)
   1276 		/* extra characters in lint comment */
   1277 		warning(257);
   1278 
   1279 	handle_lint_comment(keywtab[i].comment, a);
   1280 
   1281 skip_rest:
   1282 	while (!seen_end_of_comment) {
   1283 		int lc = c;
   1284 		if ((c = read_byte()) == EOF) {
   1285 			/* unterminated comment */
   1286 			error(256);
   1287 			break;
   1288 		}
   1289 		if (lc == '*' && c == '/')
   1290 			seen_end_of_comment = true;
   1291 	}
   1292 }
   1293 
   1294 void
   1295 lex_slash_slash_comment(void)
   1296 {
   1297 	int c;
   1298 
   1299 	if (!allow_c99 && !allow_gcc)
   1300 		/* %s does not support '//' comments */
   1301 		gnuism(312, allow_c90 ? "C90" : "traditional C");
   1302 
   1303 	while ((c = read_byte()) != EOF && c != '\n')
   1304 		continue;
   1305 }
   1306 
   1307 /*
   1308  * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
   1309  * clear_warn_flags is called after function definitions and global and
   1310  * local declarations and definitions. It is also called between
   1311  * the controlling expression and the body of control statements
   1312  * (if, switch, for, while).
   1313  */
   1314 void
   1315 clear_warn_flags(void)
   1316 {
   1317 
   1318 	lwarn = LWARN_ALL;
   1319 	suppress_longlong = false;
   1320 	suppress_constcond = false;
   1321 }
   1322 
   1323 int
   1324 lex_string(void)
   1325 {
   1326 	yylval.y_string = lex_quoted('"', false);
   1327 	return T_STRING;
   1328 }
   1329 
   1330 static size_t
   1331 wide_length(const buffer *buf)
   1332 {
   1333 
   1334 	(void)mblen(NULL, 0);
   1335 	size_t len = 0, i = 0;
   1336 	while (i < buf->len) {
   1337 		int n = mblen(buf->data + i, MB_CUR_MAX);
   1338 		if (n == -1) {
   1339 			/* invalid multibyte character */
   1340 			error(291);
   1341 			break;
   1342 		}
   1343 		i += n > 1 ? n : 1;
   1344 		len++;
   1345 	}
   1346 	return len;
   1347 }
   1348 
   1349 int
   1350 lex_wide_string(void)
   1351 {
   1352 	buffer *buf = lex_quoted('"', true);
   1353 
   1354 	buffer str;
   1355 	buf_init(&str);
   1356 	quoted_iterator it = { .start = 0 };
   1357 	while (quoted_next(buf, &it))
   1358 		buf_add_char(&str, (char)it.value);
   1359 
   1360 	free(buf->data);
   1361 	*buf = (buffer) { .len = wide_length(&str) };
   1362 
   1363 	yylval.y_string = buf;
   1364 	return T_STRING;
   1365 }
   1366 
   1367 void
   1368 lex_next_line(void)
   1369 {
   1370 	curr_pos.p_line++;
   1371 	curr_pos.p_uniq = 0;
   1372 	debug_skip_indent();
   1373 	debug_printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
   1374 	if (curr_pos.p_file == csrc_pos.p_file) {
   1375 		csrc_pos.p_line++;
   1376 		csrc_pos.p_uniq = 0;
   1377 	}
   1378 }
   1379 
   1380 void
   1381 lex_unknown_character(int c)
   1382 {
   1383 
   1384 	/* unknown character \%o */
   1385 	error(250, c);
   1386 }
   1387 
   1388 /*
   1389  * The scanner does not create new symbol table entries for symbols it cannot
   1390  * find in the symbol table. This is to avoid putting undeclared symbols into
   1391  * the symbol table if a syntax error occurs.
   1392  *
   1393  * getsym is called as soon as it is probably ok to put the symbol in the
   1394  * symbol table. It is still possible that symbols are put in the symbol
   1395  * table that are not completely declared due to syntax errors. To avoid too
   1396  * many problems in this case, symbols get type 'int' in getsym.
   1397  *
   1398  * XXX calls to getsym should be delayed until declare_1_* is called.
   1399  */
   1400 sym_t *
   1401 getsym(sbuf_t *sb)
   1402 {
   1403 
   1404 	sym_t *sym = sb->sb_sym;
   1405 
   1406 	/*
   1407 	 * During member declaration it is possible that name() looked for
   1408 	 * symbols of type SK_VCFT, although it should have looked for symbols
   1409 	 * of type SK_TAG. Same can happen for labels. Both cases are
   1410 	 * compensated here.
   1411 	 */
   1412 	if (sym_kind == SK_MEMBER || sym_kind == SK_LABEL) {
   1413 		if (sym == NULL || sym->s_kind == SK_VCFT)
   1414 			sym = symtab_search(sb->sb_name);
   1415 	}
   1416 
   1417 	if (sym != NULL) {
   1418 		lint_assert(sym->s_kind == sym_kind);
   1419 		set_sym_kind(SK_VCFT);
   1420 		free(sb);
   1421 		return sym;
   1422 	}
   1423 
   1424 	/* create a new symbol table entry */
   1425 
   1426 	/* labels must always be allocated at level 1 (outermost block) */
   1427 	decl_level *dl;
   1428 	if (sym_kind == SK_LABEL) {
   1429 		sym = level_zero_alloc(1, sizeof(*sym), "sym");
   1430 		char *s = level_zero_alloc(1, sb->sb_len + 1, "string");
   1431 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1432 		sym->s_name = s;
   1433 		sym->s_block_level = 1;
   1434 		dl = dcs;
   1435 		while (dl->d_enclosing != NULL &&
   1436 		    dl->d_enclosing->d_enclosing != NULL)
   1437 			dl = dl->d_enclosing;
   1438 		lint_assert(dl->d_kind == DLK_AUTO);
   1439 	} else {
   1440 		sym = block_zero_alloc(sizeof(*sym), "sym");
   1441 		sym->s_name = sb->sb_name;
   1442 		sym->s_block_level = block_level;
   1443 		dl = dcs;
   1444 	}
   1445 
   1446 	sym->s_def_pos = unique_curr_pos();
   1447 	if ((sym->s_kind = sym_kind) != SK_LABEL)
   1448 		sym->s_type = gettyp(INT);
   1449 
   1450 	set_sym_kind(SK_VCFT);
   1451 
   1452 	if (!in_gcc_attribute) {
   1453 		debug_printf("%s: symtab_add ", __func__);
   1454 		debug_sym("", sym, "\n");
   1455 		symtab_add(sym);
   1456 
   1457 		*dl->d_last_dlsym = sym;
   1458 		dl->d_last_dlsym = &sym->s_level_next;
   1459 	}
   1460 
   1461 	free(sb);
   1462 	return sym;
   1463 }
   1464 
   1465 /*
   1466  * Construct a temporary symbol. The symbol name starts with a digit to avoid
   1467  * name clashes with other identifiers.
   1468  */
   1469 sym_t *
   1470 mktempsym(type_t *tp)
   1471 {
   1472 	static unsigned n = 0;
   1473 	char *s = level_zero_alloc((size_t)block_level, 64, "string");
   1474 	sym_t *sym = block_zero_alloc(sizeof(*sym), "sym");
   1475 	scl_t scl;
   1476 
   1477 	(void)snprintf(s, 64, "%.8u_tmp", n++);
   1478 
   1479 	scl = dcs->d_scl;
   1480 	if (scl == NO_SCL)
   1481 		scl = block_level > 0 ? AUTO : EXTERN;
   1482 
   1483 	sym->s_name = s;
   1484 	sym->s_type = tp;
   1485 	sym->s_block_level = block_level;
   1486 	sym->s_scl = scl;
   1487 	sym->s_kind = SK_VCFT;
   1488 	sym->s_used = true;
   1489 	sym->s_set = true;
   1490 
   1491 	symtab_add(sym);
   1492 
   1493 	*dcs->d_last_dlsym = sym;
   1494 	dcs->d_last_dlsym = &sym->s_level_next;
   1495 
   1496 	return sym;
   1497 }
   1498 
   1499 /* Remove a symbol forever from the symbol table. */
   1500 void
   1501 rmsym(sym_t *sym)
   1502 {
   1503 
   1504 	debug_step("rmsym '%s' %s '%s'",
   1505 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1506 	    type_name(sym->s_type));
   1507 	symtab_remove(sym);
   1508 
   1509 	/* avoid that the symbol will later be put back to the symbol table */
   1510 	sym->s_block_level = -1;
   1511 }
   1512 
   1513 /*
   1514  * Remove all symbols from the symbol table that have the same level as the
   1515  * given symbol.
   1516  */
   1517 void
   1518 symtab_remove_level(sym_t *syms)
   1519 {
   1520 
   1521 	if (syms != NULL)
   1522 		debug_step("%s %d", __func__, syms->s_block_level);
   1523 
   1524 	/* Note the use of s_level_next instead of s_symtab_next. */
   1525 	for (sym_t *sym = syms; sym != NULL; sym = sym->s_level_next) {
   1526 		if (sym->s_block_level != -1) {
   1527 			debug_step("%s '%s' %s '%s' %d", __func__,
   1528 			    sym->s_name, symbol_kind_name(sym->s_kind),
   1529 			    type_name(sym->s_type), sym->s_block_level);
   1530 			symtab_remove(sym);
   1531 			sym->s_symtab_ref = NULL;
   1532 		}
   1533 	}
   1534 }
   1535 
   1536 /* Put a symbol into the symbol table. */
   1537 void
   1538 inssym(int level, sym_t *sym)
   1539 {
   1540 
   1541 	debug_step("%s '%s' %s '%s' %d", __func__,
   1542 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1543 	    type_name(sym->s_type), level);
   1544 	sym->s_block_level = level;
   1545 	symtab_add(sym);
   1546 
   1547 	/*
   1548 	 * Placing the inner symbols to the beginning of the list ensures that
   1549 	 * these symbols are preferred over symbols from the outer blocks that
   1550 	 * happen to have the same name.
   1551 	 */
   1552 	const sym_t *next = sym->s_symtab_next;
   1553 	if (next != NULL)
   1554 		lint_assert(sym->s_block_level >= next->s_block_level);
   1555 }
   1556 
   1557 /* Called at level 0 after syntax errors. */
   1558 void
   1559 clean_up_after_error(void)
   1560 {
   1561 
   1562 	symtab_remove_locals();
   1563 
   1564 	while (mem_block_level > 0)
   1565 		level_free_all(mem_block_level--);
   1566 }
   1567 
   1568 /* Create a new symbol with the same name as an existing symbol. */
   1569 sym_t *
   1570 pushdown(const sym_t *sym)
   1571 {
   1572 	sym_t *nsym;
   1573 
   1574 	debug_step("pushdown '%s' %s '%s'",
   1575 	    sym->s_name, symbol_kind_name(sym->s_kind),
   1576 	    type_name(sym->s_type));
   1577 	nsym = block_zero_alloc(sizeof(*nsym), "sym");
   1578 	lint_assert(sym->s_block_level <= block_level);
   1579 	nsym->s_name = sym->s_name;
   1580 	nsym->s_def_pos = unique_curr_pos();
   1581 	nsym->s_kind = sym->s_kind;
   1582 	nsym->s_block_level = block_level;
   1583 
   1584 	symtab_add(nsym);
   1585 
   1586 	*dcs->d_last_dlsym = nsym;
   1587 	dcs->d_last_dlsym = &nsym->s_level_next;
   1588 
   1589 	return nsym;
   1590 }
   1591 
   1592 /*
   1593  * Free any dynamically allocated memory referenced by
   1594  * the value stack or yylval.
   1595  * The type of information in yylval is described by tok.
   1596  */
   1597 void
   1598 freeyyv(void *sp, int tok)
   1599 {
   1600 	if (tok == T_NAME || tok == T_TYPENAME) {
   1601 		sbuf_t *sb = *(sbuf_t **)sp;
   1602 		free(sb);
   1603 	} else if (tok == T_CON) {
   1604 		val_t *val = *(val_t **)sp;
   1605 		free(val);
   1606 	} else if (tok == T_STRING) {
   1607 		buffer *str = *(buffer **)sp;
   1608 		free(str->data);
   1609 		free(str);
   1610 	}
   1611 }
   1612