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