From cb5f4605238016aec79af3a4272d6f1d0a770b02 Mon Sep 17 00:00:00 2001 From: Mike Barcroft Date: Sat, 27 Oct 2001 20:11:10 +0000 Subject: [PATCH] Only provide function information in compile environments that support the C99 variable __func__ and never for C++. Provide a more meaningful example in the assert(3) manual. Reviewed by: asmodai, bde --- lib/libc/gen/assert.c | 15 ++++++++++----- lib/libstand/assert.c | 11 +++++++---- share/man/man3/assert.3 | 18 +++++++++--------- sys/sys/cdefs.h | 5 +++++ 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/libc/gen/assert.c b/lib/libc/gen/assert.c index 9f265b71114..7cd4d0c95b7 100644 --- a/lib/libc/gen/assert.c +++ b/lib/libc/gen/assert.c @@ -45,14 +45,19 @@ __FBSDID("$FreeBSD$"); #include void -__assert(function, file, line, failedexpr) - const char *function, *file; +__assert(func, file, line, failedexpr) + const char *func, *file; int line; const char *failedexpr; { - (void)fprintf(stderr, - "assertion (%s) failed: function %s(), file %s:%d\n", - failedexpr, function, file, line); + if (func == NULL) + (void)fprintf(stderr, + "assertion (%s) failed: file %s:%d\n", failedexpr, + file, line); + else + (void)fprintf(stderr, + "assertion (%s) failed: function %s(), file %s:%d\n", + failedexpr, func, file, line); abort(); /* NOTREACHED */ } diff --git a/lib/libstand/assert.c b/lib/libstand/assert.c index 9317dcf2be9..6d0ab89fa18 100644 --- a/lib/libstand/assert.c +++ b/lib/libstand/assert.c @@ -32,10 +32,13 @@ __FBSDID("$FreeBSD$"); #include "stand.h" void -__assert(const char *function, const char *file, int line, - const char *expression) +__assert(const char *func, const char *file, int line, const char *expression) { - printf("assertion (%s) failed: function %s(), file %s:%d\n", - expression, function, file, line); + if (func == NULL) + printf("assertion (%s) failed: file %s:%d\n", expression, + file, line); + else + printf("assertion (%s) failed: function %s(), file %s:%d\n", + expression, func, file, line); exit(); } diff --git a/share/man/man3/assert.3 b/share/man/man3/assert.3 index bb233228264..ec74ae391c6 100644 --- a/share/man/man3/assert.3 +++ b/share/man/man3/assert.3 @@ -72,22 +72,22 @@ 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: +.Sh EXAMPLES +The assertion: .Bd -literal -offset indent -"assertion (%s) failed: function %s(), file %s:%d\en", \e - "expression", __func__, __FILE__, __LINE__); +assert(1 == 0); +.Ed +.Pp +generates a diagnostic message similar to the following: +.Bd -literal -offset indent +assertion (1 == 0) failed: function main(), file assertion.c:100 .Ed .Sh SEE ALSO .Xr abort 3 .Sh STANDARDS The .Fn assert -macro confirms to +macro conforms to .St -isoC-99 . .Sh HISTORY An diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h index 8705ca21390..c674f5a414e 100644 --- a/sys/sys/cdefs.h +++ b/sys/sys/cdefs.h @@ -48,6 +48,11 @@ #define __END_DECLS #endif +/* XXX: should have __STDC_VERSION__ < 199901 */ +#if !defined(__GNUC__) || defined(__cplusplus) +#define __func__ NULL +#endif + /* * The __CONCAT macro is used to concatenate parts of symbol names, e.g. * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.