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: merge duplicate debugging code



details:   https://anonhg.NetBSD.org/src/rev/04479a25302f
branches:  trunk
changeset: 1022692:04479a25302f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 01 19:11:54 2021 +0000

description:
lint: merge duplicate debugging code

The functions 'debug_node' and 'display_expression' were similar enough
to be merged.

Migrate debug_node to use the existing debug logging functions.

Remove the now unused option 'd' from the options string.

diffstat:

 usr.bin/xlint/lint1/debug.c    |  50 ++++++++++++++++++++----------
 usr.bin/xlint/lint1/externs1.h |   6 +-
 usr.bin/xlint/lint1/main1.c    |   6 +-
 usr.bin/xlint/lint1/tree.c     |  69 ++---------------------------------------
 4 files changed, 43 insertions(+), 88 deletions(-)

diffs (250 lines):

diff -r 034df3aa5835 -r 04479a25302f usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c       Sun Aug 01 18:37:29 2021 +0000
+++ b/usr.bin/xlint/lint1/debug.c       Sun Aug 01 19:11:54 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.2 2021/08/01 19:11:54 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,9 +35,11 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.2 2021/08/01 19:11:54 rillig Exp $");
 #endif
 
+#include <stdlib.h>
+
 #include "lint1.h"
 
 
@@ -104,40 +106,54 @@
 }
 
 void
-debug_node(const tnode_t *tn, int indent)
+debug_node(const tnode_t *tn)
 {
        op_t op;
 
        if (tn == NULL) {
-               printf("%*s" "null\n", indent, "");
+               debug_step("null");
                return;
        }
 
        op = tn->tn_op;
-       printf("%*s%s with type '%s'%s%s",
-           2 * indent, "",
+       debug_indent();
+       debug_printf("'%s' with type '%s'%s%s",
            op == CVT && !tn->tn_cast ? "convert" : modtab[op].m_name,
            type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "",
            tn->tn_parenthesized ? ", parenthesized" : "");
 
        if (op == NAME)
-               printf(" %s\n", tn->tn_sym->s_name);
+               debug_printf(" %s %s\n", tn->tn_sym->s_name,
+                   storage_class_name(tn->tn_sym->s_scl));
        else if (op == CON && is_floating(tn->tn_type->t_tspec))
-               printf(", value %Lg", tn->tn_val->v_ldbl);
+               debug_printf(", value %Lg", tn->tn_val->v_ldbl);
        else if (op == CON && is_uinteger(tn->tn_type->t_tspec))
-               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))
-               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)
-               printf(", unknown value\n");
-       else if (op == STRING)
-               printf(", length %zu\n", tn->tn_string->st_len);
-       else {
-               printf("\n");
+               debug_printf(", unknown value\n");
+       else if (op == STRING && tn->tn_string->st_tspec == CHAR)
+               debug_printf(", length %zu, \"%s\"\n",
+                   tn->tn_string->st_len, tn->tn_string->st_cp);
+       else if (op == STRING && tn->tn_string->st_tspec == WCHAR) {
+               char *s;
+               size_t n;
+               n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
+               s = xmalloc(n);
+               (void)wcstombs(s, tn->tn_string->st_wcp, n);
+               debug_printf(", length %zu, L\"%s\"",
+                   tn->tn_string->st_len, s);
+               free(s);
 
-               debug_node(tn->tn_left, indent + 1);
+       } else {
+               debug_printf("\n");
+
+               debug_indent_inc();
+               debug_node(tn->tn_left);
                if (modtab[op].m_binary || tn->tn_right != NULL)
-                       debug_node(tn->tn_right, indent + 1);
+                       debug_node(tn->tn_right);
+               debug_indent_dec();
        }
 }
 
diff -r 034df3aa5835 -r 04479a25302f usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Sun Aug 01 18:37:29 2021 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Sun Aug 01 19:11:54 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs1.h,v 1.127 2021/08/01 18:37:29 rillig Exp $    */
+/*     $NetBSD: externs1.h,v 1.128 2021/08/01 19:11:54 rillig Exp $    */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -115,7 +115,7 @@
  */
 
 #ifdef DEBUG
-void   debug_node(const tnode_t *, int);
+void   debug_node(const tnode_t *);
 void   debug_printf(const char *fmt, ...) __printflike(1, 2);
 void   debug_indent(void);
 void   debug_indent_inc(void);
@@ -127,7 +127,7 @@
 #define        debug_leave()           (debug_leave)(__func__)
 #else
 #define        debug_noop()            do { } while (false)
-#define        debug_node(tn, indent)  debug_noop()
+#define        debug_node(tn)          debug_noop()
 #define        debug_printf(...)       debug_noop()
 #define        debug_indent()          debug_noop()
 #define        debug_step(...)         debug_noop()
diff -r 034df3aa5835 -r 04479a25302f usr.bin/xlint/lint1/main1.c
--- a/usr.bin/xlint/lint1/main1.c       Sun Aug 01 18:37:29 2021 +0000
+++ b/usr.bin/xlint/lint1/main1.c       Sun Aug 01 19:11:54 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main1.c,v 1.52 2021/08/01 18:37:29 rillig Exp $        */
+/*     $NetBSD: main1.c,v 1.53 2021/08/01 19:11:54 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: main1.c,v 1.52 2021/08/01 18:37:29 rillig Exp $");
+__RCSID("$NetBSD: main1.c,v 1.53 2021/08/01 19:11:54 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -178,7 +178,7 @@
        setprogname(argv[0]);
 
        ERR_ZERO(&msgset);
-       while ((c = getopt(argc, argv, "abcdeghmprstuvwyzA:FPR:STX:")) != -1) {
+       while ((c = getopt(argc, argv, "abceghmprstuvwyzA:FPR:STX:")) != -1) {
                switch (c) {
                case 'a':       aflag++;        break;
                case 'b':       bflag = true;   break;
diff -r 034df3aa5835 -r 04479a25302f usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sun Aug 01 18:37:29 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sun Aug 01 19:11:54 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.325 2021/08/01 18:37:29 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.326 2021/08/01 19:11:54 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.325 2021/08/01 18:37:29 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.326 2021/08/01 19:11:54 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3643,67 +3643,6 @@
               tn->tn_op == CON && tn->tn_val->v_quad == 0;
 }
 
-#ifdef DEBUG
-/* Dump an expression to stdout. */
-static void
-display_expression(const tnode_t *tn, int offs)
-{
-       uint64_t uq;
-
-       if (tn == NULL) {
-               (void)printf("%*s%s\n", offs, "", "NULL");
-               return;
-       }
-       (void)printf("%*sop %s  ", offs, "", op_name(tn->tn_op));
-
-       if (tn->tn_op == NAME) {
-               (void)printf("%s: %s ",
-                   tn->tn_sym->s_name,
-                   storage_class_name(tn->tn_sym->s_scl));
-       } else if (tn->tn_op == CON && is_floating(tn->tn_type->t_tspec)) {
-               (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
-       } else if (tn->tn_op == CON && is_integer(tn->tn_type->t_tspec)) {
-               uq = tn->tn_val->v_quad;
-               (void)printf("0x %08lx %08lx ",
-                   (long)(uq >> 32) & 0xffffffffl,
-                   (long)uq & 0xffffffffl);
-       } else if (tn->tn_op == CON && tn->tn_type->t_tspec == BOOL) {
-               (void)printf("%s ",
-                   tn->tn_val->v_quad != 0 ? "true" : "false");
-       } else if (tn->tn_op == CON) {
-               lint_assert(tn->tn_type->t_tspec == PTR);
-               (void)printf("0x%0*lx ", (int)(sizeof(void *) * CHAR_BIT / 4),
-                   (u_long)tn->tn_val->v_quad);
-       } else if (tn->tn_op == STRING) {
-               if (tn->tn_string->st_tspec == CHAR) {
-                       (void)printf("\"%s\"", tn->tn_string->st_cp);
-               } else {
-                       char    *s;
-                       size_t  n;
-                       n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
-                       s = xmalloc(n);
-                       (void)wcstombs(s, tn->tn_string->st_wcp, n);
-                       (void)printf("L\"%s\"", s);
-                       free(s);
-               }
-               (void)printf(" ");
-       } else if (tn->tn_op == FSEL) {
-               (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
-                   tn->tn_type->t_flen);
-       }
-       (void)printf("%s\n", type_name(tn->tn_type));
-       if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
-               return;
-       display_expression(tn->tn_left, offs + 2);
-       if (modtab[tn->tn_op].m_binary ||
-       (tn->tn_op == PUSH && tn->tn_right != NULL)) {
-               display_expression(tn->tn_right, offs + 2);
-       }
-}
-#else
-#define        display_expression(tn, offs)    debug_noop()
-#endif
-
 /*
  * Perform some tests on expressions which can't be done in build_binary()
  * and functions called by build_binary(). These tests must be done here
@@ -3746,7 +3685,7 @@
                if (tn->tn_op != COMMA && !vctx && !tctx)
                        check_null_effect(tn);
        }
-       display_expression(tn, 0);
+       debug_node(tn);
 
        /* free the tree memory */
        if (dofreeblk)
@@ -4266,7 +4205,7 @@
        if (!hflag)
                return;
 
-       debug_node(tn, 0);
+       debug_node(tn);
 
        lint_assert(modtab[tn->tn_op].m_binary);
        for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)



Home | Main Index | Thread Index | Old Index