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