Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: group symbol table functions



details:   https://anonhg.NetBSD.org/src/rev/4069c9082751
branches:  trunk
changeset: 362466:4069c9082751
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 27 22:26:12 2022 +0000

description:
lint: group symbol table functions

No functional change.

diffstat:

 usr.bin/xlint/lint1/lex.c |  110 +++++++++++++++++++++++-----------------------
 1 files changed, 55 insertions(+), 55 deletions(-)

diffs (192 lines):

diff -r e8a8b04156cf -r 4069c9082751 usr.bin/xlint/lint1/lex.c
--- a/usr.bin/xlint/lint1/lex.c Sun Feb 27 21:23:39 2022 +0000
+++ b/usr.bin/xlint/lint1/lex.c Sun Feb 27 22:26:12 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.104 2022/02/27 22:26:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.104 2022/02/27 22:26:12 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -68,8 +68,6 @@
 bool in_system_header;
 
 static int     inpc(void);
-static unsigned int hash(const char *);
-static sym_t * search(sbuf_t *);
 static int     keyw(sym_t *);
 static int     get_escaped_char(int);
 
@@ -260,10 +258,24 @@
 symt_t symtyp;
 
 
+static unsigned int
+hash(const char *s)
+{
+       unsigned int v;
+       const char *p;
+
+       v = 0;
+       for (p = s; *p != '\0'; p++) {
+               v = (v << 4) + (unsigned char)*p;
+               v ^= v >> 28;
+       }
+       return v % HSHSIZ1;
+}
+
 static void
 symtab_add(sym_t *sym)
 {
-       size_t h;
+       unsigned int h;
 
        h = hash(sym->s_name);
        if ((sym->s_symtab_next = symtab[h]) != NULL)
@@ -272,6 +284,27 @@
        symtab[h] = sym;
 }
 
+static sym_t *
+symtab_search(sbuf_t *sb)
+{
+
+       unsigned int h = hash(sb->sb_name);
+       for (sym_t *sym = symtab[h]; sym != NULL; sym = sym->s_symtab_next) {
+               if (strcmp(sym->s_name, sb->sb_name) != 0)
+                       continue;
+
+               const struct keyword *kw = sym->s_keyword;
+               if (kw != NULL && !kw->kw_attr)
+                       return sym;
+               if (kw != NULL && in_gcc_attribute)
+                       return sym;
+               if (kw == NULL && !in_gcc_attribute && sym->s_kind == symtyp)
+                       return sym;
+       }
+
+       return NULL;
+}
+
 static void
 symtab_remove(sym_t *sym)
 {
@@ -281,6 +314,19 @@
        sym->s_symtab_next = NULL;
 }
 
+static void
+symtab_remove_locals(void)
+{
+
+       for (size_t i = 0; i < HSHSIZ1; i++) {
+               for (sym_t *sym = symtab[i]; sym != NULL; ) {
+                       sym_t *next = sym->s_symtab_next;
+                       if (sym->s_block_level >= 1)
+                               symtab_remove(sym);
+                       sym = next;
+               }
+       }
+}
 
 static void
 add_keyword(const struct keyword *kw, bool leading, bool trailing)
@@ -356,20 +402,6 @@
        return c;
 }
 
-static unsigned int
-hash(const char *s)
-{
-       unsigned int v;
-       const char *p;
-
-       v = 0;
-       for (p = s; *p != '\0'; p++) {
-               v = (v << 4) + (unsigned char)*p;
-               v ^= v >> 28;
-       }
-       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
@@ -394,7 +426,7 @@
        sb = xmalloc(sizeof(*sb));
        sb->sb_name = yytext;
        sb->sb_len = yyleng;
-       if ((sym = search(sb)) != NULL && sym->s_keyword != NULL) {
+       if ((sym = symtab_search(sb)) != NULL && sym->s_keyword != NULL) {
                free(sb);
                return keyw(sym);
        }
@@ -418,30 +450,6 @@
        return tok;
 }
 
-static sym_t *
-search(sbuf_t *sb)
-{
-       unsigned int h;
-       sym_t *sym;
-       const struct keyword *kw;
-
-       h = hash(sb->sb_name);
-       for (sym = symtab[h]; sym != NULL; sym = sym->s_symtab_next) {
-               if (strcmp(sym->s_name, sb->sb_name) != 0)
-                       continue;
-               kw = sym->s_keyword;
-
-               if (kw != NULL && !kw->kw_attr)
-                       return sym;
-               if (kw != NULL && in_gcc_attribute)
-                       return sym;
-               if (kw == NULL && !in_gcc_attribute && sym->s_kind == symtyp)
-                       return sym;
-       }
-
-       return NULL;
-}
-
 static int
 keyw(sym_t *sym)
 {
@@ -1338,7 +1346,7 @@
         */
        if (symtyp == FMEMBER || symtyp == FLABEL) {
                if (sym == NULL || sym->s_kind == FVFT)
-                       sym = search(sb);
+                       sym = symtab_search(sb);
        }
 
        if (sym != NULL) {
@@ -1475,18 +1483,10 @@
 void
 cleanup(void)
 {
-       sym_t   *sym, *nsym;
-       size_t  i;
 
-       for (i = 0; i < HSHSIZ1; i++) {
-               for (sym = symtab[i]; sym != NULL; sym = nsym) {
-                       nsym = sym->s_symtab_next;
-                       if (sym->s_block_level >= 1)
-                               symtab_remove(sym);
-               }
-       }
+       symtab_remove_locals();
 
-       for (i = mem_block_level; i > 0; i--)
+       for (size_t i = mem_block_level; i > 0; i--)
                level_free_all(i);
 }
 



Home | Main Index | Thread Index | Old Index