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