Module Name: src
Committed By: rillig
Date: Sat Jan 23 17:58:03 UTC 2021
Modified Files:
src/usr.bin/xlint/lint1: Makefile externs1.h scan.l
Added Files:
src/usr.bin/xlint/lint1: lex.c
Log Message:
lint: move lexer code from scan.l to lex.c
Previously, the generated scan.c was not included when running "make
lint". Similarly, cgram.c is still not included.
To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.59 src/usr.bin/xlint/lint1/Makefile
cvs rdiff -u -r1.59 -r1.60 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r0 -r1.1 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.129 -r1.130 src/usr.bin/xlint/lint1/scan.l
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/xlint/lint1/Makefile
diff -u src/usr.bin/xlint/lint1/Makefile:1.58 src/usr.bin/xlint/lint1/Makefile:1.59
--- src/usr.bin/xlint/lint1/Makefile:1.58 Sun Jan 10 12:05:07 2021
+++ src/usr.bin/xlint/lint1/Makefile Sat Jan 23 17:58:03 2021
@@ -1,9 +1,10 @@
-# $NetBSD: Makefile,v 1.58 2021/01/10 12:05:07 rillig Exp $
+# $NetBSD: Makefile,v 1.59 2021/01/23 17:58:03 rillig Exp $
.include <bsd.own.mk>
PROG= lint1
SRCS= cgram.y decl.c emit.c emit1.c err.c func.c init.c inittyp.c \
+ lex.c \
main1.c mem.c mem1.c oper.c print.c scan.l tree.c tyname.c
MAN= lint.7
Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.59 src/usr.bin/xlint/lint1/externs1.h:1.60
--- src/usr.bin/xlint/lint1/externs1.h:1.59 Mon Jan 18 16:41:57 2021
+++ src/usr.bin/xlint/lint1/externs1.h Sat Jan 23 17:58:03 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: externs1.h,v 1.59 2021/01/18 16:41:57 rillig Exp $ */
+/* $NetBSD: externs1.h,v 1.60 2021/01/23 17:58:03 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -308,6 +308,24 @@ extern void outcall(const tnode_t *, boo
extern void outusg(const sym_t *);
/*
+ * lex.c
+ */
+extern int lex_name(const char *, size_t);
+extern int lex_icon(const char *, size_t, int);
+extern int lex_fcon(const char *, size_t);
+extern int lex_operator(int, op_t);
+extern int lex_string(void);
+extern int lex_wcstrg(void);
+extern int lex_ccon(void);
+extern int lex_wccon(void);
+extern void lex_directive(const char *);
+extern void lex_incline(void);
+extern void lex_comment(void);
+extern void lex_slashslashcomment(void);
+extern void lex_badchar(int);
+extern int lex_input(void);
+
+/*
* print.c
*/
extern char *print_tnode(char *, size_t, const tnode_t *);
Index: src/usr.bin/xlint/lint1/scan.l
diff -u src/usr.bin/xlint/lint1/scan.l:1.129 src/usr.bin/xlint/lint1/scan.l:1.130
--- src/usr.bin/xlint/lint1/scan.l:1.129 Mon Jan 18 20:02:34 2021
+++ src/usr.bin/xlint/lint1/scan.l Sat Jan 23 17:58:03 2021
@@ -1,5 +1,5 @@
%{
-/* $NetBSD: scan.l,v 1.129 2021/01/18 20:02:34 rillig Exp $ */
+/* $NetBSD: scan.l,v 1.130 2021/01/23 17:58:03 rillig Exp $ */
/*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@@ -35,57 +35,12 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: scan.l,v 1.129 2021/01/18 20:02:34 rillig Exp $");
+__RCSID("$NetBSD: scan.l,v 1.130 2021/01/23 17:58:03 rillig Exp $");
#endif
-#include <ctype.h>
-#include <errno.h>
-#include <float.h>
-#include <limits.h>
-#include <math.h>
-#include <stdlib.h>
-#include <string.h>
-
#include "lint1.h"
#include "cgram.h"
-#define CHAR_MASK ((int)(~(~0U << CHAR_SIZE)))
-
-/* Current position (it's also updated when an included file is parsed) */
-pos_t curr_pos = { 1, "", 0 };
-
-/*
- * Current position in C source (not updated when an included file is
- * parsed).
- */
-pos_t csrc_pos = { 1, "", 0 };
-
-/* Are we parsing a gcc attribute? */
-bool attron;
-
-bool in_system_header = false;
-
-static void incline(void);
-static void badchar(int);
-static sbuf_t *allocsb(void);
-static void freesb(sbuf_t *);
-static int inpc(void);
-static int hash(const char *);
-static sym_t *search(sbuf_t *);
-static int name(void);
-static int keyw(sym_t *);
-static int icon(int);
-static int fcon(void);
-static int operator(int, op_t);
-static int ccon(void);
-static int wccon(void);
-static int getescc(int);
-static void directive(void);
-static void comment(void);
-static void slashslashcomment(void);
-static int string(void);
-static int wcstrg(void);
-
%}
@@ -103,53 +58,53 @@ TL ([fFlL]?[i]?)
%%
-{L}({L}|{D})* return name();
-0[bB]{BD}+[lLuU]* return icon(2);
-0{OD}*[lLuU]* return icon(8);
-{NZD}{D}*[lLuU]* return icon(10);
-0[xX]{HD}+[lLuU]* return icon(16);
+{L}({L}|{D})* return lex_name(yytext, yyleng);
+0[bB]{BD}+[lLuU]* return lex_icon(yytext, yyleng, 2);
+0{OD}*[lLuU]* return lex_icon(yytext, yyleng, 8);
+{NZD}{D}*[lLuU]* return lex_icon(yytext, yyleng, 10);
+0[xX]{HD}+[lLuU]* return lex_icon(yytext, yyleng, 16);
{D}+\.{D}*{EX}?{TL} |
{D}+{EX}{TL} |
0[xX]{HD}+\.{HD}*{HX}{TL} |
0[xX]{HD}+{HX}{TL} |
-\.{D}+{EX}?{TL} return fcon();
-"=" return operator(T_ASSIGN, NOOP);
-"*=" return operator(T_OPASSIGN, MULASS);
-"/=" return operator(T_OPASSIGN, DIVASS);
-"%=" return operator(T_OPASSIGN, MODASS);
-"+=" return operator(T_OPASSIGN, ADDASS);
-"-=" return operator(T_OPASSIGN, SUBASS);
-"<<=" return operator(T_OPASSIGN, SHLASS);
-">>=" return operator(T_OPASSIGN, SHRASS);
-"&=" return operator(T_OPASSIGN, ANDASS);
-"^=" return operator(T_OPASSIGN, XORASS);
-"|=" return operator(T_OPASSIGN, ORASS);
-"||" return operator(T_LOGOR, NOOP);
-"&&" return operator(T_LOGAND, NOOP);
-"|" return operator(T_BITOR, NOOP);
-"&" return operator(T_AMPER, NOOP);
-"^" return operator(T_XOR, NOOP);
-"==" return operator(T_EQUALITY, EQ);
-"!=" return operator(T_EQUALITY, NE);
-"<" return operator(T_RELATIONAL, LT);
-">" return operator(T_RELATIONAL, GT);
-"<=" return operator(T_RELATIONAL, LE);
-">=" return operator(T_RELATIONAL, GE);
-"<<" return operator(T_SHIFT, SHL);
-">>" return operator(T_SHIFT, SHR);
-"++" return operator(T_INCDEC, INC);
-"--" return operator(T_INCDEC, DEC);
-"->" return operator(T_MEMBACC, ARROW);
-"." return operator(T_MEMBACC, POINT);
-"+" return operator(T_ADDITIVE, PLUS);
-"-" return operator(T_ADDITIVE, MINUS);
-"*" return operator(T_ASTERISK, NOOP);
-"/" return operator(T_MULTIPLICATIVE, DIV);
-"%" return operator(T_MULTIPLICATIVE, MOD);
-"!" return operator(T_UNARY, NOT);
-"~" return operator(T_UNARY, COMPL);
-"\"" return string();
-"L\"" return wcstrg();
+\.{D}+{EX}?{TL} return lex_fcon(yytext, yyleng);
+"=" return lex_operator(T_ASSIGN, NOOP);
+"*=" return lex_operator(T_OPASSIGN, MULASS);
+"/=" return lex_operator(T_OPASSIGN, DIVASS);
+"%=" return lex_operator(T_OPASSIGN, MODASS);
+"+=" return lex_operator(T_OPASSIGN, ADDASS);
+"-=" return lex_operator(T_OPASSIGN, SUBASS);
+"<<=" return lex_operator(T_OPASSIGN, SHLASS);
+">>=" return lex_operator(T_OPASSIGN, SHRASS);
+"&=" return lex_operator(T_OPASSIGN, ANDASS);
+"^=" return lex_operator(T_OPASSIGN, XORASS);
+"|=" return lex_operator(T_OPASSIGN, ORASS);
+"||" return lex_operator(T_LOGOR, NOOP);
+"&&" return lex_operator(T_LOGAND, NOOP);
+"|" return lex_operator(T_BITOR, NOOP);
+"&" return lex_operator(T_AMPER, NOOP);
+"^" return lex_operator(T_XOR, NOOP);
+"==" return lex_operator(T_EQUALITY, EQ);
+"!=" return lex_operator(T_EQUALITY, NE);
+"<" return lex_operator(T_RELATIONAL, LT);
+">" return lex_operator(T_RELATIONAL, GT);
+"<=" return lex_operator(T_RELATIONAL, LE);
+">=" return lex_operator(T_RELATIONAL, GE);
+"<<" return lex_operator(T_SHIFT, SHL);
+">>" return lex_operator(T_SHIFT, SHR);
+"++" return lex_operator(T_INCDEC, INC);
+"--" return lex_operator(T_INCDEC, DEC);
+"->" return lex_operator(T_MEMBACC, ARROW);
+"." return lex_operator(T_MEMBACC, POINT);
+"+" return lex_operator(T_ADDITIVE, PLUS);
+"-" return lex_operator(T_ADDITIVE, MINUS);
+"*" return lex_operator(T_ASTERISK, NOOP);
+"/" return lex_operator(T_MULTIPLICATIVE, DIV);
+"%" return lex_operator(T_MULTIPLICATIVE, MOD);
+"!" return lex_operator(T_UNARY, NOT);
+"~" return lex_operator(T_UNARY, COMPL);
+"\"" return lex_string();
+"L\"" return lex_wcstrg();
";" return T_SEMI;
"{" return T_LBRACE;
"}" return T_RBRACE;
@@ -161,1541 +116,19 @@ TL ([fFlL]?[i]?)
"(" return T_LPAREN;
")" return T_RPAREN;
"..." return T_ELLIPSIS;
-"'" return ccon();
-"L'" return wccon();
-^#.*$ directive();
-\n incline();
+"'" return lex_ccon();
+"L'" return lex_wccon();
+^#.*$ lex_directive(yytext);
+\n lex_incline();
\t|" "|\f|\v ;
-"/*" comment();
-"//" slashslashcomment();
-. badchar(yytext[0]);
+"/*" lex_comment();
+"//" lex_slashslashcomment();
+. lex_badchar(yytext[0]);
%%
-static void
-incline(void)
-{
- curr_pos.p_line++;
- curr_pos.p_uniq = 0;
-#ifdef DEBUG
- printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
-#endif
- if (curr_pos.p_file == csrc_pos.p_file) {
- csrc_pos.p_line++;
- csrc_pos.p_uniq = 0;
- }
-}
-
-static void
-badchar(int c)
-{
-
- /* unknown character \%o */
- error(250, c);
-}
-
-/*
- * Keywords.
- * During initialisation they are written to the symbol table.
- */
-static struct kwtab {
- const char *kw_name; /* keyword */
- int kw_token; /* token returned by yylex() */
- scl_t kw_scl; /* storage class if kw_token T_SCLASS */
- tspec_t kw_tspec; /* type spec. if kw_token
- * T_TYPE or T_STRUCT_OR_UNION */
- tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
- bool kw_c89 : 1; /* C89 keyword */
- bool kw_c99 : 1; /* C99 keyword */
- bool kw_gcc : 1; /* GCC keyword */
- bool kw_attr : 1; /* GCC attribute, keyword */
- u_int kw_deco : 3; /* 1 = name, 2 = __name, 4 = __name__ */
-} kwtab[] = {
-#ifdef INT128_SIZE
- { "__int128_t", T_TYPE, 0, INT128, 0, 0,1,0,0,1 },
- { "__uint128_t",T_TYPE, 0, UINT128,0, 0,1,0,0,1 },
-#endif
- { "__thread", T_QUAL, 0, 0, THREAD, 0,0,1,0,1 },
- { "_Alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,1 },
- { "_Bool", T_TYPE, 0, BOOL, 0, 0,1,0,0,1 },
- { "_Complex", T_TYPE, 0, COMPLEX,0, 0,1,0,0,1 },
- { "_Generic", T_GENERIC, 0, 0, 0, 0,1,0,0,1 },
- { "_Noreturn", T_NORETURN, 0, 0, 0, 0,1,0,0,1 },
- { "_Thread_local",T_QUAL, 0, 0, THREAD, 0,1,0,0,1 },
- { "alias", T_AT_ALIAS, 0, 0, 0, 0,0,1,1,5 },
- { "aligned", T_AT_ALIGNED, 0, 0, 0, 0,0,1,1,5 },
- { "alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,4 },
- { "alloc_size", T_AT_ALLOC_SIZE,0, 0, 0, 0,0,1,1,5 },
- { "always_inline", T_AT_ALWAYS_INLINE, 0,0, 0, 0,0,1,1,5 },
- { "asm", T_ASM, 0, 0, 0, 0,0,1,0,7 },
- { "attribute", T_ATTRIBUTE, 0, 0, 0, 0,0,1,0,6 },
- { "auto", T_SCLASS, AUTO, 0, 0, 0,0,0,0,1 },
- { "bounded", T_AT_BOUNDED, 0, 0, 0, 0,0,1,1,5 },
- { "break", T_BREAK, 0, 0, 0, 0,0,0,0,1 },
- { "buffer", T_AT_BUFFER, 0, 0, 0, 0,0,1,1,5 },
- { "builtin_offsetof", T_BUILTIN_OFFSETOF, 0, 0, 0, 0,0,1,0,2 },
- { "case", T_CASE, 0, 0, 0, 0,0,0,0,1 },
- { "char", T_TYPE, 0, CHAR, 0, 0,0,0,0,1 },
- { "cold", T_AT_COLD, 0, 0, 0, 0,0,1,1,5 },
- { "const", T_QUAL, 0, 0, CONST, 1,0,0,0,7 },
- { "constructor",T_AT_CONSTRUCTOR,0, 0, 0, 0,0,1,1,5 },
- { "continue", T_CONTINUE, 0, 0, 0, 0,0,0,0,1 },
- { "default", T_DEFAULT, 0, 0, 0, 0,0,0,0,1 },
- { "deprecated", T_AT_DEPRECATED,0, 0, 0, 0,0,1,1,5 },
- { "destructor", T_AT_DESTRUCTOR,0, 0, 0, 0,0,1,1,5 },
- { "do", T_DO, 0, 0, 0, 0,0,0,0,1 },
- { "double", T_TYPE, 0, DOUBLE, 0, 0,0,0,0,1 },
- { "else", T_ELSE, 0, 0, 0, 0,0,0,0,1 },
- { "enum", T_ENUM, 0, 0, 0, 0,0,0,0,1 },
- { "extension", T_EXTENSION, 0, 0, 0, 0,0,1,0,4 },
- { "extern", T_SCLASS, EXTERN, 0, 0, 0,0,0,0,1 },
- { "float", T_TYPE, 0, FLOAT, 0, 0,0,0,0,1 },
- { "for", T_FOR, 0, 0, 0, 0,0,0,0,1 },
- { "format", T_AT_FORMAT, 0, 0, 0, 0,0,1,1,5 },
- { "format_arg", T_AT_FORMAT_ARG,0, 0, 0, 0,0,1,1,5 },
- { "gnu_inline", T_AT_GNU_INLINE,0, 0, 0, 0,0,1,1,5 },
- { "gnu_printf", T_AT_FORMAT_GNU_PRINTF,0,0, 0, 0,0,1,1,5 },
- { "goto", T_GOTO, 0, 0, 0, 0,0,0,0,1 },
- { "if", T_IF, 0, 0, 0, 0,0,0,0,1 },
- { "imag", T_IMAG, 0, 0, 0, 0,1,0,0,4 },
- { "inline", T_SCLASS, INLINE, 0, 0, 0,1,0,0,7 },
- { "int", T_TYPE, 0, INT, 0, 0,0,0,0,1 },
- { "long", T_TYPE, 0, LONG, 0, 0,0,0,0,1 },
- { "malloc", T_AT_MALLOC, 0, 0, 0, 0,0,1,1,5 },
- { "may_alias", T_AT_MAY_ALIAS, 0, 0, 0, 0,0,1,1,5 },
- { "minbytes", T_AT_MINBYTES, 0, 0, 0, 0,0,1,1,5 },
- { "mode", T_AT_MODE, 0, 0, 0, 0,0,1,1,5 },
- { "no_instrument_function", T_AT_NO_INSTRUMENT_FUNCTION,
- 0, 0, 0, 0,0,1,1,5 },
- { "nonnull", T_AT_NONNULL, 0, 0, 0, 0,0,1,1,5 },
- { "noinline", T_AT_NOINLINE, 0, 0, 0, 0,0,1,1,5 },
- { "noreturn", T_AT_NORETURN, 0, 0, 0, 0,0,1,1,5 },
- { "nothrow", T_AT_NOTHROW, 0, 0, 0, 0,0,1,1,5 },
- { "optimize", T_AT_OPTIMIZE, 0, 0, 0, 0,0,1,1,5 },
- { "packed", T_AT_PACKED, 0, 0, 0, 0,0,1,1,5 },
- { "packed", T_PACKED, 0, 0, 0, 0,0,0,0,2 },
- { "pcs", T_AT_PCS, 0, 0, 0, 0,0,0,0,5 },
- { "printf", T_AT_FORMAT_PRINTF,0, 0, 0, 0,0,1,1,5 },
- { "pure", T_AT_PURE, 0, 0, 0, 0,0,1,1,5 },
- { "real", T_REAL, 0, 0, 0, 0,1,0,0,4 },
- { "register", T_SCLASS, REG, 0, 0, 0,0,0,0,1 },
- { "restrict", T_QUAL, 0, 0, RESTRICT, 0,1,0,0,5 },
- { "return", T_RETURN, 0, 0, 0, 0,0,0,0,1 },
- { "returns_twice", T_AT_RETURNS_TWICE,0,0, 0, 0,0,1,1,5 },
- { "scanf", T_AT_FORMAT_SCANF,0, 0, 0, 0,0,1,1,5 },
- { "section", T_AT_SECTION, 0, 0, 0, 0,0,1,1,7 },
- { "sentinel", T_AT_SENTINEL, 0, 0, 0, 0,0,1,1,5 },
- { "short", T_TYPE, 0, SHORT, 0, 0,0,0,0,1 },
- { "signed", T_TYPE, 0, SIGNED, 0, 1,0,0,0,3 },
- { "sizeof", T_SIZEOF, 0, 0, 0, 0,0,0,0,1 },
- { "static", T_SCLASS, STATIC, 0, 0, 0,0,0,0,1 },
- { "strfmon", T_AT_FORMAT_STRFMON,0, 0, 0, 0,0,1,1,5 },
- { "strftime", T_AT_FORMAT_STRFTIME,0, 0, 0, 0,0,1,1,5 },
- { "string", T_AT_STRING, 0, 0, 0, 0,0,1,1,5 },
- { "struct", T_STRUCT_OR_UNION, 0, STRUCT, 0, 0,0,0,0,1 },
- { "switch", T_SWITCH, 0, 0, 0, 0,0,0,0,1 },
- { "symbolrename", T_SYMBOLRENAME,0, 0, 0, 0,0,0,0,2 },
- { "syslog", T_AT_FORMAT_SYSLOG,0, 0, 0, 0,0,1,1,5 },
- { "transparent_union",T_AT_TUNION,0, 0, 0, 0,0,1,1,5 },
- { "tls_model", T_AT_TLS_MODEL, 0, 0, 0, 0,0,1,1,5 },
- { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0,0,0,0,1 },
- { "typeof", T_TYPEOF, 0, 0, 0, 0,0,1,0,7 },
- { "union", T_STRUCT_OR_UNION, 0, UNION, 0, 0,0,0,0,1 },
- { "unsigned", T_TYPE, 0, UNSIGN, 0, 0,0,0,0,1 },
- { "unused", T_AT_UNUSED, 0, 0, 0, 0,0,1,1,5 },
- { "used", T_AT_USED, 0, 0, 0, 0,0,1,1,5 },
- { "visibility", T_AT_VISIBILITY,0, 0, 0, 0,0,1,1,5 },
- { "void", T_TYPE, 0, VOID, 0, 0,0,0,0,1 },
- { "volatile", T_QUAL, 0, 0, VOLATILE, 1,0,0,0,7 },
- { "warn_unused_result", T_AT_WARN_UNUSED_RESULT, 0, 0, 0, 0,0,1,1,5 },
- { "weak", T_AT_WEAK, 0, 0, 0, 0,0,1,1,5 },
- { "while", T_WHILE, 0, 0, 0, 0,0,0,0,1 },
- { NULL, 0, 0, 0, 0, 0,0,0,0,0 }
-};
-
-/* Symbol table */
-static sym_t *symtab[HSHSIZ1];
-
-/* bit i of the entry with index i is set */
-uint64_t qbmasks[64];
-
-/* least significant i bits are set in the entry with index i */
-uint64_t qlmasks[64 + 1];
-
-/* least significant i bits are not set in the entry with index i */
-uint64_t qumasks[64 + 1];
-
-/* free list for sbuf structures */
-static sbuf_t *sbfrlst;
-
-/* type of next expected symbol */
-symt_t symtyp;
-
-
-static void
-add_keyword(struct kwtab *kw, int deco)
-{
- sym_t *sym;
- size_t h;
- char buf[256];
- const char *name;
-
- if (!(kw->kw_deco & deco))
- return;
-
- switch (deco) {
- case 1:
- name = kw->kw_name;
- break;
- case 2:
- snprintf(buf, sizeof(buf), "__%s", kw->kw_name);
- name = strdup(buf);
- break;
- default:
- lint_assert(deco == 4);
- snprintf(buf, sizeof(buf), "__%s__", kw->kw_name);
- name = strdup(buf);
- break;
- }
-
- if (name == NULL)
- err(1, "Can't init symbol table");
-
- sym = getblk(sizeof (sym_t));
- sym->s_name = name;
- sym->s_keyword = kw;
- sym->s_value.v_quad = kw->kw_token;
- if (kw->kw_token == T_TYPE || kw->kw_token == T_STRUCT_OR_UNION) {
- sym->s_tspec = kw->kw_tspec;
- } else if (kw->kw_token == T_SCLASS) {
- sym->s_scl = kw->kw_scl;
- } else if (kw->kw_token == T_QUAL) {
- sym->s_tqual = kw->kw_tqual;
- }
- h = hash(sym->s_name);
- if ((sym->s_link = symtab[h]) != NULL)
- symtab[h]->s_rlink = &sym->s_link;
- sym->s_rlink = &symtab[h];
- symtab[h] = sym;
-}
-
-/*
- * All keywords are written to the symbol table. This saves us looking
- * in a extra table for each name we found.
- */
-void
-initscan(void)
-{
- struct kwtab *kw;
- size_t i;
- uint64_t uq;
-
- for (kw = kwtab; kw->kw_name != NULL; kw++) {
- if ((kw->kw_c89 || kw->kw_c99) && tflag)
- continue;
- if (kw->kw_c99 && !(Sflag || gflag))
- continue;
- if (kw->kw_gcc && !gflag)
- continue;
- add_keyword(kw, 1);
- add_keyword(kw, 2);
- add_keyword(kw, 4);
- }
-
- /* initialize bit-masks for quads */
- for (i = 0; i < 64; i++) {
- qbmasks[i] = (uint64_t)1 << i;
- uq = ~(uint64_t)0 << i;
- qumasks[i] = uq;
- qlmasks[i] = ~uq;
- }
- qumasks[i] = 0;
- qlmasks[i] = ~(uint64_t)0;
-}
-
-/*
- * Get a free sbuf structure, if possible from the free list
- */
-static sbuf_t *
-allocsb(void)
-{
- sbuf_t *sb;
-
- if ((sb = sbfrlst) != NULL) {
- sbfrlst = sb->sb_next;
-#ifdef BLKDEBUG
- (void)memset(sb, 0, sizeof (*sb));
-#else
- sb->sb_next = NULL;
-#endif
- } else {
- sb = xmalloc(sizeof (sbuf_t));
- (void)memset(sb, 0, sizeof (*sb));
- }
- return sb;
-}
-
-/*
- * Put a sbuf structure to the free list
- */
-static void
-freesb(sbuf_t *sb)
-{
-
- (void)memset(sb, ZERO, sizeof (*sb));
- sb->sb_next = sbfrlst;
- sbfrlst = sb;
-}
-
-/*
- * Read a character and ensure that it is positive (except EOF).
- * Increment line count(s) if necessary.
- */
-static int
-inpc(void)
-{
- int c;
-
- if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
- incline();
- return c;
-}
-
-static int
-hash(const char *s)
-{
- u_int v;
- const u_char *us;
-
- v = 0;
- for (us = (const u_char *)s; *us != '\0'; us++) {
- v = (v << sizeof (v)) + *us;
- v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
- }
- return v % HSHSIZ1;
-}
-
-/*
- * Lex has found a letter followed by zero or more letters or digits.
- * It looks for a symbol in the symbol table with the same name. This
- * symbol must either be a keyword or a symbol of the type required by
- * symtyp (label, member, tag, ...).
- *
- * If it is a keyword, the token is returned. In some cases it is described
- * more deeply by data written to yylval.
- *
- * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
- * is stored in yylval. This struct contains the name of the symbol, its
- * length and hash value. If there is already a symbol of the same name
- * and type in the symbol table, the sbuf struct also contains a pointer
- * to the symbol table entry.
- */
-static int
-name(void)
-{
- char *s;
- sbuf_t *sb;
- sym_t *sym;
- int tok;
-
- sb = allocsb();
- sb->sb_name = yytext;
- sb->sb_len = yyleng;
- sb->sb_hash = hash(yytext);
- if ((sym = search(sb)) != NULL && sym->s_keyword) {
- freesb(sb);
- return keyw(sym);
- }
-
- sb->sb_sym = sym;
-
- if (sym != NULL) {
- lint_assert(blklev >= sym->s_blklev);
- sb->sb_name = sym->s_name;
- sb->sb_len = strlen(sym->s_name);
- tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
- } else {
- s = getblk(yyleng + 1);
- (void)memcpy(s, yytext, yyleng + 1);
- sb->sb_name = s;
- sb->sb_len = yyleng;
- tok = T_NAME;
- }
-
- yylval.y_sb = sb;
- return tok;
-}
-
-static sym_t *
-search(sbuf_t *sb)
-{
- sym_t *sym;
-
- for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
- if (strcmp(sym->s_name, sb->sb_name) == 0) {
- if (sym->s_keyword) {
- struct kwtab *kw = sym->s_keyword;
- if (!kw->kw_attr || attron)
- return sym;
- } else if (!attron && sym->s_kind == symtyp)
- return sym;
- }
- }
-
- return NULL;
-}
-
-static int
-keyw(sym_t *sym)
-{
- int t;
-
- if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
- yylval.y_scl = sym->s_scl;
- } else if (t == T_TYPE || t == T_STRUCT_OR_UNION) {
- yylval.y_tspec = sym->s_tspec;
- } else if (t == T_QUAL) {
- yylval.y_tqual = sym->s_tqual;
- }
- return t;
-}
-
-/*
- * Convert a string representing an integer into internal representation.
- * The value is returned in yylval. icon() (and yylex()) returns T_CON.
- */
-static int
-icon(int base)
-{
- int l_suffix, u_suffix;
- int len;
- const char *cp;
- char c, *eptr;
- tspec_t typ;
- bool ansiu;
-#ifdef TARG_INT128_MAX
- __uint128_t uq = 0;
- static tspec_t contypes[2][4] = {
- { INT, LONG, QUAD, INT128, },
- { UINT, ULONG, UQUAD, UINT128, }
- };
-#else
- uint64_t uq = 0;
- static tspec_t contypes[2][3] = {
- { INT, LONG, QUAD, },
- { UINT, ULONG, UQUAD, }
- };
-#endif
-
- cp = yytext;
- len = yyleng;
-
- /* skip 0[xX] or 0[bB] */
- if (base == 16 || base == 2) {
- cp += 2;
- len -= 2;
- }
-
- /* read suffixes */
- l_suffix = u_suffix = 0;
- for (;;) {
- if ((c = cp[len - 1]) == 'l' || c == 'L') {
- l_suffix++;
- } else if (c == 'u' || c == 'U') {
- u_suffix++;
- } else {
- break;
- }
- len--;
- }
- if (l_suffix > 2 || u_suffix > 1) {
- /* malformed integer constant */
- warning(251);
- if (l_suffix > 2)
- l_suffix = 2;
- if (u_suffix > 1)
- u_suffix = 1;
- }
- if (tflag && u_suffix != 0) {
- /* suffix U is illegal in traditional C */
- warning(97);
- }
- typ = contypes[u_suffix][l_suffix];
-
- errno = 0;
-
- uq = strtouq(cp, &eptr, base);
- lint_assert(eptr == cp + len);
- if (errno != 0)
- /* integer constant out of range */
- warning(252);
-
- /*
- * If the value is too big for the current type, we must choose
- * another type.
- */
- ansiu = false;
- switch (typ) {
- case INT:
- if (uq <= TARG_INT_MAX) {
- /* ok */
- } else if (uq <= TARG_UINT_MAX && base != 10) {
- typ = UINT;
- } else if (uq <= TARG_LONG_MAX) {
- typ = LONG;
- } else {
- typ = ULONG;
- if (uq > TARG_ULONG_MAX) {
- /* integer constant out of range */
- warning(252);
- }
- }
- if (typ == UINT || typ == ULONG) {
- if (tflag) {
- typ = LONG;
- } else if (!sflag) {
- /*
- * Remember that the constant is unsigned
- * only in ANSI C
- */
- ansiu = true;
- }
- }
- break;
- case UINT:
- if (uq > TARG_UINT_MAX) {
- typ = ULONG;
- if (uq > TARG_ULONG_MAX) {
- /* integer constant out of range */
- warning(252);
- }
- }
- break;
- case LONG:
- if (uq > TARG_LONG_MAX && !tflag) {
- typ = ULONG;
- if (!sflag)
- ansiu = true;
- if (uq > TARG_ULONG_MAX) {
- /* integer constant out of range */
- warning(252);
- }
- }
- break;
- case ULONG:
- if (uq > TARG_ULONG_MAX) {
- /* integer constant out of range */
- warning(252);
- }
- break;
- case QUAD:
- if (uq > TARG_QUAD_MAX && !tflag) {
- typ = UQUAD;
- if (!sflag)
- ansiu = true;
- }
- break;
- case UQUAD:
- if (uq > TARG_UQUAD_MAX) {
- /* integer constant out of range */
- warning(252);
- }
- break;
-#ifdef INT128_SIZE
- case INT128:
-#ifdef TARG_INT128_MAX
- if (uq > TARG_INT128_MAX && !tflag) {
- typ = UINT128;
- if (!sflag)
- ansiu = true;
- }
-#endif
- break;
- case UINT128:
-#ifdef TARG_INT128_MAX
- if (uq > TARG_UINT128_MAX) {
- /* integer constant out of range */
- warning(252);
- }
-#endif
- break;
-#endif
- /* LINTED206: (enumeration values not handled in switch) */
- case STRUCT:
- case VOID:
- case LDOUBLE:
- case FUNC:
- case ARRAY:
- case PTR:
- case ENUM:
- case UNION:
- case SIGNED:
- case NOTSPEC:
- case DOUBLE:
- case FLOAT:
- case USHORT:
- case SHORT:
- case UCHAR:
- case SCHAR:
- case CHAR:
- case BOOL:
- case UNSIGN:
- case FCOMPLEX:
- case DCOMPLEX:
- case LCOMPLEX:
- case COMPLEX:
- break;
- }
-
- uq = (uint64_t)xsign((int64_t)uq, typ, -1);
-
- (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
- yylval.y_val->v_ansiu = ansiu;
- yylval.y_val->v_quad = (int64_t)uq;
-
- return T_CON;
-}
-
-/*
- * Returns 1 if t is a signed type and the value is negative.
- *
- * len is the number of significant bits. If len is -1, len is set
- * to the width of type t.
- */
-int
-sign(int64_t q, tspec_t t, int len)
-{
-
- if (t == PTR || is_uinteger(t))
- return 0;
- return msb(q, t, len);
-}
-
int
-msb(int64_t q, tspec_t t, int len)
-{
-
- if (len <= 0)
- len = size(t);
- return (q & qbmasks[len - 1]) != 0;
-}
-
-/*
- * Extends the sign of q.
- */
-int64_t
-xsign(int64_t q, tspec_t t, int len)
-{
-
- if (len <= 0)
- len = size(t);
-
- if (t == PTR || is_uinteger(t) || !sign(q, t, len)) {
- q &= qlmasks[len];
- } else {
- q |= qumasks[len];
- }
- return q;
-}
-
-/*
- * Convert a string representing a floating point value into its integral
- * representation. Type and value are returned in yylval. fcon()
- * (and yylex()) returns T_CON.
- * XXX Currently it is not possible to convert constants of type
- * long double which are greater than DBL_MAX.
- */
-static int
-fcon(void)
-{
- const char *cp;
- int len;
- tspec_t typ;
- char c, *eptr;
- double d;
- float f = 0;
-
- cp = yytext;
- len = yyleng;
-
- if (cp[len - 1] == 'i') {
- /* imaginary, do nothing for now */
- len--;
- }
- if ((c = cp[len - 1]) == 'f' || c == 'F') {
- typ = FLOAT;
- len--;
- } else if (c == 'l' || c == 'L') {
- typ = LDOUBLE;
- len--;
- } else {
- if (c == 'd' || c == 'D')
- len--;
- typ = DOUBLE;
- }
-
- if (tflag && typ != DOUBLE) {
- /* suffixes F and L are illegal in traditional C */
- warning(98);
- }
-
- errno = 0;
- d = strtod(cp, &eptr);
- if (eptr != cp + len) {
- switch (*eptr) {
- /*
- * XXX: non-native non-current strtod() may not handle hex
- * floats, ignore the rest if we find traces of hex float
- * syntax...
- */
- case 'p':
- case 'P':
- case 'x':
- case 'X':
- d = 0;
- errno = 0;
- break;
- default:
- LERROR("fcon(%s->%s)", cp, eptr);
- }
- }
- if (errno != 0)
- /* floating-point constant out of range */
- warning(248);
-
- if (typ == FLOAT) {
- f = (float)d;
- if (!finite(f)) {
- /* floating-point constant out of range */
- warning(248);
- f = f > 0 ? FLT_MAX : -FLT_MAX;
- }
- }
-
- (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
- if (typ == FLOAT) {
- yylval.y_val->v_ldbl = f;
- } else {
- yylval.y_val->v_ldbl = d;
- }
-
- return T_CON;
-}
-
-static int
-operator(int t, op_t o)
-{
-
- yylval.y_op = o;
- return t;
-}
-
-/*
- * Called if lex found a leading \'.
- */
-static int
-ccon(void)
-{
- size_t n;
- int val, c;
- char cv;
-
- n = 0;
- val = 0;
- while ((c = getescc('\'')) >= 0) {
- val = (val << CHAR_SIZE) + c;
- n++;
- }
- if (c == -2) {
- /* unterminated character constant */
- error(253);
- } else {
- if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
- /* too many characters in character constant */
- error(71);
- } else if (n > 1) {
- /* multi-character character constant */
- warning(294);
- } else if (n == 0) {
- /* empty character constant */
- error(73);
- }
- }
- if (n == 1) {
- cv = (char)val;
- val = cv;
- }
-
- yylval.y_val = xcalloc(1, sizeof (val_t));
- yylval.y_val->v_tspec = INT;
- yylval.y_val->v_quad = val;
-
- return T_CON;
-}
-
-/*
- * Called if lex found a leading L\'
- */
-static int
-wccon(void)
-{
- static char buf[MB_LEN_MAX + 1];
- size_t i;
- int c;
- wchar_t wc;
-
- i = 0;
- while ((c = getescc('\'')) >= 0) {
- if (i < MB_CUR_MAX)
- buf[i] = (char)c;
- i++;
- }
-
- wc = 0;
-
- if (c == -2) {
- /* unterminated character constant */
- error(253);
- } else if (c == 0) {
- /* empty character constant */
- error(73);
- } else {
- if (i > MB_CUR_MAX) {
- i = MB_CUR_MAX;
- /* too many characters in character constant */
- error(71);
- } else {
- buf[i] = '\0';
- (void)mbtowc(NULL, NULL, 0);
- if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
- /* invalid multibyte character */
- error(291);
- }
- }
-
- yylval.y_val = xcalloc(1, sizeof (val_t));
- yylval.y_val->v_tspec = WCHAR;
- yylval.y_val->v_quad = wc;
-
- return T_CON;
-}
-
-/*
- * Read a character which is part of a character constant or of a string
- * and handle escapes.
- *
- * The Argument is the character which delimits the character constant or
- * string.
- *
- * Returns -1 if the end of the character constant or string is reached,
- * -2 if the EOF is reached, and the character otherwise.
- */
-static int
-getescc(int d)
-{
- static int pbc = -1;
- int n, c, v;
-
- if (pbc == -1) {
- c = inpc();
- } else {
- c = pbc;
- pbc = -1;
- }
- if (c == d)
- return -1;
- switch (c) {
- case '\n':
- if (tflag) {
- /* newline in string or char constant */
- error(254);
- return -2;
- }
- return c;
- case EOF:
- return -2;
- case '\\':
- switch (c = inpc()) {
- case '"':
- if (tflag && d == '\'')
- /* \" inside character constants undef... */
- warning(262);
- return '"';
- case '\'':
- return '\'';
- case '?':
- if (tflag)
- /* \? undefined in traditional C */
- warning(263);
- return '?';
- case '\\':
- return '\\';
- case 'a':
- if (tflag)
- /* \a undefined in traditional C */
- warning(81);
- return '\a';
- case 'b':
- return '\b';
- case 'f':
- return '\f';
- case 'n':
- return '\n';
- case 'r':
- return '\r';
- case 't':
- return '\t';
- case 'v':
- if (tflag)
- /* \v undefined in traditional C */
- warning(264);
- return '\v';
- case '8': case '9':
- /* bad octal digit %c */
- warning(77, c);
- /* FALLTHROUGH */
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- n = 3;
- v = 0;
- do {
- v = (v << 3) + (c - '0');
- c = inpc();
- } while (--n && isdigit(c) && (tflag || c <= '7'));
- if (tflag && n > 0 && isdigit(c))
- /* bad octal digit %c */
- warning(77, c);
- pbc = c;
- if (v > TARG_UCHAR_MAX) {
- /* character escape does not fit in character */
- warning(76);
- v &= CHAR_MASK;
- }
- return v;
- case 'x':
- if (tflag)
- /* \x undefined in traditional C */
- warning(82);
- v = 0;
- n = 0;
- while ((c = inpc()) >= 0 && isxdigit(c)) {
- c = isdigit(c) ?
- c - '0' : toupper(c) - 'A' + 10;
- v = (v << 4) + c;
- if (n >= 0) {
- if ((v & ~CHAR_MASK) != 0) {
- /* overflow in hex escape */
- warning(75);
- n = -1;
- } else {
- n++;
- }
- }
- }
- pbc = c;
- if (n == 0) {
- /* no hex digits follow \x */
- error(74);
- } if (n == -1) {
- v &= CHAR_MASK;
- }
- return v;
- case '\n':
- return getescc(d);
- case EOF:
- return -2;
- default:
- if (isprint(c)) {
- /* dubious escape \%c */
- warning(79, c);
- } else {
- /* dubious escape \%o */
- warning(80, c);
- }
- }
- }
- return c;
-}
-
-/* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
-static void
-parse_line_directive_flags(const char *p)
-{
-
- in_system_header = false;
-
- while (ch_isspace(*p))
- p++;
- while (ch_isdigit(*p)) {
- if (*p == '3' && !ch_isdigit(p[1]))
- in_system_header = true;
- p++;
- while (ch_isspace(*p))
- p++;
- }
-
-#if 0
- if (c != '\0')
- warning("extra character(s) after directive");
-#endif
-}
-
-/*
- * Called for preprocessor directives. Currently implemented are:
- * # lineno
- * # lineno "filename"
- */
-static void
-directive(void)
-{
- const char *cp, *fn;
- char c, *eptr;
- size_t fnl;
- long ln;
- static bool first = true;
-
- /* Go to first non-whitespace after # */
- for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
- continue;
-
- if (!ch_isdigit(c)) {
- if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
- return;
- error:
- /* undefined or invalid # directive */
- warning(255);
- return;
- }
- ln = strtol(--cp, &eptr, 10);
- if (cp == eptr)
- goto error;
- if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
- goto error;
- while ((c = *cp++) == ' ' || c == '\t')
- continue;
- if (c != '\0') {
- if (c != '"')
- goto error;
- fn = cp;
- while ((c = *cp) != '"' && c != '\0')
- cp++;
- if (c != '"')
- goto error;
- if ((fnl = cp++ - fn) > PATH_MAX)
- goto error;
- parse_line_directive_flags(cp);
-
- /* empty string means stdin */
- if (fnl == 0) {
- fn = "{standard input}";
- fnl = 16; /* strlen (fn) */
- }
- curr_pos.p_file = fnnalloc(fn, fnl);
- /*
- * If this is the first directive, the name is the name
- * of the C source file as specified at the command line.
- * It is written to the output file.
- */
- if (first) {
- csrc_pos.p_file = curr_pos.p_file;
- outsrc(fnxform(curr_pos.p_file,
- strlen(curr_pos.p_file)));
- first = false;
- }
- }
- curr_pos.p_line = (int)ln - 1;
- curr_pos.p_uniq = 0;
- if (curr_pos.p_file == csrc_pos.p_file) {
- csrc_pos.p_line = (int)ln - 1;
- csrc_pos.p_uniq = 0;
- }
-}
-
-/*
- * Handle lint comments. Following comments are currently understood:
- * ARGSUSEDn
- * BITFIELDTYPE
- * CONSTCOND CONSTANTCOND CONSTANTCONDITION
- * FALLTHRU FALLTHROUGH
- * LINTLIBRARY
- * LINTEDn NOSTRICTn
- * LONGLONG
- * NOTREACHED
- * PRINTFLIKEn
- * PROTOLIB
- * SCANFLIKEn
- * VARARGSn
- * If one of these comments is recognized, the argument, if any, is
- * parsed and a function which handles this comment is called.
- */
-static void
-comment(void)
-{
- int c, lc;
- static const struct {
- const char *keywd;
- int arg;
- void (*func)(int);
- } keywtab[] = {
- { "ARGSUSED", 1, argsused },
- { "BITFIELDTYPE", 0, bitfieldtype },
- { "CONSTCOND", 0, constcond },
- { "CONSTANTCOND", 0, constcond },
- { "CONSTANTCONDITION", 0, constcond },
- { "FALLTHRU", 0, fallthru },
- { "FALLTHROUGH", 0, fallthru },
- { "LINTLIBRARY", 0, lintlib },
- { "LINTED", 1, linted },
- { "LONGLONG", 0, longlong },
- { "NOSTRICT", 1, linted },
- { "NOTREACHED", 0, notreach },
- { "PRINTFLIKE", 1, printflike },
- { "PROTOLIB", 1, protolib },
- { "SCANFLIKE", 1, scanflike },
- { "VARARGS", 1, varargs },
- };
- char keywd[32];
- char arg[32];
- size_t l, i;
- int a;
- bool eoc;
-
- eoc = false;
-
- /* Skip whitespace after the start of the comment */
- while ((c = inpc()) != EOF && isspace(c))
- continue;
-
- /* Read the potential keyword to keywd */
- l = 0;
- while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
- keywd[l++] = (char)c;
- c = inpc();
- }
- keywd[l] = '\0';
-
- /* look for the keyword */
- for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
- if (strcmp(keywtab[i].keywd, keywd) == 0)
- break;
- }
- if (i == sizeof (keywtab) / sizeof (keywtab[0]))
- goto skip_rest;
-
- /* skip whitespace after the keyword */
- while (c != EOF && isspace(c))
- c = inpc();
-
- /* read the argument, if the keyword accepts one and there is one */
- l = 0;
- if (keywtab[i].arg) {
- while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
- arg[l++] = (char)c;
- c = inpc();
- }
- }
- arg[l] = '\0';
- a = l != 0 ? atoi(arg) : -1;
-
- /* skip whitespace after the argument */
- while (c != EOF && isspace(c))
- c = inpc();
-
- if (c != '*' || (c = inpc()) != '/') {
- if (keywtab[i].func != linted)
- /* extra characters in lint comment */
- warning(257);
- } else {
- /*
- * remember that we have already found the end of the
- * comment
- */
- eoc = true;
- }
-
- if (keywtab[i].func != NULL)
- (*keywtab[i].func)(a);
-
- skip_rest:
- while (!eoc) {
- lc = c;
- if ((c = inpc()) == EOF) {
- /* unterminated comment */
- error(256);
- break;
- }
- if (lc == '*' && c == '/')
- eoc = true;
- }
-}
-
-/*
- * Handle // style comments
- */
-static void
-slashslashcomment(void)
-{
- int c;
-
- if (!Sflag && !gflag)
- /* %s C does not support // comments */
- gnuism(312, tflag ? "traditional" : "ANSI");
-
- while ((c = inpc()) != EOF && c != '\n')
- continue;
-}
-
-/*
- * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
- * clear_warn_flags() is called after function definitions and global and
- * local declarations and definitions. It is also called between
- * the controlling expression and the body of control statements
- * (if, switch, for, while).
- */
-void
-clear_warn_flags(void)
-{
-
- lwarn = LWARN_ALL;
- quadflg = false;
- constcond_flag = false;
-}
-
-/*
- * Strings are stored in a dynamically allocated buffer and passed
- * in yylval.y_xstrg to the parser. The parser or the routines called
- * by the parser are responsible for freeing this buffer.
- */
-static int
-string(void)
-{
- u_char *s;
- int c;
- size_t len, max;
- strg_t *strg;
-
- s = xmalloc(max = 64);
-
- len = 0;
- while ((c = getescc('"')) >= 0) {
- /* +1 to reserve space for a trailing NUL character */
- if (len + 1 == max)
- s = xrealloc(s, max *= 2);
- s[len++] = (char)c;
- }
- s[len] = '\0';
- if (c == -2)
- /* unterminated string constant */
- error(258);
-
- strg = xcalloc(1, sizeof (strg_t));
- strg->st_tspec = CHAR;
- strg->st_len = len;
- strg->st_cp = s;
-
- yylval.y_string = strg;
- return T_STRING;
-}
-
-static int
-wcstrg(void)
-{
- char *s;
- int c, n;
- size_t i, wi;
- size_t len, max, wlen;
- wchar_t *ws;
- strg_t *strg;
-
- s = xmalloc(max = 64);
- len = 0;
- while ((c = getescc('"')) >= 0) {
- /* +1 to save space for a trailing NUL character */
- if (len + 1 >= max)
- s = xrealloc(s, max *= 2);
- s[len++] = (char)c;
- }
- s[len] = '\0';
- if (c == -2)
- /* unterminated string constant */
- error(258);
-
- /* get length of wide-character string */
- (void)mblen(NULL, 0);
- for (i = 0, wlen = 0; i < len; i += n, wlen++) {
- if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
- /* invalid multibyte character */
- error(291);
- break;
- }
- if (n == 0)
- n = 1;
- }
-
- ws = xmalloc((wlen + 1) * sizeof (wchar_t));
-
- /* convert from multibyte to wide char */
- (void)mbtowc(NULL, NULL, 0);
- for (i = 0, wi = 0; i < len; i += n, wi++) {
- if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
- break;
- if (n == 0)
- n = 1;
- }
- ws[wi] = 0;
- free(s);
-
- strg = xcalloc(1, sizeof (strg_t));
- strg->st_tspec = WCHAR;
- strg->st_len = wlen;
- strg->st_wcp = ws;
-
- yylval.y_string = strg;
- return T_STRING;
-}
-
-/*
- * As noted above the scanner does not create new symbol table entries
- * for symbols it cannot find in the symbol table. This is to avoid
- * putting undeclared symbols into the symbol table if a syntax error
- * occurs.
- *
- * getsym() is called as soon as it is probably ok to put the symbol to
- * the symbol table. This does not mean that it is not possible that
- * symbols are put to the symbol table which are not completely
- * declared due to syntax errors. To avoid too many problems in this
- * case, symbols get type int in getsym().
- *
- * XXX calls to getsym() should be delayed until decl1*() is called
- */
-sym_t *
-getsym(sbuf_t *sb)
-{
- dinfo_t *di;
- char *s;
- sym_t *sym;
-
- sym = sb->sb_sym;
-
- /*
- * During member declaration it is possible that name() looked
- * for symbols of type FVFT, although it should have looked for
- * symbols of type FTAG. Same can happen for labels. Both cases
- * are compensated here.
- */
- if (symtyp == FMEMBER || symtyp == FLABEL) {
- if (sym == NULL || sym->s_kind == FVFT)
- sym = search(sb);
- }
-
- if (sym != NULL) {
- if (sym->s_kind != symtyp)
- LERROR("getsym(%d, %d)", sym->s_kind, symtyp);
- symtyp = FVFT;
- freesb(sb);
- return sym;
- }
-
- /* create a new symbol table entry */
-
- /* labels must always be allocated at level 1 (outermost block) */
- if (symtyp == FLABEL) {
- sym = getlblk(1, sizeof (sym_t));
- s = getlblk(1, sb->sb_len + 1);
- (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
- sym->s_name = s;
- sym->s_blklev = 1;
- di = dcs;
- while (di->d_next != NULL && di->d_next->d_next != NULL)
- di = di->d_next;
- lint_assert(di->d_ctx == AUTO);
- } else {
- sym = getblk(sizeof (sym_t));
- sym->s_name = sb->sb_name;
- sym->s_blklev = blklev;
- di = dcs;
- }
-
- UNIQUE_CURR_POS(sym->s_def_pos);
- if ((sym->s_kind = symtyp) != FLABEL)
- sym->s_type = gettyp(INT);
-
- symtyp = FVFT;
-
- if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
- symtab[sb->sb_hash]->s_rlink = &sym->s_link;
- sym->s_rlink = &symtab[sb->sb_hash];
- symtab[sb->sb_hash] = sym;
-
- *di->d_ldlsym = sym;
- di->d_ldlsym = &sym->s_dlnxt;
-
- freesb(sb);
- return sym;
-}
-
-/*
- * Construct a temporary symbol. The symbol starts with a digit, so that
- * it is illegal.
- */
-sym_t *
-mktempsym(type_t *t)
-{
- static int n = 0;
- int h;
- char *s = getlblk(blklev, 64);
- sym_t *sym = getblk(sizeof (sym_t));
-
- (void)snprintf(s, 64, "%.8d_tmp", n++);
- h = hash(s);
-
- sym->s_name = s;
- sym->s_type = t;
- sym->s_blklev = blklev;
- sym->s_scl = AUTO;
- sym->s_kind = FVFT;
- sym->s_used = true;
- sym->s_set = true;
-
- if ((sym->s_link = symtab[h]) != NULL)
- symtab[h]->s_rlink = &sym->s_link;
- sym->s_rlink = &symtab[h];
- symtab[h] = sym;
-
- *dcs->d_ldlsym = sym;
- dcs->d_ldlsym = &sym->s_dlnxt;
-
- return sym;
-}
-
-/*
- * Remove a symbol forever from the symbol table. s_blklev
- * is set to -1 to avoid that the symbol will later be put
- * back to the symbol table.
- */
-void
-rmsym(sym_t *sym)
-{
-
- if ((*sym->s_rlink = sym->s_link) != NULL)
- sym->s_link->s_rlink = sym->s_rlink;
- sym->s_blklev = -1;
- sym->s_link = NULL;
-}
-
-/*
- * Remove a list of symbols declared at one level from the symbol
- * table.
- */
-void
-rmsyms(sym_t *syms)
-{
- sym_t *sym;
-
- for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
- if (sym->s_blklev != -1) {
- if ((*sym->s_rlink = sym->s_link) != NULL)
- sym->s_link->s_rlink = sym->s_rlink;
- sym->s_link = NULL;
- sym->s_rlink = NULL;
- }
- }
-}
-
-/*
- * Put a symbol into the symbol table
- */
-void
-inssym(int bl, sym_t *sym)
-{
- int h;
-
- h = hash(sym->s_name);
- if ((sym->s_link = symtab[h]) != NULL)
- symtab[h]->s_rlink = &sym->s_link;
- sym->s_rlink = &symtab[h];
- symtab[h] = sym;
- sym->s_blklev = bl;
- lint_assert(sym->s_link == NULL ||
- sym->s_blklev >= sym->s_link->s_blklev);
-}
-
-/*
- * Called at level 0 after syntax errors
- * Removes all symbols which are not declared at level 0 from the
- * symbol table. Also frees all memory which is not associated with
- * level 0.
- */
-void
-cleanup(void)
-{
- sym_t *sym, *nsym;
- int i;
-
- for (i = 0; i < HSHSIZ1; i++) {
- for (sym = symtab[i]; sym != NULL; sym = nsym) {
- nsym = sym->s_link;
- if (sym->s_blklev >= 1) {
- if ((*sym->s_rlink = nsym) != NULL)
- nsym->s_rlink = sym->s_rlink;
- }
- }
- }
-
- for (i = mblklev; i > 0; i--)
- freelblk(i);
-}
-
-/*
- * Create a new symbol with the name of an existing symbol.
- */
-sym_t *
-pushdown(sym_t *sym)
-{
- int h;
- sym_t *nsym;
-
- h = hash(sym->s_name);
- nsym = getblk(sizeof (sym_t));
- lint_assert(sym->s_blklev <= blklev);
- nsym->s_name = sym->s_name;
- UNIQUE_CURR_POS(nsym->s_def_pos);
- nsym->s_kind = sym->s_kind;
- nsym->s_blklev = blklev;
-
- if ((nsym->s_link = symtab[h]) != NULL)
- symtab[h]->s_rlink = &nsym->s_link;
- nsym->s_rlink = &symtab[h];
- symtab[h] = nsym;
-
- *dcs->d_ldlsym = nsym;
- dcs->d_ldlsym = &nsym->s_dlnxt;
-
- return nsym;
-}
-
-/*
- * Free any dynamically allocated memory referenced by
- * the value stack or yylval.
- * The type of information in yylval is described by tok.
- */
-void
-freeyyv(void *sp, int tok)
+lex_input(void)
{
- if (tok == T_NAME || tok == T_TYPENAME) {
- sbuf_t *sb = *(sbuf_t **)sp;
- freesb(sb);
- } else if (tok == T_CON) {
- val_t *val = *(val_t **)sp;
- free(val);
- } else if (tok == T_STRING) {
- strg_t *strg = *(strg_t **)sp;
- if (strg->st_tspec == CHAR) {
- free(strg->st_cp);
- } else {
- lint_assert(strg->st_tspec == WCHAR);
- free(strg->st_wcp);
- }
- free(strg);
- }
+ return input();
}
Added files:
Index: src/usr.bin/xlint/lint1/lex.c
diff -u /dev/null src/usr.bin/xlint/lint1/lex.c:1.1
--- /dev/null Sat Jan 23 17:58:03 2021
+++ src/usr.bin/xlint/lint1/lex.c Sat Jan 23 17:58:03 2021
@@ -0,0 +1,1602 @@
+/* $NetBSD: lex.c,v 1.1 2021/01/23 17:58:03 rillig Exp $ */
+
+/*
+ * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
+ * Copyright (c) 1994, 1995 Jochen Pohl
+ * All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Jochen Pohl for
+ * The NetBSD Project.
+ * 4. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: lex.c,v 1.1 2021/01/23 17:58:03 rillig Exp $");
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "lint1.h"
+#include "cgram.h"
+
+#define CHAR_MASK ((int)(~(~0U << CHAR_SIZE)))
+
+
+/* Current position (it's also updated when an included file is parsed) */
+pos_t curr_pos = { 1, "", 0 };
+
+/*
+ * Current position in C source (not updated when an included file is
+ * parsed).
+ */
+pos_t csrc_pos = { 1, "", 0 };
+
+/* Are we parsing a gcc attribute? */
+bool attron;
+
+bool in_system_header = false;
+
+static sbuf_t *allocsb(void);
+static void freesb(sbuf_t *);
+static int inpc(void);
+static int hash(const char *);
+static sym_t * search(sbuf_t *);
+static int keyw(sym_t *);
+static int getescc(int);
+
+void
+lex_incline(void)
+{
+ curr_pos.p_line++;
+ curr_pos.p_uniq = 0;
+#ifdef DEBUG
+ printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
+#endif
+ if (curr_pos.p_file == csrc_pos.p_file) {
+ csrc_pos.p_line++;
+ csrc_pos.p_uniq = 0;
+ }
+}
+
+void
+lex_badchar(int c)
+{
+
+ /* unknown character \%o */
+ error(250, c);
+}
+
+/*
+ * Keywords.
+ * During initialisation they are written to the symbol table.
+ */
+static struct kwtab {
+ const char *kw_name; /* keyword */
+ int kw_token; /* token returned by yylex() */
+ scl_t kw_scl; /* storage class if kw_token T_SCLASS */
+ tspec_t kw_tspec; /* type spec. if kw_token
+ * T_TYPE or T_STRUCT_OR_UNION */
+ tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
+ bool kw_c89 : 1; /* C89 keyword */
+ bool kw_c99 : 1; /* C99 keyword */
+ bool kw_gcc : 1; /* GCC keyword */
+ bool kw_attr : 1; /* GCC attribute, keyword */
+ u_int kw_deco : 3; /* 1 = name, 2 = __name, 4 = __name__ */
+} kwtab[] = {
+#ifdef INT128_SIZE
+ { "__int128_t", T_TYPE, 0, INT128, 0, 0,1,0,0,1 },
+ { "__uint128_t",T_TYPE, 0, UINT128,0, 0,1,0,0,1 },
+#endif
+ { "__thread", T_QUAL, 0, 0, THREAD, 0,0,1,0,1 },
+ { "_Alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,1 },
+ { "_Bool", T_TYPE, 0, BOOL, 0, 0,1,0,0,1 },
+ { "_Complex", T_TYPE, 0, COMPLEX,0, 0,1,0,0,1 },
+ { "_Generic", T_GENERIC, 0, 0, 0, 0,1,0,0,1 },
+ { "_Noreturn", T_NORETURN, 0, 0, 0, 0,1,0,0,1 },
+ { "_Thread_local",T_QUAL, 0, 0, THREAD, 0,1,0,0,1 },
+ { "alias", T_AT_ALIAS, 0, 0, 0, 0,0,1,1,5 },
+ { "aligned", T_AT_ALIGNED, 0, 0, 0, 0,0,1,1,5 },
+ { "alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,4 },
+ { "alloc_size", T_AT_ALLOC_SIZE,0, 0, 0, 0,0,1,1,5 },
+ { "always_inline", T_AT_ALWAYS_INLINE, 0,0, 0, 0,0,1,1,5 },
+ { "asm", T_ASM, 0, 0, 0, 0,0,1,0,7 },
+ { "attribute", T_ATTRIBUTE, 0, 0, 0, 0,0,1,0,6 },
+ { "auto", T_SCLASS, AUTO, 0, 0, 0,0,0,0,1 },
+ { "bounded", T_AT_BOUNDED, 0, 0, 0, 0,0,1,1,5 },
+ { "break", T_BREAK, 0, 0, 0, 0,0,0,0,1 },
+ { "buffer", T_AT_BUFFER, 0, 0, 0, 0,0,1,1,5 },
+ { "builtin_offsetof", T_BUILTIN_OFFSETOF, 0, 0, 0, 0,0,1,0,2 },
+ { "case", T_CASE, 0, 0, 0, 0,0,0,0,1 },
+ { "char", T_TYPE, 0, CHAR, 0, 0,0,0,0,1 },
+ { "cold", T_AT_COLD, 0, 0, 0, 0,0,1,1,5 },
+ { "const", T_QUAL, 0, 0, CONST, 1,0,0,0,7 },
+ { "constructor",T_AT_CONSTRUCTOR,0, 0, 0, 0,0,1,1,5 },
+ { "continue", T_CONTINUE, 0, 0, 0, 0,0,0,0,1 },
+ { "default", T_DEFAULT, 0, 0, 0, 0,0,0,0,1 },
+ { "deprecated", T_AT_DEPRECATED,0, 0, 0, 0,0,1,1,5 },
+ { "destructor", T_AT_DESTRUCTOR,0, 0, 0, 0,0,1,1,5 },
+ { "do", T_DO, 0, 0, 0, 0,0,0,0,1 },
+ { "double", T_TYPE, 0, DOUBLE, 0, 0,0,0,0,1 },
+ { "else", T_ELSE, 0, 0, 0, 0,0,0,0,1 },
+ { "enum", T_ENUM, 0, 0, 0, 0,0,0,0,1 },
+ { "extension", T_EXTENSION, 0, 0, 0, 0,0,1,0,4 },
+ { "extern", T_SCLASS, EXTERN, 0, 0, 0,0,0,0,1 },
+ { "float", T_TYPE, 0, FLOAT, 0, 0,0,0,0,1 },
+ { "for", T_FOR, 0, 0, 0, 0,0,0,0,1 },
+ { "format", T_AT_FORMAT, 0, 0, 0, 0,0,1,1,5 },
+ { "format_arg", T_AT_FORMAT_ARG,0, 0, 0, 0,0,1,1,5 },
+ { "gnu_inline", T_AT_GNU_INLINE,0, 0, 0, 0,0,1,1,5 },
+ { "gnu_printf", T_AT_FORMAT_GNU_PRINTF,0,0, 0, 0,0,1,1,5 },
+ { "goto", T_GOTO, 0, 0, 0, 0,0,0,0,1 },
+ { "if", T_IF, 0, 0, 0, 0,0,0,0,1 },
+ { "imag", T_IMAG, 0, 0, 0, 0,1,0,0,4 },
+ { "inline", T_SCLASS, INLINE, 0, 0, 0,1,0,0,7 },
+ { "int", T_TYPE, 0, INT, 0, 0,0,0,0,1 },
+ { "long", T_TYPE, 0, LONG, 0, 0,0,0,0,1 },
+ { "malloc", T_AT_MALLOC, 0, 0, 0, 0,0,1,1,5 },
+ { "may_alias", T_AT_MAY_ALIAS, 0, 0, 0, 0,0,1,1,5 },
+ { "minbytes", T_AT_MINBYTES, 0, 0, 0, 0,0,1,1,5 },
+ { "mode", T_AT_MODE, 0, 0, 0, 0,0,1,1,5 },
+ { "no_instrument_function", T_AT_NO_INSTRUMENT_FUNCTION,
+ 0, 0, 0, 0,0,1,1,5 },
+ { "nonnull", T_AT_NONNULL, 0, 0, 0, 0,0,1,1,5 },
+ { "noinline", T_AT_NOINLINE, 0, 0, 0, 0,0,1,1,5 },
+ { "noreturn", T_AT_NORETURN, 0, 0, 0, 0,0,1,1,5 },
+ { "nothrow", T_AT_NOTHROW, 0, 0, 0, 0,0,1,1,5 },
+ { "optimize", T_AT_OPTIMIZE, 0, 0, 0, 0,0,1,1,5 },
+ { "packed", T_AT_PACKED, 0, 0, 0, 0,0,1,1,5 },
+ { "packed", T_PACKED, 0, 0, 0, 0,0,0,0,2 },
+ { "pcs", T_AT_PCS, 0, 0, 0, 0,0,0,0,5 },
+ { "printf", T_AT_FORMAT_PRINTF,0, 0, 0, 0,0,1,1,5 },
+ { "pure", T_AT_PURE, 0, 0, 0, 0,0,1,1,5 },
+ { "real", T_REAL, 0, 0, 0, 0,1,0,0,4 },
+ { "register", T_SCLASS, REG, 0, 0, 0,0,0,0,1 },
+ { "restrict", T_QUAL, 0, 0, RESTRICT, 0,1,0,0,5 },
+ { "return", T_RETURN, 0, 0, 0, 0,0,0,0,1 },
+ { "returns_twice", T_AT_RETURNS_TWICE,0,0, 0, 0,0,1,1,5 },
+ { "scanf", T_AT_FORMAT_SCANF,0, 0, 0, 0,0,1,1,5 },
+ { "section", T_AT_SECTION, 0, 0, 0, 0,0,1,1,7 },
+ { "sentinel", T_AT_SENTINEL, 0, 0, 0, 0,0,1,1,5 },
+ { "short", T_TYPE, 0, SHORT, 0, 0,0,0,0,1 },
+ { "signed", T_TYPE, 0, SIGNED, 0, 1,0,0,0,3 },
+ { "sizeof", T_SIZEOF, 0, 0, 0, 0,0,0,0,1 },
+ { "static", T_SCLASS, STATIC, 0, 0, 0,0,0,0,1 },
+ { "strfmon", T_AT_FORMAT_STRFMON,0, 0, 0, 0,0,1,1,5 },
+ { "strftime", T_AT_FORMAT_STRFTIME,0, 0, 0, 0,0,1,1,5 },
+ { "string", T_AT_STRING, 0, 0, 0, 0,0,1,1,5 },
+ { "struct", T_STRUCT_OR_UNION, 0, STRUCT, 0, 0,0,0,0,1 },
+ { "switch", T_SWITCH, 0, 0, 0, 0,0,0,0,1 },
+ { "symbolrename", T_SYMBOLRENAME,0, 0, 0, 0,0,0,0,2 },
+ { "syslog", T_AT_FORMAT_SYSLOG,0, 0, 0, 0,0,1,1,5 },
+ { "transparent_union",T_AT_TUNION,0, 0, 0, 0,0,1,1,5 },
+ { "tls_model", T_AT_TLS_MODEL, 0, 0, 0, 0,0,1,1,5 },
+ { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0,0,0,0,1 },
+ { "typeof", T_TYPEOF, 0, 0, 0, 0,0,1,0,7 },
+ { "union", T_STRUCT_OR_UNION, 0, UNION, 0, 0,0,0,0,1 },
+ { "unsigned", T_TYPE, 0, UNSIGN, 0, 0,0,0,0,1 },
+ { "unused", T_AT_UNUSED, 0, 0, 0, 0,0,1,1,5 },
+ { "used", T_AT_USED, 0, 0, 0, 0,0,1,1,5 },
+ { "visibility", T_AT_VISIBILITY,0, 0, 0, 0,0,1,1,5 },
+ { "void", T_TYPE, 0, VOID, 0, 0,0,0,0,1 },
+ { "volatile", T_QUAL, 0, 0, VOLATILE, 1,0,0,0,7 },
+ { "warn_unused_result", T_AT_WARN_UNUSED_RESULT, 0, 0, 0, 0,0,1,1,5 },
+ { "weak", T_AT_WEAK, 0, 0, 0, 0,0,1,1,5 },
+ { "while", T_WHILE, 0, 0, 0, 0,0,0,0,1 },
+ { NULL, 0, 0, 0, 0, 0,0,0,0,0 }
+};
+
+/* Symbol table */
+static sym_t *symtab[HSHSIZ1];
+
+/* bit i of the entry with index i is set */
+uint64_t qbmasks[64];
+
+/* least significant i bits are set in the entry with index i */
+uint64_t qlmasks[64 + 1];
+
+/* least significant i bits are not set in the entry with index i */
+uint64_t qumasks[64 + 1];
+
+/* free list for sbuf structures */
+static sbuf_t *sbfrlst;
+
+/* type of next expected symbol */
+symt_t symtyp;
+
+
+static void
+add_keyword(struct kwtab *kw, int deco)
+{
+ sym_t *sym;
+ size_t h;
+ char buf[256];
+ const char *name;
+
+ if (!(kw->kw_deco & deco))
+ return;
+
+ switch (deco) {
+ case 1:
+ name = kw->kw_name;
+ break;
+ case 2:
+ snprintf(buf, sizeof(buf), "__%s", kw->kw_name);
+ name = strdup(buf);
+ break;
+ default:
+ lint_assert(deco == 4);
+ snprintf(buf, sizeof(buf), "__%s__", kw->kw_name);
+ name = strdup(buf);
+ break;
+ }
+
+ if (name == NULL)
+ err(1, "Can't init symbol table");
+
+ sym = getblk(sizeof (sym_t));
+ sym->s_name = name;
+ sym->s_keyword = kw;
+ sym->s_value.v_quad = kw->kw_token;
+ if (kw->kw_token == T_TYPE || kw->kw_token == T_STRUCT_OR_UNION) {
+ sym->s_tspec = kw->kw_tspec;
+ } else if (kw->kw_token == T_SCLASS) {
+ sym->s_scl = kw->kw_scl;
+ } else if (kw->kw_token == T_QUAL) {
+ sym->s_tqual = kw->kw_tqual;
+ }
+ h = hash(sym->s_name);
+ if ((sym->s_link = symtab[h]) != NULL)
+ symtab[h]->s_rlink = &sym->s_link;
+ sym->s_rlink = &symtab[h];
+ symtab[h] = sym;
+}
+
+/*
+ * All keywords are written to the symbol table. This saves us looking
+ * in a extra table for each name we found.
+ */
+void
+initscan(void)
+{
+ struct kwtab *kw;
+ size_t i;
+ uint64_t uq;
+
+ for (kw = kwtab; kw->kw_name != NULL; kw++) {
+ if ((kw->kw_c89 || kw->kw_c99) && tflag)
+ continue;
+ if (kw->kw_c99 && !(Sflag || gflag))
+ continue;
+ if (kw->kw_gcc && !gflag)
+ continue;
+ add_keyword(kw, 1);
+ add_keyword(kw, 2);
+ add_keyword(kw, 4);
+ }
+
+ /* initialize bit-masks for quads */
+ for (i = 0; i < 64; i++) {
+ qbmasks[i] = (uint64_t)1 << i;
+ uq = ~(uint64_t)0 << i;
+ qumasks[i] = uq;
+ qlmasks[i] = ~uq;
+ }
+ qumasks[i] = 0;
+ qlmasks[i] = ~(uint64_t)0;
+}
+
+/*
+ * Get a free sbuf structure, if possible from the free list
+ */
+static sbuf_t *
+allocsb(void)
+{
+ sbuf_t *sb;
+
+ if ((sb = sbfrlst) != NULL) {
+ sbfrlst = sb->sb_next;
+#ifdef BLKDEBUG
+ (void)memset(sb, 0, sizeof (*sb));
+#else
+ sb->sb_next = NULL;
+#endif
+ } else {
+ sb = xmalloc(sizeof (sbuf_t));
+ (void)memset(sb, 0, sizeof (*sb));
+ }
+ return sb;
+}
+
+/*
+ * Put a sbuf structure to the free list
+ */
+static void
+freesb(sbuf_t *sb)
+{
+
+ (void)memset(sb, ZERO, sizeof (*sb));
+ sb->sb_next = sbfrlst;
+ sbfrlst = sb;
+}
+
+/*
+ * Read a character and ensure that it is positive (except EOF).
+ * Increment line count(s) if necessary.
+ */
+static int
+inpc(void)
+{
+ int c;
+
+ if ((c = lex_input()) != EOF && (c &= CHAR_MASK) == '\n')
+ lex_incline();
+ return c;
+}
+
+static int
+hash(const char *s)
+{
+ u_int v;
+ const u_char *us;
+
+ v = 0;
+ for (us = (const u_char *)s; *us != '\0'; us++) {
+ v = (v << sizeof (v)) + *us;
+ v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
+ }
+ return v % HSHSIZ1;
+}
+
+/*
+ * Lex has found a letter followed by zero or more letters or digits.
+ * It looks for a symbol in the symbol table with the same name. This
+ * symbol must either be a keyword or a symbol of the type required by
+ * symtyp (label, member, tag, ...).
+ *
+ * If it is a keyword, the token is returned. In some cases it is described
+ * more deeply by data written to yylval.
+ *
+ * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
+ * is stored in yylval. This struct contains the name of the symbol, its
+ * length and hash value. If there is already a symbol of the same name
+ * and type in the symbol table, the sbuf struct also contains a pointer
+ * to the symbol table entry.
+ */
+extern int
+lex_name(const char *yytext, size_t yyleng)
+{
+ char *s;
+ sbuf_t *sb;
+ sym_t *sym;
+ int tok;
+
+ sb = allocsb();
+ sb->sb_name = yytext;
+ sb->sb_len = yyleng;
+ sb->sb_hash = hash(yytext);
+ if ((sym = search(sb)) != NULL && sym->s_keyword) {
+ freesb(sb);
+ return keyw(sym);
+ }
+
+ sb->sb_sym = sym;
+
+ if (sym != NULL) {
+ lint_assert(blklev >= sym->s_blklev);
+ sb->sb_name = sym->s_name;
+ sb->sb_len = strlen(sym->s_name);
+ tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
+ } else {
+ s = getblk(yyleng + 1);
+ (void)memcpy(s, yytext, yyleng + 1);
+ sb->sb_name = s;
+ sb->sb_len = yyleng;
+ tok = T_NAME;
+ }
+
+ yylval.y_sb = sb;
+ return tok;
+}
+
+static sym_t *
+search(sbuf_t *sb)
+{
+ sym_t *sym;
+
+ for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
+ if (strcmp(sym->s_name, sb->sb_name) == 0) {
+ if (sym->s_keyword) {
+ struct kwtab *kw = sym->s_keyword;
+ if (!kw->kw_attr || attron)
+ return sym;
+ } else if (!attron && sym->s_kind == symtyp)
+ return sym;
+ }
+ }
+
+ return NULL;
+}
+
+static int
+keyw(sym_t *sym)
+{
+ int t;
+
+ if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
+ yylval.y_scl = sym->s_scl;
+ } else if (t == T_TYPE || t == T_STRUCT_OR_UNION) {
+ yylval.y_tspec = sym->s_tspec;
+ } else if (t == T_QUAL) {
+ yylval.y_tqual = sym->s_tqual;
+ }
+ return t;
+}
+
+/*
+ * Convert a string representing an integer into internal representation.
+ * The value is returned in yylval. icon() (and yylex()) returns T_CON.
+ */
+int
+lex_icon(const char *yytext, size_t yyleng, int base)
+{
+ int l_suffix, u_suffix;
+ int len;
+ const char *cp;
+ char c, *eptr;
+ tspec_t typ;
+ bool ansiu;
+#ifdef TARG_INT128_MAX
+ __uint128_t uq = 0;
+ static tspec_t contypes[2][4] = {
+ { INT, LONG, QUAD, INT128, },
+ { UINT, ULONG, UQUAD, UINT128, }
+ };
+#else
+ uint64_t uq = 0;
+ static tspec_t contypes[2][3] = {
+ { INT, LONG, QUAD, },
+ { UINT, ULONG, UQUAD, }
+ };
+#endif
+
+ cp = yytext;
+ len = yyleng;
+
+ /* skip 0[xX] or 0[bB] */
+ if (base == 16 || base == 2) {
+ cp += 2;
+ len -= 2;
+ }
+
+ /* read suffixes */
+ l_suffix = u_suffix = 0;
+ for (;;) {
+ if ((c = cp[len - 1]) == 'l' || c == 'L') {
+ l_suffix++;
+ } else if (c == 'u' || c == 'U') {
+ u_suffix++;
+ } else {
+ break;
+ }
+ len--;
+ }
+ if (l_suffix > 2 || u_suffix > 1) {
+ /* malformed integer constant */
+ warning(251);
+ if (l_suffix > 2)
+ l_suffix = 2;
+ if (u_suffix > 1)
+ u_suffix = 1;
+ }
+ if (tflag && u_suffix != 0) {
+ /* suffix U is illegal in traditional C */
+ warning(97);
+ }
+ typ = contypes[u_suffix][l_suffix];
+
+ errno = 0;
+
+ uq = strtouq(cp, &eptr, base);
+ lint_assert(eptr == cp + len);
+ if (errno != 0)
+ /* integer constant out of range */
+ warning(252);
+
+ /*
+ * If the value is too big for the current type, we must choose
+ * another type.
+ */
+ ansiu = false;
+ switch (typ) {
+ case INT:
+ if (uq <= TARG_INT_MAX) {
+ /* ok */
+ } else if (uq <= TARG_UINT_MAX && base != 10) {
+ typ = UINT;
+ } else if (uq <= TARG_LONG_MAX) {
+ typ = LONG;
+ } else {
+ typ = ULONG;
+ if (uq > TARG_ULONG_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+ }
+ if (typ == UINT || typ == ULONG) {
+ if (tflag) {
+ typ = LONG;
+ } else if (!sflag) {
+ /*
+ * Remember that the constant is unsigned
+ * only in ANSI C
+ */
+ ansiu = true;
+ }
+ }
+ break;
+ case UINT:
+ if (uq > TARG_UINT_MAX) {
+ typ = ULONG;
+ if (uq > TARG_ULONG_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+ }
+ break;
+ case LONG:
+ if (uq > TARG_LONG_MAX && !tflag) {
+ typ = ULONG;
+ if (!sflag)
+ ansiu = true;
+ if (uq > TARG_ULONG_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+ }
+ break;
+ case ULONG:
+ if (uq > TARG_ULONG_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+ break;
+ case QUAD:
+ if (uq > TARG_QUAD_MAX && !tflag) {
+ typ = UQUAD;
+ if (!sflag)
+ ansiu = true;
+ }
+ break;
+ case UQUAD:
+ if (uq > TARG_UQUAD_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+ break;
+#ifdef INT128_SIZE
+ case INT128:
+#ifdef TARG_INT128_MAX
+ if (uq > TARG_INT128_MAX && !tflag) {
+ typ = UINT128;
+ if (!sflag)
+ ansiu = true;
+ }
+#endif
+ break;
+ case UINT128:
+#ifdef TARG_INT128_MAX
+ if (uq > TARG_UINT128_MAX) {
+ /* integer constant out of range */
+ warning(252);
+ }
+#endif
+ break;
+#endif
+ /* LINTED206: (enumeration values not handled in switch) */
+ case STRUCT:
+ case VOID:
+ case LDOUBLE:
+ case FUNC:
+ case ARRAY:
+ case PTR:
+ case ENUM:
+ case UNION:
+ case SIGNED:
+ case NOTSPEC:
+ case DOUBLE:
+ case FLOAT:
+ case USHORT:
+ case SHORT:
+ case UCHAR:
+ case SCHAR:
+ case CHAR:
+ case BOOL:
+ case UNSIGN:
+ case FCOMPLEX:
+ case DCOMPLEX:
+ case LCOMPLEX:
+ case COMPLEX:
+ break;
+ }
+
+ uq = (uint64_t)xsign((int64_t)uq, typ, -1);
+
+ (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
+ yylval.y_val->v_ansiu = ansiu;
+ yylval.y_val->v_quad = (int64_t)uq;
+
+ return T_CON;
+}
+
+/*
+ * Returns 1 if t is a signed type and the value is negative.
+ *
+ * len is the number of significant bits. If len is -1, len is set
+ * to the width of type t.
+ */
+int
+sign(int64_t q, tspec_t t, int len)
+{
+
+ if (t == PTR || is_uinteger(t))
+ return 0;
+ return msb(q, t, len);
+}
+
+int
+msb(int64_t q, tspec_t t, int len)
+{
+
+ if (len <= 0)
+ len = size(t);
+ return (q & qbmasks[len - 1]) != 0;
+}
+
+/*
+ * Extends the sign of q.
+ */
+int64_t
+xsign(int64_t q, tspec_t t, int len)
+{
+
+ if (len <= 0)
+ len = size(t);
+
+ if (t == PTR || is_uinteger(t) || !sign(q, t, len)) {
+ q &= qlmasks[len];
+ } else {
+ q |= qumasks[len];
+ }
+ return q;
+}
+
+/*
+ * Convert a string representing a floating point value into its integral
+ * representation. Type and value are returned in yylval. fcon()
+ * (and yylex()) returns T_CON.
+ * XXX Currently it is not possible to convert constants of type
+ * long double which are greater than DBL_MAX.
+ */
+int
+lex_fcon(const char *yytext, size_t yyleng)
+{
+ const char *cp;
+ int len;
+ tspec_t typ;
+ char c, *eptr;
+ double d;
+ float f = 0;
+
+ cp = yytext;
+ len = yyleng;
+
+ if (cp[len - 1] == 'i') {
+ /* imaginary, do nothing for now */
+ len--;
+ }
+ if ((c = cp[len - 1]) == 'f' || c == 'F') {
+ typ = FLOAT;
+ len--;
+ } else if (c == 'l' || c == 'L') {
+ typ = LDOUBLE;
+ len--;
+ } else {
+ if (c == 'd' || c == 'D')
+ len--;
+ typ = DOUBLE;
+ }
+
+ if (tflag && typ != DOUBLE) {
+ /* suffixes F and L are illegal in traditional C */
+ warning(98);
+ }
+
+ errno = 0;
+ d = strtod(cp, &eptr);
+ if (eptr != cp + len) {
+ switch (*eptr) {
+ /*
+ * XXX: non-native non-current strtod() may not handle hex
+ * floats, ignore the rest if we find traces of hex float
+ * syntax...
+ */
+ case 'p':
+ case 'P':
+ case 'x':
+ case 'X':
+ d = 0;
+ errno = 0;
+ break;
+ default:
+ LERROR("fcon(%s->%s)", cp, eptr);
+ }
+ }
+ if (errno != 0)
+ /* floating-point constant out of range */
+ warning(248);
+
+ if (typ == FLOAT) {
+ f = (float)d;
+ if (!finite(f)) {
+ /* floating-point constant out of range */
+ warning(248);
+ f = f > 0 ? FLT_MAX : -FLT_MAX;
+ }
+ }
+
+ (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
+ if (typ == FLOAT) {
+ yylval.y_val->v_ldbl = f;
+ } else {
+ yylval.y_val->v_ldbl = d;
+ }
+
+ return T_CON;
+}
+
+int
+lex_operator(int t, op_t o)
+{
+
+ yylval.y_op = o;
+ return t;
+}
+
+/*
+ * Called if lex found a leading \'.
+ */
+int
+lex_ccon(void)
+{
+ size_t n;
+ int val, c;
+ char cv;
+
+ n = 0;
+ val = 0;
+ while ((c = getescc('\'')) >= 0) {
+ val = (val << CHAR_SIZE) + c;
+ n++;
+ }
+ if (c == -2) {
+ /* unterminated character constant */
+ error(253);
+ } else {
+ if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
+ /* too many characters in character constant */
+ error(71);
+ } else if (n > 1) {
+ /* multi-character character constant */
+ warning(294);
+ } else if (n == 0) {
+ /* empty character constant */
+ error(73);
+ }
+ }
+ if (n == 1) {
+ cv = (char)val;
+ val = cv;
+ }
+
+ yylval.y_val = xcalloc(1, sizeof (val_t));
+ yylval.y_val->v_tspec = INT;
+ yylval.y_val->v_quad = val;
+
+ return T_CON;
+}
+
+/*
+ * Called if lex found a leading L\'
+ */
+int
+lex_wccon(void)
+{
+ static char buf[MB_LEN_MAX + 1];
+ size_t i;
+ int c;
+ wchar_t wc;
+
+ i = 0;
+ while ((c = getescc('\'')) >= 0) {
+ if (i < MB_CUR_MAX)
+ buf[i] = (char)c;
+ i++;
+ }
+
+ wc = 0;
+
+ if (c == -2) {
+ /* unterminated character constant */
+ error(253);
+ } else if (c == 0) {
+ /* empty character constant */
+ error(73);
+ } else {
+ if (i > MB_CUR_MAX) {
+ i = MB_CUR_MAX;
+ /* too many characters in character constant */
+ error(71);
+ } else {
+ buf[i] = '\0';
+ (void)mbtowc(NULL, NULL, 0);
+ if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
+ /* invalid multibyte character */
+ error(291);
+ }
+ }
+
+ yylval.y_val = xcalloc(1, sizeof (val_t));
+ yylval.y_val->v_tspec = WCHAR;
+ yylval.y_val->v_quad = wc;
+
+ return T_CON;
+}
+
+/*
+ * Read a character which is part of a character constant or of a string
+ * and handle escapes.
+ *
+ * The Argument is the character which delimits the character constant or
+ * string.
+ *
+ * Returns -1 if the end of the character constant or string is reached,
+ * -2 if the EOF is reached, and the character otherwise.
+ */
+static int
+getescc(int d)
+{
+ static int pbc = -1;
+ int n, c, v;
+
+ if (pbc == -1) {
+ c = inpc();
+ } else {
+ c = pbc;
+ pbc = -1;
+ }
+ if (c == d)
+ return -1;
+ switch (c) {
+ case '\n':
+ if (tflag) {
+ /* newline in string or char constant */
+ error(254);
+ return -2;
+ }
+ return c;
+ case EOF:
+ return -2;
+ case '\\':
+ switch (c = inpc()) {
+ case '"':
+ if (tflag && d == '\'')
+ /* \" inside character constants undef... */
+ warning(262);
+ return '"';
+ case '\'':
+ return '\'';
+ case '?':
+ if (tflag)
+ /* \? undefined in traditional C */
+ warning(263);
+ return '?';
+ case '\\':
+ return '\\';
+ case 'a':
+ if (tflag)
+ /* \a undefined in traditional C */
+ warning(81);
+ return '\a';
+ case 'b':
+ return '\b';
+ case 'f':
+ return '\f';
+ case 'n':
+ return '\n';
+ case 'r':
+ return '\r';
+ case 't':
+ return '\t';
+ case 'v':
+ if (tflag)
+ /* \v undefined in traditional C */
+ warning(264);
+ return '\v';
+ case '8': case '9':
+ /* bad octal digit %c */
+ warning(77, c);
+ /* FALLTHROUGH */
+ case '0': case '1': case '2': case '3':
+ case '4': case '5': case '6': case '7':
+ n = 3;
+ v = 0;
+ do {
+ v = (v << 3) + (c - '0');
+ c = inpc();
+ } while (--n && isdigit(c) && (tflag || c <= '7'));
+ if (tflag && n > 0 && isdigit(c))
+ /* bad octal digit %c */
+ warning(77, c);
+ pbc = c;
+ if (v > TARG_UCHAR_MAX) {
+ /* character escape does not fit in character */
+ warning(76);
+ v &= CHAR_MASK;
+ }
+ return v;
+ case 'x':
+ if (tflag)
+ /* \x undefined in traditional C */
+ warning(82);
+ v = 0;
+ n = 0;
+ while ((c = inpc()) >= 0 && isxdigit(c)) {
+ c = isdigit(c) ?
+ c - '0' : toupper(c) - 'A' + 10;
+ v = (v << 4) + c;
+ if (n >= 0) {
+ if ((v & ~CHAR_MASK) != 0) {
+ /* overflow in hex escape */
+ warning(75);
+ n = -1;
+ } else {
+ n++;
+ }
+ }
+ }
+ pbc = c;
+ if (n == 0) {
+ /* no hex digits follow \x */
+ error(74);
+ } if (n == -1) {
+ v &= CHAR_MASK;
+ }
+ return v;
+ case '\n':
+ return getescc(d);
+ case EOF:
+ return -2;
+ default:
+ if (isprint(c)) {
+ /* dubious escape \%c */
+ warning(79, c);
+ } else {
+ /* dubious escape \%o */
+ warning(80, c);
+ }
+ }
+ }
+ return c;
+}
+
+/* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
+static void
+parse_line_directive_flags(const char *p)
+{
+
+ in_system_header = false;
+
+ while (ch_isspace(*p))
+ p++;
+ while (ch_isdigit(*p)) {
+ if (*p == '3' && !ch_isdigit(p[1]))
+ in_system_header = true;
+ p++;
+ while (ch_isspace(*p))
+ p++;
+ }
+
+#if 0
+ if (c != '\0')
+ warning("extra character(s) after directive");
+#endif
+}
+
+/*
+ * Called for preprocessor directives. Currently implemented are:
+ * # lineno
+ * # lineno "filename"
+ */
+void
+lex_directive(const char *yytext)
+{
+ const char *cp, *fn;
+ char c, *eptr;
+ size_t fnl;
+ long ln;
+ static bool first = true;
+
+ /* Go to first non-whitespace after # */
+ for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
+ continue;
+
+ if (!ch_isdigit(c)) {
+ if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
+ return;
+ error:
+ /* undefined or invalid # directive */
+ warning(255);
+ return;
+ }
+ ln = strtol(--cp, &eptr, 10);
+ if (cp == eptr)
+ goto error;
+ if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
+ goto error;
+ while ((c = *cp++) == ' ' || c == '\t')
+ continue;
+ if (c != '\0') {
+ if (c != '"')
+ goto error;
+ fn = cp;
+ while ((c = *cp) != '"' && c != '\0')
+ cp++;
+ if (c != '"')
+ goto error;
+ if ((fnl = cp++ - fn) > PATH_MAX)
+ goto error;
+ parse_line_directive_flags(cp);
+
+ /* empty string means stdin */
+ if (fnl == 0) {
+ fn = "{standard input}";
+ fnl = 16; /* strlen (fn) */
+ }
+ curr_pos.p_file = fnnalloc(fn, fnl);
+ /*
+ * If this is the first directive, the name is the name
+ * of the C source file as specified at the command line.
+ * It is written to the output file.
+ */
+ if (first) {
+ csrc_pos.p_file = curr_pos.p_file;
+ outsrc(fnxform(curr_pos.p_file,
+ strlen(curr_pos.p_file)));
+ first = false;
+ }
+ }
+ curr_pos.p_line = (int)ln - 1;
+ curr_pos.p_uniq = 0;
+ if (curr_pos.p_file == csrc_pos.p_file) {
+ csrc_pos.p_line = (int)ln - 1;
+ csrc_pos.p_uniq = 0;
+ }
+}
+
+/*
+ * Handle lint comments. Following comments are currently understood:
+ * ARGSUSEDn
+ * BITFIELDTYPE
+ * CONSTCOND CONSTANTCOND CONSTANTCONDITION
+ * FALLTHRU FALLTHROUGH
+ * LINTLIBRARY
+ * LINTEDn NOSTRICTn
+ * LONGLONG
+ * NOTREACHED
+ * PRINTFLIKEn
+ * PROTOLIB
+ * SCANFLIKEn
+ * VARARGSn
+ * If one of these comments is recognized, the argument, if any, is
+ * parsed and a function which handles this comment is called.
+ */
+void
+lex_comment(void)
+{
+ int c, lc;
+ static const struct {
+ const char *keywd;
+ int arg;
+ void (*func)(int);
+ } keywtab[] = {
+ { "ARGSUSED", 1, argsused },
+ { "BITFIELDTYPE", 0, bitfieldtype },
+ { "CONSTCOND", 0, constcond },
+ { "CONSTANTCOND", 0, constcond },
+ { "CONSTANTCONDITION", 0, constcond },
+ { "FALLTHRU", 0, fallthru },
+ { "FALLTHROUGH", 0, fallthru },
+ { "LINTLIBRARY", 0, lintlib },
+ { "LINTED", 1, linted },
+ { "LONGLONG", 0, longlong },
+ { "NOSTRICT", 1, linted },
+ { "NOTREACHED", 0, notreach },
+ { "PRINTFLIKE", 1, printflike },
+ { "PROTOLIB", 1, protolib },
+ { "SCANFLIKE", 1, scanflike },
+ { "VARARGS", 1, varargs },
+ };
+ char keywd[32];
+ char arg[32];
+ size_t l, i;
+ int a;
+ bool eoc;
+
+ eoc = false;
+
+ /* Skip whitespace after the start of the comment */
+ while ((c = inpc()) != EOF && isspace(c))
+ continue;
+
+ /* Read the potential keyword to keywd */
+ l = 0;
+ while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
+ keywd[l++] = (char)c;
+ c = inpc();
+ }
+ keywd[l] = '\0';
+
+ /* look for the keyword */
+ for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
+ if (strcmp(keywtab[i].keywd, keywd) == 0)
+ break;
+ }
+ if (i == sizeof (keywtab) / sizeof (keywtab[0]))
+ goto skip_rest;
+
+ /* skip whitespace after the keyword */
+ while (c != EOF && isspace(c))
+ c = inpc();
+
+ /* read the argument, if the keyword accepts one and there is one */
+ l = 0;
+ if (keywtab[i].arg) {
+ while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
+ arg[l++] = (char)c;
+ c = inpc();
+ }
+ }
+ arg[l] = '\0';
+ a = l != 0 ? atoi(arg) : -1;
+
+ /* skip whitespace after the argument */
+ while (c != EOF && isspace(c))
+ c = inpc();
+
+ if (c != '*' || (c = inpc()) != '/') {
+ if (keywtab[i].func != linted)
+ /* extra characters in lint comment */
+ warning(257);
+ } else {
+ /*
+ * remember that we have already found the end of the
+ * comment
+ */
+ eoc = true;
+ }
+
+ if (keywtab[i].func != NULL)
+ (*keywtab[i].func)(a);
+
+skip_rest:
+ while (!eoc) {
+ lc = c;
+ if ((c = inpc()) == EOF) {
+ /* unterminated comment */
+ error(256);
+ break;
+ }
+ if (lc == '*' && c == '/')
+ eoc = true;
+ }
+}
+
+/*
+ * Handle // style comments
+ */
+void
+lex_slashslashcomment(void)
+{
+ int c;
+
+ if (!Sflag && !gflag)
+ /* %s C does not support // comments */
+ gnuism(312, tflag ? "traditional" : "ANSI");
+
+ while ((c = inpc()) != EOF && c != '\n')
+ continue;
+}
+
+/*
+ * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
+ * clear_warn_flags() is called after function definitions and global and
+ * local declarations and definitions. It is also called between
+ * the controlling expression and the body of control statements
+ * (if, switch, for, while).
+ */
+void
+clear_warn_flags(void)
+{
+
+ lwarn = LWARN_ALL;
+ quadflg = false;
+ constcond_flag = false;
+}
+
+/*
+ * Strings are stored in a dynamically allocated buffer and passed
+ * in yylval.y_xstrg to the parser. The parser or the routines called
+ * by the parser are responsible for freeing this buffer.
+ */
+int
+lex_string(void)
+{
+ u_char *s;
+ int c;
+ size_t len, max;
+ strg_t *strg;
+
+ s = xmalloc(max = 64);
+
+ len = 0;
+ while ((c = getescc('"')) >= 0) {
+ /* +1 to reserve space for a trailing NUL character */
+ if (len + 1 == max)
+ s = xrealloc(s, max *= 2);
+ s[len++] = (char)c;
+ }
+ s[len] = '\0';
+ if (c == -2)
+ /* unterminated string constant */
+ error(258);
+
+ strg = xcalloc(1, sizeof (strg_t));
+ strg->st_tspec = CHAR;
+ strg->st_len = len;
+ strg->st_cp = s;
+
+ yylval.y_string = strg;
+ return T_STRING;
+}
+
+int
+lex_wcstrg(void)
+{
+ char *s;
+ int c, n;
+ size_t i, wi;
+ size_t len, max, wlen;
+ wchar_t *ws;
+ strg_t *strg;
+
+ s = xmalloc(max = 64);
+ len = 0;
+ while ((c = getescc('"')) >= 0) {
+ /* +1 to save space for a trailing NUL character */
+ if (len + 1 >= max)
+ s = xrealloc(s, max *= 2);
+ s[len++] = (char)c;
+ }
+ s[len] = '\0';
+ if (c == -2)
+ /* unterminated string constant */
+ error(258);
+
+ /* get length of wide-character string */
+ (void)mblen(NULL, 0);
+ for (i = 0, wlen = 0; i < len; i += n, wlen++) {
+ if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
+ /* invalid multibyte character */
+ error(291);
+ break;
+ }
+ if (n == 0)
+ n = 1;
+ }
+
+ ws = xmalloc((wlen + 1) * sizeof (wchar_t));
+
+ /* convert from multibyte to wide char */
+ (void)mbtowc(NULL, NULL, 0);
+ for (i = 0, wi = 0; i < len; i += n, wi++) {
+ if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
+ break;
+ if (n == 0)
+ n = 1;
+ }
+ ws[wi] = 0;
+ free(s);
+
+ strg = xcalloc(1, sizeof (strg_t));
+ strg->st_tspec = WCHAR;
+ strg->st_len = wlen;
+ strg->st_wcp = ws;
+
+ yylval.y_string = strg;
+ return T_STRING;
+}
+
+/*
+ * As noted above the scanner does not create new symbol table entries
+ * for symbols it cannot find in the symbol table. This is to avoid
+ * putting undeclared symbols into the symbol table if a syntax error
+ * occurs.
+ *
+ * getsym() is called as soon as it is probably ok to put the symbol to
+ * the symbol table. This does not mean that it is not possible that
+ * symbols are put to the symbol table which are not completely
+ * declared due to syntax errors. To avoid too many problems in this
+ * case, symbols get type int in getsym().
+ *
+ * XXX calls to getsym() should be delayed until decl1*() is called
+ */
+sym_t *
+getsym(sbuf_t *sb)
+{
+ dinfo_t *di;
+ char *s;
+ sym_t *sym;
+
+ sym = sb->sb_sym;
+
+ /*
+ * During member declaration it is possible that name() looked
+ * for symbols of type FVFT, although it should have looked for
+ * symbols of type FTAG. Same can happen for labels. Both cases
+ * are compensated here.
+ */
+ if (symtyp == FMEMBER || symtyp == FLABEL) {
+ if (sym == NULL || sym->s_kind == FVFT)
+ sym = search(sb);
+ }
+
+ if (sym != NULL) {
+ if (sym->s_kind != symtyp)
+ LERROR("getsym(%d, %d)", sym->s_kind, symtyp);
+ symtyp = FVFT;
+ freesb(sb);
+ return sym;
+ }
+
+ /* create a new symbol table entry */
+
+ /* labels must always be allocated at level 1 (outermost block) */
+ if (symtyp == FLABEL) {
+ sym = getlblk(1, sizeof (sym_t));
+ s = getlblk(1, sb->sb_len + 1);
+ (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
+ sym->s_name = s;
+ sym->s_blklev = 1;
+ di = dcs;
+ while (di->d_next != NULL && di->d_next->d_next != NULL)
+ di = di->d_next;
+ lint_assert(di->d_ctx == AUTO);
+ } else {
+ sym = getblk(sizeof (sym_t));
+ sym->s_name = sb->sb_name;
+ sym->s_blklev = blklev;
+ di = dcs;
+ }
+
+ UNIQUE_CURR_POS(sym->s_def_pos);
+ if ((sym->s_kind = symtyp) != FLABEL)
+ sym->s_type = gettyp(INT);
+
+ symtyp = FVFT;
+
+ if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
+ symtab[sb->sb_hash]->s_rlink = &sym->s_link;
+ sym->s_rlink = &symtab[sb->sb_hash];
+ symtab[sb->sb_hash] = sym;
+
+ *di->d_ldlsym = sym;
+ di->d_ldlsym = &sym->s_dlnxt;
+
+ freesb(sb);
+ return sym;
+}
+
+/*
+ * Construct a temporary symbol. The symbol starts with a digit, so that
+ * it is illegal.
+ */
+sym_t *
+mktempsym(type_t *t)
+{
+ static int n = 0;
+ int h;
+ char *s = getlblk(blklev, 64);
+ sym_t *sym = getblk(sizeof (sym_t));
+
+ (void)snprintf(s, 64, "%.8d_tmp", n++);
+ h = hash(s);
+
+ sym->s_name = s;
+ sym->s_type = t;
+ sym->s_blklev = blklev;
+ sym->s_scl = AUTO;
+ sym->s_kind = FVFT;
+ sym->s_used = true;
+ sym->s_set = true;
+
+ if ((sym->s_link = symtab[h]) != NULL)
+ symtab[h]->s_rlink = &sym->s_link;
+ sym->s_rlink = &symtab[h];
+ symtab[h] = sym;
+
+ *dcs->d_ldlsym = sym;
+ dcs->d_ldlsym = &sym->s_dlnxt;
+
+ return sym;
+}
+
+/*
+ * Remove a symbol forever from the symbol table. s_blklev
+ * is set to -1 to avoid that the symbol will later be put
+ * back to the symbol table.
+ */
+void
+rmsym(sym_t *sym)
+{
+
+ if ((*sym->s_rlink = sym->s_link) != NULL)
+ sym->s_link->s_rlink = sym->s_rlink;
+ sym->s_blklev = -1;
+ sym->s_link = NULL;
+}
+
+/*
+ * Remove a list of symbols declared at one level from the symbol
+ * table.
+ */
+void
+rmsyms(sym_t *syms)
+{
+ sym_t *sym;
+
+ for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
+ if (sym->s_blklev != -1) {
+ if ((*sym->s_rlink = sym->s_link) != NULL)
+ sym->s_link->s_rlink = sym->s_rlink;
+ sym->s_link = NULL;
+ sym->s_rlink = NULL;
+ }
+ }
+}
+
+/*
+ * Put a symbol into the symbol table
+ */
+void
+inssym(int bl, sym_t *sym)
+{
+ int h;
+
+ h = hash(sym->s_name);
+ if ((sym->s_link = symtab[h]) != NULL)
+ symtab[h]->s_rlink = &sym->s_link;
+ sym->s_rlink = &symtab[h];
+ symtab[h] = sym;
+ sym->s_blklev = bl;
+ lint_assert(sym->s_link == NULL ||
+ sym->s_blklev >= sym->s_link->s_blklev);
+}
+
+/*
+ * Called at level 0 after syntax errors
+ * Removes all symbols which are not declared at level 0 from the
+ * symbol table. Also frees all memory which is not associated with
+ * level 0.
+ */
+void
+cleanup(void)
+{
+ sym_t *sym, *nsym;
+ int i;
+
+ for (i = 0; i < HSHSIZ1; i++) {
+ for (sym = symtab[i]; sym != NULL; sym = nsym) {
+ nsym = sym->s_link;
+ if (sym->s_blklev >= 1) {
+ if ((*sym->s_rlink = nsym) != NULL)
+ nsym->s_rlink = sym->s_rlink;
+ }
+ }
+ }
+
+ for (i = mblklev; i > 0; i--)
+ freelblk(i);
+}
+
+/*
+ * Create a new symbol with the name of an existing symbol.
+ */
+sym_t *
+pushdown(sym_t *sym)
+{
+ int h;
+ sym_t *nsym;
+
+ h = hash(sym->s_name);
+ nsym = getblk(sizeof (sym_t));
+ lint_assert(sym->s_blklev <= blklev);
+ nsym->s_name = sym->s_name;
+ UNIQUE_CURR_POS(nsym->s_def_pos);
+ nsym->s_kind = sym->s_kind;
+ nsym->s_blklev = blklev;
+
+ if ((nsym->s_link = symtab[h]) != NULL)
+ symtab[h]->s_rlink = &nsym->s_link;
+ nsym->s_rlink = &symtab[h];
+ symtab[h] = nsym;
+
+ *dcs->d_ldlsym = nsym;
+ dcs->d_ldlsym = &nsym->s_dlnxt;
+
+ return nsym;
+}
+
+/*
+ * Free any dynamically allocated memory referenced by
+ * the value stack or yylval.
+ * The type of information in yylval is described by tok.
+ */
+void
+freeyyv(void *sp, int tok)
+{
+ if (tok == T_NAME || tok == T_TYPENAME) {
+ sbuf_t *sb = *(sbuf_t **)sp;
+ freesb(sb);
+ } else if (tok == T_CON) {
+ val_t *val = *(val_t **)sp;
+ free(val);
+ } else if (tok == T_STRING) {
+ strg_t *strg = *(strg_t **)sp;
+ if (strg->st_tspec == CHAR) {
+ free(strg->st_cp);
+ } else {
+ lint_assert(strg->st_tspec == WCHAR);
+ free(strg->st_wcp);
+ }
+ free(strg);
+ }
+}