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