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