xref: /dflybsd-src/contrib/gdb-7/gdb/common/gdb_assert.h (revision de8e141f24382815c10a4012d209bbbf7abf1112)
1a45ae5f8SJohn Marino /* GDB-friendly replacement for <assert.h>.
2*ef5ccd6cSJohn Marino    Copyright (C) 2000-2013 Free Software Foundation, Inc.
3a45ae5f8SJohn Marino 
4a45ae5f8SJohn Marino    This file is part of GDB.
5a45ae5f8SJohn Marino 
6a45ae5f8SJohn Marino    This program is free software; you can redistribute it and/or modify
7a45ae5f8SJohn Marino    it under the terms of the GNU General Public License as published by
8a45ae5f8SJohn Marino    the Free Software Foundation; either version 3 of the License, or
9a45ae5f8SJohn Marino    (at your option) any later version.
10a45ae5f8SJohn Marino 
11a45ae5f8SJohn Marino    This program is distributed in the hope that it will be useful,
12a45ae5f8SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13a45ae5f8SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14a45ae5f8SJohn Marino    GNU General Public License for more details.
15a45ae5f8SJohn Marino 
16a45ae5f8SJohn Marino    You should have received a copy of the GNU General Public License
17a45ae5f8SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18a45ae5f8SJohn Marino 
19a45ae5f8SJohn Marino #ifndef GDB_ASSERT_H
20a45ae5f8SJohn Marino #define GDB_ASSERT_H
21a45ae5f8SJohn Marino 
22a45ae5f8SJohn Marino /* A static assertion.  This will cause a compile-time error if EXPR,
23a45ae5f8SJohn Marino    which must be a compile-time constant, is false.  */
24a45ae5f8SJohn Marino 
25*ef5ccd6cSJohn Marino #define gdb_static_assert(expr) \
26a45ae5f8SJohn Marino   extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]
27a45ae5f8SJohn Marino 
28a45ae5f8SJohn Marino /* PRAGMATICS: "gdb_assert.h":gdb_assert() is a lower case (rather
29a45ae5f8SJohn Marino    than upper case) macro since that provides the closest fit to the
30a45ae5f8SJohn Marino    existing lower case macro <assert.h>:assert() that it is
31a45ae5f8SJohn Marino    replacing.  */
32a45ae5f8SJohn Marino 
33a45ae5f8SJohn Marino #define gdb_assert(expr)                                                      \
34a45ae5f8SJohn Marino   ((void) ((expr) ? 0 :                                                       \
35a45ae5f8SJohn Marino 	   (gdb_assert_fail (#expr, __FILE__, __LINE__, ASSERT_FUNCTION), 0)))
36a45ae5f8SJohn Marino 
37a45ae5f8SJohn Marino /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
38a45ae5f8SJohn Marino    which contains the name of the function currently being defined.
39a45ae5f8SJohn Marino    This is broken in G++ before version 2.6.
40a45ae5f8SJohn Marino    C9x has a similar variable called __func__, but prefer the GCC one since
41a45ae5f8SJohn Marino    it demangles C++ function names.  */
42a45ae5f8SJohn Marino #if (GCC_VERSION >= 2004)
43a45ae5f8SJohn Marino #define ASSERT_FUNCTION		__PRETTY_FUNCTION__
44a45ae5f8SJohn Marino #else
45a45ae5f8SJohn Marino #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
46a45ae5f8SJohn Marino #define ASSERT_FUNCTION		__func__
47a45ae5f8SJohn Marino #endif
48a45ae5f8SJohn Marino #endif
49a45ae5f8SJohn Marino 
50a45ae5f8SJohn Marino /* This prints an "Assertion failed" message, asking the user if they
51a45ae5f8SJohn Marino    want to continue, dump core, or just exit.  */
52a45ae5f8SJohn Marino #if defined (ASSERT_FUNCTION)
53a45ae5f8SJohn Marino #define gdb_assert_fail(assertion, file, line, function)                      \
54a45ae5f8SJohn Marino   internal_error (file, line, _("%s: Assertion `%s' failed."),                \
55a45ae5f8SJohn Marino 		  function, assertion)
56a45ae5f8SJohn Marino #else
57a45ae5f8SJohn Marino #define gdb_assert_fail(assertion, file, line, function)                      \
58a45ae5f8SJohn Marino   internal_error (file, line, _("Assertion `%s' failed."),                    \
59a45ae5f8SJohn Marino 		  assertion)
60a45ae5f8SJohn Marino #endif
61a45ae5f8SJohn Marino 
62a45ae5f8SJohn Marino /* The canonical form of gdb_assert (0).
63a45ae5f8SJohn Marino    MESSAGE is a string to include in the error message.  */
64a45ae5f8SJohn Marino 
65a45ae5f8SJohn Marino #if defined (ASSERT_FUNCTION)
66a45ae5f8SJohn Marino #define gdb_assert_not_reached(message) \
67a45ae5f8SJohn Marino   internal_error (__FILE__, __LINE__, "%s: %s", ASSERT_FUNCTION, _(message))
68a45ae5f8SJohn Marino #else
69a45ae5f8SJohn Marino #define gdb_assert_not_reached(message) \
70a45ae5f8SJohn Marino   internal_error (__FILE__, __LINE__, _(message))
71a45ae5f8SJohn Marino #endif
72a45ae5f8SJohn Marino 
73a45ae5f8SJohn Marino #endif /* gdb_assert.h */
74