source: src/Helpers/Assert.hpp@ 13d5a9

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 13d5a9 was 13d5a9, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added macros that allow type safe casting using the Assert mechanism

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