FreeRDP
assert-api.h
1 
21 #pragma once
22 
23 #include <stdlib.h>
24 #include <assert.h>
25 
26 #include <winpr/config.h>
27 #include <winpr/platform.h>
28 
29 #if defined(WITH_VERBOSE_WINPR_ASSERT) && (WITH_VERBOSE_WINPR_ASSERT != 0)
30 #define winpr_internal_assert(cond, file, fkt, line) \
31  do \
32  { \
33  if (!(cond)) \
34  winpr_int_assert(#cond, (file), (fkt), (line)); \
35  } while (0)
36 
37 #else
38 #define winpr_internal_assert(cond, file, fkt, line) assert(cond)
39 #endif
40 
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45 
46  /* this function meant only to be used by WINPR_ASSERT
47  * it needs to be exported as our assert implementation calls this for debug logging.
48  *
49  * also export when WITH_VERBOSE_WINPR_ASSERT is disabled as other software might compile with
50  * it enabled
51  */
52  WINPR_API WINPR_NORETURN(void winpr_int_assert(const char* condstr, const char* file,
53  const char* fkt, size_t line));
54 
55 #ifdef __cplusplus
56 }
57 #endif
58 
59 #define WINPR_ASSERT_AT(cond, file, fkt, line) \
60  do \
61  { \
62  WINPR_PRAGMA_DIAG_PUSH \
63  WINPR_PRAGMA_DIAG_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE \
64  WINPR_PRAGMA_DIAG_TAUTOLOGICAL_VALUE_RANGE_COMPARE \
65  WINPR_PRAGMA_DIAG_IGNORED_UNKNOWN_PRAGMAS \
66  WINPR_DO_COVERITY_PRAGMA(coverity compliance block \(deviate "CONSTANT_EXPRESSION_RESULT" \
67  "WINPR_ASSERT" \) \
68  \(deviate "NO_EFFECT" \
69  "WINPR_ASSERT" \)) \
70  \
71  winpr_internal_assert((cond), (file), (fkt), (line)); \
72  \
73  WINPR_DO_COVERITY_PRAGMA(coverity compliance end_block "CONSTANT_EXPRESSION_RESULT" \
74  "NO_EFFECT") \
75  WINPR_PRAGMA_DIAG_POP \
76  } while (0)
77 #define WINPR_ASSERT(cond) WINPR_ASSERT_AT((cond), __FILE__, __func__, __LINE__)
78 
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #endif
83 #if defined(__cplusplus) && (__cplusplus >= 201703L) // C++ 17
84 #define WINPR_STATIC_ASSERT(cond) static_assert(cond)
85 #elif defined(__cplusplus) && (__cplusplus >= 201103L) // C++ 11
86 #define WINPR_STATIC_ASSERT(cond) static_assert(cond, #cond)
87 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L) // C23
88 #define WINPR_STATIC_ASSERT(cond) static_assert(cond)
89 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) // C11
90 #define WINPR_STATIC_ASSERT(cond) _Static_assert(cond, #cond)
91 #else
92 WINPR_PRAGMA_WARNING("static-assert macro not supported on this platform")
93 #define WINPR_STATIC_ASSERT(cond) assert(cond)
94 #endif
95 
96 #ifdef __cplusplus
97 }
98 #endif