* gdb_assert.h: New file.
[external/binutils.git] / gdb / gdb_assert.h
1 /* GDB-friendly replacement for <assert.h>.
2    Copyright 2000 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #ifndef GDB_ASSERT_H
22 #define GDB_ASSERT_H
23
24 #define gdb_assert(expr)                                                      \
25   ((void) ((expr) ? 0 :                                                       \
26            (gdb_assert_fail (#expr, __FILE__, __LINE__, ASSERT_FUNCTION), 0)))
27
28 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
29    which contains the name of the function currently being defined.
30    This is broken in G++ before version 2.6.
31    C9x has a similar variable called __func__, but prefer the GCC one since
32    it demangles C++ function names.  */
33 #if (GCC_VERSION >= 2004)
34 #define ASSERT_FUNCTION         __PRETTY_FUNCTION__
35 #else
36 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
37 #define ASSERT_FUNCTION         __func__
38 #else
39 #define ASSERT_FUNCTION         ((const char *) 0)
40 #endif
41 #endif
42
43 /* This prints an "Assertion failed" message, aksing the user if they
44    want to continue, dump core, or just exit.  */
45 #define gdb_assert_fail(assertion, file, line, function)                      \
46   internal_error ("%s:%u: %s%sAssertion `%s' failed.",                        \
47                   file, line,                                                 \
48                   function ? function : "", function ? ": " : "",             \
49                   assertion)
50
51 #endif /* gdb_assert.h */