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