Source-Changes-HG archive

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

[src/trunk]: src C99: Print the name of the function enclosing the assertion, ...



details:   https://anonhg.NetBSD.org/src/rev/ba0227711c8e
branches:  trunk
changeset: 500845:ba0227711c8e
user:      kleink <kleink%NetBSD.org@localhost>
date:      Tue Dec 19 14:32:59 2000 +0000

description:
C99: Print the name of the function enclosing the assertion, if possible.

diffstat:

 include/assert.h        |  28 +++++++++++++++++++++++-----
 lib/libc/gen/assert.c   |  42 ++++++++++++++++++++++++++++++++++++++++--
 share/man/man3/assert.3 |  19 ++++++++++++-------
 3 files changed, 75 insertions(+), 14 deletions(-)

diffs (184 lines):

diff -r 994be9798309 -r ba0227711c8e include/assert.h
--- a/include/assert.h  Tue Dec 19 14:28:40 2000 +0000
+++ b/include/assert.h  Tue Dec 19 14:32:59 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: assert.h,v 1.9 2000/08/07 16:21:32 kleink Exp $        */
+/*     $NetBSD: assert.h,v 1.10 2000/12/19 14:32:59 kleink Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -59,9 +59,13 @@
 #else /* !NDEBUG */
 # define _assert(e)    assert(e)
 # if __STDC__
-#  define assert(e)    ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
+#  define assert(e)                                                    \
+       ((e) ? (void)0 : __assert13(__FILE__, __LINE__,                 \
+                                   __assert_function__, #e))
 # else /* PCC */
-#  define assert(e)    ((e) ? (void)0 : __assert(__FILE__, __LINE__, "e"))
+#  define assert(e)                                                    \
+       ((e) ? (void)0 : __assert13(__FILE__, __LINE__,                 \
+                                   __assert_function__, "e"))
 # endif /* !__STDC__ */
 #endif /* NDEBUG */
 
@@ -74,16 +78,30 @@
 # endif /* lint */
 #else /* _DIAGNOSTIC */
 # if __STDC__
-#  define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, #e))
+#  define _DIAGASSERT(e)                                               \
+       ((e) ? (void)0 : __diagassert13(__FILE__, __LINE__,             \
+                                       __assert_function__, #e))
 # else /* !__STDC__ */
-#  define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, "e"))
+#  define _DIAGASSERT(e)                                               \
+       ((e) ? (void)0 : __diagassert13(__FILE__, __LINE__,             \
+                                       __assert_function__, "e"))
 # endif
 #endif /* _DIAGNOSTIC */
 
 
 #include <sys/cdefs.h>
 
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+#define        __assert_function__     __func__
+#elif __GNUC_PREREQ__(2, 6)
+#define        __assert_function__     __PRETTY_FUNCTION__
+#else
+#define        __assert_function__     ((const void *)0)
+#endif
+
 __BEGIN_DECLS
 void __assert __P((const char *, int, const char *));
+void __assert13 __P((const char *, int, const char *, const char *));
 void __diagassert __P((const char *, int, const char *));
+void __diagassert13 __P((const char *, int, const char *, const char *));
 __END_DECLS
diff -r 994be9798309 -r ba0227711c8e lib/libc/gen/assert.c
--- a/lib/libc/gen/assert.c     Tue Dec 19 14:28:40 2000 +0000
+++ b/lib/libc/gen/assert.c     Tue Dec 19 14:32:59 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: assert.c,v 1.9 2000/12/10 03:59:00 christos Exp $      */
+/*     $NetBSD: assert.c,v 1.10 2000/12/19 14:32:59 kleink Exp $       */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)assert.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: assert.c,v 1.9 2000/12/10 03:59:00 christos Exp $");
+__RCSID("$NetBSD: assert.c,v 1.10 2000/12/19 14:32:59 kleink Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -52,6 +52,21 @@
 extern char *__progname;
 
 void
+__assert13(file, line, function, failedexpr)
+       const char *file, *function, *failedexpr;
+       int line;
+{
+       (void)fprintf(stderr,
+           "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
+           failedexpr, file, line,
+           function ? ", function \"" : "",
+           function ? function : "",
+           function ? "\"" : "");
+       abort();
+       /* NOTREACHED */
+}
+
+void
 __assert(file, line, failedexpr)
        const char *file, *failedexpr;
        int line;
@@ -64,6 +79,29 @@
 }
 
 void
+__diagassert13(file, line, function, failedexpr)
+       const char *file, *function, *failedexpr;
+       int line;
+{
+               /*
+                * XXX: check $DIAGASSERT here, and do user-defined actions
+                */
+       (void)fprintf(stderr,
+        "%s: assertion \"%s\" failed: file \"%s\", line %d%s%s\n",
+            __progname, failedexpr, file, line,
+           function ? ", function \"" : "",
+           function ? function : "",
+           function ? "\"" : "");
+       syslog(LOG_DEBUG|LOG_USER,
+           "assertion \"%s\" failed: file \"%s\", line %d%s%s%s",
+           failedexpr, file, line,
+           function ? ", function \"" : "",
+           function ? function : "",
+           function ? "\"" : "");
+       return;
+}
+
+void
 __diagassert(file, line, failedexpr)
        const char *file, *failedexpr;
        int line;
diff -r 994be9798309 -r ba0227711c8e share/man/man3/assert.3
--- a/share/man/man3/assert.3   Tue Dec 19 14:28:40 2000 +0000
+++ b/share/man/man3/assert.3   Tue Dec 19 14:32:59 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: assert.3,v 1.5 1994/11/30 15:24:30 jtc Exp $
+.\"    $NetBSD: assert.3,v 1.6 2000/12/19 14:33:00 kleink Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -33,7 +33,7 @@
 .\"
 .\"     @(#)assert.3   8.1 (Berkeley) 6/9/93
 .\"
-.Dd June 9, 1993
+.Dd December 17, 2000
 .Dt ASSERT 3
 .Os
 .Sh NAME
@@ -49,8 +49,10 @@
 .Ar expression
 and if it is false,
 the calling process is terminated.
-A
-diagnostic message is written to
+A diagnostic message, consisting of the text of the expression,
+the name of the source file, the line number and the enclosing
+function,
+is written to
 .Em stderr
 and the
 .Xr abort 3
@@ -78,8 +80,8 @@
 .Ar expression
 is false:
 .Bd -literal -offset indent
-"assertion \e"%s\e" failed: file \e"%s\e", line %d\en", \e
-                   "expression", __FILE__, __LINE__);
+"assertion \e"%s\e" failed: file \e"%s\e", line %d, function \e"%s\e"\en", \e
+                   "expression", __FILE__, __LINE__, __func__);
 .Ed
 .Sh SEE ALSO
 .Xr cc 1 ,
@@ -88,9 +90,12 @@
 The
 .Fn assert
 macro conforms to
-.St -ansiC .
+.St -isoC99 .
 .Sh HISTORY
 A
 .Nm assert
 macro appeared in
 .At v6 .
+.Pp
+Information on the name of the enclosing function appeared in
+.St -isoC99 .



Home | Main Index | Thread Index | Old Index