11/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
22#if !defined INC_PUBNUB_ASSERT
3- #define INC_PUBNUB_ASSERT
3+ #define INC_PUBNUB_ASSERT
44
55#include <stdbool.h>
66
77
88/** The Pubnub ASSERT macros. There are several layers:
9- - highest (PUBNUB_ASSERT_LEVEL_EX): all checks enabled,
9+ - highest (PUBNUB_ASSERT_LEVEL_EX): all checks enabled,
1010 even the long lasting ones
1111 - regular (PUBNUB_ASSERT_LEVEL): only the long lasting
1212 checks are disabled
2323 * PUBNUB_ASSERT_LEVEL_NONE is defined.
2424 */
2525
26- #if defined(PUBNUB_ASSERT_LEVEL_EX ) && ( \
27- defined(PUBNUB_ASSERT_LEVEL ) || defined(PUBNUB_ASSERT_LEVEL_OPT ) || \
28- defined(PUBNUB_ASSERT_LEVEL_NONE ) )
29- #error Cannott define PUBNUB_ASSERT_LEVEL_EX and any lower level (regular, _OPT, _NONE)
26+ #if defined(PUBNUB_ASSERT_LEVEL_EX ) \
27+ && ( defined(PUBNUB_ASSERT_LEVEL ) || defined(PUBNUB_ASSERT_LEVEL_OPT ) \
28+ || defined(PUBNUB_ASSERT_LEVEL_NONE ))
29+ #error Cannot define PUBNUB_ASSERT_LEVEL_EX and any lower level (regular, _OPT, _NONE)
3030#endif
3131
32- #if defined(PUBNUB_ASSERT_LEVEL ) && ( \
33- defined(PUBNUB_ASSERT_LEVEL_OPT ) || defined(PUBNUB_ASSERT_LEVEL_NONE ) )
34- #error Cannott define PUBNUB_ASSERT_LEVEL and any lower level (_OPT, _NONE)
32+ #if defined(PUBNUB_ASSERT_LEVEL ) \
33+ && ( defined(PUBNUB_ASSERT_LEVEL_OPT ) || defined(PUBNUB_ASSERT_LEVEL_NONE ))
34+ #error Cannot define PUBNUB_ASSERT_LEVEL and any lower level (_OPT, _NONE)
3535#endif
3636
3737
38- #if defined(PUBNUB_ASSERT_LEVEL_OPT ) && defined(PUBNUB_ASSERT_LEVEL_NONE )
39- #error Cannott define PUBNUB_ASSERT_LEVEL_OPT and PUBNUB_ASSERT_LEVEL_NONE
38+ #if defined(PUBNUB_ASSERT_LEVEL_OPT ) && defined(PUBNUB_ASSERT_LEVEL_NONE )
39+ #error Cannot define PUBNUB_ASSERT_LEVEL_OPT and PUBNUB_ASSERT_LEVEL_NONE
4040#endif
4141
4242/* If none of ASSERT level defining macros is defined, let's assume
4343 * the highest level.
4444 */
4545
46- #if !defined(PUBNUB_ASSERT_LEVEL_EX ) && \
47- !defined(PUBNUB_ASSERT_LEVEL ) && \
48- !defined(PUBNUB_ASSERT_LEVEL_OPT ) && \
49- !defined(PUBNUB_ASSERT_LEVEL_NONE )
46+ #if !defined(PUBNUB_ASSERT_LEVEL_EX ) && !defined(PUBNUB_ASSERT_LEVEL ) \
47+ && !defined(PUBNUB_ASSERT_LEVEL_OPT ) && !defined(PUBNUB_ASSERT_LEVEL_NONE )
5048#define PUBNUB_ASSERT_LEVEL_EX
5149#endif
5250
5755#endif
5856
5957/** The common ASSERT implementation */
60- #define PUBNUB_ASSERT_IMPL (X ) do { \
61- PUBNUB_ANALYSIS_ASSUME(X); \
62- (X) ? (void)0 : pubnub_assert_failed(#X, __FILE__, __LINE__); \
58+ #define PUBNUB_ASSERT_IMPL (X ) \
59+ do { \
60+ PUBNUB_ANALYSIS_ASSUME(X); \
61+ (X) ? (void)0 : pubnub_assert_failed(#X, __FILE__, __LINE__); \
6362 } while (false)
6463
6564/** Should make the compiler not report "unused variable"
6665 warnings.
6766*/
68- #define PUBNUB_UNUSED (x ) do { (void)sizeof(x); } while (false)
67+ #define PUBNUB_UNUSED (x ) \
68+ do { \
69+ (void)sizeof(x); \
70+ } while (false)
6971
7072
7173/* Define the ASSERT macro for the highest (_EX) level.
7678#define PUBNUB_ASSERT_EX (X ) PUBNUB_UNUSED(X)
7779#endif
7880
79- /* Determine if regular level is to be used.
81+ /* Determine if regular level is to be used.
8082 */
8183#if defined(PUBNUB_ASSERT_LEVEL_EX ) || defined(PUBNUB_ASSERT_LEVEL )
82- #define PUBNUB_ASSERT_IS_ACTIVE // also usable directly in client code
84+ #define PUBNUB_ASSERT_IS_ACTIVE // also usable directly in client code
8385#endif
8486
8587/* Define the ASSERT macro for the regular level.
105107
106108 Users should use the "front-ends" for individual types.
107109*/
108- #define PUBNUB_ASSERT_RICH_IMPL (T , fmt , a , op , b ) do { \
109- T M_a_ = (a), M_b_ = (b); \
110- int holds = M_a_ op M_b_; \
111- PUBNUB_ANALYSIS_ASSUME(holds); \
112- if (!holds) { \
113- char s[300]; \
114- snprintf(s, sizeof s, "`" #a " " #op " " #b "`; `" #a "`=" fmt ", `" #b "`=" fmt, M_a_, M_b_);\
115- pubnub_assert_failed(s, __FILE__, __LINE__); \
116- } \
110+ #define PUBNUB_ASSERT_RICH_IMPL (T , fmt , a , op , b ) \
111+ do { \
112+ T M_a_ = (a), M_b_ = (b); \
113+ int holds = M_a_ op M_b_; \
114+ PUBNUB_ANALYSIS_ASSUME(holds); \
115+ if (!holds) { \
116+ char s[300]; \
117+ snprintf(s, \
118+ sizeof s, \
119+ "`" #a " " #op " " #b "`; `" #a "`=" fmt ", `" #b \
120+ "`=" fmt, \
121+ M_a_, \
122+ M_b_); \
123+ pubnub_assert_failed(s, __FILE__, __LINE__); \
124+ } \
117125 } while (false)
118126
119- #define PUBNUB_ASSERT_INT_IMPL (a , op , b ) PUBNUB_ASSERT_RICH_IMPL(int, "%d", a, op, b)
120- #define PUBNUB_ASSERT_UINT_IMPL (a , op , b ) PUBNUB_ASSERT_RICH_IMPL(unsigned, "%u", a, op, b)
127+ #define PUBNUB_ASSERT_INT_IMPL (a , op , b ) \
128+ PUBNUB_ASSERT_RICH_IMPL(int, "%d", a, op, b)
129+ #define PUBNUB_ASSERT_UINT_IMPL (a , op , b ) \
130+ PUBNUB_ASSERT_RICH_IMPL(unsigned, "%u", a, op, b)
121131
122132/* Define the ASSERT_INT macro for the highest (_EX) level.
123133 */
124134#if defined PUBNUB_ASSERT_LEVEL_EX
125135#define PUBNUB_ASSERT_INT_EX (a , op , b ) PUBNUB_ASSERT_INT_IMPL(a, op, b)
126136#else
127- #define PUBNUB_ASSERT_INT_EX (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
137+ #define PUBNUB_ASSERT_INT_EX (a , op , b ) \
138+ PUBNUB_UNUSED(a); \
139+ PUBNUB_UNUSED(b)
128140#endif
129141
130142/* Define the ASSERT_INT macro for the regular level.
131143 */
132144#if defined(PUBNUB_ASSERT_IS_ACTIVE )
133145#define PUBNUB_ASSERT_INT (a , op , b ) PUBNUB_ASSERT_INT_IMPL(a, op, b)
134146#else
135- #define PUBNUB_ASSERT_INT (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
147+ #define PUBNUB_ASSERT_INT (a , op , b ) \
148+ PUBNUB_UNUSED(a); \
149+ PUBNUB_UNUSED(b)
136150#endif
137151
138152
141155#if !defined(PUBNUB_ASSERT_LEVEL_NONE )
142156#define PUBNUB_ASSERT_INT_OPT (a , op , b ) PUBNUB_ASSERT_INT_IMPL(a, op, b)
143157#else
144- #define PUBNUB_ASSERT_INT_OPT (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
158+ #define PUBNUB_ASSERT_INT_OPT (a , op , b ) \
159+ PUBNUB_UNUSED(a); \
160+ PUBNUB_UNUSED(b)
145161#endif
146162
147163
150166#if defined PUBNUB_ASSERT_LEVEL_EX
151167#define PUBNUB_ASSERT_UINT_EX (a , op , b ) PUBNUB_ASSERT_UINT_IMPL(a, op, b)
152168#else
153- #define PUBNUB_ASSERT_UINT_EX (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
169+ #define PUBNUB_ASSERT_UINT_EX (a , op , b ) \
170+ PUBNUB_UNUSED(a); \
171+ PUBNUB_UNUSED(b)
154172#endif
155173
156174/* Define the ASSERT_UINT macro for the regular level.
157175 */
158176#if defined(PUBNUB_ASSERT_IS_ACTIVE )
159177#define PUBNUB_ASSERT_UINT (a , op , b ) PUBNUB_ASSERT_UINT_IMPL(a, op, b)
160178#else
161- #define PUBNUB_ASSERT_UINT (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
179+ #define PUBNUB_ASSERT_UINT (a , op , b ) \
180+ PUBNUB_UNUSED(a); \
181+ PUBNUB_UNUSED(b)
162182#endif
163183
164184
167187#if !defined(PUBNUB_ASSERT_LEVEL_NONE )
168188#define PUBNUB_ASSERT_UINT_OPT (a , op , b ) PUBNUB_ASSERT_UINT_IMPL(a, op, b)
169189#else
170- #define PUBNUB_ASSERT_UINT_OPT (a , op , b ) PUBNUB_UNUSED(a); PUBNUB_UNUSED(b)
190+ #define PUBNUB_ASSERT_UINT_OPT (a , op , b ) \
191+ PUBNUB_UNUSED(a); \
192+ PUBNUB_UNUSED(b)
171193#endif
172194
173195
182204/** This will invoke the installed assert handler. The default
183205 behavior is pubnub_assert_handler_abort().
184206 */
185- void PUBNUB_NORETURN pubnub_assert_failed (char const * s , char const * file , long line );
207+ void PUBNUB_NORETURN pubnub_assert_failed (char const * s , char const * file , long line );
186208
187209/** Prototype of a Pubnub assertion failure handler. There are several
188210 standard handlers, but you can also provide your own.
189211 @param s The string that defines the failure condition
190212 @param file The name of the source file with the condition
191213 @param line Number of the line of @c file with the condition
192214 */
193- typedef void PUBNUB_NORETURN (* pubnub_assert_handler_t )(char const * s , char const * file , long line );
215+ typedef void PUBNUB_NORETURN (* pubnub_assert_handler_t )(char const * s ,
216+ char const * file ,
217+ long line );
194218
195219/** This will install an assert handler. It can be one of the standard
196220 ones, or your own.
@@ -203,12 +227,16 @@ void pubnub_assert_set_handler(pubnub_assert_handler_t handler);
203227/** This handler will print a message formed from the parameters and
204228 then go to infinite loop. Useful for debugging.
205229*/
206- void PUBNUB_NORETURN pubnub_assert_handler_loop (char const * s , char const * file , long line );
230+ void PUBNUB_NORETURN pubnub_assert_handler_loop (char const * s ,
231+ char const * file ,
232+ long line );
207233
208234/** This handler will print a message formed from the parameters and
209235 then abort (exit, end) the process. Useful for testing.
210236 */
211- void PUBNUB_NORETURN pubnub_assert_handler_abort (char const * s , char const * file , long line );
237+ void PUBNUB_NORETURN pubnub_assert_handler_abort (char const * s ,
238+ char const * file ,
239+ long line );
212240
213241/** This handler will print a message formed from the parameters and
214242 that's it. Useful for getting data from a program execution "in
@@ -222,27 +250,28 @@ void PUBNUB_NORETURN pubnub_assert_handler_abort(char const *s, char const *file
222250 "noreturn" attribute (and we know that and use it), you'll get a
223251 warning or error and will need to ignore or disable it.
224252 */
225- void pubnub_assert_handler_printf (char const * s , char const * file , long line );
253+ void pubnub_assert_handler_printf (char const * s , char const * file , long line );
226254
227255
228- #define PUBNUB_CTASRT2 (pre ,post ,lex ) pre ## post ## lex
229- #define PUBNUB_CTASRT (pre ,post ,lex ) PUBNUB_CTASRT2(pre,post,lex)
256+ #define PUBNUB_CTASRT2 (pre , post , lex ) pre## post## lex
257+ #define PUBNUB_CTASRT (pre , post , lex ) PUBNUB_CTASRT2(pre, post, lex)
230258
231259#if !defined __has_feature
232260#define __has_feature (x ) 0
233261#endif
234262
235- #if __has_feature (c_static_assert ) || (defined(__STDC_VERSION__ ) && (__STDC_VERSION__ >= 201112L ))
263+ #if __has_feature (c_static_assert ) \
264+ || (defined(__STDC_VERSION__ ) && (__STDC_VERSION__ >= 201112L ))
236265
237266/* Delegate to C11 static assert */
238- #define PUBNUB_STATIC_ASSERT (cond ,msg ) _Static_assert((cond), #msg)
267+ #define PUBNUB_STATIC_ASSERT (cond , msg ) _Static_assert((cond), #msg)
239268
240269#elif (defined(_MSC_VER )) && (_MSC_VER >= 1600 )
241270
242271/* This compiler doesn't implement the standard `_Static_assert`, but
243272 does the `static_assert`, "w/out the middleman".
244273*/
245- #define PUBNUB_STATIC_ASSERT (cond ,msg ) static_assert((cond), #msg)
274+ #define PUBNUB_STATIC_ASSERT (cond , msg ) static_assert((cond), #msg)
246275
247276#else
248277
@@ -254,9 +283,11 @@ void pubnub_assert_handler_printf(char const *s, char const *file, long line);
254283 @param msg A message "disguised" as an identifier. So, instead of
255284 `"unknown value"`, use `unknown_value`
256285 */
257- #define PUBNUB_STATIC_ASSERT (cond ,msg ) \
258- extern struct { int PUBNUB_CTASRT(static_assert, _failed_, msg) : !!(cond); } \
259- PUBNUB_CTASRT(static_assert_failed_, msg, __LINE__)
286+ #define PUBNUB_STATIC_ASSERT (cond , msg ) \
287+ extern struct PUBNUB_CTASRT(static_assert, _failed_, msg) { \
288+ int PUBNUB_CTASRT(static_assert, _failed_, msg) \
289+ : !!(cond); \
290+ } PUBNUB_CTASRT(static_assert_failed_, msg, __LINE__)
260291
261292#endif
262293
0 commit comments