source: src/CodePatterns/Assert.hpp@ 084729c

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 084729c was 084729c, checked in by Frederik Heber <heber@…>, 8 years ago

Squashed 'ThirdParty/CodePatterns/' content from commit c1e1041

git-subtree-dir: ThirdParty/CodePatterns
git-subtree-split: c1e10418c454f98be2f43d93167642b0008428fc

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Assert.hpp
3 *
4 * Created on: Mar 18, 2010
5 * Author: crueger
6 */
7
8#ifndef ASSERT_HPP_
9#define ASSERT_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include<sstream>
17#include<string>
18#include<iosfwd>
19#include<vector>
20#include<map>
21
22#include "CodePatterns/toString.hpp"
23
24/**
25 * \file Helpers/Assert.hpp
26 * <H1> ASSERT Howto </H1>
27 *
28 * <H2> Introduction </H2>
29 *
30 * ASSERT() is a small macro that allows easier debugging, when it is widely used. The custom
31 * ASSERT macro defined in this file works mainly the same way as the assert() macro that
32 * is defined in the Ansi-C standard, but includes a few nice additions.
33 *
34 * <H3> What ASSERT() does </H3>
35 *
36 * ASSERT can be used to make sure that a condition that always needs to be true for the code to
37 * work correctly is holding. If you have a function that takes a value greater than 0 and a value
38 * smaller than 0 indicates a mistake you should always do it the following way: <br>
39 * @code
40 * void foo(int a) // a should be greater 0
41 * {
42 * ASSERT(a>0,"Parameter passed to foo was smaller than 0");
43 * ...
44 * }
45 * @endcode
46 *
47 * (Note: some people say, that assertions like these should not be used to check function parameters.
48 * This is mainly due to the reason, that a failed assertion will show up inside the function. The buggy
49 * code however is at a completely different place, i.e. at the callers side. Always put the
50 * Assertions as close to the code that produces the value as possible, when looking at function
51 * parameters however this would mean, that any code calling foo would have an ASSERT(...) before
52 * it, which makes it easy to forget the Assertion at some places. Also this makes an easy example.)
53 *
54 * If the condition inside the ASSERT does not evaluate to true the user is shown a message, including
55 * the condition that failed, the line in which the failure was observed and the message of the assertion.
56 * In the above case that would look something like this:<br>
57 * @code
58 * Assertion "a>0" failed in foo.cpp in line 3.
59 * Assertion Message: Parameter passed to foo was smaller than 0
60 * @endcode
61 *
62 * In normal conditions, i.e. when no default action is set (see below for default actions) the user
63 * is then shown a short choice menu, on how to handle the assertion. The user can choose to abort the
64 * program, throw an exception of type AssertionFailure that contains the file, line and message,
65 * ignore the assertion or even to always ignore the assertion at that point (i.e. the ASSERT() macro
66 * at this file and line is fully disabled).
67 *
68 * Both ASSERT() and assert() handle debugging in the same way, i.e. they are only used when the
69 * NDEBUG macro is not defined. If the NDEBUG macro is defined, for example using a CXXFLAG then
70 * all asserts and ASSERTs will be disabled in the compiled program. That way in a end-user version
71 * all assertions can be removed with a single switch, thus not hassling the end-user with potential
72 * bugs.
73 *
74 * <H2> Special functions of ASSERT() </H2>
75 *
76 * Compared to the standard assert() macro the custom ASSERT() contains a few special functions. As
77 * first it is possible to set a global default behavior that is used anytime an assertion fails.
78 * This default behavior can be either of Assert::Ask, Assert::Abort, Assert::Throw or Assert::ignore.
79 * The default behavior is set using the ASSERT_DO() macro. For example if you want to check in a
80 * unittest that wrong code at another point actually makes a certain assert fail you could set
81 * ASSERT_DO(Assert::Throw) to make sure a exception is thrown and catch that exception using
82 * the CPPUNIT_ASSERT_THROW() macro. The current set default behavior can be queried as a string
83 * using the ASSERT_DEFAULT macro.
84 *
85 * As a second enhancement it is possible to install callback functions as hooks that will be executed
86 * when an assertion aborts the program. These callback functions could for example be used to flush
87 * any open streams, thus making sure files on the disk are not corrupted by a unexpected abortion.
88 * It would also be possible to install functions that produce some kind of "coredump" of important
89 * internal data-structures, thus giving the person looking for the bug some valuable information.
90 * These assertion hooks should however not be used to clean up the reserved memory of the program,
91 * because a) this memory is under normal circumstances reclaimed by the OS anyway, once the program
92 * has aborted and b) the memory might still contain some hints that could be useful when running
93 * the program inside a debugger and which could be destroyed by the clean-up. To use the hooking
94 * mechanism you can simply use the ASSERT_HOOK() macro, passing this macro any kind of void function.
95 * For example:<br/>
96 * @code
97 * void foo(){
98 * // produce a coredump
99 * ...
100 * // close and flush all open handles
101 * ...
102 * }
103 *
104 * int main(int argc, char **argv){
105 * ASSERT_HOOK(foo);
106 * ...
107 * return 0;
108 * }
109 * @endcode
110 *
111 * All hooks will be executed in the reverse order of hooking, i.e. the function hooked last will be
112 * executed first when the abortion is handled. It is also possible to remove a hook to any function
113 * using the ASSERT_UNHOOK() macro and passing it the pointer to the function one wants to remove.
114 *
115 * Assertion hooks will only be executed when the program is terminated by an assertion using the
116 * abort mechanism. They will not be executed when the program exits in any other way. They also
117 * wont be executed when the assertion is ignored or an exception is thrown (even when the exception
118 * is not caught and thus terminates the program).
119 *
120 * <H2> Rules for using ASSERT() </H2>
121 *
122 * The rules for using ASSERT() are basically the same ones that can be used as guidlines for the
123 * standard assert() macro. So if you think you know those guidelines you can skip the following.
124 *
125 * <ul>
126 * <li> ASSERT() should be used only for problems that indicate a bug, i.e. problems that can be
127 * improved by rewriting parts of the program. ASSERT() should not be used to query problems that
128 * can go wrong during the normal execution of the program. For example ASSERT() should not be
129 * used to test whether a file could be opened, or memory could be reserved, as a failure of either
130 * of those tasks can not be improved upon by rewriting the code.
131 * <li> The condition in the ASSERT() macro should never contain any side-effects. Only call methods,
132 * when you are absolutely certain that these methods wont have any side-effects. Calling ASSERT()
133 * should in no way change the state of the program, because once the end-user version is produced
134 * using the NDEBUG flag all assertions are removed and so are the conditions. If the condition did
135 * cause a state transition, this state transition would be removed and the behavior of the end-user
136 * and the debug version might differ. Things you should watch out for are for example<br/>
137 * @code
138 * ASSERT(++i,"i was zero after incrementing");
139 * @endcode
140 * instead always do
141 * @code
142 * ++i;
143 * ASSERT(i,"i was zero after incrementing");
144 * @endcode
145 * <li> Give descriptive error messages. This one is a bit obvious but easy to do wrong, so I included
146 * it here. An
147 * @code
148 * ASSERT(ptr,"Pointer was zero");
149 * @endcode
150 * wont help anyone. If you do <br/>
151 * @code
152 * ASSERT(ptr,"Second argument of function foo should have pointed to an object of type bar, but was zero.");
153 * @endcode
154 * instead, people will almost immidiately know what to look for.
155 * </ul>
156 *
157 * <H2> Differences between ASSERT() and assert() </H2>
158 *
159 * This chapter is to explain why a custom ASSERT() macro was introduced and should be used in place
160 * of the standard assert(). Here are the main differences between ASSERT() and assert().
161 *
162 * <ul>
163 * <li> ASSERT() makes it easy to add a more verbose message about the nature of the failure. For
164 * assert() it has become customary to add messages using constructs like
165 * @code
166 * assert(c>0 && "Counter should be at least 1");
167 * @endcode in order to add descriptions. However both the syntax and the final output for this are
168 * a bit awkward. The custom ASSERT() handles messages in a much better way, as well as making them
169 * mandatory instead of optional.
170 * <li> ASSERT() leaves the user and the programmer a choice how to handle an assertion. While the
171 * assert() macro will always abort the program, the ASSERT() macro normally gives the user a choice on
172 * what to do. For debugging it might also be interesting how a broken assumption influences the rest
173 * of the program, so the assertion can also be ignored. Also the Exception mechanism allows
174 * assertions to be part of unittests, whereas they would always fail if the assert() macro was used.
175 * <li> ASSERT() does not unwind the stack (at least when compiled using gcc). The normal assert()
176 * exits the program, which unwinds the stack and destroys any hope for recovering a stack trace.
177 * ASSERT() on the other hand aborts the program using a special trap function, that leaves the
178 * stack intact. This way, when the program is run inside a debugger the stack is still available
179 * and can be inspected. This is the main reason, why it is safe to use ASSERT() to check function
180 * parameters, whereas assert() would give problems in such cases.
181 * <li> ASSERT() allows for hooks to be installed when the program exits. As mentioned above this
182 * makes it possible to produce coredumps, make sure all files are in a usable state or other tasks
183 * that have to be performed before killing the program.
184 * </ul>
185 *
186 * <H2> Tips and tricks and FAQ </H2>
187 *
188 * <ul>
189 * <li> <H4> How can I add values to the failure message of ASSERT(), e.g. I want to say that above "i"
190 * failed to be zero, with i == ...?</H4>
191 * This can be done in the following way:
192 * @code
193 * ASSERT(!i,"i was not zero but "+toString(i)+"after incrementing");
194 * @endcode
195 * Note that this works because of the template function toString() (in src/Helpers/toString.hpp) that
196 * uses stringstreams to convert any value to std::string if the respective operator<< is implemented.
197 * <li> <H4> ASSERT() is broken. When I abort the program it says something about an
198 * "Illegal instruction"</H4>
199 * The complaints about the illegal instruction after an abortion are no need to worry. This
200 * illegal instruction is part of the trap that is used to exit the program while leaving the stack
201 * intact. This illegal instruction can be detected by the debugger, which means it will give you the
202 * usual prompt once it is encountered. The illegal instruction is guaranteed not to mess up anything,
203 * so there is no need to worry about it.
204 * <li> <H4> When compiling the program with $NON_GCC_COMPILER and then debugging it, it will
205 * unwind the stack. I need the backtrace however to find the bug </H4>
206 * The mechanism to preserve the stack is compiler specific. For now only a mechanism that is supported
207 * by gcc is implemented, because this compiler is widely used. For other compilers the program
208 * is simply exited, and the stack is destroyed. If you need a backtrace and you cannot use gcc you
209 * have to figure out a way to have your compiler produce a trap instruction in the program. You might
210 * want to use google to find out how to get your compiler to do that. For many compilers a
211 * _asm {int 3} is said to work. Also for VC++ the instruction __debugbreak() might produce a trap.
212 * Also dividing by zero is a hack that could be used as a last hope if you don't find a way to produce
213 * traps with your compiler even after a longer search. If you found a way to handle the traps you can
214 * then add the macro DEBUG_BREAK for your compiler and the stack will be preserved.
215 * <li> <H4> I have a portion of the program that should never be executed. How can I assure this
216 * using assert.</H4>
217 * This is a common task for assertions. For example you might have an exhaustive switch/case where
218 * the default value indicates that something went wrong. Simply use the following construct:
219 * @code
220 * switch(foo){
221 * case Bar:
222 * ...
223 * break;
224 * case Baz:
225 * ...
226 * break;
227 * ...
228 * default:
229 * ASSERT(0,"This switch should always be exhaustive.\nDid somebody add values to the enum?");
230 * }
231 * @endcode
232 * </ul>
233 */
234
235#ifndef NDEBUG
236 #ifndef STRINGIFY
237 #define STRINGIFY(x) #x
238 #endif
239
240 #ifdef __GNUC__
241 // on gcc we know how to exit to the Debugger
242 #define DEBUG_BREAK __builtin_trap()
243 #else
244 #define DEBUG_BREAK exit(1)
245 #endif
246
247 #define ASSERT(condition,message) \
248 do{\
249 static bool ignore = false;\
250 if(!ignore){\
251 if(!(condition) && Assert::_my_assert::check(STRINGIFY(condition),(message),\
252 __FILE__,__LINE__,ignore)){\
253 Assert::_my_assert::doHooks();\
254 DEBUG_BREAK;\
255 }\
256 } \
257 }while(0)
258
259 #define ASSERT_NOCATCH(message) \
260 catch(Assert::AssertionFailure&){throw;}\
261 catch(...){\
262 static bool ignore = false; \
263 if(!ignore){\
264 if(Assert::_my_assert::check("Exception caught",(message),__FILE__,__LINE__,ignore)){\
265 Assert::_my_assert::doHooks();\
266 DEBUG_BREAK;\
267 }\
268 }\
269 } do{(void)(0);}while(0)
270
271 #define assert_cast Assert::_wrapper(__LINE__,__FILE__)._convert
272
273 #define ASSERT_DO(action) do{Assert::_my_assert::setDefault(action);}while(0)
274 #define ASSERT_HOOK(hook) do{Assert::_my_assert::addHook(hook);}while(0)
275 #define ASSERT_UNHOOK(hook) do{Assert::_my_assert::removeHook(hook);}while(0)
276 #define ASSERT_DEFAULT (Assert::_myAssert::printDefault())
277#else
278 // we need to do something, so this is the usual solution (e.g. assert.h)
279 #define ASSERT(condition,message) (void)(0)
280 #define ASSERT_NOCATCH(message) catch(...) {throw;} do{(void)(0);}while(0)
281 #define assert_cast static_cast
282 #define ASSERT_DO(action) (void)(0)
283 #define ASSERT_HOOK(hook) (void)(0)
284 #define ASSERT_UNHOOK(hook) (void)(0)
285 #define ASSERT_DEFAULT std::string("Deactivated")
286#endif
287
288namespace Assert{
289
290 typedef void (*hook_t)(void);
291
292
293 enum Action {Ask,Abort,Throw,Ignore,MAX_ACTION};
294 extern const char ActionKeys[MAX_ACTION];
295 extern const char* ActionNames[MAX_ACTION];
296
297 class AssertionFailure{
298 public:
299 AssertionFailure(std::string _condition, std::string _file, int _line, std::string _message);
300 std::string getFile();
301 int getLine();
302 std::string getMessage();
303
304 std::ostream& operator<<(std::ostream&);
305 private:
306 std::string condition;
307 std::string file;
308 int line;
309 std::string message;
310 };
311
312 //! @cond
313#ifndef NDEBUG
314 class _my_assert{
315 public:
316 static bool check(const char* condition,
317 std::string message,
318 const char* filename,
319 const int line,
320 bool& ignore);
321#ifdef __GNUC__
322 static void backtrace(const char *file, int line);
323#endif /* __GNUC__ */
324 static void addHook(Assert::hook_t hook);
325 static void removeHook(Assert::hook_t hook);
326 static void doHooks();
327 static void setDefault(Assert::Action);
328 static Assert::Action getDefault();
329 static std::string printDefault();
330 private:
331 static Assert::Action defaultAction;
332 static std::vector<Assert::hook_t> hooks;
333 };
334
335
336 class _wrapper{
337 public:
338 _wrapper(int _line,const char* _file) :
339 line(_line),
340 file(_file)
341 {}
342
343 // Overloaded template for pointers
344 template<typename target,typename source>
345 target _convert(source *src){
346 std::stringstream sstr;
347 sstr << file << ":" << line;
348 bool &ignore = ignores[sstr.str()];
349
350 if(!ignore){
351 bool res = dynamic_cast<target>(src)==static_cast<target>(src);
352 if(!res && _my_assert::check("type-safe typecast",message_ptr,file,line,ignore)){
353 _my_assert::doHooks();
354 DEBUG_BREAK;
355 }
356 }
357 return static_cast<target>(src);
358 }
359
360 // Overloaded template for references
361 template<typename target, typename source>
362 target _convert(source &src){
363 std::stringstream sstr;
364 sstr << file << ":" << line;
365 bool &ignore = ignores[sstr.str()];
366
367 try{
368 target res =dynamic_cast<target>(src);
369 return res;
370 }
371 catch(...){
372 if(!ignore){
373 if(_my_assert::check("type-safe typecast",message_ref,file,line,ignore)){
374 _my_assert::doHooks();
375 DEBUG_BREAK;
376 }
377 }
378 }
379 // The error was ignored. Just return whatever a static_cast would do
380 return static_cast<target>(src);
381 }
382 private:
383 int line;
384 const char *file;
385 static std::map<std::string,bool> ignores;
386 // this avoids duplication of the strings when templates are instantiated
387 static const char* message_ptr;
388 static const char* message_ref;
389 };
390#endif
391 //! @endcond
392}
393
394
395
396
397#endif /* ASSERT_HPP_ */
Note: See TracBrowser for help on using the repository browser.