2 /* $NetBSD: scan.l,v 1.37 2007/02/06 00:08:31 he Exp $ */
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: scan.l,v 1.37 2007/02/06 00:08:31 he Exp $");
40 __FBSDID("$FreeBSD$");
54 #define CHAR_MASK (~(~0 << CHAR_BIT))
56 /* Current position (its also updated when an included file is parsed) */
57 pos_t curr_pos = { 1, "", 0 };
60 * Current position in C source (not updated when an included file is
63 pos_t csrc_pos = { 1, "", 0 };
65 static void incline(void);
66 static void badchar(int);
67 static sbuf_t *allocsb(void);
68 static void freesb(sbuf_t *);
69 static int inpc(void);
70 static int hash(const char *);
71 static sym_t *search(sbuf_t *);
72 static int name(void);
73 static int keyw(sym_t *);
75 static int fcon(void);
76 static int operator(int, op_t);
77 static int ccon(void);
78 static int wccon(void);
79 static int getescc(int);
80 static void directive(void);
81 static void comment(void);
82 static void slashslashcomment(void);
83 static int string(void);
84 static int wcstrg(void);
99 {L}({L}|{D})* return (name());
100 0{OD}*[lLuU]* return (icon(8));
101 {NZD}{D}*[lLuU]* return (icon(10));
102 0[xX]{HD}+[lLuU]* return (icon(16));
103 {D}+\.{D}*{EX}?[fFlL]? |
105 0[xX]{HD}+p{HD}+[fFlL]? |
106 \.{D}+{EX}?[fFlL]? return (fcon());
107 "=" return (operator(T_ASSIGN, ASSIGN));
108 "*=" return (operator(T_OPASS, MULASS));
109 "/=" return (operator(T_OPASS, DIVASS));
110 "%=" return (operator(T_OPASS, MODASS));
111 "+=" return (operator(T_OPASS, ADDASS));
112 "-=" return (operator(T_OPASS, SUBASS));
113 "<<=" return (operator(T_OPASS, SHLASS));
114 ">>=" return (operator(T_OPASS, SHRASS));
115 "&=" return (operator(T_OPASS, ANDASS));
116 "^=" return (operator(T_OPASS, XORASS));
117 "|=" return (operator(T_OPASS, ORASS));
118 "||" return (operator(T_LOGOR, LOGOR));
119 "&&" return (operator(T_LOGAND, LOGAND));
120 "|" return (operator(T_OR, OR));
121 "&" return (operator(T_AND, AND));
122 "^" return (operator(T_XOR, XOR));
123 "==" return (operator(T_EQOP, EQ));
124 "!=" return (operator(T_EQOP, NE));
125 "<" return (operator(T_RELOP, LT));
126 ">" return (operator(T_RELOP, GT));
127 "<=" return (operator(T_RELOP, LE));
128 ">=" return (operator(T_RELOP, GE));
129 "<<" return (operator(T_SHFTOP, SHL));
130 ">>" return (operator(T_SHFTOP, SHR));
131 "++" return (operator(T_INCDEC, INC));
132 "--" return (operator(T_INCDEC, DEC));
133 "->" return (operator(T_STROP, ARROW));
134 "." return (operator(T_STROP, POINT));
135 "+" return (operator(T_ADDOP, PLUS));
136 "-" return (operator(T_ADDOP, MINUS));
137 "*" return (operator(T_MULT, MULT));
138 "/" return (operator(T_DIVOP, DIV));
139 "%" return (operator(T_DIVOP, MOD));
140 "!" return (operator(T_UNOP, NOT));
141 "~" return (operator(T_UNOP, COMPL));
142 "\"" return (string());
143 "L\"" return (wcstrg());
145 "{" return (T_LBRACE);
146 "}" return (T_RBRACE);
147 "," return (T_COMMA);
148 ":" return (T_COLON);
149 "?" return (T_QUEST);
150 "[" return (T_LBRACK);
151 "]" return (T_RBRACK);
152 "(" return (T_LPARN);
153 ")" return (T_RPARN);
154 "..." return (T_ELLIPSE);
156 "L'" return (wccon());
161 "//" slashslashcomment();
162 . badchar(yytext[0]);
171 if (curr_pos.p_file == csrc_pos.p_file) {
181 /* unknown character \%o */
187 * During initialisation they are written to the symbol table.
189 static struct kwtab {
190 const char *kw_name; /* keyword */
191 int kw_token; /* token returned by yylex() */
192 scl_t kw_scl; /* storage class if kw_token T_SCLASS */
193 tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */
194 tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
195 u_int kw_c89; /* c89 keyword */
196 u_int kw_c99; /* c99 keyword */
197 u_int kw_gcc; /* GCC keyword */
199 { "asm", T_ASM, 0, 0, 0, 0, 0, 1 },
200 { "__asm", T_ASM, 0, 0, 0, 0, 0, 0 },
201 { "__asm__", T_ASM, 0, 0, 0, 0, 0, 0 },
202 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0, 0 },
203 { "break", T_BREAK, 0, 0, 0, 0, 0, 0 },
204 { "case", T_CASE, 0, 0, 0, 0, 0, 0 },
205 { "char", T_TYPE, 0, CHAR, 0, 0, 0, 0 },
206 { "const", T_QUAL, 0, 0, CONST, 1, 0, 0 },
207 { "__const__", T_QUAL, 0, 0, CONST, 0, 0, 0 },
208 { "__const", T_QUAL, 0, 0, CONST, 0, 0, 0 },
209 { "continue", T_CONTINUE, 0, 0, 0, 0, 0, 0 },
210 { "default", T_DEFAULT, 0, 0, 0, 0, 0, 0 },
211 { "do", T_DO, 0, 0, 0, 0, 0, 0 },
212 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0, 0 },
213 { "else", T_ELSE, 0, 0, 0, 0, 0, 0 },
214 { "enum", T_ENUM, 0, 0, 0, 0, 0, 0 },
215 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0, 0 },
216 { "float", T_TYPE, 0, FLOAT, 0, 0, 0, 0 },
217 { "for", T_FOR, 0, 0, 0, 0, 0, 0 },
218 { "goto", T_GOTO, 0, 0, 0, 0, 0, 0 },
219 { "if", T_IF, 0, 0, 0, 0, 0, 0 },
220 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1, 0 },
221 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0, 0 },
222 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0, 0 },
223 { "int", T_TYPE, 0, INT, 0, 0, 0, 0 },
224 { "__symbolrename", T_SYMBOLRENAME, 0, 0, 0, 0, 0, 0 },
225 { "long", T_TYPE, 0, LONG, 0, 0, 0, 0 },
226 { "register", T_SCLASS, REG, 0, 0, 0, 0, 0 },
227 { "return", T_RETURN, 0, 0, 0, 0, 0, 0 },
228 { "short", T_TYPE, 0, SHORT, 0, 0, 0, 0 },
229 { "signed", T_TYPE, 0, SIGNED, 0, 1, 0, 0 },
230 { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0, 0 },
231 { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0, 0 },
232 { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0, 0 },
233 { "static", T_SCLASS, STATIC, 0, 0, 0, 0, 0 },
234 { "struct", T_SOU, 0, STRUCT, 0, 0, 0, 0 },
235 { "switch", T_SWITCH, 0, 0, 0, 0, 0, 0 },
236 { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0, 0 },
237 { "union", T_SOU, 0, UNION, 0, 0, 0, 0 },
238 { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0, 0 },
239 { "void", T_TYPE, 0, VOID, 0, 0, 0, 0 },
240 { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0, 0 },
241 { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0, 0 },
242 { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0, 0 },
243 { "while", T_WHILE, 0, 0, 0, 0, 0, 0 },
244 { NULL, 0, 0, 0, 0, 0, 0, 0 }
248 static sym_t *symtab[HSHSIZ1];
250 /* bit i of the entry with index i is set */
251 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
253 /* least significant i bits are set in the entry with index i */
254 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
256 /* least significant i bits are not set in the entry with index i */
257 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
259 /* free list for sbuf structures */
260 static sbuf_t *sbfrlst;
262 /* Typ of next expected symbol */
267 * All keywords are written to the symbol table. This saves us looking
268 * in an extra table for each name we found.
278 for (kw = kwtab; kw->kw_name != NULL; kw++) {
279 if ((kw->kw_c89 || kw->kw_c99) && tflag)
281 if (kw->kw_c99 && !(Sflag || gflag))
283 if (kw->kw_gcc && !gflag)
285 sym = getblk(sizeof (sym_t));
286 sym->s_name = kw->kw_name;
288 sym->s_value.v_quad = kw->kw_token;
289 if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
290 sym->s_tspec = kw->kw_tspec;
291 } else if (kw->kw_token == T_SCLASS) {
292 sym->s_scl = kw->kw_scl;
293 } else if (kw->kw_token == T_QUAL) {
294 sym->s_tqual = kw->kw_tqual;
296 h = hash(sym->s_name);
297 if ((sym->s_link = symtab[h]) != NULL)
298 symtab[h]->s_rlink = &sym->s_link;
299 (symtab[h] = sym)->s_rlink = &symtab[h];
302 /* initialize bit-masks for quads */
303 for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
304 qbmasks[i] = (uint64_t)1 << i;
305 uq = ~(uint64_t)0 << i;
310 qlmasks[i] = ~(uint64_t)0;
314 * Get a free sbuf structure, if possible from the free list
321 if ((sb = sbfrlst) != NULL) {
322 sbfrlst = sb->sb_nxt;
324 if ((sb = malloc(sizeof (sbuf_t))) == NULL)
327 (void)memset(sb, 0, sizeof (*sb));
332 * Put a sbuf structure to the free list
338 sb->sb_nxt = sbfrlst;
343 * Read a character and ensure that it is positive (except EOF).
344 * Increment line count(s) if necessary.
351 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
363 for (us = (const u_char *)s; *us != '\0'; us++) {
364 v = (v << sizeof (v)) + *us;
365 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
367 return (v % HSHSIZ1);
371 * Lex has found a letter followed by zero or more letters or digits.
372 * It looks for a symbol in the symbol table with the same name. This
373 * symbol must either be a keyword or a symbol of the type required by
374 * symtyp (label, member, tag, ...).
376 * If it is a keyword, the token is returned. In some cases it is described
377 * more deeply by data written to yylval.
379 * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
380 * is stored in yylval. This struct contains the name of the symbol, it's
381 * length and hash value. If there is already a symbol of the same name
382 * and type in the symbol table, the sbuf struct also contains a pointer
383 * to the symbol table entry.
394 sb->sb_name = yytext;
396 sb->sb_hash = hash(yytext);
398 if ((sym = search(sb)) != NULL && sym->s_keyw) {
406 if (blklev < sym->s_blklev)
408 sb->sb_name = sym->s_name;
409 sb->sb_len = strlen(sym->s_name);
410 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
412 s = getblk(yyleng + 1);
413 (void)memcpy(s, yytext, yyleng + 1);
428 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
429 if (strcmp(sym->s_name, sb->sb_name) == 0) {
430 if (sym->s_keyw || sym->s_kind == symtyp)
443 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
444 yylval.y_scl = sym->s_scl;
445 } else if (t == T_TYPE || t == T_SOU) {
446 yylval.y_tspec = sym->s_tspec;
447 } else if (t == T_QUAL) {
448 yylval.y_tqual = sym->s_tqual;
454 * Convert a string representing an integer into internal representation.
455 * The value is returned in yylval. icon() (and yylex()) returns T_CON.
460 int l_suffix, u_suffix;
468 static tspec_t contypes[2][3] = {
470 { UINT, ULONG, UQUAD }
483 l_suffix = u_suffix = 0;
485 if ((c = cp[len - 1]) == 'l' || c == 'L') {
487 } else if (c == 'u' || c == 'U') {
494 if (l_suffix > 2 || u_suffix > 1) {
495 /* malformed integer constant */
502 if (tflag && u_suffix != 0) {
503 /* suffix U is illegal in traditional C */
506 typ = contypes[u_suffix][l_suffix];
510 ul = strtoul(cp, &eptr, base);
512 uq = strtouq(cp, &eptr, base);
514 if (eptr != cp + len)
517 /* integer constant out of range */
521 * If the value is to big for the current type, we must choose
529 } else if (ul <= (unsigned)UINT_MAX && base != 10) {
531 #if INT_MAX != LONG_MAX
532 } else if (ul <= LONG_MAX) {
538 if (typ == UINT || typ == ULONG) {
543 * Remember that the constant is unsigned
551 if (ul > (u_int)UINT_MAX)
555 if (ul > LONG_MAX && !tflag) {
562 if (uq > QUAD_MAX && !tflag) {
568 /* LINTED (enumeration values not handled in switch) */
592 if (typ != QUAD && typ != UQUAD) {
596 uq = (int64_t)(long)ul;
600 uq = (uint64_t)xsign((int64_t)uq, typ, -1);
602 if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL)
604 yylval.y_val->v_tspec = typ;
605 yylval.y_val->v_ansiu = ansiu;
606 yylval.y_val->v_quad = (int64_t)uq;
612 * Returns 1 if t is a signed type and the value is negative.
614 * len is the number of significant bits. If len is -1, len is set
615 * to the width of type t.
618 sign(int64_t q, tspec_t t, int len)
621 if (t == PTR || isutyp(t))
623 return (msb(q, t, len));
627 msb(int64_t q, tspec_t t, int len)
632 return ((q & qbmasks[len - 1]) != 0);
636 * Extends the sign of q.
639 xsign(int64_t q, tspec_t t, int len)
645 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
654 * Convert a string representing a floating point value into its interal
655 * representation. Type and value are returned in yylval. fcon()
656 * (and yylex()) returns T_CON.
657 * XXX Currently it is not possible to convert constants of type
658 * long double which are greater than DBL_MAX.
673 if ((c = cp[len - 1]) == 'f' || c == 'F') {
676 } else if (c == 'l' || c == 'L') {
683 if (tflag && typ != DOUBLE) {
684 /* suffixes F and L are illegal in traditional C */
689 d = strtod(cp, &eptr);
690 if (eptr != cp + len) {
693 * XXX: non-native non-current strtod() may not handle hex
694 * floats, ignore the rest if we find traces of hex float
709 /* floating-point constant out of range */
715 /* floating-point constant out of range */
717 f = f > 0 ? FLT_MAX : -FLT_MAX;
721 if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
723 yylval.y_val->v_tspec = typ;
725 yylval.y_val->v_ldbl = f;
727 yylval.y_val->v_ldbl = d;
734 operator(int t, op_t o)
742 * Called if lex found a leading \'.
752 while ((c = getescc('\'')) >= 0) {
753 val = (val << CHAR_BIT) + c;
757 /* unterminated character constant */
760 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
761 /* too many characters in character constant */
764 /* multi-character character constant */
767 /* empty character constant */
776 yylval.y_val = xcalloc(1, sizeof (val_t));
777 yylval.y_val->v_tspec = INT;
778 yylval.y_val->v_quad = val;
784 * Called if lex found a leading L\'
789 static char buf[MB_LEN_MAX + 1];
794 while ((c = getescc('\'')) >= 0) {
803 /* unterminated character constant */
806 /* empty character constant */
809 if (i > MB_CUR_MAX) {
811 /* too many characters in character constant */
815 (void)mbtowc(NULL, NULL, 0);
816 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
817 /* invalid multibyte character */
822 if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
824 yylval.y_val->v_tspec = WCHAR;
825 yylval.y_val->v_quad = wc;
831 * Read a character which is part of a character constant or of a string
832 * and handle escapes.
834 * The Argument is the character which delimits the character constant or
837 * Returns -1 if the end of the character constant or string is reached,
838 * -2 if the EOF is reached, and the character otherwise.
857 /* newline in string or char constant */
865 switch (c = inpc()) {
867 if (tflag && d == '\'')
868 /* \" inside character constant undef. ... */
875 /* \? undefined in traditional C */
882 /* \a undefined in traditional C */
897 /* \v undefined in traditional C */
901 /* bad octal digit %c */
904 case '0': case '1': case '2': case '3':
905 case '4': case '5': case '6': case '7':
909 v = (v << 3) + (c - '0');
911 } while (--n && isdigit(c) && (tflag || c <= '7'));
912 if (tflag && n > 0 && isdigit(c))
913 /* bad octal digit %c */
917 /* character escape does not fit in char. */
924 /* \x undefined in traditional C */
928 while ((c = inpc()) >= 0 && isxdigit(c)) {
930 c - '0' : toupper(c) - 'A' + 10;
933 if ((v & ~CHAR_MASK) != 0) {
934 /* overflow in hex escape */
944 /* no hex digits follow \x */
956 /* dubious escape \%c */
959 /* dubious escape \%o */
968 * Called for preprocessor directives. Currently implemented are:
970 * # lineno "filename"
979 static int first = 1;
981 /* Go to first non-whitespace after # */
982 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
985 if (!isdigit((unsigned char)c)) {
987 /* undefined or invalid # directive */
991 ln = strtol(--cp, &eptr, 10);
994 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
996 while ((c = *cp++) == ' ' || c == '\t')
1002 while ((c = *cp) != '"' && c != '\0')
1006 if ((fnl = cp++ - fn) > PATH_MAX)
1008 while ((c = *cp++) == ' ' || c == '\t')
1012 warning("extra character(s) after directive");
1015 /* empty string means stdin */
1017 fn = "{standard input}";
1018 fnl = 16; /* strlen (fn) */
1020 curr_pos.p_file = fnnalloc(fn, fnl);
1022 * If this is the first directive, the name is the name
1023 * of the C source file as specified at the command line.
1024 * It is written to the output file.
1027 csrc_pos.p_file = curr_pos.p_file;
1028 outsrc(curr_pos.p_file);
1032 curr_pos.p_line = (int)ln - 1;
1033 curr_pos.p_uniq = 0;
1034 if (curr_pos.p_file == csrc_pos.p_file) {
1035 csrc_pos.p_line = (int)ln - 1;
1036 csrc_pos.p_uniq = 0;
1041 * Handle lint comments. Following comments are currently understood:
1044 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1045 * FALLTHRU FALLTHROUGH
1054 * If one of this comments is recognized, the arguments, if any, are
1055 * parsed and a function which handles this comment is called.
1066 { "ARGSUSED", 1, argsused },
1067 { "BITFIELDTYPE", 0, bitfieldtype },
1068 { "CONSTCOND", 0, constcond },
1069 { "CONSTANTCOND", 0, constcond },
1070 { "CONSTANTCONDITION", 0, constcond },
1071 { "FALLTHRU", 0, fallthru },
1072 { "FALLTHROUGH", 0, fallthru },
1073 { "LINTLIBRARY", 0, lintlib },
1074 { "LINTED", 0, linted },
1075 { "LONGLONG", 0, longlong },
1076 { "NOSTRICT", 0, linted },
1077 { "NOTREACHED", 0, notreach },
1078 { "PRINTFLIKE", 1, printflike },
1079 { "PROTOLIB", 1, protolib },
1080 { "SCANFLIKE", 1, scanflike },
1081 { "VARARGS", 1, varargs },
1090 /* Skip white spaces after the start of the comment */
1091 while ((c = inpc()) != EOF && isspace(c))
1094 /* Read the potential keyword to keywd */
1096 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1097 keywd[l++] = (char)c;
1102 /* look for the keyword */
1103 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1104 if (strcmp(keywtab[i].keywd, keywd) == 0)
1107 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1110 /* skip white spaces after the keyword */
1111 while (c != EOF && isspace(c))
1114 /* read the argument, if the keyword accepts one and there is one */
1116 if (keywtab[i].arg) {
1117 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1123 a = l != 0 ? atoi(arg) : -1;
1125 /* skip white spaces after the argument */
1126 while (c != EOF && isspace(c))
1129 if (c != '*' || (c = inpc()) != '/') {
1130 if (keywtab[i].func != linted)
1131 /* extra characters in lint comment */
1135 * remember that we have already found the end of the
1141 if (keywtab[i].func != NULL)
1142 (*keywtab[i].func)(a);
1147 if ((c = inpc()) == EOF) {
1148 /* unterminated comment */
1152 if (lc == '*' && c == '/')
1158 * Handle // style comments
1161 slashslashcomment(void)
1165 if (!Sflag && !gflag)
1166 /* // comments only supported in C99 */
1167 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1169 while ((c = inpc()) != EOF && c != '\n')
1174 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1175 * clrwflgs() is called after function definitions and global and
1176 * local declarations and definitions. It is also called between
1177 * the controlling expression and the body of control statements
1178 * (if, switch, for, while).
1190 * Strings are stored in a dynamically alloceted buffer and passed
1191 * in yylval.y_xstrg to the parser. The parser or the routines called
1192 * by the parser are responsible for freeing this buffer.
1202 if ((s = malloc(max = 64)) == NULL)
1206 while ((c = getescc('"')) >= 0) {
1207 /* +1 to reserve space for a trailing NUL character */
1209 if ((s = realloc(s, max *= 2)) == NULL)
1215 /* unterminated string constant */
1218 if ((strg = calloc(1, sizeof (strg_t))) == NULL)
1220 strg->st_tspec = CHAR;
1224 yylval.y_strg = strg;
1233 size_t len, max, wlen;
1237 if ((s = malloc(max = 64)) == NULL)
1240 while ((c = getescc('"')) >= 0) {
1241 /* +1 to save space for a trailing NUL character */
1243 if ((s = realloc(s, max *= 2)) == NULL)
1249 /* unterminated string constant */
1252 /* get length of wide character string */
1253 (void)mblen(NULL, 0);
1254 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1255 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1256 /* invalid multibyte character */
1264 if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL)
1267 /* convert from multibyte to wide char */
1268 (void)mbtowc(NULL, NULL, 0);
1269 for (i = 0, wi = 0; i < len; i += n, wi++) {
1270 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1278 if ((strg = calloc(1, sizeof (strg_t))) == NULL)
1280 strg->st_tspec = WCHAR;
1281 strg->st_len = wlen;
1284 yylval.y_strg = strg;
1289 * As noted above the scanner does not create new symbol table entries
1290 * for symbols it cannot find in the symbol table. This is to avoid
1291 * putting undeclared symbols into the symbol table if a syntax error
1294 * getsym() is called as soon as it is probably ok to put the symbol to
1295 * the symbol table. This does not mean that it is not possible that
1296 * symbols are put to the symbol table which are than not completely
1297 * declared due to syntax errors. To avoid too many problems in this
1298 * case symbols get type int in getsym().
1300 * XXX calls to getsym() should be delayed until decl1*() is called
1312 * During member declaration it is possible that name() looked
1313 * for symbols of type FVFT, although it should have looked for
1314 * symbols of type FTAG. Same can happen for labels. Both cases
1315 * are compensated here.
1317 if (symtyp == FMOS || symtyp == FLAB) {
1318 if (sym == NULL || sym->s_kind == FVFT)
1323 if (sym->s_kind != symtyp)
1324 LERROR("storesym()");
1330 /* create a new symbol table entry */
1332 /* labels must always be allocated at level 1 (outhermost block) */
1333 if (symtyp == FLAB) {
1334 sym = getlblk(1, sizeof (sym_t));
1335 s = getlblk(1, sb->sb_len + 1);
1336 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1340 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1342 if (di->d_ctx != AUTO)
1343 LERROR("storesym()");
1345 sym = getblk(sizeof (sym_t));
1346 sym->s_name = sb->sb_name;
1347 sym->s_blklev = blklev;
1351 UNIQUE_CURR_POS(sym->s_dpos);
1352 if ((sym->s_kind = symtyp) != FLAB)
1353 sym->s_type = gettyp(INT);
1357 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1358 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1359 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1361 *di->d_ldlsym = sym;
1362 di->d_ldlsym = &sym->s_dlnxt;
1369 * Construct a temporary symbol. The symbol starts with a digit, so that
1373 mktempsym(type_t *t)
1377 char *s = getlblk(blklev, 64);
1378 sym_t *sym = getblk(sizeof (sym_t));
1380 (void)snprintf(s, 64, "%.8d_tmp", n++);
1385 sym->s_blklev = blklev;
1391 if ((sym->s_link = symtab[h]) != NULL)
1392 symtab[h]->s_rlink = &sym->s_link;
1393 (symtab[h] = sym)->s_rlink = &symtab[h];
1395 *dcs->d_ldlsym = sym;
1396 dcs->d_ldlsym = &sym->s_dlnxt;
1402 * Remove a symbol forever from the symbol table. s_blklev
1403 * is set to -1 to avoid that the symbol will later be put
1404 * back to the symbol table.
1410 if ((*sym->s_rlink = sym->s_link) != NULL)
1411 sym->s_link->s_rlink = sym->s_rlink;
1417 * Remove a list of symbols declared at one level from the symbol
1425 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1426 if (sym->s_blklev != -1) {
1427 if ((*sym->s_rlink = sym->s_link) != NULL)
1428 sym->s_link->s_rlink = sym->s_rlink;
1430 sym->s_rlink = NULL;
1436 * Put a symbol into the symbol table
1439 inssym(int bl, sym_t *sym)
1443 h = hash(sym->s_name);
1444 if ((sym->s_link = symtab[h]) != NULL)
1445 symtab[h]->s_rlink = &sym->s_link;
1446 (symtab[h] = sym)->s_rlink = &symtab[h];
1448 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1453 * Called at level 0 after syntax errors
1454 * Removes all symbols which are not declared at level 0 from the
1455 * symbol table. Also frees all memory which is not associated with
1464 for (i = 0; i < HSHSIZ1; i++) {
1465 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1467 if (sym->s_blklev >= 1) {
1468 if ((*sym->s_rlink = nsym) != NULL)
1469 nsym->s_rlink = sym->s_rlink;
1474 for (i = mblklev; i > 0; i--)
1479 * Create a new symbol with the name of an existing symbol.
1482 pushdown(sym_t *sym)
1487 h = hash(sym->s_name);
1488 nsym = getblk(sizeof (sym_t));
1489 if (sym->s_blklev > blklev)
1490 LERROR("pushdown()");
1491 nsym->s_name = sym->s_name;
1492 UNIQUE_CURR_POS(nsym->s_dpos);
1493 nsym->s_kind = sym->s_kind;
1494 nsym->s_blklev = blklev;
1496 if ((nsym->s_link = symtab[h]) != NULL)
1497 symtab[h]->s_rlink = &nsym->s_link;
1498 (symtab[h] = nsym)->s_rlink = &symtab[h];
1500 *dcs->d_ldlsym = nsym;
1501 dcs->d_ldlsym = &nsym->s_dlnxt;
1507 * Free any dynamically allocated memory referenced by
1508 * the value stack or yylval.
1509 * The type of information in yylval is described by tok.
1512 freeyyv(void *sp, int tok)
1514 if (tok == T_NAME || tok == T_TYPENAME) {
1515 sbuf_t *sb = *(sbuf_t **)sp;
1517 } else if (tok == T_CON) {
1518 val_t *val = *(val_t **)sp;
1520 } else if (tok == T_STRING) {
1521 strg_t *strg = *(strg_t **)sp;
1522 if (strg->st_tspec == CHAR) {
1524 } else if (strg->st_tspec == WCHAR) {