C99 (and POSIX) require assert to include the __func__ magic macro as part of
the produced assertion message. OK to apply? Cygwin will need a followup
patch to export __assert_func, but must continue exporting __assert for apps
compiled prior to this change.
2007-06-26 Eric Blake <ebb9@byu.net>
Support __func__ in assert, as required by C99.
* libc/stdlib/assert.c (__assert_func): New function.
(__assert): Adjust, but keep for backwards-compatibility.
* libc/include/assert.h (assert) [!NDEBUG]: Use __assert_func.
Index: libc/stdlib/assert.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/assert.c,v
retrieving revision 1.2
diff -u -p -r1.2 assert.c
--- libc/stdlib/assert.c 28 Oct 2005 21:21:07 -0000 1.2
+++ libc/stdlib/assert.c 26 Jun 2007 16:18:55 -0000
@@ -9,11 +9,6 @@ ANSI_SYNOPSIS
#include <assert.h>
void assert(int <[expression]>);
-TRAD_SYNOPSIS
- #include <assert.h>
- assert(<[expression]>)
- int <[expression]>;
-
DESCRIPTION
Use this macro to embed debuggging diagnostic statements in
your programs. The argument <[expression]> should be an
@@ -24,7 +19,7 @@ DESCRIPTION
calls <<abort>>, after first printing a message showing what
failed and where:
-. Assertion failed: <[expression]>, file <[filename]>, line <[lineno]>
+. Assertion failed: <[expression]>, file <[filename]>, line <[lineno]>,
function <[func]>
The macro is defined to permit you to turn off all uses of
<<assert>> at compile time by defining <<NDEBUG>> as a
@@ -49,14 +44,26 @@ Supporting OS subroutines required (only
#include <stdio.h>
void
-_DEFUN (__assert, (file, line, failedexpr),
+_DEFUN (__assert_func, (file, line, func, failedexpr),
const char *file _AND
int line _AND
+ const char *func _AND
const char *failedexpr)
{
- (void)fiprintf(stderr,
- "assertion \"%s\" failed: file \"%s\", line %d\n",
- failedexpr, file, line);
+ fiprintf(stderr,
+ "assertion \"%s\" failed: file \"%s\", line %d, function %s\n",
+ failedexpr, file, line, func);
abort();
/* NOTREACHED */
}
+
+/* Provided for backwards compatibility. Do not expose in headers. */
+void
+_DEFUN (__assert, (file, line, failedexpr),
+ const char *file _AND
+ int line _AND
+ const char *failedexpr)
+{
+ __assert_func (file, line, "", failedexpr);
+ /* NOTREACHED */
+}
Index: libc/include/assert.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/assert.h,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 assert.h
--- libc/include/assert.h 17 Feb 2000 19:39:46 -0000 1.1.1.1
+++ libc/include/assert.h 26 Jun 2007 16:18:55 -0000
@@ -11,18 +11,24 @@ extern "C" {
#undef assert
#ifdef NDEBUG /* required by ANSI standard */
-#define assert(p) ((void)0)
+# define assert(p) ((void)0)
#else
+# define assert(e) ((e) ? (void)0 : __assert_func (__FILE__, __LINE__, \
+ __ASSERT_FUNC, #e))
+#endif /* NDEBUG */
-#ifdef __STDC__
-#define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
-#else /* PCC */
-#define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, "e"))
+#ifdef __cplusplus && defined __GNUC__
+ /* Use g++'s demangled names. */
+# define __ASSERT_FUNC __PRETTY_FUNCTION__
+#elif __cplusplus || __STDC_VERSION__ < 199901L
+ /* C++03 and C89 do not have __func__. */
+# define __ASSERT_FUNC ""
+#else /* C99 compatible */
+# define __ASSERT_FUNC __func__
#endif
-#endif /* NDEBUG */
-
-void _EXFUN(__assert,(const char *, int, const char *));
+void _EXFUN(__assert_func,(const char *, int, const char *, const char *)
+ _ATTRIBUTE ((__noreturn__)));
#ifdef __cplusplus
}