/src/FreeRDP/winpr/include/winpr/assert-api.h
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Runtime ASSERT macros |
4 | | * |
5 | | * Copyright 2021 Armin Novak <armin.novak@thincast.com> |
6 | | * Copyright 2021 Thincast Technologies GmbH |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | |
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 | 100M | do \ |
32 | 100M | { \ |
33 | 100M | if (!(cond)) \ |
34 | 100M | winpr_int_assert(#cond, (file), (fkt), (line)); \ |
35 | 100M | } 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 | 100M | do \ |
61 | 100M | { \ |
62 | 100M | WINPR_PRAGMA_DIAG_PUSH \ |
63 | 100M | WINPR_PRAGMA_DIAG_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE \ |
64 | 100M | WINPR_PRAGMA_DIAG_TAUTOLOGICAL_VALUE_RANGE_COMPARE \ |
65 | 100M | WINPR_PRAGMA_DIAG_IGNORED_UNKNOWN_PRAGMAS \ |
66 | 100M | WINPR_DO_COVERITY_PRAGMA( \ |
67 | 100M | coverity compliance block deviate 'CONSTANT_EXPRESSION_RESULT' 'WINPR_ASSERT') \ |
68 | 100M | WINPR_DO_COVERITY_PRAGMA(coverity compliance block deviate : 2 'NO_EFFECT' 'WINPR_ASSERT') \ |
69 | 100M | \ |
70 | 100M | winpr_internal_assert((cond), (file), (fkt), (line)); \ |
71 | 100M | \ |
72 | 100M | WINPR_DO_COVERITY_PRAGMA( \ |
73 | 100M | coverity compliance end_block 'CONSTANT_EXPRESSION_RESULT' 'NO_EFFECT') \ |
74 | 100M | WINPR_PRAGMA_DIAG_POP \ |
75 | 100M | } while (0) |
76 | 100M | #define WINPR_ASSERT(cond) WINPR_ASSERT_AT((cond), __FILE__, __func__, __LINE__) |
77 | | |
78 | | #ifdef __cplusplus |
79 | | extern "C" |
80 | | { |
81 | | #endif |
82 | | #if defined(__cplusplus) && (__cplusplus >= 201703L) // C++ 17 |
83 | | #define WINPR_STATIC_ASSERT(cond) static_assert(cond) |
84 | | #elif defined(__cplusplus) && (__cplusplus >= 201103L) // C++ 11 |
85 | | #define WINPR_STATIC_ASSERT(cond) static_assert(cond, #cond) |
86 | | #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L) // C23 |
87 | | #define WINPR_STATIC_ASSERT(cond) static_assert(cond) |
88 | | #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) // C11 |
89 | | #define WINPR_STATIC_ASSERT(cond) _Static_assert(cond, #cond) |
90 | | #else |
91 | | WINPR_PRAGMA_WARNING("static-assert macro not supported on this platform") |
92 | | #define WINPR_STATIC_ASSERT(cond) assert(cond) |
93 | | #endif |
94 | | |
95 | | #ifdef __cplusplus |
96 | | } |
97 | | #endif |