Module Name: src Committed By: rillig Date: Sat Jul 31 18:16:42 UTC 2021
Modified Files: src/usr.bin/xlint/lint1: Makefile externs1.h init.c tree.c Added Files: src/usr.bin/xlint/lint1: debug.c Log Message: lint: extract debug logging to separate file Lint currently has several different kinds of debug log: * The -DDEBUG log is controlled at compile time. * The -d command line options enables some other debug logging. * The -DYYDEBUG log for parsing is controlled at compile time. * The -y command line option only has an effect in -DYYDEBUG mode. Extracting the logging into a separate file is a first step towards unifying these logs and making the code for debug logging stand out less than the current #ifdef DEBUG. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.81 -r1.82 src/usr.bin/xlint/lint1/Makefile cvs rdiff -u -r0 -r1.1 src/usr.bin/xlint/lint1/debug.c cvs rdiff -u -r1.123 -r1.124 src/usr.bin/xlint/lint1/externs1.h cvs rdiff -u -r1.204 -r1.205 src/usr.bin/xlint/lint1/init.c cvs rdiff -u -r1.321 -r1.322 src/usr.bin/xlint/lint1/tree.c 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.81 src/usr.bin/xlint/lint1/Makefile:1.82 --- src/usr.bin/xlint/lint1/Makefile:1.81 Sat Jul 31 17:09:21 2021 +++ src/usr.bin/xlint/lint1/Makefile Sat Jul 31 18:16:42 2021 @@ -1,10 +1,10 @@ -# $NetBSD: Makefile,v 1.81 2021/07/31 17:09:21 rillig Exp $ +# $NetBSD: Makefile,v 1.82 2021/07/31 18:16:42 rillig Exp $ .include <bsd.own.mk> PROG= lint1 SRCS= cgram.y \ - ckbool.c ckctype.c ckgetopt.c \ + ckbool.c ckctype.c ckgetopt.c debug.c \ decl.c emit.c emit1.c err.c func.c init.c inittyp.c lex.c \ main1.c mem.c mem1.c oper.c scan.l tree.c tyname.c Index: src/usr.bin/xlint/lint1/externs1.h diff -u src/usr.bin/xlint/lint1/externs1.h:1.123 src/usr.bin/xlint/lint1/externs1.h:1.124 --- src/usr.bin/xlint/lint1/externs1.h:1.123 Sat Jul 31 17:09:21 2021 +++ src/usr.bin/xlint/lint1/externs1.h Sat Jul 31 18:16:42 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: externs1.h,v 1.123 2021/07/31 17:09:21 rillig Exp $ */ +/* $NetBSD: externs1.h,v 1.124 2021/07/31 18:16:42 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -112,6 +112,42 @@ extern struct memory_block *expr_save_me extern void expr_restore_memory(struct memory_block *); /* + * debug.c + */ + +#ifdef DEBUG +void debug_node(const tnode_t *, int); +void debug_printf(const char *fmt, ...) __printflike(1, 2); +void debug_indent(void); +void debug_indent_inc(void); +void debug_indent_dec(void); +void debug_enter(const char *); +void debug_step(const char *fmt, ...) __printflike(1, 2); +#define debug_step0 debug_step +#define debug_step1 debug_step +#define debug_step2 debug_step +void debug_leave(const char *); +#define debug_enter() (debug_enter)(__func__) +#define debug_leave() (debug_leave)(__func__) +#else +#define debug_noop() do { } while (false) +#define debug_node(tn, indent) debug_noop() +/* ARGSUSED */ +static inline void __printflike(1, 2) debug_printf(const char *fmt, ...) {} +#define debug_indent() debug_noop() +/* ARGSUSED */ +static inline void __printflike(1, 2) debug_step(const char *fmt, ...) {} +#define debug_indent() debug_noop() +#define debug_indent_inc() debug_noop() +#define debug_indent_dec() debug_noop() +#define debug_enter() debug_noop() +#define debug_step0(fmt) debug_noop() +#define debug_step1(fmt, arg0) debug_noop() +#define debug_step2(fmt, arg1, arg2) debug_noop() +#define debug_leave() debug_noop() +#endif + +/* * err.c */ extern int nerr; @@ -235,11 +271,6 @@ extern void check_expr_misc(const tnode_ extern bool constant_addr(const tnode_t *, const sym_t **, ptrdiff_t *); extern strg_t *cat_strings(strg_t *, strg_t *); extern int64_t type_size_in_bits(const type_t *); -#ifdef DEBUG -extern void debug_node(const tnode_t *, int); -#else -#define debug_node(tn, indent) do { } while (false) -#endif /* * func.c Index: src/usr.bin/xlint/lint1/init.c diff -u src/usr.bin/xlint/lint1/init.c:1.204 src/usr.bin/xlint/lint1/init.c:1.205 --- src/usr.bin/xlint/lint1/init.c:1.204 Sat Jul 31 11:03:04 2021 +++ src/usr.bin/xlint/lint1/init.c Sat Jul 31 18:16:42 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: init.c,v 1.204 2021/07/31 11:03:04 rillig Exp $ */ +/* $NetBSD: init.c,v 1.205 2021/07/31 18:16:42 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: init.c,v 1.204 2021/07/31 11:03:04 rillig Exp $"); +__RCSID("$NetBSD: init.c,v 1.205 2021/07/31 18:16:42 rillig Exp $"); #endif #include <stdlib.h> @@ -172,74 +172,6 @@ struct initialization { }; -#ifdef DEBUG -static int debug_indentation = 0; -#endif - - -#ifdef DEBUG - -static void __printflike(1, 2) -debug_printf(const char *fmt, ...) -{ - va_list va; - - va_start(va, fmt); - vfprintf(stdout, fmt, va); - va_end(va); -} - -static void -debug_indent(void) -{ - - debug_printf("%*s", 2 * debug_indentation, ""); -} - -static void -debug_enter(const char *func) -{ - - printf("%*s+ %s\n", 2 * debug_indentation++, "", func); -} - -static void __printflike(1, 2) -debug_step(const char *fmt, ...) -{ - va_list va; - - debug_indent(); - va_start(va, fmt); - vfprintf(stdout, fmt, va); - va_end(va); - printf("\n"); -} -#define debug_step0 debug_step -#define debug_step1 debug_step -#define debug_step2 debug_step - -static void -debug_leave(const char *func) -{ - - printf("%*s- %s\n", 2 * --debug_indentation, "", func); -} - -#define debug_enter() (debug_enter)(__func__) -#define debug_leave() (debug_leave)(__func__) - -#else - -#define debug_indent() do { } while (false) -#define debug_enter() do { } while (false) -#define debug_step0(fmt) do { } while (false) -#define debug_step1(fmt, arg0) do { } while (false) -#define debug_step2(fmt, arg1, arg2) do { } while (false) -#define debug_leave() do { } while (false) - -#endif - - static void * unconst_cast(const void *p) { @@ -1037,9 +969,7 @@ begin_initialization(sym_t *sym) struct initialization *in; debug_step1("begin initialization of '%s'", type_name(sym->s_type)); -#ifdef DEBUG - debug_indentation++; -#endif + debug_indent_inc(); in = initialization_new(sym); in->in_enclosing = init; @@ -1055,9 +985,7 @@ end_initialization(void) init = in->in_enclosing; initialization_free(in); -#ifdef DEBUG - debug_indentation--; -#endif + debug_indent_dec(); debug_step0("end initialization"); } Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.321 src/usr.bin/xlint/lint1/tree.c:1.322 --- src/usr.bin/xlint/lint1/tree.c:1.321 Sat Jul 31 11:37:53 2021 +++ src/usr.bin/xlint/lint1/tree.c Sat Jul 31 18:16:42 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: tree.c,v 1.321 2021/07/31 11:37:53 rillig Exp $ */ +/* $NetBSD: tree.c,v 1.322 2021/07/31 18:16:42 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.321 2021/07/31 11:37:53 rillig Exp $"); +__RCSID("$NetBSD: tree.c,v 1.322 2021/07/31 18:16:42 rillig Exp $"); #endif #include <float.h> @@ -103,46 +103,6 @@ op_name(op_t op) return modtab[op].m_name; } -#ifdef DEBUG -void -debug_node(const tnode_t *tn, int indent) -{ - op_t op; - - if (tn == NULL) { - printf("%*s" "null\n", indent, ""); - return; - } - - op = tn->tn_op; - printf("%*s%s with type '%s'%s%s", - 2 * indent, "", - op == CVT && !tn->tn_cast ? "convert" : op_name(op), - type_name(tn->tn_type), tn->tn_lvalue ? ", lvalue" : "", - tn->tn_parenthesized ? ", parenthesized" : ""); - - if (op == NAME) - printf(" %s\n", tn->tn_sym->s_name); - else if (op == CON && is_floating(tn->tn_type->t_tspec)) - 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); - else if (op == CON && is_integer(tn->tn_type->t_tspec)) - 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_node(tn->tn_left, indent + 1); - if (modtab[op].m_binary || tn->tn_right != NULL) - debug_node(tn->tn_right, indent + 1); - } -} -#endif - /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */ type_t * derive_type(type_t *tp, tspec_t t) Added files: Index: src/usr.bin/xlint/lint1/debug.c diff -u /dev/null src/usr.bin/xlint/lint1/debug.c:1.1 --- /dev/null Sat Jul 31 18:16:42 2021 +++ src/usr.bin/xlint/lint1/debug.c Sat Jul 31 18:16:42 2021 @@ -0,0 +1,144 @@ +/* $NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $ */ + +/*- + * Copyright (c) 2021 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Roland Illig <ril...@netbsd.org>. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``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 FOUNDATION OR CONTRIBUTORS + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include <sys/cdefs.h> +#if defined(__RCSID) && !defined(lint) +__RCSID("$NetBSD: debug.c,v 1.1 2021/07/31 18:16:42 rillig Exp $"); +#endif + +#include "lint1.h" + + +#ifdef DEBUG + +static int debug_indentation = 0; + + +void __printflike(1, 2) +debug_printf(const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + vfprintf(stdout, fmt, va); + va_end(va); +} + +void +debug_indent(void) +{ + + debug_printf("%*s", 2 * debug_indentation, ""); +} + +void +debug_indent_inc(void) +{ + + debug_indentation++; +} + +void +debug_indent_dec(void) +{ + + debug_indentation--; +} + +void +(debug_enter)(const char *func) +{ + + printf("%*s+ %s\n", 2 * debug_indentation++, "", func); +} + +void __printflike(1, 2) +debug_step(const char *fmt, ...) +{ + va_list va; + + debug_indent(); + va_start(va, fmt); + vfprintf(stdout, fmt, va); + va_end(va); + printf("\n"); +} + +void +(debug_leave)(const char *func) +{ + + printf("%*s- %s\n", 2 * --debug_indentation, "", func); +} + +void +debug_node(const tnode_t *tn, int indent) +{ + op_t op; + + if (tn == NULL) { + printf("%*s" "null\n", indent, ""); + return; + } + + op = tn->tn_op; + printf("%*s%s with type '%s'%s%s", + 2 * indent, "", + 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); + else if (op == CON && is_floating(tn->tn_type->t_tspec)) + 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); + else if (op == CON && is_integer(tn->tn_type->t_tspec)) + 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_node(tn->tn_left, indent + 1); + if (modtab[op].m_binary || tn->tn_right != NULL) + debug_node(tn->tn_right, indent + 1); + } +} + +#endif