Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: reduce verbosity of assertions



details:   https://anonhg.NetBSD.org/src/rev/146a2ffda9e4
branches:  trunk
changeset: 979488:146a2ffda9e4
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Dec 30 01:33:30 2020 +0000

description:
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.

diffstat:

 usr.bin/xlint/lint1/err.c      |  16 ++++++++++++++--
 usr.bin/xlint/lint1/externs1.h |   4 +++-
 usr.bin/xlint/lint1/init.c     |  32 ++++++++++++--------------------
 usr.bin/xlint/lint1/lint1.h    |   8 +++++++-
 4 files changed, 36 insertions(+), 24 deletions(-)

diffs (166 lines):

diff -r 185c7b304c90 -r 146a2ffda9e4 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Wed Dec 30 01:02:38 2020 +0000
+++ b/usr.bin/xlint/lint1/err.c Wed Dec 30 01:33:30 2020 +0000
@@ -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 @@
 }
 
 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;
diff -r 185c7b304c90 -r 146a2ffda9e4 usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Wed Dec 30 01:02:38 2020 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Wed Dec 30 01:33:30 2020 +0000
@@ -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     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
diff -r 185c7b304c90 -r 146a2ffda9e4 usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c        Wed Dec 30 01:02:38 2020 +0000
+++ b/usr.bin/xlint/lint1/init.c        Wed Dec 30 01:33:30 2020 +0000
@@ -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 @@
        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 @@
            !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 @@
                 * 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 @@
        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;
diff -r 185c7b304c90 -r 146a2ffda9e4 usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Wed Dec 30 01:02:38 2020 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Wed Dec 30 01:33:30 2020 +0000
@@ -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 @@
 
 #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



Home | Main Index | Thread Index | Old Index