diff options
| author | kleink <kleink@NetBSD.org> | 2000-12-19 14:32:59 +0000 |
|---|---|---|
| committer | kleink <kleink@NetBSD.org> | 2000-12-19 14:32:59 +0000 |
| commit | f02540ff10b332b8ef5ad9e89ded894e39d68bec (patch) | |
| tree | 775e107e82bd688c68133f74df9dd03a49eb1db5 | |
| parent | d615c85a36834952f43c49ca587cb3d530cd8189 (diff) | |
C99: Print the name of the function enclosing the assertion, if possible.
| -rw-r--r-- | include/assert.h | 28 | ||||
| -rw-r--r-- | lib/libc/gen/assert.c | 42 | ||||
| -rw-r--r-- | share/man/man3/assert.3 | 19 |
3 files changed, 75 insertions, 14 deletions
diff --git a/include/assert.h b/include/assert.h index 125b21a034b..f8c6e7bd2d6 100644 --- a/include/assert.h +++ b/include/assert.h @@ -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 --git a/lib/libc/gen/assert.c b/lib/libc/gen/assert.c index b757de05aa1..ff250c82a35 100644 --- a/lib/libc/gen/assert.c +++ b/lib/libc/gen/assert.c @@ -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 @@ __RCSID("$NetBSD: assert.c,v 1.9 2000/12/10 03:59:00 christos Exp $"); 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 @@ __assert(file, line, failedexpr) } 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 --git a/share/man/man3/assert.3 b/share/man/man3/assert.3 index af51048f4d3..f0a9d4cfcb2 100644 --- a/share/man/man3/assert.3 +++ b/share/man/man3/assert.3 @@ -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 @@ macro tests the given .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 @@ if .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 @@ is false: 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 . |
