1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/bzdebug.h Debugging macros |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
8 | * |
---|
9 | * This file is a part of Blitz. |
---|
10 | * |
---|
11 | * Blitz is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License |
---|
13 | * as published by the Free Software Foundation, either version 3 |
---|
14 | * of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * Blitz is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public |
---|
22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
26 | * |
---|
27 | * For more information, please see the Blitz++ Home Page: |
---|
28 | * https://sourceforge.net/projects/blitz/ |
---|
29 | * |
---|
30 | ***************************************************************************/ |
---|
31 | |
---|
32 | #ifndef BZ_DEBUG_H |
---|
33 | #define BZ_DEBUG_H |
---|
34 | |
---|
35 | #ifdef BZ_HAVE_STD |
---|
36 | #include <cstdlib> |
---|
37 | #include <cassert> |
---|
38 | #else |
---|
39 | #include <stdlib.h> |
---|
40 | #include <assert.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #ifdef BZ_HAVE_RTTI |
---|
44 | #include <typeinfo> |
---|
45 | #endif |
---|
46 | |
---|
47 | BZ_NAMESPACE(blitz) |
---|
48 | |
---|
49 | /* |
---|
50 | * These globals are used by the Blitz++ testsuite. The _bz_global |
---|
51 | * modifier ensures that they will reside in libblitz.a, but appear |
---|
52 | * "extern" elsewhere. |
---|
53 | */ |
---|
54 | |
---|
55 | _bz_global bool assertFailMode BZ_GLOBAL_INIT(false); |
---|
56 | _bz_global int assertFailCount BZ_GLOBAL_INIT(0); |
---|
57 | _bz_global int assertSuccessCount BZ_GLOBAL_INIT(0); |
---|
58 | |
---|
59 | |
---|
60 | #if defined(BZ_TESTSUITE) |
---|
61 | /* |
---|
62 | * In testsuite mode, these routines allow a test suite to check |
---|
63 | * that precondition checking is being done properly. A typical |
---|
64 | * use looks like this: |
---|
65 | * |
---|
66 | * beginCheckAssert(); |
---|
67 | * // Some operation which should cause an assert to fail |
---|
68 | * endCheckAssert(); |
---|
69 | * |
---|
70 | * The routine beginCheckAssert() sets a flag which results in |
---|
71 | * failed asserts being silently tallied. If no asserts have |
---|
72 | * failed by the time endCheckAssert() is invoked, the program |
---|
73 | * halts and issues an error code. |
---|
74 | * |
---|
75 | * In normal operation (i.e. when beginCheckAssert() has not |
---|
76 | * been called), failed preconditions will cause the program |
---|
77 | * to halt and issue an error code. -- TV 980226 |
---|
78 | */ |
---|
79 | |
---|
80 | inline void checkAssert(bool condition, const char* where=0, |
---|
81 | int line=0) |
---|
82 | { |
---|
83 | if (assertFailMode == true) |
---|
84 | { |
---|
85 | if (condition == true) |
---|
86 | ++assertSuccessCount; |
---|
87 | else |
---|
88 | ++assertFailCount; |
---|
89 | } |
---|
90 | else { |
---|
91 | if (!condition) |
---|
92 | { |
---|
93 | BZ_STD_SCOPE(cerr) << "Unexpected assert failure!" << BZ_STD_SCOPE(endl); |
---|
94 | if (where) |
---|
95 | BZ_STD_SCOPE(cerr) << where << ":" << line << BZ_STD_SCOPE(endl); |
---|
96 | BZ_STD_SCOPE(cerr).flush(); |
---|
97 | assert(0); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | inline void beginCheckAssert() |
---|
103 | { |
---|
104 | assertFailMode = true; |
---|
105 | assertSuccessCount = 0; |
---|
106 | assertFailCount = 0; |
---|
107 | } |
---|
108 | |
---|
109 | inline void endCheckAssert() |
---|
110 | { |
---|
111 | assert(assertFailMode == true); |
---|
112 | assertFailMode = false; |
---|
113 | if (assertFailCount == 0) |
---|
114 | { |
---|
115 | BZ_STD_SCOPE(cerr) << "Assert check failed!" << BZ_STD_SCOPE(endl); |
---|
116 | assert(0); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | #define BZASSERT(X) checkAssert(X, __FILE__, __LINE__) |
---|
121 | #define BZPRECONDITION(X) checkAssert(X, __FILE__, __LINE__) |
---|
122 | #define BZPOSTCONDITION(X) checkAssert(X, __FILE__, __LINE__) |
---|
123 | #define BZSTATECHECK(X,Y) checkAssert(X == Y, __FILE__, __LINE__) |
---|
124 | #define BZPRECHECK(X,Y) \ |
---|
125 | { \ |
---|
126 | if ((assertFailMode == false) && (!(X))) \ |
---|
127 | BZ_STD_SCOPE(cerr) << Y << BZ_STD_SCOPE(endl); \ |
---|
128 | checkAssert(X, __FILE__, __LINE__); \ |
---|
129 | } |
---|
130 | |
---|
131 | #define BZ_DEBUG_MESSAGE(X) \ |
---|
132 | { \ |
---|
133 | if (assertFailMode == false) \ |
---|
134 | { \ |
---|
135 | BZ_STD_SCOPE(cout) << __FILE__ << ":" << __LINE__ << " " \ |
---|
136 | << X << BZ_STD_SCOPE(endl); \ |
---|
137 | } \ |
---|
138 | } |
---|
139 | |
---|
140 | #define BZ_DEBUG_PARAM(X) X |
---|
141 | #define BZ_PRE_FAIL checkAssert(0) |
---|
142 | #define BZ_ASM_DEBUG_MARKER |
---|
143 | |
---|
144 | #elif defined(BZ_DEBUG) |
---|
145 | |
---|
146 | #define BZASSERT(X) assert(X) |
---|
147 | #define BZPRECONDITION(X) assert(X) |
---|
148 | #define BZPOSTCONDITION(X) assert(X) |
---|
149 | #define BZSTATECHECK(X,Y) assert(X == Y) |
---|
150 | #define BZPRECHECK(X,Y) \ |
---|
151 | { if (!(X)) \ |
---|
152 | { BZ_STD_SCOPE(cerr) << "[Blitz++] Precondition failure: Module " \ |
---|
153 | << __FILE__ \ |
---|
154 | << " line " << __LINE__ << BZ_STD_SCOPE(endl) \ |
---|
155 | << Y << BZ_STD_SCOPE(endl); \ |
---|
156 | BZ_STD_SCOPE(cerr).flush(); \ |
---|
157 | assert(0); \ |
---|
158 | } \ |
---|
159 | } |
---|
160 | |
---|
161 | #define BZ_DEBUG_MESSAGE(X) \ |
---|
162 | { BZ_STD_SCOPE(cout) << __FILE__ << ":" << __LINE__ << " " \ |
---|
163 | << X << BZ_STD_SCOPE(endl); } |
---|
164 | |
---|
165 | #define BZ_DEBUG_PARAM(X) X |
---|
166 | #define BZ_PRE_FAIL assert(0) |
---|
167 | |
---|
168 | // This routine doesn't exist anywhere; it's used to mark a |
---|
169 | // position of interest in assembler (.s) files |
---|
170 | void _bz_debug_marker(); |
---|
171 | #define BZ_ASM_DEBUG_MARKER _bz_debug_marker(); |
---|
172 | |
---|
173 | #else // !BZ_TESTSUITE && !BZ_DEBUG |
---|
174 | |
---|
175 | #define BZASSERT(X) |
---|
176 | #define BZPRECONDITION(X) |
---|
177 | #define BZPOSTCONDITION(X) |
---|
178 | #define BZSTATECHECK(X,Y) |
---|
179 | #define BZPRECHECK(X,Y) |
---|
180 | #define BZ_DEBUG_MESSAGE(X) |
---|
181 | #define BZ_DEBUG_PARAM(X) |
---|
182 | #define BZ_PRE_FAIL |
---|
183 | #define BZ_ASM_DEBUG_MARKER |
---|
184 | |
---|
185 | #endif // !BZ_TESTSUITE && !BZ_DEBUG |
---|
186 | |
---|
187 | #define BZ_NOT_IMPLEMENTED() \ |
---|
188 | { BZ_STD_SCOPE(cerr) << "[Blitz++] Not implemented: module " \ |
---|
189 | << __FILE__ << " line " << __LINE__ << BZ_STD_SCOPE(endl); \ |
---|
190 | BZ_STD_SCOPE(exit)(1); } |
---|
191 | |
---|
192 | #ifdef BZ_HAVE_RTTI |
---|
193 | #define BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(X) typeid(X).name() |
---|
194 | #else |
---|
195 | |
---|
196 | template<typename T> |
---|
197 | class _bz_stringLiteralForNumericType { |
---|
198 | public: |
---|
199 | static const char* string() |
---|
200 | { return "unknown"; } |
---|
201 | }; |
---|
202 | |
---|
203 | #define BZ_DECL_SLFNT(X,Y) \ |
---|
204 | template<> \ |
---|
205 | class _bz_stringLiteralForNumericType< X > { \ |
---|
206 | public: \ |
---|
207 | static const char* string() \ |
---|
208 | { return Y; } \ |
---|
209 | } |
---|
210 | |
---|
211 | #ifdef BZ_HAVE_BOOL |
---|
212 | BZ_DECL_SLFNT(bool, "bool"); |
---|
213 | #endif |
---|
214 | |
---|
215 | BZ_DECL_SLFNT(char, "char"); |
---|
216 | BZ_DECL_SLFNT(unsigned char, "unsigned char"); |
---|
217 | BZ_DECL_SLFNT(short int, "short int"); |
---|
218 | BZ_DECL_SLFNT(short unsigned int, "short unsigned int"); |
---|
219 | BZ_DECL_SLFNT(int, "int"); |
---|
220 | BZ_DECL_SLFNT(unsigned int, "unsigned int"); |
---|
221 | BZ_DECL_SLFNT(long, "long"); |
---|
222 | BZ_DECL_SLFNT(unsigned long, "unsigned long"); |
---|
223 | BZ_DECL_SLFNT(float, "float"); |
---|
224 | BZ_DECL_SLFNT(double, "double"); |
---|
225 | BZ_DECL_SLFNT(long double, "long double"); |
---|
226 | |
---|
227 | #ifdef BZ_HAVE_COMPLEX |
---|
228 | BZ_DECL_SLFNT(complex<float>, "complex<float>"); |
---|
229 | BZ_DECL_SLFNT(complex<double>, "complex<double>"); |
---|
230 | BZ_DECL_SLFNT(complex<long double>, "complex<long double>"); |
---|
231 | #endif |
---|
232 | |
---|
233 | #define BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(X) \ |
---|
234 | _bz_stringLiteralForNumericType<X>::string() |
---|
235 | |
---|
236 | #endif // !BZ_HAVE_RTTI |
---|
237 | |
---|
238 | BZ_NAMESPACE_END |
---|
239 | |
---|
240 | #endif // BZ_DEBUG_H |
---|