From: Peter Avalos Date: Fri, 13 Feb 2009 18:43:06 +0000 (-0500) Subject: Sync assert with FreeBSD: X-Git-Tag: v2.3.1~152^2~50 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/f7eaf18313812d4246336bea70f8710f6bb48208 Sync assert with FreeBSD: * Change __assert() function to print failing function name. * Make the output from assert() look more like the example in the C99 standard. --- diff --git a/include/assert.h b/include/assert.h index 1d48d0472d..dbc2ecbff4 100644 --- a/include/assert.h +++ b/include/assert.h @@ -36,9 +36,12 @@ * SUCH DAMAGE. * * @(#)assert.h 8.2 (Berkeley) 1/21/94 + * $FreeBSD: src/include/assert.h,v 1.6 2007/12/01 19:28:13 phk Exp $ * $DragonFly: src/include/assert.h,v 1.4 2005/04/26 10:41:57 joerg Exp $ */ +#include + /* * Unlike other ANSI header files, may usefully be included * multiple times, with and without NDEBUG defined. @@ -52,19 +55,23 @@ #define _assert(e) ((void)0) #else #define _assert(e) assert(e) -#define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e)) -#endif + +#define assert(e) ((e) ? (void)0 : __assert(__func__, __FILE__, \ + __LINE__, #e)) +#endif /* NDEBUG */ #undef _DIAGASSERT #ifdef _DIAGNOSTIC -#define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, __func__, #e)) +#define _DIAGASSERT(e) ((e) ? (void)0 : __diagassert(__FILE__, __LINE__, \ + __func__, #e)) #else #define _DIAGASSERT(e) ((void)0) #endif -#include - +#ifndef _ASSERT_H_ +#define _ASSERT_H_ __BEGIN_DECLS -void __assert(const char *, int, const char *); +void __assert(const char *, const char *, int, const char *); void __diagassert(const char *, int, const char *, const char *); __END_DECLS +#endif /* !_ASSERT_H_ */ diff --git a/lib/libc/gen/assert.c b/lib/libc/gen/assert.c index 88da83ba36..63d63ea2e2 100644 --- a/lib/libc/gen/assert.c +++ b/lib/libc/gen/assert.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,6 +27,7 @@ * SUCH DAMAGE. * * @(#)assert.c 8.1 (Berkeley) 6/4/93 + * $FreeBSD: src/lib/libc/gen/assert.c,v 1.8 2007/01/09 00:27:53 imp Exp $ * $DragonFly: src/lib/libc/gen/assert.c,v 1.5 2005/04/26 10:41:57 joerg Exp $ */ @@ -40,10 +37,17 @@ #include void -__assert(const char *file, int line, const char *failedexpr) +__assert(const char *func, const char *file, int line, const char *failedexpr) { - fprintf(stderr, "assertion \"%s\" failed: file \"%s\", line %d\n", - failedexpr, file, line); + if (func == NULL) { + fprintf(stderr, + "Assertion failed: (%s), file %s, line %d.\n", failedexpr, + file, line); + } else { + fprintf(stderr, + "Assertion failed: (%s), function %s, file %s, line %d.\n", + failedexpr, func, file, line); + } abort(); /* NOTREACHED */ } diff --git a/lib/libstand/assert.c b/lib/libstand/assert.c index 78c06574fb..24a342156c 100644 --- a/lib/libstand/assert.c +++ b/lib/libstand/assert.c @@ -23,19 +23,25 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libstand/assert.c,v 1.2 1999/08/28 00:05:29 peter Exp $ + * $FreeBSD: src/lib/libstand/assert.c,v 1.7 2001/10/29 07:07:25 mike Exp $ * $DragonFly: src/lib/libstand/assert.c,v 1.3 2004/01/23 14:43:52 joerg Exp $ */ -#include #include +#include "stand.h" + void exit(int code); void -__assert(const char *file, int line, const char *expression) +__assert(const char *func, const char *file, int line, const char *expression) { - printf("assertion \"%s\" failed: file \"%s\", line %d\n", expression, - file, line); + if (func == NULL) { + printf("Assertion failed: (%s), file %s, line %d.\n", + expression, file, line); + } else { + printf("Assertion failed: (%s), function %s, file %s, line " + "%d.\n", expression, func, file, line); + } exit(1); } diff --git a/share/man/man3/assert.3 b/share/man/man3/assert.3 index 5057c73c3b..3d6fc211e9 100644 --- a/share/man/man3/assert.3 +++ b/share/man/man3/assert.3 @@ -30,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)assert.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/share/man/man3/assert.3,v 1.7.2.3 2001/12/17 11:30:11 ru Exp $ +.\" $FreeBSD: src/share/man/man3/assert.3,v 1.15 2002/12/04 18:57:46 ru Exp $ .\" $DragonFly: src/share/man/man3/assert.3,v 1.2 2003/06/17 04:36:58 dillon Exp $ .\" .Dd January 26, 1999 @@ -51,7 +51,7 @@ and if it is false, the calling process is terminated. A diagnostic message is written to -.Em stderr +.Dv stderr and the function .Xr abort 3 is called, effectively terminating the program. @@ -73,18 +73,21 @@ as a macro .Xr cc 1 option .Fl D Ns Dv NDEBUG ) . -.Sh DIAGNOSTICS -The following diagnostic message is written to -.Em stderr -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__); -.Ed +.Sh EXAMPLES +The assertion: +.Pp +.Dl "assert(1 == 0);" +.Pp +generates a diagnostic message similar to the following: +.Pp +.Dl "Assertion failed: (1 == 0), function main, file assertion.c, line 100." .Sh SEE ALSO .Xr abort 3 +.Sh STANDARDS +The +.Fn assert +macro conforms to +.St -isoC-99 . .Sh HISTORY An .Nm