[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[groff] 02/02: Make our assert() C99-conformant.
From: |
G. Branden Robinson |
Subject: |
[groff] 02/02: Make our assert() C99-conformant. |
Date: |
Mon, 13 Apr 2020 04:01:14 -0400 (EDT) |
gbranden pushed a commit to branch master
in repository groff.
commit 642353ef06b3c2b49f74b71e0d5ecdfa866fe3de
Author: G. Branden Robinson <address@hidden>
AuthorDate: Mon Apr 13 17:42:18 2020 +1000
Make our assert() C99-conformant.
groff has its own implementation of the standard C library's assert()
macro. It hasn't been updated since C89. C99 requires that the
diagnostic emitted by assert() contain the failing expression and name
of the function in scope.
* src/include/assert.h: Add additional pointer to const char arguments
to do_assert() and assertion_failed() for function name and
stringified expression.
(assertion_failed): Update prototype.
(do_assert): Accept 'func' and 'msg' parameters and pass them to
assertion_failed().
(assert): Update macro to collect '__func__' and stringify the expr
parameter (as 'msg') and pass them do do_assert().
* src/libs/libgroff/assert.cpp (assertion_failed): Rewrite diagnostic to
more closely match GNU Coding Style format and also report function
and failing expression.
Example output:
troff: ../src/roff/troff/input.cpp:2644: do_request(): assertion
failed: '0 == "But first, here's a rotten old BBC programme."'
We have no excuse to assert(0) ever again. Express the invariant that
has been violated.
---
ChangeLog | 29 +++++++++++++++++++++++++++++
src/include/assert.h | 10 ++++++----
src/libs/libgroff/assert.cpp | 7 ++++---
3 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 6b93e50..00fe35f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,34 @@
2020-04-13 G. Branden Robinson <address@hidden>
+ Make our assert() C99-conformant.
+
+ groff has its own implementation of the standard C library's
+ assert() macro. It hasn't been updated since C89. C99 requires
+ that the diagnostic emitted by assert() contain the failing
+ expression and name of the function in scope.
+
+ * src/include/assert.h: Add additional pointer to const char
+ arguments to do_assert() and assertion_failed() for function
+ name and stringified expression.
+ (assertion_failed): Update prototype.
+ (do_assert): Accept 'func' and 'msg' parameters and pass them to
+ assertion_failed().
+ (assert): Update macro to collect '__func__' and stringify the
+ expr parameter (as 'msg') and pass them do do_assert().
+ * src/libs/libgroff/assert.cpp (assertion_failed): Rewrite
+ diagnostic to more closely match GNU Coding Style format and
+ also report function and failing expression.
+
+ Example output:
+ troff: ../src/roff/troff/input.cpp:2644: do_request():
+ assertion failed: '0 == "But first, here's a rotten old
+ BBC programme."'
+
+ We have no excuse to assert(0) ever again. Express the
+ invariant that has been violated.
+
+2020-04-13 G. Branden Robinson <address@hidden>
+
* src/roff/groff/tests/smoke-test_html_device.sh: Set
LC_CTYPE=C.UTF-8 so that byte sequences in the pipelines are
handled correctly. Thanks to Bjarni Ingi Gislason for the
diff --git a/src/include/assert.h b/src/include/assert.h
index d717c98..581cf88 100644
--- a/src/include/assert.h
+++ b/src/include/assert.h
@@ -20,12 +20,13 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
#ifndef ASSERT_H
#define ASSERT_H
-void assertion_failed(int, const char *);
+void assertion_failed(int, const char *, const char *, const char *);
-inline void do_assert(int expr, int line, const char *file)
+inline void do_assert(int expr, int line, const char *file,
+ const char *func, const char *msg)
{
if (!expr)
- assertion_failed(line, file);
+ assertion_failed(line, file, func, msg);
}
#endif /* ASSERT_H */
@@ -34,5 +35,6 @@ inline void do_assert(int expr, int line, const char *file)
#ifdef NDEBUG
#define assert(ignore) /* as nothing */
#else
-#define assert(expr) do_assert(expr, __LINE__, __FILE__)
+#define assert(expr) do_assert(expr, __LINE__, __FILE__, __func__, \
+ #expr)
#endif
diff --git a/src/libs/libgroff/assert.cpp b/src/libs/libgroff/assert.cpp
index aceed05..80a9946 100644
--- a/src/libs/libgroff/assert.cpp
+++ b/src/libs/libgroff/assert.cpp
@@ -22,12 +22,13 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
extern "C" const char *program_name;
-void assertion_failed(int lineno, const char *filename)
+void assertion_failed(int lineno, const char *filename,
+ const char *function, const char *msg)
{
if (program_name != 0)
fprintf(stderr, "%s: ", program_name);
- fprintf(stderr, "Failed assertion at line %d, file '%s'.\n",
- lineno, filename);
+ fprintf(stderr, "%s:%d: %s(): assertion failed: '%s'\n", filename,
+ lineno, function, msg);
fflush(stderr);
abort();
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [groff] 02/02: Make our assert() C99-conformant.,
G. Branden Robinson <=