1/*
2 * cc_macros.h
3 * corecrypto
4 *
5 * Created on 01/11/2012
6 *
7 * Copyright (c) 2012,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11#ifndef _CORECRYPTO_CC_MACROS_H_
12#define _CORECRYPTO_CC_MACROS_H_
13
14#include <corecrypto/cc_config.h>
15
16#ifndef __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING
17#define __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING ""
18#endif
19
20#ifndef __CC_DEBUG_ASSERT_PRODUCTION_CODE
21#define __CC_DEBUG_ASSERT_PRODUCTION_CODE !CORECRYPTO_DEBUG
22#endif
23
24#if CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS
25
26#if !CC_KERNEL
27 #include <string.h> // for strstr
28#endif // !CC_KERNEL
29
30CC_UNUSED static char *cc_strstr(const char *file) {
31#if CC_KERNEL
32 (void) file;
33#else
34 const char cc_char []="corecrypto";
35 char *p=strstr(file, cc_char);
36 if (p) return (p+strlen(cc_char)+1);
37#endif
38 return NULL;
39}
40
41#define __CC_DEBUG_REQUIRE_MESSAGE(name, assertion, label, message, file, line, value) \
42{char *___t = cc_strstr(file); cc_printf( "require: %s, %s%s:%d\n", assertion, (message!=0) ? message : "", ___t==NULL?file:___t, line);}
43
44#endif // CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS
45
46#ifndef cc_require
47#if (__CC_DEBUG_ASSERT_PRODUCTION_CODE) || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS)
48 #if defined(_WIN32) && defined (__clang__)
49 #define cc_require(assertion, exceptionLabel) \
50 do { \
51 if (!(assertion) ) { \
52 goto exceptionLabel; \
53 } \
54 } while ( 0 )
55 #else
56 #define cc_require(assertion, exceptionLabel) \
57 do { \
58 if ( __builtin_expect(!(assertion), 0) ) { \
59 goto exceptionLabel; \
60 } \
61 } while ( 0 )
62 #endif
63#else
64 #define cc_require(assertion, exceptionLabel) \
65 do { \
66 if ( __builtin_expect(!(assertion), 0) ) { \
67 __CC_DEBUG_REQUIRE_MESSAGE(__CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \
68 #assertion, #exceptionLabel, 0, __FILE__, __LINE__, 0); \
69 goto exceptionLabel; \
70 } \
71 } while ( 0 )
72#endif
73#endif
74
75#ifndef cc_require_action
76#if __CC_DEBUG_ASSERT_PRODUCTION_CODE || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS)
77 #if defined(_WIN32) && defined(__clang__)
78 #define cc_require_action(assertion, exceptionLabel, action) \
79 do \
80 { \
81 if (!(assertion)) \
82 { \
83 { \
84 action; \
85 } \
86 goto exceptionLabel; \
87 } \
88 } while ( 0 )
89 #else
90 #define cc_require_action(assertion, exceptionLabel, action) \
91 do \
92 { \
93 if ( __builtin_expect(!(assertion), 0) ) \
94 { \
95 { \
96 action; \
97 } \
98 goto exceptionLabel; \
99 } \
100 } while ( 0 )
101 #endif
102#else
103 #define cc_require_action(assertion, exceptionLabel, action) \
104 do \
105 { \
106 if ( __builtin_expect(!(assertion), 0) ) \
107 { \
108 __CC_DEBUG_REQUIRE_MESSAGE( \
109 __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \
110 #assertion, #exceptionLabel, 0, __FILE__, __LINE__, 0); \
111 { \
112 action; \
113 } \
114 goto exceptionLabel; \
115 } \
116 } while ( 0 )
117#endif
118#endif
119
120#endif /* _CORECRYPTO_CC_MACROS_H_ */
121