IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
compiler_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/compiler_macros.h
3  * \brief Various compiler workarounds
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPKERNEL_COMPILER_MACROS_H
9 #define IMPKERNEL_COMPILER_MACROS_H
10 
11 // Deprecated: just use C++11 range-based for instead
12 #define IMP_FOREACH(v, r) for (v : r)
13 
14 #define IMP_STRINGIFY(x) #x
15 
16 // recommended by http://gcc.gnu.org/gcc/Function-Names.html
17 #if defined(_MSC_VER)
18 #define IMP_CURRENT_FUNCTION __FUNCTION__
19 #define IMP_CURRENT_PRETTY_FUNCTION __FUNCTION__
20 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
21 #define IMP_CURRENT_FUNCTION __FUNCTION__
22 #define IMP_CURRENT_PRETTY_FUNCTION __PRETTY_FUNCTION__
23 #else
24 #define IMP_CURRENT_FUNCTION __func__
25 #define IMP_CURRENT_PRETTY_FUNCTION __PRETTY_FUNCTION__
26 #endif
27 
28 #ifndef IMP_DOXYGEN
29 #ifdef __GNUC__
30 #define IMP_NO_SIDEEFFECTS __attribute__((pure))
31 #define IMP_UNUSED_FUNCTION __attribute__((unused))
32 #define IMP_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
33 #define IMP_RESTRICT __restrict__
34 
35 #else
36 #define IMP_NO_SIDEEFFECTS
37 #define IMP_UNUSED_FUNCTION
38 #define IMP_WARN_UNUSED_RESULT
39 #define IMP_RESTRICT
40 #endif
41 
42 #else
43 //! Use this to label a function with no side effects
44 /** \advancedmethod */
45 #define IMP_NO_SIDEEFFECTS
46 //! Use this to make the compiler (possibly) warn if the result is not used
47 /** \advancedmethod */
48 #define IMP_WARN_UNUSED_RESULT
49 //! Label a function that is never called
50 /** \advancedmethod */
51 #define IMP_UNUSED_FUNCTION
52 //! restrict means that a variable is not aliased with this function
53 #define IMP_RESTRICT
54 #endif
55 
56 
57 // Deprecated: just use the 'override' keyword directly
58 #ifdef IMP_DOXYGEN
59 #define IMP_OVERRIDE
60 #else
61 #define IMP_OVERRIDE override
62 #endif
63 
64 #ifdef IMP_DOXYGEN
65 #define IMP_FINAL
66 //! Have the compiler report an error if anything overrides this method
67 #define IMP_SWIG_FINAL
68 #else
69 #if defined(IMP_SWIG_WRAPPER) || defined(SWIG)
70 #define IMP_FINAL
71 #define IMP_SWIG_FINAL
72 #else
73 #define IMP_FINAL final
74 #define IMP_SWIG_FINAL final
75 #endif
76 #endif
77 
78 #if defined(__GNUC__) && __cplusplus >= 201103L
79 #define IMP_HAS_NOEXCEPT 1
80 #elif defined(__clang__) && defined(__has_feature)
81 #define IMP_HAS_NOEXCEPT __has_feature(cxx_noexcept)
82 #else
83 #define IMP_HAS_NOEXCEPT 0
84 #endif
85 
86 #if IMP_HAS_NOEXCEPT
87 #define IMP_NOEXCEPT noexcept
88 #define IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Name) \
89  Name(const Name &) = default; \
90  Name &operator=(const Name &) = default
91 #else
92 // probably should be finer here
93 #define IMP_NOEXCEPT throw()
94 #define IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Name)
95 #endif
96 
97 #if defined(__clang__) || defined(__GNUC__)
98 #define IMP_PRAGMA(x) _Pragma(IMP_STRINGIFY(x))
99 
100 #if defined(__clang__)
101 #define IMP_CLANG_PRAGMA(x) IMP_PRAGMA(clang x)
102 #define IMP_GCC_PRAGMA(x)
103 #define IMP_VC_PRAGMA(x)
104 #else
105 #define IMP_CLANG_PRAGMA(x)
106 #define IMP_GCC_PRAGMA(x) IMP_PRAGMA(GCC x)
107 #define IMP_VC_PRAGMA(x)
108 #endif
109 
110 #elif defined(_MSC_VER)
111 #define IMP_PRAGMA(x) __pragma(x)
112 #define IMP_CLANG_PRAGMA(x)
113 #define IMP_GCC_PRAGMA(x)
114 #define IMP_VC_PRAGMA(x) IMP_PRAGMA(x)
115 
116 #else
117 #define IMP_PRAGMA(x)
118 #define IMP_CLANG_PRAGMA(x)
119 #define IMP_GCC_PRAGMA(x)
120 #define IMP_VC_PRAGMA(x)
121 #endif
122 
123 #ifdef __clang__
124 
125 #define IMP_GCC_PUSH_POP(x)
126 
127 #define IMP_COMPILER_ENABLE_WARNINGS \
128  IMP_CLANG_PRAGMA(diagnostic push) \
129  IMP_CLANG_PRAGMA(diagnostic warning "-Wall") \
130  IMP_CLANG_PRAGMA(diagnostic warning "-Wextra") \
131  IMP_CLANG_PRAGMA(diagnostic warning "-Weverything") \
132  IMP_CLANG_PRAGMA(diagnostic ignored "-Wconversion") \
133  IMP_CLANG_PRAGMA(diagnostic ignored "-Wc++11-extensions") \
134  IMP_CLANG_PRAGMA(diagnostic ignored "-Wc++11-compat") \
135  IMP_CLANG_PRAGMA(diagnostic warning "-Wsign-compare") \
136  IMP_CLANG_PRAGMA(diagnostic ignored "-Wunused-member-function")
137 
138 #define IMP_HELPER_MACRO_PUSH_WARNINGS IMP_CLANG_PRAGMA(diagnostic push)
139 
140 #define IMP_HELPER_MACRO_POP_WARNINGS IMP_CLANG_PRAGMA(diagnostic pop)
141 
142 #define IMP_COMPILER_DISABLE_WARNINGS IMP_CLANG_PRAGMA(diagnostic pop)
143 
144 #elif defined(__GNUC__)
145 
146 /*ret+=["-Wno-deprecated",
147  "-Wstrict-aliasing=2",
148  -fno-operator-names",]*/
149 #if __GNUC__ > 4 || __GNUC_MINOR__ >= 6
150 #define IMP_GCC_PUSH_POP(x) IMP_PRAGMA(x)
151 #define IMP_GCC_CXX0X_COMPAT \
152  IMP_GCC_PRAGMA(diagnostic ignored \
153  "-Wc++0x-compat")
154 #ifdef IMP_SWIG_WRAPPER
155 #define IMP_GCC_PROTOTYPES
156 #else
157 #define IMP_GCC_PROTOTYPES \
158  IMP_GCC_PRAGMA(diagnostic warning "-Wmissing-declarations")
159 #endif
160 
161 #define IMP_HELPER_MACRO_PUSH_WARNINGS IMP_GCC_PRAGMA(diagnostic push)
162 
163 #define IMP_HELPER_MACRO_POP_WARNINGS IMP_GCC_PRAGMA(diagnostic pop)
164 
165 #else
166 #define IMP_GCC_PUSH_POP(x)
167 #define IMP_GCC_CXX0X_COMPAT
168 #define IMP_GCC_PROTOTYPES
169 #define IMP_HELPER_MACRO_PUSH_WARNINGS
170 #define IMP_HELPER_MACRO_POP_WARNINGS
171 #endif
172 
173 // Warn about missing IMP_OVERRIDE on virtual methods if gcc is new enough
174 #if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
175 #ifdef IMP_SWIG_WRAPPER
176 #define IMP_GCC_OVERRIDE
177 #else
178 #define IMP_GCC_OVERRIDE \
179  IMP_GCC_PRAGMA(diagnostic warning "-Wsuggest-override")
180 #endif
181 #else
182 #define IMP_GCC_OVERRIDE
183 #endif
184 
185 #define IMP_COMPILER_ENABLE_WARNINGS \
186  IMP_GCC_PUSH_POP(GCC diagnostic push) \
187  IMP_GCC_PRAGMA(diagnostic warning "-Wall") \
188  IMP_GCC_PRAGMA(diagnostic warning "-Wextra") \
189  IMP_GCC_PRAGMA(diagnostic warning "-Winit-self") \
190  IMP_GCC_PRAGMA(diagnostic warning "-Wcast-align") \
191  IMP_GCC_PRAGMA(diagnostic warning "-Woverloaded-virtual") \
192  IMP_GCC_PRAGMA(diagnostic warning "-Wdeprecated-declarations") \
193  IMP_GCC_PRAGMA(diagnostic warning "-Wundef") \
194  IMP_GCC_PROTOTYPES IMP_GCC_CXX0X_COMPAT IMP_GCC_OVERRIDE
195 
196 #define IMP_COMPILER_DISABLE_WARNINGS IMP_GCC_PUSH_POP(GCC diagnostic pop)
197 
198 #elif defined(_MSC_VER)
199 #define IMP_GCC_PUSH_POP(x)
200 
201 #define IMP_COMPILER_ENABLE_WARNINGS \
202  IMP_VC_PRAGMA(warning(push)) IMP_VC_PRAGMA(warning(disable : 4273)) \
203  IMP_VC_PRAGMA(warning(disable : 4244)) \
204  IMP_VC_PRAGMA(warning(disable : 4068)) \
205  IMP_VC_PRAGMA(warning(disable : 4297))
206 
207 #define IMP_COMPILER_DISABLE_WARNINGS IMP_VC_PRAGMA(warning(pop))
208 
209 #define IMP_HELPER_MACRO_PUSH_WARNINGS
210 #define IMP_HELPER_MACRO_POP_WARNINGS
211 
212 #else
213 #define IMP_COMPILER_ENABLE_WARNINGS
214 #define IMP_COMPILER_DISABLE_WARNINGS
215 #define IMP_HELPER_MACRO_PUSH_WARNINGS
216 #define IMP_HELPER_MACRO_POP_WARNINGS
217 #endif
218 
219 #if defined(__GNUC__) || defined(__clang__)
220 #define IMP_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
221 #else
222 #define IMP_DEPRECATED_ATTRIBUTE
223 #endif
224 
225 // likely/unlikely macros provide manual optimization of branch
226 // prediction - particularly useful for rare cases in scoring
227 // functions, but use only if you know what you're doing cause
228 // profiler is better at this in 99.9% of times:
229 #if defined(__GNUC__)
230 #define IMP_LIKELY(x) __builtin_expect (!!(x), 1)
231 #define IMP_UNLIKELY(x) __builtin_expect (!!(x), 0)
232 #else // ifdef __GNUC__
233 #define IMP_LIKELY(x) x
234 #define IMP_UNLIKELY(x) x
235 #endif // ifdef __GNUC__
236 
237 #if defined(_MSC_VER)
238 //! platform independent C open() method
239 #define IMP_C_OPEN _open
240 #define IMP_C_CLOSE _close
241  //! platform independent C flag for open() method
242 #define IMP_C_OPEN_FLAG(x) _##x
243 #define IMP_C_OPEN_BINARY _O_BINARY
244 #else
245 //! platform independent C open() method
246 #define IMP_C_OPEN open
247 #define IMP_C_CLOSE close
248 //! platform independent C flag for open() method
249 #define IMP_C_OPEN_FLAG(x) x
250 #define IMP_C_OPEN_BINARY 0
251 #endif
252 
253 #endif /* IMPKERNEL_COMPILER_MACROS_H */