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
This commit is contained in:
mike 2001-10-27 20:11:10 +00:00
parent b37309f764
commit 186071821a
4 changed files with 31 additions and 18 deletions

View File

@ -45,14 +45,19 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
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 */
}

View File

@ -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();
}

View File

@ -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

View File

@ -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.