Botan  2.4.0
Crypto and TLS for C++11
assert.h
Go to the documentation of this file.
1 /*
2 * Runtime assertion checking
3 * (C) 2010 Jack Lloyd
4 * 2017 Simon Warta (Kullo GmbH)
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_ASSERTION_CHECKING_H_
10 #define BOTAN_ASSERTION_CHECKING_H_
11 
12 #include <botan/build.h>
13 #include <botan/compiler.h>
14 
15 namespace Botan {
16 
17 /**
18 * Called when an assertion fails
19 */
20 BOTAN_NORETURN void BOTAN_PUBLIC_API(2,0) assertion_failure(const char* expr_str,
21  const char* assertion_made,
22  const char* func,
23  const char* file,
24  int line);
25 
26 /**
27 * Make an assertion
28 */
29 #define BOTAN_ASSERT(expr, assertion_made) \
30  do { \
31  if(!(expr)) \
32  Botan::assertion_failure(#expr, \
33  assertion_made, \
34  BOTAN_CURRENT_FUNCTION, \
35  __FILE__, \
36  __LINE__); \
37  } while(0)
38 
39 /**
40 * Make an assertion
41 */
42 #define BOTAN_ASSERT_NOMSG(expr) \
43  do { \
44  if(!(expr)) \
45  Botan::assertion_failure(#expr, \
46  "", \
47  BOTAN_CURRENT_FUNCTION, \
48  __FILE__, \
49  __LINE__); \
50  } while(0)
51 
52 /**
53 * Assert that value1 == value2
54 */
55 #define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made) \
56  do { \
57  if((expr1) != (expr2)) \
58  Botan::assertion_failure(#expr1 " == " #expr2, \
59  assertion_made, \
60  BOTAN_CURRENT_FUNCTION, \
61  __FILE__, \
62  __LINE__); \
63  } while(0)
64 
65 /**
66 * Assert that expr1 (if true) implies expr2 is also true
67 */
68 #define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg) \
69  do { \
70  if((expr1) && !(expr2)) \
71  Botan::assertion_failure(#expr1 " implies " #expr2, \
72  msg, \
73  BOTAN_CURRENT_FUNCTION, \
74  __FILE__, \
75  __LINE__); \
76  } while(0)
77 
78 /**
79 * Assert that a pointer is not null
80 */
81 #define BOTAN_ASSERT_NONNULL(ptr) \
82  do { \
83  if((ptr) == nullptr) \
84  Botan::assertion_failure(#ptr " is not null", \
85  "", \
86  BOTAN_CURRENT_FUNCTION, \
87  __FILE__, \
88  __LINE__); \
89  } while(0)
90 
91 /**
92 * Mark variable as unused. Takes between 1 and 9 arguments and marks all as unused,
93 * e.g. BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
94 */
95 #define _BOTAN_UNUSED_IMPL1(a) static_cast<void>(a)
96 #define _BOTAN_UNUSED_IMPL2(a, b) static_cast<void>(a); _BOTAN_UNUSED_IMPL1(b)
97 #define _BOTAN_UNUSED_IMPL3(a, b, c) static_cast<void>(a); _BOTAN_UNUSED_IMPL2(b, c)
98 #define _BOTAN_UNUSED_IMPL4(a, b, c, d) static_cast<void>(a); _BOTAN_UNUSED_IMPL3(b, c, d)
99 #define _BOTAN_UNUSED_IMPL5(a, b, c, d, e) static_cast<void>(a); _BOTAN_UNUSED_IMPL4(b, c, d, e)
100 #define _BOTAN_UNUSED_IMPL6(a, b, c, d, e, f) static_cast<void>(a); _BOTAN_UNUSED_IMPL5(b, c, d, e, f)
101 #define _BOTAN_UNUSED_IMPL7(a, b, c, d, e, f, g) static_cast<void>(a); _BOTAN_UNUSED_IMPL6(b, c, d, e, f, g)
102 #define _BOTAN_UNUSED_IMPL8(a, b, c, d, e, f, g, h) static_cast<void>(a); _BOTAN_UNUSED_IMPL7(b, c, d, e, f, g, h)
103 #define _BOTAN_UNUSED_IMPL9(a, b, c, d, e, f, g, h, i) static_cast<void>(a); _BOTAN_UNUSED_IMPL8(b, c, d, e, f, g, h, i)
104 #define _BOTAN_UNUSED_GET_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, IMPL_NAME, ...) IMPL_NAME
105 
106 #define BOTAN_UNUSED(...) _BOTAN_UNUSED_GET_IMPL(__VA_ARGS__, \
107  _BOTAN_UNUSED_IMPL9, \
108  _BOTAN_UNUSED_IMPL8, \
109  _BOTAN_UNUSED_IMPL7, \
110  _BOTAN_UNUSED_IMPL6, \
111  _BOTAN_UNUSED_IMPL5, \
112  _BOTAN_UNUSED_IMPL4, \
113  _BOTAN_UNUSED_IMPL3, \
114  _BOTAN_UNUSED_IMPL2, \
115  _BOTAN_UNUSED_IMPL1, \
116  unused dummy rest value \
117  ) /* we got an one of _BOTAN_UNUSED_IMPL*, now call it */ (__VA_ARGS__)
118 
119 }
120 
121 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: alg_id.cpp:13
#define BOTAN_NORETURN
Definition: compiler.h:132
void assertion_failure(const char *expr_str, const char *assertion_made, const char *func, const char *file, int line)
Definition: assert.cpp:13