Module Name:    src
Committed By:   rillig
Date:           Sat Feb 20 15:23:07 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: init.c

Log Message:
lint: add hierarchical debug logging for initializations

No functional change for default mode.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/usr.bin/xlint/lint1/init.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/init.c
diff -u src/usr.bin/xlint/lint1/init.c:1.67 src/usr.bin/xlint/lint1/init.c:1.68
--- src/usr.bin/xlint/lint1/init.c:1.67	Fri Feb 19 22:16:12 2021
+++ src/usr.bin/xlint/lint1/init.c	Sat Feb 20 15:23:07 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: init.c,v 1.67 2021/02/19 22:16:12 rillig Exp $	*/
+/*	$NetBSD: init.c,v 1.68 2021/02/20 15:23:07 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: init.c,v 1.67 2021/02/19 22:16:12 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.68 2021/02/20 15:23:07 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -113,9 +113,57 @@ namlist_t	*namedmem = NULL;
 static	bool	initstack_string(tnode_t *);
 
 #ifndef DEBUG
-#define DPRINTF(a)
+#define debug_printf(fmt, ...) (void)0
+#define debug_indent() (void)0
+#define debug_enter(a) (void)0
+#define debug_step(fmt, ...) (void)0
+#define debug_leave(a) (void)0
 #else
-#define DPRINTF(a) printf a
+static int debug_ind = 0;
+
+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_ind, "");
+}
+
+static void
+debug_enter(const char *func)
+{
+	printf("%*s+ %s\n", 2 * debug_ind++, "", func);
+}
+
+static void __printflike(1, 2)
+debug_step(const char *fmt, ...)
+{
+	va_list va;
+
+	printf("%*s", 2 * debug_ind, "");
+	va_start(va, fmt);
+	vfprintf(stdout, fmt, va);
+	va_end(va);
+	printf("\n");
+}
+
+static void
+debug_leave(const char *func)
+{
+	printf("%*s- %s\n", 2 * --debug_ind, "", func);
+}
+
+#define debug_enter() debug_enter(__func__)
+#define debug_leave() debug_leave(__func__)
+
 #endif
 
 void
@@ -123,7 +171,7 @@ push_member(sbuf_t *sb)
 {
 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
 	nam->n_name = sb->sb_name;
-	DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
+	debug_step("%s: %s %p", __func__, nam->n_name, nam);
 	if (namedmem == NULL) {
 		/*
 		 * XXX: Why is this a circular list?
@@ -143,7 +191,7 @@ push_member(sbuf_t *sb)
 static void
 pop_member(void)
 {
-	DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
+	debug_step("%s: %s %p", __func__, namedmem->n_name, namedmem);
 	if (namedmem->n_next == namedmem) {
 		free(namedmem);
 		namedmem = NULL;
@@ -157,19 +205,20 @@ pop_member(void)
 }
 
 static void
-named_member_dprint(void)
+debug_named_member(void)
 {
 	namlist_t *name;
 
 	if (namedmem == NULL)
 		return;
 	name = namedmem;
-	DPRINTF(("named member:"));
+	debug_indent();
+	debug_printf("named member:");
 	do {
-		DPRINTF((" %s", name->n_name));
+		debug_printf(" %s", name->n_name);
 		name = name->n_next;
 	} while (name != namedmem);
-	DPRINTF(("\n"));
+	debug_printf("\n");
 }
 
 /*
@@ -190,7 +239,7 @@ initstack_init(void)
 		free(istk);
 	}
 
-	DPRINTF(("%s\n", __func__));
+	debug_step("%s", __func__);
 
 	/*
 	 * If the type which is to be initialized is an incomplete type,
@@ -210,50 +259,54 @@ initstack_pop_item(void)
 	istk_t	*istk;
 	sym_t	*m;
 
+	debug_enter();
+
 	istk = initstk;
-	DPRINTF(("%s: pop type=%s, brace=%d remaining=%d named=%d\n", __func__,
+	debug_step("pop type=%s, brace=%d remaining=%d named=%d",
 	    type_name(istk->i_type ? istk->i_type : istk->i_subt),
-	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
+	    istk->i_brace, istk->i_remaining, istk->i_namedmem);
 
 	initstk = istk->i_next;
 	free(istk);
 	istk = initstk;
 	lint_assert(istk != NULL);
 
-	DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__,
+	debug_step("top type=%s, brace=%d remaining=%d named=%d",
 	    type_name(istk->i_type ? istk->i_type : istk->i_subt),
-	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
+	    istk->i_brace, istk->i_remaining, istk->i_namedmem);
 
 	istk->i_remaining--;
 	lint_assert(istk->i_remaining >= 0);
 
-	DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__,
-	    istk->i_remaining, namedmem ? namedmem->n_name : "*null*"));
+	debug_step("top remaining=%d rhs.name=%s",
+	    istk->i_remaining, namedmem ? namedmem->n_name : "*null*");
 
 	if (istk->i_remaining >= 0 && namedmem != NULL) {
 
-		DPRINTF(("%s: named remaining=%d type=%s, rhs.name=%s\n",
-		    __func__, istk->i_remaining,
-		    type_name(istk->i_type), namedmem->n_name));
+		debug_step("named remaining=%d type=%s, rhs.name=%s",
+		    istk->i_remaining, type_name(istk->i_type),
+		    namedmem->n_name);
 
 		for (m = istk->i_type->t_str->sou_first_member;
 		     m != NULL; m = m->s_next) {
-			DPRINTF(("%s: pop lhs.name=%s rhs.name=%s\n", __func__,
-			    m->s_name, namedmem->n_name));
+			debug_step("pop lhs.name=%s rhs.name=%s",
+			    m->s_name, namedmem->n_name);
 			if (m->s_bitfield && m->s_name == unnamed)
 				continue;
 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
 				istk->i_subt = m->s_type;
 				istk->i_remaining++;
 				pop_member();
+				debug_leave();
 				return;
 			}
 		}
 		/* undefined struct/union member: %s */
 		error(101, namedmem->n_name);
-		DPRINTF(("%s: end rhs.name=%s\n", __func__, namedmem->n_name));
+		debug_step("end rhs.name=%s", namedmem->n_name);
 		pop_member();
 		istk->i_namedmem = true;
+		debug_leave();
 		return;
 	}
 	/*
@@ -265,10 +318,11 @@ initstack_pop_item(void)
 		do {
 			m = istk->i_mem = istk->i_mem->s_next;
 			lint_assert(m != NULL);
-			DPRINTF(("%s: pop %s\n", __func__, m->s_name));
+			debug_step("pop %s", m->s_name);
 		} while (m->s_bitfield && m->s_name == unnamed);
 		istk->i_subt = m->s_type;
 	}
+	debug_leave();
 }
 
 /*
@@ -280,13 +334,13 @@ initstack_pop_brace(void)
 {
 	bool brace;
 
-	DPRINTF(("%s\n", __func__));
+	debug_enter();
 	do {
 		brace = initstk->i_brace;
-		DPRINTF(("%s: loop brace=%d\n", __func__, brace));
+		debug_step("loop brace=%d", brace);
 		initstack_pop_item();
 	} while (!brace);
-	DPRINTF(("%s: done\n", __func__));
+	debug_leave();
 }
 
 /*
@@ -297,11 +351,11 @@ static void
 initstack_pop_nobrace(void)
 {
 
-	DPRINTF(("%s\n", __func__));
+	debug_enter();
 	while (!initstk->i_brace && initstk->i_remaining == 0 &&
 	       !initstk->i_nolimit)
 		initstack_pop_item();
-	DPRINTF(("%s: done\n", __func__));
+	debug_leave();
 }
 
 static void
@@ -311,11 +365,13 @@ initstack_push(void)
 	int	cnt;
 	sym_t	*m;
 
+	debug_enter();
+
 	istk = initstk;
 
 	/* Extend an incomplete array type by one element */
 	if (istk->i_remaining == 0) {
-		DPRINTF(("%s(extend) %s\n", __func__, type_name(istk->i_type)));
+		debug_step("(extend) %s", type_name(istk->i_type));
 		/*
 		 * Inside of other aggregate types must not be an incomplete
 		 * type.
@@ -338,17 +394,17 @@ initstack_push(void)
 again:
 	istk = initstk;
 
-	DPRINTF(("%s(%s)\n", __func__, type_name(istk->i_type)));
+	debug_step("typename %s", type_name(istk->i_type));
 	switch (istk->i_type->t_tspec) {
 	case ARRAY:
 		if (namedmem != NULL) {
-			DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
-			    namedmem->n_name, istk->i_brace));
+			debug_step("ARRAY %s brace=%d",
+			    namedmem->n_name, istk->i_brace);
 			goto pop;
 		} else if (istk->i_next->i_namedmem) {
 			istk->i_brace = true;
-			DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
-			    istk->i_brace, istk->i_next->i_namedmem));
+			debug_step("ARRAY brace=%d, namedmem=%d",
+			    istk->i_brace, istk->i_next->i_namedmem);
 		}
 
 		if (is_incomplete(istk->i_type) &&
@@ -356,14 +412,15 @@ again:
 			/* initialisation of an incomplete type */
 			error(175);
 			initerr = true;
+			debug_leave();
 			return;
 		}
 		istk->i_subt = istk->i_type->t_subt;
 		istk->i_nolimit = is_incomplete(istk->i_type);
 		istk->i_remaining = istk->i_type->t_dim;
-		DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
+		debug_step("elements array %s[%d] %s",
 		    type_name(istk->i_subt), istk->i_remaining,
-		    namedmem ? namedmem->n_name : "*none*"));
+		    namedmem ? namedmem->n_name : "*none*");
 		break;
 	case UNION:
 		if (tflag)
@@ -375,19 +432,20 @@ again:
 			/* initialisation of an incomplete type */
 			error(175);
 			initerr = true;
+			debug_leave();
 			return;
 		}
 		cnt = 0;
-		DPRINTF(("%s: lookup type=%s, name=%s named=%d\n", __func__,
+		debug_step("lookup type=%s, name=%s named=%d",
 		    type_name(istk->i_type),
-		    namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
+		    namedmem ? namedmem->n_name : "*none*", istk->i_namedmem);
 		for (m = istk->i_type->t_str->sou_first_member;
 		     m != NULL; m = m->s_next) {
 			if (m->s_bitfield && m->s_name == unnamed)
 				continue;
 			if (namedmem != NULL) {
-				DPRINTF(("%s: named lhs.member=%s, rhs.member=%s\n",
-				    __func__, m->s_name, namedmem->n_name));
+				debug_step("named lhs.member=%s, rhs.member=%s",
+				    m->s_name, namedmem->n_name);
 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
 					cnt++;
 					break;
@@ -401,32 +459,32 @@ again:
 		}
 		if (namedmem != NULL) {
 			if (m == NULL) {
-				DPRINTF(("%s: pop struct\n", __func__));
+				debug_step("pop struct");
 				goto pop;
 			}
 			istk->i_mem = m;
 			istk->i_subt = m->s_type;
 			istk->i_namedmem = true;
-			DPRINTF(("%s: named name=%s\n", __func__,
-			    namedmem->n_name));
+			debug_step("named name=%s", namedmem->n_name);
 			pop_member();
 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
 		}
 		istk->i_brace = true;
-		DPRINTF(("%s: unnamed type=%s, brace=%d\n", __func__,
+		debug_step("unnamed type=%s, brace=%d",
 		    type_name(istk->i_type ? istk->i_type : istk->i_subt),
-		    istk->i_brace));
+		    istk->i_brace);
 		if (cnt == 0) {
 			/* cannot init. struct/union with no named member */
 			error(179);
 			initerr = true;
+			debug_leave();
 			return;
 		}
 		istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
 		break;
 	default:
 		if (namedmem != NULL) {
-			DPRINTF(("%s: pop\n", __func__));
+			debug_step("pop");
 	pop:
 			inxt = initstk->i_next;
 			free(istk);
@@ -436,6 +494,8 @@ again:
 		istk->i_remaining = 1;
 		break;
 	}
+
+	debug_leave();
 }
 
 static void
@@ -473,7 +533,8 @@ static void
 initstack_next_brace(void)
 {
 
-	DPRINTF(("%s\n", __func__));
+	debug_enter();
+
 	if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
 		/* invalid initializer type %s */
 		error(176, type_name(initstk->i_type));
@@ -485,16 +546,18 @@ initstack_next_brace(void)
 		initstack_push();
 	if (!initerr) {
 		initstk->i_brace = true;
-		DPRINTF(("%s: %p %s\n", __func__, namedmem, type_name(
-			initstk->i_type ? initstk->i_type : initstk->i_subt)));
+		debug_step("%p %s", namedmem, type_name(
+			initstk->i_type ? initstk->i_type : initstk->i_subt));
 	}
+
+	debug_leave();
 }
 
 static void
 initstack_next_nobrace(void)
 {
+	debug_enter();
 
-	DPRINTF(("%s\n", __func__));
 	if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
 		/* {}-enclosed initializer required */
 		error(181);
@@ -509,16 +572,18 @@ initstack_next_nobrace(void)
 			break;
 		initstack_push();
 	}
+
+	debug_leave();
 }
 
 void
 init_lbrace(void)
 {
-	DPRINTF(("%s\n", __func__));
-
 	if (initerr)
 		return;
 
+	debug_enter();
+
 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
 	    initstk->i_next == NULL) {
 		if (tflag && !is_scalar(initstk->i_subt->t_tspec))
@@ -533,17 +598,21 @@ init_lbrace(void)
 	initstack_pop_nobrace();
 
 	initstack_next_brace();
+
+	debug_leave();
 }
 
 void
 init_rbrace(void)
 {
-	DPRINTF(("%s\n", __func__));
-
 	if (initerr)
 		return;
 
+	debug_enter();
+
 	initstack_pop_brace();
+
+	debug_leave();
 }
 
 void
@@ -559,13 +628,17 @@ mkinit(tnode_t *tn)
 	char	sbuf[64];
 #endif
 
-	DPRINTF(("%s: type=%s, value=%s\n", __func__,
+	debug_enter();
+
+	debug_step("type=%s, value=%s",
 	    type_name(tn->tn_type),
-	    print_tnode(sbuf, sizeof(sbuf), tn)));
-	named_member_dprint();
+	    print_tnode(sbuf, sizeof(sbuf), tn));
+	debug_named_member();
 
-	if (initerr || tn == NULL)
+	if (initerr || tn == NULL) {
+		debug_leave();
 		return;
+	}
 
 	sc = initsym->s_scl;
 
@@ -587,6 +660,7 @@ mkinit(tnode_t *tn)
 		ln->tn_type->t_const = false;
 		tn = build(ASSIGN, ln, tn);
 		expr(tn, false, false, false, false);
+		debug_leave();
 		return;
 	}
 
@@ -597,16 +671,19 @@ mkinit(tnode_t *tn)
 	initstack_pop_nobrace();
 
 	/* Initialisations by strings are done in initstack_string(). */
-	if (initstack_string(tn))
+	if (initstack_string(tn)) {
+		debug_leave();
 		return;
+	}
 
 	initstack_next_nobrace();
-	if (initerr || tn == NULL)
+	if (initerr || tn == NULL) {
+		debug_leave();
 		return;
+	}
 
 	initstk->i_remaining--;
-	DPRINTF(("%s: remaining=%d tn=%p\n", __func__,
-	    initstk->i_remaining, tn));
+	debug_step("remaining=%d tn=%p", initstk->i_remaining, tn);
 	/* Create a temporary node for the left side. */
 	ln = tgetblk(sizeof (tnode_t));
 	ln->tn_op = NAME;
@@ -622,8 +699,10 @@ mkinit(tnode_t *tn)
 
 	lint_assert(is_scalar(lt));
 
-	if (!typeok(INIT, 0, ln, tn))
+	if (!typeok(INIT, 0, ln, tn)) {
+		debug_leave();
 		return;
+	}
 
 	/*
 	 * Store the tree memory. This is necessary because otherwise
@@ -659,6 +738,8 @@ mkinit(tnode_t *tn)
 			}
 		}
 	}
+
+	debug_leave();
 }
 
 
@@ -673,6 +754,8 @@ initstack_string(tnode_t *tn)
 	if (tn->tn_op != STRING)
 		return false;
 
+	debug_enter();
+
 	istk = initstk;
 	strg = tn->tn_string;
 
@@ -681,22 +764,24 @@ initstack_string(tnode_t *tn)
 	 * the string.
 	 */
 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
-		DPRINTF(("%s: subt array\n", __func__));
+		debug_step("subt array");
 		t = istk->i_subt->t_subt->t_tspec;
 		if (!((strg->st_tspec == CHAR &&
 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
+			debug_leave();
 			return false;
 		}
 		/* Put the array at top of stack */
 		initstack_push();
 		istk = initstk;
 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
-		DPRINTF(("%s: type array\n", __func__));
+		debug_step("type array");
 		t = istk->i_type->t_subt->t_tspec;
 		if (!((strg->st_tspec == CHAR &&
 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
+			debug_leave();
 			return false;
 		}
 		/*
@@ -704,8 +789,10 @@ initstack_string(tnode_t *tn)
 		 * wrong here.
 		 */
 		if (istk->i_remaining != istk->i_type->t_dim)
+			debug_leave();
 			return false;
 	} else {
+		debug_leave();
 		return false;
 	}
 
@@ -726,5 +813,6 @@ initstack_string(tnode_t *tn)
 	/* In every case the array is initialized completely. */
 	istk->i_remaining = 0;
 
+	debug_leave();
 	return true;
 }

Reply via email to