1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Misc debug functions and macros
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998-2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #if !defined(__WXPALMOS5__) && !defined(__WXWINCE__)
16 #endif // systems without assert.h
18 #include <limits.h> // for CHAR_BIT used below
20 #include "wx/chartype.h" // for __TFILE__ and wxChar
22 // ----------------------------------------------------------------------------
23 // Defines controlling the debugging macros
24 // ----------------------------------------------------------------------------
26 // if _DEBUG is defined (MS VC++ and others use it in debug builds), define
31 #endif // !__WXDEBUG__
34 // if NDEBUG is defined (<assert.h> uses it), undef __WXDEBUG__ and WXDEBUG
40 // if __WXDEBUG__ is defined, make sure that WXDEBUG is defined and >= 1
42 #if !defined(WXDEBUG) || !WXDEBUG
48 #ifndef __WXFUNCTION__
49 /* TODO: add more compilers supporting __FUNCTION__ */
52 __FUNCTION__ happens to be not defined within class members
53 http://www.digitalmars.com/drn-bin/wwwnews?c%2B%2B.beta/485
55 #define __WXFUNCTION__ (NULL)
56 #elif defined(__GNUC__) || \
57 (defined(_MSC_VER) && _MSC_VER >= 1300) || \
59 #define __WXFUNCTION__ __FUNCTION__
61 /* still define __WXFUNCTION__ to avoid #ifdefs elsewhere */
62 #define __WXFUNCTION__ (NULL)
64 #endif /* __WXFUNCTION__ already defined */
66 // ----------------------------------------------------------------------------
69 // All debugging macros rely on ASSERT() which in turn calls the user-defined
70 // OnAssert() function. To keep things simple, it's called even when the
71 // expression is true (i.e. everything is ok) and by default does nothing: just
72 // returns the same value back. But if you redefine it to do something more sexy
73 // (popping up a message box in your favourite GUI, sending you e-mail or
74 // whatever) it will affect all ASSERTs, FAILs and CHECKs in your code.
76 // Warning: if you don't like advice on programming style, don't read
79 // Extensive use of these macros is recommended! Remember that ASSERTs are
80 // disabled in final build (without __WXDEBUG__ defined), so they add strictly
81 // nothing to your program's code. On the other hand, CHECK macros do stay
82 // even in release builds, but in general are not much of a burden, while
83 // a judicious use of them might increase your program's stability.
84 // ----------------------------------------------------------------------------
86 // Macros which are completely disabled in 'release' mode
88 // NB: these functions are implemented in src/common/appcmn.cpp
89 #if defined(__WXDEBUG__)
91 This function is called whenever one of debugging macros fails (i.e.
92 condition is false in an assertion). To customize its behaviour, override
93 wxApp::OnAssertFailure().
96 szFile and nLine - file name and line number of the ASSERT
97 szFunc - function name of the ASSERT, may be NULL (NB: ASCII)
98 szCond - text form of the condition which failed
99 szMsg - optional message explaining the reason
102 /* this version is for compatibility with wx 2.8 Unicode build only: */
103 extern void WXDLLIMPEXP_BASE
wxOnAssert(const wxChar
*szFile
,
106 const wxChar
*szCond
,
107 const wxChar
*szMsg
= NULL
);
110 /* char versions are used by debugging macros; we have to provide
111 wxChar* szMsg version because it's common to use _T() in the macros
112 and finally, we can't use const wx(char)* szMsg = NULL, because that
113 would be ambiguous: */
114 extern void WXDLLIMPEXP_BASE
wxOnAssert(const char *szFile
,
119 extern void WXDLLIMPEXP_BASE
wxOnAssert(const char *szFile
,
125 extern void WXDLLIMPEXP_BASE
wxOnAssert(const char *szFile
,
129 const wxChar
*szMsg
);
130 #endif /* wxUSE_UNICODE */
132 class WXDLLIMPEXP_FWD_BASE wxString
;
133 class WXDLLIMPEXP_FWD_BASE wxCStrData
;
135 /* these two work when szMsg passed to debug macro is a string,
136 we also have to provide wxCStrData overload to resolve ambiguity
137 which would otherwise arise from wxASSERT( s.c_str() ): */
138 extern void WXDLLIMPEXP_BASE
wxOnAssert(const wxString
& szFile
,
140 const wxString
& szFunc
,
141 const wxString
& szCond
,
142 const wxString
& szMsg
);
144 extern void WXDLLIMPEXP_BASE
wxOnAssert(const wxString
& szFile
,
146 const wxString
& szFunc
,
147 const wxString
& szCond
);
149 extern void WXDLLIMPEXP_BASE
wxOnAssert(const char *szFile
,
153 const wxCStrData
& msg
);
155 extern void WXDLLIMPEXP_BASE
wxOnAssert(const char *szFile
,
159 const wxString
& szMsg
);
161 // call this function to break into the debugger unconditionally (assuming
162 // the program is running under debugger, of course)
163 extern void WXDLLIMPEXP_BASE
wxTrap();
165 // generic assert macro
166 #define wxASSERT(cond) wxASSERT_MSG(cond, (const char*)NULL)
169 // assert with additional message explaining its cause
171 // Note: some compilers will give a warning (such as
172 // "possible unwanted ;") when using a ";" instead of the "{}".
173 #define wxASSERT_MSG(cond, msg) \
177 wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, #cond, msg)
179 // special form of assert: always triggers it (in debug mode)
180 #define wxFAIL wxFAIL_MSG((const char*)NULL)
182 // FAIL with some message
183 #define wxFAIL_MSG(msg) wxFAIL_COND_MSG("wxAssertFailure", msg)
185 // FAIL with some message and a condition
186 #define wxFAIL_COND_MSG(cond, msg) \
187 wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, cond, msg)
189 // An assert helper used to avoid warning when testing constant expressions,
190 // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
191 // expression being always true, but not using
192 // wxASSERT( wxAssertIsEqual(sizeof(int), 4) )
194 // NB: this is made obsolete by wxCOMPILE_TIME_ASSERT() and should no
196 extern bool WXDLLIMPEXP_BASE
wxAssertIsEqual(int x
, int y
);
200 // nothing to do in release mode (hopefully at this moment there are
202 #define wxASSERT(cond)
203 #define wxASSERT_MSG(cond, msg)
205 #define wxFAIL_MSG(msg)
206 #define wxFAIL_COND_MSG(cond, msg)
207 #endif /* __WXDEBUG__ */
209 // Use of wxFalse instead of false suppresses compiler warnings about testing
210 // constant expression
211 extern WXDLLIMPEXP_DATA_BASE(const bool) wxFalse
;
213 #define wxAssertFailure wxFalse
215 // NB: the following macros also work in release mode!
218 These macros must be used only in invalid situation: for example, an
219 invalid parameter (e.g. a NULL pointer) is passed to a function. Instead of
220 dereferencing it and causing core dump the function might try using
221 CHECK( p != NULL ) or CHECK( p != NULL, return LogError("p is NULL!!") )
224 // check that expression is true, "return" if not (also FAILs in debug mode)
225 #define wxCHECK(cond, rc) wxCHECK_MSG(cond, rc, (const char*)NULL)
227 // as wxCHECK but with a message explaining why we fail
228 #define wxCHECK_MSG(cond, rc, msg) wxCHECK2_MSG(cond, return rc, msg)
230 // check that expression is true, perform op if not
231 #define wxCHECK2(cond, op) wxCHECK2_MSG(cond, op, (const char*)NULL)
233 // as wxCHECK2 but with a message explaining why we fail
235 #define wxCHECK2_MSG(cond, op, msg) \
240 wxFAIL_COND_MSG(#cond, msg); \
243 struct wxDummyCheckStruct /* just to force a semicolon */
245 // special form of wxCHECK2: as wxCHECK, but for use in void functions
247 // NB: there is only one form (with msg parameter) and it's intentional:
248 // there is no other way to tell the caller what exactly went wrong
249 // from the void function (of course, the function shouldn't be void
251 #define wxCHECK_RET(cond, msg) wxCHECK2_MSG(cond, return, msg)
253 // ----------------------------------------------------------------------------
254 // Compile time asserts
256 // Unlike the normal assert and related macros above which are checked during
257 // the program tun-time the macros below will result in a compilation error if
258 // the condition they check is false. This is usually used to check the
259 // expressions containing sizeof()s which cannot be tested with the
260 // preprocessor. If you can use the #if's, do use them as you can give a more
261 // detailed error message then.
262 // ----------------------------------------------------------------------------
265 How this works (you don't have to understand it to be able to use the
266 macros): we rely on the fact that it is invalid to define a named bit field
267 in a struct of width 0. All the rest are just the hacks to minimize the
268 possibility of the compiler warnings when compiling this macro: in
269 particular, this is why we define a struct and not an object (which would
270 result in a warning about unused variable) and a named struct (otherwise we'd
271 get a warning about an unnamed struct not used to define an object!).
274 #define wxMAKE_UNIQUE_ASSERT_NAME wxMAKE_UNIQUE_NAME(wxAssert_)
277 The second argument of this macro must be a valid C++ identifier and not a
278 string. I.e. you should use it like this:
280 wxCOMPILE_TIME_ASSERT( sizeof(int) >= 2, YourIntsAreTooSmall );
282 It may be used both within a function and in the global scope.
284 #if defined(__WATCOMC__)
285 /* avoid "unused symbol" warning */
286 #define wxCOMPILE_TIME_ASSERT(expr, msg) \
287 class wxMAKE_UNIQUE_ASSERT_NAME { \
288 unsigned int msg: expr; \
289 wxMAKE_UNIQUE_ASSERT_NAME() { wxUnusedVar(msg); } \
292 #define wxCOMPILE_TIME_ASSERT(expr, msg) \
293 struct wxMAKE_UNIQUE_ASSERT_NAME { unsigned int msg: expr; }
297 When using VC++ 6 with "Edit and Continue" on, the compiler completely
298 mishandles __LINE__ and so wxCOMPILE_TIME_ASSERT() doesn't work, provide a
299 way to make "unique" assert names by specifying a unique prefix explicitly
301 #define wxMAKE_UNIQUE_ASSERT_NAME2(text) wxCONCAT(wxAssert_, text)
303 #define wxCOMPILE_TIME_ASSERT2(expr, msg, text) \
304 struct wxMAKE_UNIQUE_ASSERT_NAME2(text) { unsigned int msg: expr; }
306 // helpers for wxCOMPILE_TIME_ASSERT below, for private use only
307 #define wxMAKE_BITSIZE_MSG(type, size) type ## SmallerThan ## size ## Bits
309 // a special case of compile time assert: check that the size of the given type
310 // is at least the given number of bits
311 #define wxASSERT_MIN_BITSIZE(type, size) \
312 wxCOMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size, \
313 wxMAKE_BITSIZE_MSG(type, size))
315 // ----------------------------------------------------------------------------
316 // other miscellaneous debugger-related functions
317 // ----------------------------------------------------------------------------
320 Return true if we're running under debugger.
322 Currently this only really works under Win32 and Mac in CodeWarrior builds,
323 it always returns false in other cases.
325 #if defined(__WXMAC__) || defined(__WIN32__)
326 extern bool WXDLLIMPEXP_BASE
wxIsDebuggerRunning();
328 inline bool wxIsDebuggerRunning() { return false; }
331 #endif // _WX_DEBUG_H_