compiler_macros.h
Go to the documentation of this file.
1 #ifndef cetlib_compiler_macros_h
2 #define cetlib_compiler_macros_h
3 
4 ////////////////////////////////////////////////////////////////////////
5 // Define GCC and Clang version tests:
6 ////////////////////////////////////////////////////////////////////////
7 
8 #ifndef GCC_VERSION
9 #if defined __GNUC_PATCHLEVEL__
10 #define GCC_VERSION \
11  (100000 * __GNUC__ + 1000 * __GNUC_MINOR__ + __GNUC_PATCHLEVEL__)
12 #elif defined __GNUC_MINOR__
13 #define GCC_VERSION (10000 * __GNUC__ + 1000 * __GNUC_MINOR__)
14 #elif defined __GNUC__
15 #define GCC_VERSION (10000 * __GNUC__)
16 #else
17 #define GCC_VERSION 0
18 #endif
19 #endif
20 
21 #ifndef CLANG_VERSION
22 #if defined __clang_patchlevel__
23 #define CLANG_VERSION \
24  (100000 * __clang_major__ + 1000 * __clang_minor__ + __clang_patchlevel__)
25 #elif defined __clang_minor__
26 #define CLANG_VERSION (10000 * __clang_major__ + 1000 * __clang_minor__)
27 #elif defined __clang_major__
28 #define CLANG_VERSION (10000 * __clang_major__)
29 #else
30 #define CLANG_VERSION 0
31 #endif
32 #endif
33 
34 #ifndef GCC_IS_AT_LEAST
35 #ifndef __clang__
36 #define GCC_IS_AT_LEAST(major, minor, patch) \
37  GCC_VERSION >= ((100000 * (major)) + (1000 * (minor)) + (patch))
38 #else
39 #define GCC_IS_AT_LEAST(major, minor, patch) 0
40 #endif
41 #endif
42 
43 #ifdef __apple_build_version__
44 #ifndef APPLE_CLANG_IS_AT_LEAST
45 #define APPLE_CLANG_IS_AT_LEAST(major, minor, patch) \
46  CLANG_VERSION >= ((100000 * (major)) + (1000 * (minor)) + (patch))
47 #endif
48 #ifndef CLANG_IS_AT_LEAST
49 #define CLANG_IS_AT_LEAST(major, minor, patch) 0
50 #endif
51 #else
52 #ifndef CLANG_IS_AT_LEAST
53 #define CLANG_IS_AT_LEAST(major, minor, patch) \
54  CLANG_VERSION >= ((100000 * (major)) + (1000 * (minor)) + (patch))
55 #endif
56 #ifndef APPLE_CLANG_IS_AT_LEAST
57 #define APPLE_CLANG_IS_AT_LEAST(major, minor, patch) 0
58 #endif
59 #endif
60 
61 ////////////////////////////////////////////////////////////////////////
62 // Define macros EXTERN_C_FUNC_DECLARE_START and
63 // EXTERN_C_FUNC_DECLARE_END to allow for C++ return types on extern "C"
64 // functions.
65 ////////////////////////////////////////////////////////////////////////
66 
67 #ifndef EXTERN_C_FUNC_DECLARE_START_DETAIL
68 #define EXTERN_C_FUNC_DECLARE_START_DETAIL extern "C" {
69 
70 #ifdef __clang__
71 #define EXTERN_C_FUNC_DECLARE_START \
72  _Pragma("clang diagnostic push") \
73  _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"") \
74  EXTERN_C_FUNC_DECLARE_START_DETAIL
75 #define EXTERN_C_FUNC_DECLARE_END \
76  } \
77  _Pragma("clang diagnostic pop")
78 #else
79 #define EXTERN_C_FUNC_DECLARE_START EXTERN_C_FUNC_DECLARE_START_DETAIL
80 #define EXTERN_C_FUNC_DECLARE_END }
81 #endif
82 #endif
83 
84 ////////////////////////////////////////////////////////////////////////
85 // Define FALLTHROUGH macro to allow case fallthrough.
86 ////////////////////////////////////////////////////////////////////////
87 
88 #ifndef FALLTHROUGH
89 #if __cplusplus >= 201703L
90 #if __has_cpp_attribute(fallthrough)
91 #define FALLTHROUGH [[fallthrough]]
92 #else
93 #define FALLTHROUGH [[gnu::fallthrough]]
94 #endif
95 #else
96 #define FALLTHROUGH while (0)
97 #endif
98 #endif
99 
100 ////////////////////////////////////////////////////////////////////////
101 // Define IGNORE_FALLTHROUGH_START and IGNORE_FALLTHROUGH_END to ignore
102 // implicit fallthrough warnings (e.g.) in included headers.
103 ////////////////////////////////////////////////////////////////////////
104 
105 #ifndef IGNORE_FALLTHROUGH_START
106 #if defined(__clang__) || GCC_IS_AT_LEAST(7, 1, 0)
107 #define IGNORE_FALLTHROUGH_START \
108  _Pragma("GCC diagnostic push") \
109  _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
110 #define IGNORE_FALLTHROUGH_END _Pragma("GCC diagnostic pop")
111 #else
112 #define IGNORE_FALLTHROUGH_START
113 #define IGNORE_FALLTHROUGH_END
114 #endif
115 #endif
116 
117 ////////////////////////////////////////////////////////////////////////
118 // Define UNUSED_PRIVATE_FIELD to label a private unused class data
119 // member.
120 ////////////////////////////////////////////////////////////////////////
121 
122 #ifndef UNUSED_PRIVATE_FIELD
123 #ifdef __clang__
124 #if __has_cpp_attribute(maybe_unused)
125 #define UNUSED_PRIVATE_FIELD [[maybe_unused]]
126 #else
127 #define UNUSED_PRIVATE_FIELD [[gnu::unused]]
128 #endif
129 #else
130 #define UNUSED_PRIVATE_FIELD
131 #endif
132 #endif
133 
134 #endif /* cetlib_compiler_macros_h */
135 
136 // Local Variables:
137 // mode: c++
138 // End: