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: add debug logging for symbols and ...



details:   https://anonhg.NetBSD.org/src/rev/9eb3ec67f876
branches:  trunk
changeset: 362476:9eb3ec67f876
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 01 00:17:12 2022 +0000

description:
lint: add debug logging for symbols and the symbol table

This logging is not active by default, the functions debug_sym and
debug_symtab can be called as needed during a debug session.

diffstat:

 usr.bin/xlint/lint1/cgram.y    |    9 +-
 usr.bin/xlint/lint1/debug.c    |  145 +++++++++++++++++++++++++++++++++++++++-
 usr.bin/xlint/lint1/decl.c     |   22 +-----
 usr.bin/xlint/lint1/externs1.h |    8 +-
 usr.bin/xlint/lint1/lex.c      |   83 ++++++++++++++++++----
 5 files changed, 220 insertions(+), 47 deletions(-)

diffs (truncated from 406 to 300 lines):

diff -r 6e92cb9c6e85 -r 9eb3ec67f876 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Mon Feb 28 22:41:07 2022 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Tue Mar 01 00:17:12 2022 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.386 2022/02/27 19:32:51 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.387 2022/03/01 00:17:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.386 2022/02/27 19:32:51 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.387 2022/03/01 00:17:12 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -2162,9 +2162,6 @@
 static const char *
 cgram_to_string(int token, YYSTYPE val)
 {
-       static const char *tqual_name[] = {
-               "const", "volatile", "restrict", "_Thread_local"
-       };
 
        switch (token) {
        case T_INCDEC:
@@ -2181,7 +2178,7 @@
        case T_STRUCT_OR_UNION:
                return tspec_name(val.y_tspec);
        case T_QUAL:
-               return tqual_name[val.y_tqual];
+               return tqual_name(val.y_tqual);
        case T_NAME:
                return val.y_name->sb_name;
        default:
diff -r 6e92cb9c6e85 -r 9eb3ec67f876 usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c       Mon Feb 28 22:41:07 2022 +0000
+++ b/usr.bin/xlint/lint1/debug.c       Tue Mar 01 00:17:12 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.8 2022/02/27 18:29:14 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.9 2022/03/01 00:17:12 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,12 +35,13 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: debug.c,v 1.8 2022/02/27 18:29:14 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.9 2022/03/01 00:17:12 rillig Exp $");
 #endif
 
 #include <stdlib.h>
 
 #include "lint1.h"
+#include "cgram.h"
 
 
 #ifdef DEBUG
@@ -129,9 +130,11 @@
        else if (op == CON && is_floating(tn->tn_type->t_tspec))
                debug_printf(", value %Lg", tn->tn_val->v_ldbl);
        else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
-               debug_printf(", value %llu\n", (unsigned long long)tn->tn_val->v_quad);
+               debug_printf(", value %llu\n",
+                   (unsigned long long)tn->tn_val->v_quad);
        else if (op == CON && is_integer(tn->tn_type->t_tspec))
-               debug_printf(", value %lld\n", (long long)tn->tn_val->v_quad);
+               debug_printf(", value %lld\n",
+                   (long long)tn->tn_val->v_quad);
        else if (op == CON && tn->tn_type->t_tspec == BOOL)
                debug_printf(", value %s\n",
                    tn->tn_val->v_quad != 0 ? "true" : "false");
@@ -160,4 +163,138 @@
        }
 }
 
+static const char *
+def_name(def_t def)
+{
+       static const char *const name[] = {
+               "not-declared",
+               "declared",
+               "tentative-defined",
+               "defined",
+       };
+
+       return name[def];
+}
+
+const char *
+scl_name(scl_t scl)
+{
+       static const char *const name[] = {
+               "none",
+               "extern",
+               "static",
+               "auto",
+               "register",
+               "typedef",
+               "struct",
+               "union",
+               "enum",
+               "member-of-struct",
+               "member-of-union",
+               "compile-time-constant",
+               "abstract",
+               "old-style-function-argument",
+               "prototype-argument",
+               "inline",
+       };
+
+       return name[scl];
+}
+
+const char *
+symt_name(symt_t kind)
+{
+       static const char *const name[] = {
+               "var-func-type",
+               "member",
+               "tag",
+               "label",
+       };
+
+       return name[kind];
+}
+
+const char *
+tqual_name(tqual_t qual)
+{
+       static const char *const name[] = {
+               "const",
+               "volatile",
+               "restrict",
+               "_Thread_local",
+       };
+
+       return name[qual];
+}
+
+static void
+debug_word(bool flag, const char *name)
+{
+
+       if (flag)
+               debug_printf(" %s", name);
+}
+
+void
+debug_sym(const sym_t *sym)
+{
+
+       debug_print_indent();
+       debug_printf("%s", sym->s_name);
+       if (sym->s_type != NULL)
+               debug_printf(" type='%s'", type_name(sym->s_type));
+       if (sym->s_rename != NULL)
+               debug_printf(" rename=%s", sym->s_rename);
+       debug_printf(" %s", symt_name(sym->s_kind));
+       debug_word(sym->s_keyword != NULL, "keyword");
+       debug_word(sym->s_bitfield, "bit-field");
+       debug_word(sym->s_set, "set");
+       debug_word(sym->s_used, "used");
+       debug_word(sym->s_arg, "argument");
+       debug_word(sym->s_register, "register");
+       debug_word(sym->s_defarg, "old-style-undefined");
+       debug_word(sym->s_return_type_implicit_int, "return-int");
+       debug_word(sym->s_osdef, "old-style");
+       debug_word(sym->s_inline, "inline");
+       debug_word(sym->s_ext_sym != NULL, "has-external");
+       debug_word(sym->s_scl != NOSCL, scl_name(sym->s_scl));
+       debug_word(sym->s_keyword == NULL, def_name(sym->s_def));
+
+       if (sym->s_def_pos.p_file != NULL)
+               debug_printf(" defined-at=%s:%d",
+                   sym->s_def_pos.p_file, sym->s_def_pos.p_line);
+       if (sym->s_set_pos.p_file != NULL)
+               debug_printf(" set-at=%s:%d",
+                   sym->s_set_pos.p_file, sym->s_set_pos.p_line);
+       if (sym->s_use_pos.p_file != NULL)
+               debug_printf(" used-at=%s:%d",
+                   sym->s_use_pos.p_file, sym->s_use_pos.p_line);
+
+       if (sym->s_type != NULL &&
+           (sym->s_type->t_is_enum || sym->s_type->t_tspec == BOOL))
+               debug_printf(" value=%d", (int)sym->s_value.v_quad);
+
+       if ((sym->s_scl == MOS || sym->s_scl == MOU) &&
+           sym->s_sou_type != NULL) {
+               const char *tag = sym->s_sou_type->sou_tag->s_name;
+               const sym_t *def = sym->s_sou_type->sou_first_typedef;
+               if (tag == unnamed && def != NULL)
+                       debug_printf(" sou='typedef %s'", def->s_name);
+               else
+                       debug_printf(" sou=%s", tag);
+       }
+
+       if (sym->s_keyword != NULL) {
+               int t = (int)sym->s_value.v_quad;
+               if (t == T_TYPE || t == T_STRUCT_OR_UNION)
+                       debug_printf(" %s", tspec_name(sym->s_tspec));
+               else if (t == T_QUAL)
+                       debug_printf(" %s", tqual_name(sym->s_tqual));
+       }
+
+       debug_word(sym->s_osdef && sym->s_args != NULL, "old-style-args");
+
+       debug_printf("\n");
+}
+
 #endif
diff -r 6e92cb9c6e85 -r 9eb3ec67f876 usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Mon Feb 28 22:41:07 2022 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Tue Mar 01 00:17:12 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.251 2022/02/27 20:02:43 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.252 2022/03/01 00:17: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: decl.c,v 1.251 2022/02/27 20:02:43 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.252 2022/03/01 00:17:12 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -140,22 +140,6 @@
        typetab[LCOMPLEX].t_tspec = LCOMPLEX;
 }
 
-#ifdef DEBUG
-/* Return the name of the "storage class" in the wider sense. */
-const char *
-scl_name(scl_t scl)
-{
-       static const char *const names[] = {
-           "none", "extern", "static", "auto", "register", "typedef",
-           "struct", "union", "enum", "member of struct", "member of union",
-           "compile-time constant", "abstract",
-           "old-style function argument", "prototype argument", "inline"
-       };
-
-       return names[scl];
-}
-#endif
-
 /*
  * Returns a shared type structure for arithmetic types and void.
  *
@@ -1946,7 +1930,7 @@
 
                /*
                 * If the old symbol stems from an old style function
-                * definition, we have remembered the params in rdsmy->s_args
+                * definition, we have remembered the params in rdsym->s_args
                 * and compare them with the params of the prototype.
                 */
                if (rdsym->s_osdef && dsym->s_type->t_proto) {
diff -r 6e92cb9c6e85 -r 9eb3ec67f876 usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Mon Feb 28 22:41:07 2022 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Tue Mar 01 00:17:12 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs1.h,v 1.148 2022/02/27 10:31:58 rillig Exp $    */
+/*     $NetBSD: externs1.h,v 1.149 2022/03/01 00:17:12 rillig Exp $    */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -115,7 +115,12 @@
  */
 
 #ifdef DEBUG
+const char *scl_name(scl_t);
+const char *symt_name(symt_t);
+const char *tqual_name(tqual_t);
 void   debug_node(const tnode_t *);
+void   debug_sym(const sym_t *);
+void   debug_symtab(void);
 void   debug_printf(const char *fmt, ...) __printflike(1, 2);
 void   debug_print_indent(void);
 void   debug_indent_inc(void);
@@ -223,7 +228,6 @@
 extern void    check_global_symbols(void);
 extern void    print_previous_declaration(int, const sym_t *);
 extern int     to_int_constant(tnode_t *, bool);
-extern const char *scl_name(scl_t);
 
 /*
  * tree.c
diff -r 6e92cb9c6e85 -r 9eb3ec67f876 usr.bin/xlint/lint1/lex.c
--- a/usr.bin/xlint/lint1/lex.c Mon Feb 28 22:41:07 2022 +0000
+++ b/usr.bin/xlint/lint1/lex.c Tue Mar 01 00:17:12 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.106 2022/02/28 22:41:07 rillig Exp $ */



Home | Main Index | Thread Index | Old Index