Module Name:    src
Committed By:   rillig
Date:           Wed Dec 30 01:33:30 UTC 2020

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

Log Message:
lint: reduce verbosity of assertions

Having 2 lines of source code per assertion is too much, especially
since most of this code is redundant anyway.  Extract the common code
and the additional negation into a simple function instead.


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.59 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.40 -r1.41 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.43 -r1.44 src/usr.bin/xlint/lint1/init.c
cvs rdiff -u -r1.36 -r1.37 src/usr.bin/xlint/lint1/lint1.h

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/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.58 src/usr.bin/xlint/lint1/err.c:1.59
--- src/usr.bin/xlint/lint1/err.c:1.58	Tue Dec 29 12:18:42 2020
+++ src/usr.bin/xlint/lint1/err.c	Wed Dec 30 01:33:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.58 2020/12/29 12:18:42 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.59 2020/12/30 01:33:30 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: err.c,v 1.58 2020/12/29 12:18:42 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.59 2020/12/30 01:33:30 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -490,6 +490,18 @@ lerror(const char *file, int line, const
 }
 
 void
+assert_failed(const char *file, int line, const char *func, const char *cond)
+{
+	const	char *fn;
+
+	fn = lbasename(curr_pos.p_file);
+	(void)fprintf(stderr,
+	    "lint: assertion \"%s\" failed in %s at %s:%d near %s:%d\n",
+	    cond, func, file, line, fn, curr_pos.p_line);
+	abort();
+}
+
+void
 warning(int n, ...)
 {
 	va_list	ap;

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.40 src/usr.bin/xlint/lint1/externs1.h:1.41
--- src/usr.bin/xlint/lint1/externs1.h:1.40	Tue Dec 29 17:29:31 2020
+++ src/usr.bin/xlint/lint1/externs1.h	Wed Dec 30 01:33:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.40 2020/12/29 17:29:31 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.41 2020/12/30 01:33:30 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -127,6 +127,8 @@ extern	int	gnuism(int, ...);
 extern	int	c99ism(int, ...);
 extern	void	lerror(const char *, int, const char *, ...)
      __attribute__((__noreturn__,__format__(__printf__, 3, 4)));
+extern	void	assert_failed(const char *, int, const char *, const char *)
+		__attribute__((__noreturn__));
 
 /*
  * decl.c

Index: src/usr.bin/xlint/lint1/init.c
diff -u src/usr.bin/xlint/lint1/init.c:1.43 src/usr.bin/xlint/lint1/init.c:1.44
--- src/usr.bin/xlint/lint1/init.c:1.43	Tue Dec 29 23:12:48 2020
+++ src/usr.bin/xlint/lint1/init.c	Wed Dec 30 01:33:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: init.c,v 1.43 2020/12/29 23:12:48 rillig Exp $	*/
+/*	$NetBSD: init.c,v 1.44 2020/12/30 01:33:30 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.43 2020/12/29 23:12:48 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.44 2020/12/30 01:33:30 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -161,16 +161,14 @@ initstack_pop_item(void)
 	initstk = istk->i_nxt;
 	free(istk);
 	istk = initstk;
-	if (istk == NULL)
-		LERROR("initstack_pop_item()");
+	lint_assert(istk != NULL);
 
 	DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__,
 	    tyname(buf, sizeof buf, istk->i_type ? istk->i_type : istk->i_subt),
 	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
 
 	istk->i_remaining--;
-	if (istk->i_remaining < 0)
-		LERROR("initstack_pop_item()");
+	lint_assert(istk->i_remaining >= 0);
 
 	DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__,
 	    istk->i_remaining, namedmem ? namedmem->n_name : "*null*"));
@@ -207,8 +205,7 @@ initstack_pop_item(void)
 	    !istk->i_namedmem) {
 		do {
 			m = istk->i_mem = istk->i_mem->s_nxt;
-			if (m == NULL)
-				LERROR("initstack_pop_item()");
+			lint_assert(m != NULL);
 			DPRINTF(("%s: pop %s\n", __func__, m->s_name));
 		} while (m->s_field && m->s_name == unnamed);
 		istk->i_subt = m->s_type;
@@ -268,25 +265,21 @@ initstack_push(void)
 		 * Inside of other aggregate types must not be an incomplete
 		 * type.
 		 */
-		if (istk->i_nxt->i_nxt != NULL)
-			LERROR("initstack_push()");
+		lint_assert(istk->i_nxt->i_nxt == NULL);
 		istk->i_remaining = 1;
-		if (istk->i_type->t_tspec != ARRAY)
-			LERROR("initstack_push()");
+		lint_assert(istk->i_type->t_tspec == ARRAY);
 		istk->i_type->t_dim++;
 		setcomplete(istk->i_type, 1);
 	}
 
-	if (istk->i_remaining <= 0)
-		LERROR("initstack_push()");
-	if (istk->i_type != NULL && tspec_is_scalar(istk->i_type->t_tspec))
-		LERROR("initstack_push()");
+	lint_assert(istk->i_remaining > 0);
+	lint_assert(istk->i_type == NULL ||
+	    !tspec_is_scalar(istk->i_type->t_tspec));
 
 	initstk = xcalloc(1, sizeof (istk_t));
 	initstk->i_nxt = istk;
 	initstk->i_type = istk->i_subt;
-	if (initstk->i_type->t_tspec == FUNC)
-		LERROR("initstack_push()");
+	lint_assert(initstk->i_type->t_tspec != FUNC);
 
 again:
 	istk = initstk;
@@ -575,8 +568,7 @@ mkinit(tnode_t *tn)
 	lt = ln->tn_type->t_tspec;
 	rt = tn->tn_type->t_tspec;
 
-	if (!tspec_is_scalar(lt))
-		LERROR("mkinit()");
+	lint_assert(tspec_is_scalar(lt));
 
 	if (!typeok(INIT, 0, ln, tn))
 		return;

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.36 src/usr.bin/xlint/lint1/lint1.h:1.37
--- src/usr.bin/xlint/lint1/lint1.h:1.36	Tue Dec 29 23:12:48 2020
+++ src/usr.bin/xlint/lint1/lint1.h	Wed Dec 30 01:33:30 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.36 2020/12/29 23:12:48 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.37 2020/12/30 01:33:30 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -436,6 +436,12 @@ typedef	struct err_set {
 
 #define LERROR(fmt, args...)	lerror(__FILE__, __LINE__, fmt, ##args)
 
+#define lint_assert(cond)						\
+	do {								\
+		if (!(cond))						\
+			assert_failed(__FILE__, __LINE__, __func__, #cond); \
+	} while (/*CONSTCOND*/0)
+
 #ifdef BLKDEBUG
 #define ZERO	0xa5
 #else

Reply via email to