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