Mon Jan 4 17:06:21 2021 UTC ()
lint: in debug mode, print node tree for precedence

>From the code alone, it is too difficult to see how the various internal
operators are combined and what properties they have.  A simple tree
visualization helps to see all the details.

This is used to track down the typo in check_precedence_confusion, to
see whether it could have possibly had any influence at all.


(rillig)
diff -r1.123 -r1.124 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.123 -r1.124 src/usr.bin/xlint/lint1/tree.c (expand / switch to context diff)
--- src/usr.bin/xlint/lint1/tree.c 2021/01/04 15:52:51 1.123
+++ src/usr.bin/xlint/lint1/tree.c 2021/01/04 17:06:20 1.124
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.123 2021/01/04 15:52:51 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.124 2021/01/04 17:06:20 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.123 2021/01/04 15:52:51 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.124 2021/01/04 17:06:20 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3972,6 +3972,48 @@
 	return strg1;
 }
 
+#ifdef DEBUG
+static void
+dprint_node(const tnode_t *tn)
+{
+	static int indent = 0;
+
+	op_t op = tn->tn_op;
+
+	if (tn == NULL) {
+		printf("%*s" "null\n", indent, "");
+		return;
+	}
+
+	printf("%*s%s: %s%s%s",
+	    indent, "",
+	    op == CVT && !tn->tn_cast ? "convert" :
+	    op == NAME ? "name" : getopname(op),
+	    type_name(tn->tn_type), tn->tn_lvalue ? " lvalue" : "",
+	    tn->tn_parenthesized ? " ()" : "");
+	indent += 2;
+	if (op == NAME)
+		printf(" %s\n", tn->tn_sym->s_name);
+	else if (op == CON)
+		printf(" value=?\n");
+	else if (op == STRING)
+		printf(" length=%zu\n", tn->tn_string->st_len);
+	else {
+		printf("\n");
+		dprint_node(tn->tn_left);
+		if (modtab[op].m_binary)
+			dprint_node(tn->tn_right);
+	}
+	indent -= 2;
+}
+#else
+/*ARGSUSED*/
+static void
+dprint_node(const tnode_t *tn)
+{
+}
+#endif
+
 /*
  * Print a warning if the given node has operands which should be
  * parenthesized.
@@ -3998,6 +4040,8 @@
 		lparn |= ln->tn_parenthesized;
 	lparn |= ln->tn_parenthesized;
 	lop = ln->tn_op;
+
+	dprint_node(tn);
 
 	if (mp->m_binary) {
 		rparn = 0;