]>
Commit | Line | Data |
---|---|---|
993df040 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/debug.h | |
3 | // Purpose: Misc debug functions and macros | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 29/01/98 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 1998-2009 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
e0c749a7 | 10 | |
993df040 VZ |
11 | #ifndef _WX_DEBUG_H_ |
12 | #define _WX_DEBUG_H_ | |
c801d85f | 13 | |
993df040 VZ |
14 | #if !defined(__WXPALMOS5__) && !defined(__WXWINCE__) |
15 | #include <assert.h> | |
16 | #endif // systems without assert.h | |
c801d85f | 17 | |
993df040 | 18 | #include <limits.h> // for CHAR_BIT used below |
c801d85f | 19 | |
993df040 | 20 | #include "wx/chartype.h" // for __TFILE__ and wxChar |
a6ebdba6 | 21 | #include "wx/cpp.h" // for __WXFUNCTION__ |
9e3d3318 | 22 | |
993df040 VZ |
23 | // ---------------------------------------------------------------------------- |
24 | // Defines controlling the debugging macros | |
25 | // ---------------------------------------------------------------------------- | |
8b0bd21b | 26 | |
993df040 VZ |
27 | // if _DEBUG is defined (MS VC++ and others use it in debug builds), define |
28 | // __WXDEBUG__ too | |
8b0bd21b VZ |
29 | #ifdef _DEBUG |
30 | #ifndef __WXDEBUG__ | |
31 | #define __WXDEBUG__ | |
993df040 VZ |
32 | #endif // !__WXDEBUG__ |
33 | #endif // _DEBUG | |
8b0bd21b | 34 | |
993df040 | 35 | // if NDEBUG is defined (<assert.h> uses it), undef __WXDEBUG__ and WXDEBUG |
8b0bd21b VZ |
36 | #ifdef NDEBUG |
37 | #undef __WXDEBUG__ | |
38 | #undef WXDEBUG | |
993df040 | 39 | #endif // NDEBUG |
8b0bd21b | 40 | |
993df040 | 41 | // if __WXDEBUG__ is defined, make sure that WXDEBUG is defined and >= 1 |
8b0bd21b VZ |
42 | #ifdef __WXDEBUG__ |
43 | #if !defined(WXDEBUG) || !WXDEBUG | |
44 | #undef WXDEBUG | |
45 | #define WXDEBUG 1 | |
993df040 VZ |
46 | #endif // !WXDEBUG |
47 | #endif // __WXDEBUG__ | |
e0c749a7 | 48 | |
993df040 VZ |
49 | // ---------------------------------------------------------------------------- |
50 | // Debugging macros | |
51 | // | |
52 | // All debugging macros rely on ASSERT() which in turn calls the user-defined | |
53 | // OnAssert() function. To keep things simple, it's called even when the | |
54 | // expression is true (i.e. everything is ok) and by default does nothing: just | |
55 | // returns the same value back. But if you redefine it to do something more sexy | |
56 | // (popping up a message box in your favourite GUI, sending you e-mail or | |
57 | // whatever) it will affect all ASSERTs, FAILs and CHECKs in your code. | |
58 | // | |
59 | // Warning: if you don't like advice on programming style, don't read | |
60 | // further! ;-) | |
61 | // | |
62 | // Extensive use of these macros is recommended! Remember that ASSERTs are | |
63 | // disabled in final build (without __WXDEBUG__ defined), so they add strictly | |
64 | // nothing to your program's code. On the other hand, CHECK macros do stay | |
65 | // even in release builds, but in general are not much of a burden, while | |
66 | // a judicious use of them might increase your program's stability. | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // Macros which are completely disabled in 'release' mode | |
70 | // | |
71 | // NB: these functions are implemented in src/common/appcmn.cpp | |
72 | #if defined(__WXDEBUG__) | |
7ba4fbeb | 73 | /* |
497a2d6d VZ |
74 | This function is called whenever one of debugging macros fails (i.e. |
75 | condition is false in an assertion). To customize its behaviour, override | |
2b232d20 | 76 | wxApp::OnAssertFailure(). |
7ba4fbeb | 77 | |
497a2d6d | 78 | Parameters: |
7ba4fbeb | 79 | szFile and nLine - file name and line number of the ASSERT |
acc476c5 | 80 | szFunc - function name of the ASSERT, may be NULL (NB: ASCII) |
497a2d6d | 81 | szCond - text form of the condition which failed |
7ba4fbeb | 82 | szMsg - optional message explaining the reason |
c801d85f | 83 | */ |
0accd1cf | 84 | |
2b232d20 | 85 | /* this version is for compatibility with wx 2.8 Unicode build only: */ |
bddd7a8d | 86 | extern void WXDLLIMPEXP_BASE wxOnAssert(const wxChar *szFile, |
886dd7d2 | 87 | int nLine, |
acc476c5 | 88 | const char *szFunc, |
886dd7d2 VZ |
89 | const wxChar *szCond, |
90 | const wxChar *szMsg = NULL); | |
749b01f0 | 91 | |
0accd1cf VS |
92 | #if wxUSE_UNICODE |
93 | /* char versions are used by debugging macros; we have to provide | |
94 | wxChar* szMsg version because it's common to use _T() in the macros | |
95 | and finally, we can't use const wx(char)* szMsg = NULL, because that | |
96 | would be ambiguous: */ | |
97 | extern void WXDLLIMPEXP_BASE wxOnAssert(const char *szFile, | |
98 | int nLine, | |
99 | const char *szFunc, | |
100 | const char *szCond); | |
101 | ||
102 | extern void WXDLLIMPEXP_BASE wxOnAssert(const char *szFile, | |
103 | int nLine, | |
104 | const char *szFunc, | |
105 | const char *szCond, | |
106 | const char *szMsg); | |
107 | ||
108 | extern void WXDLLIMPEXP_BASE wxOnAssert(const char *szFile, | |
109 | int nLine, | |
110 | const char *szFunc, | |
111 | const char *szCond, | |
112 | const wxChar *szMsg); | |
28efe654 | 113 | #endif /* wxUSE_UNICODE */ |
0accd1cf | 114 | |
b5dbe15d VS |
115 | class WXDLLIMPEXP_FWD_BASE wxString; |
116 | class WXDLLIMPEXP_FWD_BASE wxCStrData; | |
2b232d20 VZ |
117 | |
118 | /* these two work when szMsg passed to debug macro is a string, | |
119 | we also have to provide wxCStrData overload to resolve ambiguity | |
120 | which would otherwise arise from wxASSERT( s.c_str() ): */ | |
0accd1cf VS |
121 | extern void WXDLLIMPEXP_BASE wxOnAssert(const wxString& szFile, |
122 | int nLine, | |
123 | const wxString& szFunc, | |
124 | const wxString& szCond, | |
125 | const wxString& szMsg); | |
126 | ||
127 | extern void WXDLLIMPEXP_BASE wxOnAssert(const wxString& szFile, | |
128 | int nLine, | |
129 | const wxString& szFunc, | |
130 | const wxString& szCond); | |
131 | ||
2b232d20 VZ |
132 | extern void WXDLLIMPEXP_BASE wxOnAssert(const char *szFile, |
133 | int nLine, | |
134 | const char *szFunc, | |
135 | const char *szCond, | |
136 | const wxCStrData& msg); | |
137 | ||
fbaf7d45 VS |
138 | extern void WXDLLIMPEXP_BASE wxOnAssert(const char *szFile, |
139 | int nLine, | |
140 | const char *szFunc, | |
141 | const char *szCond, | |
142 | const wxString& szMsg); | |
143 | ||
993df040 VZ |
144 | // call this function to break into the debugger unconditionally (assuming |
145 | // the program is running under debugger, of course) | |
bddd7a8d | 146 | extern void WXDLLIMPEXP_BASE wxTrap(); |
c801d85f | 147 | |
993df040 | 148 | // generic assert macro |
0accd1cf | 149 | #define wxASSERT(cond) wxASSERT_MSG(cond, (const char*)NULL) |
3b1de9c2 | 150 | |
9d73af58 | 151 | |
993df040 | 152 | // assert with additional message explaining its cause |
9d73af58 | 153 | |
993df040 VZ |
154 | // Note: some compilers will give a warning (such as |
155 | // "possible unwanted ;") when using a ";" instead of the "{}". | |
0d654944 SN |
156 | #define wxASSERT_MSG(cond, msg) \ |
157 | if ( cond ) \ | |
158 | {} \ | |
159 | else \ | |
160 | wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, #cond, msg) | |
497a2d6d | 161 | |
993df040 | 162 | // special form of assert: always triggers it (in debug mode) |
0accd1cf | 163 | #define wxFAIL wxFAIL_MSG((const char*)NULL) |
497a2d6d | 164 | |
993df040 | 165 | // FAIL with some message |
b503b407 VZ |
166 | #define wxFAIL_MSG(msg) wxFAIL_COND_MSG("wxAssertFailure", msg) |
167 | ||
993df040 | 168 | // FAIL with some message and a condition |
b503b407 | 169 | #define wxFAIL_COND_MSG(cond, msg) \ |
0accd1cf | 170 | wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, cond, msg) |
749b01f0 | 171 | |
993df040 VZ |
172 | // An assert helper used to avoid warning when testing constant expressions, |
173 | // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about | |
174 | // expression being always true, but not using | |
175 | // wxASSERT( wxAssertIsEqual(sizeof(int), 4) ) | |
176 | // | |
177 | // NB: this is made obsolete by wxCOMPILE_TIME_ASSERT() and should no | |
178 | // longer be used. | |
bddd7a8d | 179 | extern bool WXDLLIMPEXP_BASE wxAssertIsEqual(int x, int y); |
c801d85f | 180 | #else |
749b01f0 VZ |
181 | #define wxTrap() |
182 | ||
993df040 VZ |
183 | // nothing to do in release mode (hopefully at this moment there are |
184 | // no more bugs ;-) | |
7ba4fbeb | 185 | #define wxASSERT(cond) |
dfa0b52f | 186 | #define wxASSERT_MSG(cond, msg) |
497a2d6d VZ |
187 | #define wxFAIL |
188 | #define wxFAIL_MSG(msg) | |
b503b407 | 189 | #define wxFAIL_COND_MSG(cond, msg) |
e0c749a7 | 190 | #endif /* __WXDEBUG__ */ |
c801d85f | 191 | |
993df040 VZ |
192 | // Use of wxFalse instead of false suppresses compiler warnings about testing |
193 | // constant expression | |
194 | extern WXDLLIMPEXP_DATA_BASE(const bool) wxFalse; | |
34cbe514 | 195 | |
2b5f62a0 | 196 | #define wxAssertFailure wxFalse |
8b0bd21b | 197 | |
993df040 | 198 | // NB: the following macros also work in release mode! |
c801d85f | 199 | |
7ba4fbeb | 200 | /* |
c801d85f | 201 | These macros must be used only in invalid situation: for example, an |
6a17b868 | 202 | invalid parameter (e.g. a NULL pointer) is passed to a function. Instead of |
c801d85f KB |
203 | dereferencing it and causing core dump the function might try using |
204 | CHECK( p != NULL ) or CHECK( p != NULL, return LogError("p is NULL!!") ) | |
c801d85f | 205 | */ |
7ba4fbeb | 206 | |
993df040 | 207 | // check that expression is true, "return" if not (also FAILs in debug mode) |
0accd1cf | 208 | #define wxCHECK(cond, rc) wxCHECK_MSG(cond, rc, (const char*)NULL) |
7ba4fbeb | 209 | |
993df040 | 210 | // as wxCHECK but with a message explaining why we fail |
dfa0b52f | 211 | #define wxCHECK_MSG(cond, rc, msg) wxCHECK2_MSG(cond, return rc, msg) |
7ba4fbeb | 212 | |
993df040 | 213 | // check that expression is true, perform op if not |
0accd1cf | 214 | #define wxCHECK2(cond, op) wxCHECK2_MSG(cond, op, (const char*)NULL) |
7ba4fbeb | 215 | |
993df040 | 216 | // as wxCHECK2 but with a message explaining why we fail |
9d73af58 | 217 | |
0d654944 SN |
218 | #define wxCHECK2_MSG(cond, op, msg) \ |
219 | if ( cond ) \ | |
220 | {} \ | |
221 | else \ | |
222 | { \ | |
223 | wxFAIL_COND_MSG(#cond, msg); \ | |
224 | op; \ | |
225 | } \ | |
226 | struct wxDummyCheckStruct /* just to force a semicolon */ | |
7ba4fbeb | 227 | |
993df040 VZ |
228 | // special form of wxCHECK2: as wxCHECK, but for use in void functions |
229 | // | |
230 | // NB: there is only one form (with msg parameter) and it's intentional: | |
231 | // there is no other way to tell the caller what exactly went wrong | |
232 | // from the void function (of course, the function shouldn't be void | |
233 | // to begin with...) | |
dfa0b52f | 234 | #define wxCHECK_RET(cond, msg) wxCHECK2_MSG(cond, return, msg) |
c801d85f | 235 | |
993df040 VZ |
236 | // ---------------------------------------------------------------------------- |
237 | // Compile time asserts | |
238 | // | |
239 | // Unlike the normal assert and related macros above which are checked during | |
240 | // the program tun-time the macros below will result in a compilation error if | |
241 | // the condition they check is false. This is usually used to check the | |
242 | // expressions containing sizeof()s which cannot be tested with the | |
243 | // preprocessor. If you can use the #if's, do use them as you can give a more | |
244 | // detailed error message then. | |
245 | // ---------------------------------------------------------------------------- | |
8f5d9104 VZ |
246 | |
247 | /* | |
248 | How this works (you don't have to understand it to be able to use the | |
249 | macros): we rely on the fact that it is invalid to define a named bit field | |
250 | in a struct of width 0. All the rest are just the hacks to minimize the | |
251 | possibility of the compiler warnings when compiling this macro: in | |
252 | particular, this is why we define a struct and not an object (which would | |
253 | result in a warning about unused variable) and a named struct (otherwise we'd | |
254 | get a warning about an unnamed struct not used to define an object!). | |
255 | */ | |
256 | ||
9bf41e06 | 257 | #define wxMAKE_UNIQUE_ASSERT_NAME wxMAKE_UNIQUE_NAME(wxAssert_) |
8f5d9104 VZ |
258 | |
259 | /* | |
260 | The second argument of this macro must be a valid C++ identifier and not a | |
261 | string. I.e. you should use it like this: | |
262 | ||
263 | wxCOMPILE_TIME_ASSERT( sizeof(int) >= 2, YourIntsAreTooSmall ); | |
264 | ||
265 | It may be used both within a function and in the global scope. | |
266 | */ | |
993df040 | 267 | #if defined(__WATCOMC__) |
1c6f2414 WS |
268 | /* avoid "unused symbol" warning */ |
269 | #define wxCOMPILE_TIME_ASSERT(expr, msg) \ | |
270 | class wxMAKE_UNIQUE_ASSERT_NAME { \ | |
271 | unsigned int msg: expr; \ | |
871cc651 | 272 | wxMAKE_UNIQUE_ASSERT_NAME() { wxUnusedVar(msg); } \ |
1c6f2414 WS |
273 | } |
274 | #else | |
275 | #define wxCOMPILE_TIME_ASSERT(expr, msg) \ | |
276 | struct wxMAKE_UNIQUE_ASSERT_NAME { unsigned int msg: expr; } | |
277 | #endif | |
8f5d9104 | 278 | |
871cc651 VZ |
279 | /* |
280 | When using VC++ 6 with "Edit and Continue" on, the compiler completely | |
281 | mishandles __LINE__ and so wxCOMPILE_TIME_ASSERT() doesn't work, provide a | |
282 | way to make "unique" assert names by specifying a unique prefix explicitly | |
283 | */ | |
284 | #define wxMAKE_UNIQUE_ASSERT_NAME2(text) wxCONCAT(wxAssert_, text) | |
7bb688a8 | 285 | |
17dc7ddd WS |
286 | #define wxCOMPILE_TIME_ASSERT2(expr, msg, text) \ |
287 | struct wxMAKE_UNIQUE_ASSERT_NAME2(text) { unsigned int msg: expr; } | |
8de1759c | 288 | |
993df040 | 289 | // helpers for wxCOMPILE_TIME_ASSERT below, for private use only |
8f5d9104 VZ |
290 | #define wxMAKE_BITSIZE_MSG(type, size) type ## SmallerThan ## size ## Bits |
291 | ||
993df040 VZ |
292 | // a special case of compile time assert: check that the size of the given type |
293 | // is at least the given number of bits | |
8f5d9104 VZ |
294 | #define wxASSERT_MIN_BITSIZE(type, size) \ |
295 | wxCOMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size, \ | |
296 | wxMAKE_BITSIZE_MSG(type, size)) | |
297 | ||
993df040 VZ |
298 | // ---------------------------------------------------------------------------- |
299 | // other miscellaneous debugger-related functions | |
300 | // ---------------------------------------------------------------------------- | |
a434b43f | 301 | |
c50a4038 VZ |
302 | /* |
303 | Return true if we're running under debugger. | |
304 | ||
305 | Currently this only really works under Win32 and Mac in CodeWarrior builds, | |
306 | it always returns false in other cases. | |
307 | */ | |
993df040 VZ |
308 | #if defined(__WXMAC__) || defined(__WIN32__) |
309 | extern bool WXDLLIMPEXP_BASE wxIsDebuggerRunning(); | |
310 | #else // !Mac | |
311 | inline bool wxIsDebuggerRunning() { return false; } | |
312 | #endif // Mac/!Mac | |
313 | ||
314 | #endif // _WX_DEBUG_H_ |