Botan  2.6.0
Crypto and TLS for C++11
assert.h
Go to the documentation of this file.
1 /*
2 * Runtime assertion checking
3 * (C) 2010,2018 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 */
21  assertion_failure(const char* expr_str,
22  const char* assertion_made,
23  const char* func,
24  const char* file,
25  int line);
26 
27 /**
28 * Make an assertion
29 */
30 #define BOTAN_ASSERT(expr, assertion_made) \
31  do { \
32  if(!(expr)) \
33  Botan::assertion_failure(#expr, \
34  assertion_made, \
35  BOTAN_CURRENT_FUNCTION, \
36  __FILE__, \
37  __LINE__); \
38  } while(0)
39 
40 /**
41 * Make an assertion
42 */
43 #define BOTAN_ASSERT_NOMSG(expr) \
44  do { \
45  if(!(expr)) \
46  Botan::assertion_failure(#expr, \
47  "", \
48  BOTAN_CURRENT_FUNCTION, \
49  __FILE__, \
50  __LINE__); \
51  } while(0)
52 
53 /**
54 * Assert that value1 == value2
55 */
56 #define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made) \
57  do { \
58  if((expr1) != (expr2)) \
59  Botan::assertion_failure(#expr1 " == " #expr2, \
60  assertion_made, \
61  BOTAN_CURRENT_FUNCTION, \
62  __FILE__, \
63  __LINE__); \
64  } while(0)
65 
66 /**
67 * Assert that expr1 (if true) implies expr2 is also true
68 */
69 #define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg) \
70  do { \
71  if((expr1) && !(expr2)) \
72  Botan::assertion_failure(#expr1 " implies " #expr2, \
73  msg, \
74  BOTAN_CURRENT_FUNCTION, \
75  __FILE__, \
76  __LINE__); \
77  } while(0)
78 
79 /**
80 * Assert that a pointer is not null
81 */
82 #define BOTAN_ASSERT_NONNULL(ptr) \
83  do { \
84  if((ptr) == nullptr) \
85  Botan::assertion_failure(#ptr " is not null", \
86  "", \
87  BOTAN_CURRENT_FUNCTION, \
88  __FILE__, \
89  __LINE__); \
90  } while(0)
91 
92 #if defined(BOTAN_ENABLE_DEBUG_ASSERTS)
93 
94 #define BOTAN_DEBUG_ASSERT(expr) BOTAN_ASSERT_NOMSG(expr)
95 
96 #else
97 
98 #define BOTAN_DEBUG_ASSERT(expr) do {} while(0)
99 
100 #endif
101 
102 /**
103 * Mark variable as unused. Takes between 1 and 9 arguments and marks all as unused,
104 * e.g. BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
105 */
106 #define _BOTAN_UNUSED_IMPL1(a) static_cast<void>(a)
107 #define _BOTAN_UNUSED_IMPL2(a, b) static_cast<void>(a); _BOTAN_UNUSED_IMPL1(b)
108 #define _BOTAN_UNUSED_IMPL3(a, b, c) static_cast<void>(a); _BOTAN_UNUSED_IMPL2(b, c)
109 #define _BOTAN_UNUSED_IMPL4(a, b, c, d) static_cast<void>(a); _BOTAN_UNUSED_IMPL3(b, c, d)
110 #define _BOTAN_UNUSED_IMPL5(a, b, c, d, e) static_cast<void>(a); _BOTAN_UNUSED_IMPL4(b, c, d, e)
111 #define _BOTAN_UNUSED_IMPL6(a, b, c, d, e, f) static_cast<void>(a); _BOTAN_UNUSED_IMPL5(b, c, d, e, f)
112 #define _BOTAN_UNUSED_IMPL7(a, b, c, d, e, f, g) static_cast<void>(a); _BOTAN_UNUSED_IMPL6(b, c, d, e, f, g)
113 #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)
114 #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)
115 #define _BOTAN_UNUSED_GET_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, IMPL_NAME, ...) IMPL_NAME
116 
117 #define BOTAN_UNUSED(...) _BOTAN_UNUSED_GET_IMPL(__VA_ARGS__, \
118  _BOTAN_UNUSED_IMPL9, \
119  _BOTAN_UNUSED_IMPL8, \
120  _BOTAN_UNUSED_IMPL7, \
121  _BOTAN_UNUSED_IMPL6, \
122  _BOTAN_UNUSED_IMPL5, \
123  _BOTAN_UNUSED_IMPL4, \
124  _BOTAN_UNUSED_IMPL3, \
125  _BOTAN_UNUSED_IMPL2, \
126  _BOTAN_UNUSED_IMPL1, \
127  unused dummy rest value \
128  ) /* we got an one of _BOTAN_UNUSED_IMPL*, now call it */ (__VA_ARGS__)
129 
130 }
131 
132 #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