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 
13 #elif defined __GNUC_MINOR__
14 #define GCC_VERSION (10000 * __GNUC__ + 1000 * __GNUC_MINOR__)
15 
16 #elif defined __GNUC__
17 #define GCC_VERSION (10000 * __GNUC__)
18 
19 #else
20 #define GCC_VERSION 0
21 
22 #endif
23 #endif
24 
25 #ifndef CLANG_VERSION
26 #if defined __clang_patchlevel__
27 #define CLANG_VERSION \
28  (100000 * __clang_major__ + 1000 * __clang_minor__ + __clang_patchlevel__)
29 
30 #elif defined __clang_minor__
31 #define CLANG_VERSION (10000 * __clang_major__ + 1000 * __clang_minor__)
32 
33 #elif defined __clang_major__
34 #define CLANG_VERSION (10000 * __clang_major__)
35 
36 #else
37 #define CLANG_VERSION 0
38 
39 #endif
40 #endif
41 
42 #ifndef GCC_IS_AT_LEAST
43 #define GCC_IS_AT_LEAST(major, minor, patch) \
44  (GCC_VERSION >= ((100000 * (major)) + (1000 * (minor)) + (patch)))
45 #endif
46 
47 #ifndef CLANG_IS_AT_LEAST
48 #define CLANG_IS_AT_LEAST(major, minor, patch) \
49  (CLANG_VERSION >= ((100000 * (major)) + (1000 * (minor)) + (patch)))
50 #endif
51 
52 ////////////////////////////////////////////////////////////////////////
53 // Define macros EXTERN_C_FUNC_DECLARE_START and
54 // EXTERN_C_FUNC_DECLARE_END to allow for C++ return types on extern "C"
55 // functions.
56 ////////////////////////////////////////////////////////////////////////
57 
58 #ifndef EXTERN_C_FUNC_DECLARE_START_DETAIL
59 #define EXTERN_C_FUNC_DECLARE_START_DETAIL extern "C" {
60 
61 #ifdef __clang__
62 #define EXTERN_C_FUNC_DECLARE_START \
63  _Pragma("clang diagnostic push") \
64  _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"") \
65  EXTERN_C_FUNC_DECLARE_START_DETAIL
66 #define EXTERN_C_FUNC_DECLARE_END \
67  } \
68  _Pragma("clang diagnostic pop")
69 #else
70 #define EXTERN_C_FUNC_DECLARE_START EXTERN_C_FUNC_DECLARE_START_DETAIL
71 #define EXTERN_C_FUNC_DECLARE_END }
72 #endif
73 #endif
74 
75 ////////////////////////////////////////////////////////////////////////
76 // Define FALLTHROUGH macro to allow case fallthrough.
77 ////////////////////////////////////////////////////////////////////////
78 #ifndef FALLTHROUGH
79 #if __cplusplus >= 201703L
80 #if __has_cpp_attribute(fallthrough)
81 #define FALLTHROUGH [[fallthrough]]
82 #else
83 #define FALLTHROUGH [[gnu::fallthrough]]
84 #endif
85 #else
86 #define FALLTHROUGH while (0)
87 #endif
88 #endif
89 
90 ////////////////////////////////////////////////////////////////////////
91 // Define IGNORE_FALLTHROUGH_START and IGNORE_FALLTHROUGH_END to ignore
92 // implicit fallthrough warnings (e.g.) in included headers.
93 ////////////////////////////////////////////////////////////////////////
94 
95 #ifndef IGNORE_FALLTHROUGH_START
96 #if GCC_IS_AT_LEAST(7, 1, 0) || defined(__clang__)
97 #define IGNORE_FALLTHROUGH_START \
98  _Pragma("GCC diagnostic push") \
99  _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
100 #define IGNORE_FALLTHROUGH_END _Pragma("GCC diagnostic pop")
101 #else
102 #define IGNORE_FALLTHROUGH_START
103 #define IGNORE_FALLTHROUGH_END
104 #endif
105 #endif
106 
107 #ifndef UNUSED_PRIVATE_FIELD
108 #ifdef __clang__
109 #define UNUSED_PRIVATE_FIELD [[gnu::unused]]
110 #else
111 #define UNUSED_PRIVATE_FIELD
112 #endif
113 #endif
114 
115 #endif /* cetlib_compiler_macros_h */
116 
117 // Local Variables:
118 // mode: c++
119 // End: