Module Name:    src
Committed By:   rillig
Date:           Sun Aug  1 19:11:54 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: debug.c externs1.h main1.c tree.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.127 -r1.128 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/xlint/lint1/main1.c
cvs rdiff -u -r1.325 -r1.326 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/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.1 src/usr.bin/xlint/lint1/debug.c:1.2
--- src/usr.bin/xlint/lint1/debug.c:1.1	Sat Jul 31 18:16:42 2021
+++ src/usr.bin/xlint/lint1/debug.c	Sun Aug  1 19:11:54 2021
@@ -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
 }
 
 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);
+
+	} else {
+		debug_printf("\n");
 
-		debug_node(tn->tn_left, indent + 1);
+		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();
 	}
 }
 

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.127 src/usr.bin/xlint/lint1/externs1.h:1.128
--- src/usr.bin/xlint/lint1/externs1.h:1.127	Sun Aug  1 18:37:29 2021
+++ src/usr.bin/xlint/lint1/externs1.h	Sun Aug  1 19:11:54 2021
@@ -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 @@ extern	void	expr_restore_memory(struct m
  */
 
 #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 @@ void	debug_leave(const char *);
 #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()

Index: src/usr.bin/xlint/lint1/main1.c
diff -u src/usr.bin/xlint/lint1/main1.c:1.52 src/usr.bin/xlint/lint1/main1.c:1.53
--- src/usr.bin/xlint/lint1/main1.c:1.52	Sun Aug  1 18:37:29 2021
+++ src/usr.bin/xlint/lint1/main1.c	Sun Aug  1 19:11:54 2021
@@ -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 @@ main(int argc, char *argv[])
 	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;

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.325 src/usr.bin/xlint/lint1/tree.c:1.326
--- src/usr.bin/xlint/lint1/tree.c:1.325	Sun Aug  1 18:37:29 2021
+++ src/usr.bin/xlint/lint1/tree.c	Sun Aug  1 19:11:54 2021
@@ -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 @@ is_constcond_false(const tnode_t *tn, ts
 	       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 @@ expr(tnode_t *tn, bool vctx, bool tctx, 
 		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 @@ check_precedence_confusion(tnode_t *tn)
 	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)

Reply via email to