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