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