• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/lib/utils/assert.h
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/compiler.h>
13

14
namespace Botan {
15

16
/**
17
* Called when an assertion fails
18
* Throws an Exception object
19
*/
20
[[noreturn]] void BOTAN_PUBLIC_API(2, 0)
21
   assertion_failure(const char* expr_str, const char* assertion_made, const char* func, const char* file, int line);
22

23
/**
24
* Called when an invalid argument is used
25
* Throws Invalid_Argument
26
*/
27
[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_argument(const char* message, const char* func, const char* file);
28

29
#define BOTAN_ARG_CHECK(expr, msg)                               \
30
   do {                                                          \
31
      if(!(expr))                                                \
32
         Botan::throw_invalid_argument(msg, __func__, __FILE__); \
33
   } while(0)
34

35
/**
36
* Called when an invalid state is encountered
37
* Throws Invalid_State
38
*/
39
[[noreturn]] void BOTAN_UNSTABLE_API throw_invalid_state(const char* message, const char* func, const char* file);
40

41
#define BOTAN_STATE_CHECK(expr)                                 \
42
   do {                                                         \
43
      if(!(expr))                                               \
44
         Botan::throw_invalid_state(#expr, __func__, __FILE__); \
45
   } while(0)
46

47
/**
48
* Make an assertion
49
*/
50
#define BOTAN_ASSERT(expr, assertion_made)                                              \
51
   do {                                                                                 \
52
      if(!(expr))                                                                       \
53
         Botan::assertion_failure(#expr, assertion_made, __func__, __FILE__, __LINE__); \
54
   } while(0)
55

56
/**
57
* Make an assertion
58
*/
59
#define BOTAN_ASSERT_NOMSG(expr)                                            \
60
   do {                                                                     \
61
      if(!(expr))                                                           \
62
         Botan::assertion_failure(#expr, "", __func__, __FILE__, __LINE__); \
63
   } while(0)
64

65
/**
66
* Assert that value1 == value2
67
*/
68
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)                                               \
69
   do {                                                                                                \
70
      if((expr1) != (expr2))                                                                           \
71
         Botan::assertion_failure(#expr1 " == " #expr2, assertion_made, __func__, __FILE__, __LINE__); \
72
   } while(0)
73

74
/**
75
* Assert that expr1 (if true) implies expr2 is also true
76
*/
77
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)                                              \
78
   do {                                                                                          \
79
      if((expr1) && !(expr2))                                                                    \
80
         Botan::assertion_failure(#expr1 " implies " #expr2, msg, __func__, __FILE__, __LINE__); \
81
   } while(0)
82

83
/**
84
* Assert that a pointer is not null
85
*/
86
#define BOTAN_ASSERT_NONNULL(ptr)                                                         \
87
   do {                                                                                   \
88
      if((ptr) == nullptr)                                                                \
89
         Botan::assertion_failure(#ptr " is not null", "", __func__, __FILE__, __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) \
99
      do {                          \
100
      } while(0)
101

102
#endif
103

104
/**
105
* Mark variable as unused.
106
*
107
* Takes any number of arguments and marks all as unused, for instance
108
* BOTAN_UNUSED(a); or BOTAN_UNUSED(x, y, z);
109
*/
110
template <typename T>
111
void ignore_param(T&&) {}
112

113
template <typename... T>
114
void ignore_params(T&&... args) {
928✔
115
   (ignore_param(args), ...);
116
}
185✔
117

118
#define BOTAN_UNUSED Botan::ignore_params
119

120
/*
121
* Define Botan::unreachable()
122
*
123
* There is a pending WG21 proposal for `std::unreachable()`
124
*   http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0627r3.pdf
125
*/
126
[[noreturn]] BOTAN_FORCE_INLINE void unreachable() {
×
127
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_unreachable)
128
   __builtin_unreachable();
×
129
#elif defined(_MSC_VER)  // MSVC
130
   __assume(false);
131
#else
132
   return;  // undefined behaviour, just like the others...
133
#endif
134
}
135

136
}
137

138
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc