IMP  2.4.0
The Integrative Modeling Platform
compiler_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/compiler_macros.h
3  * \brief Various compiler workarounds
4  *
5  * Copyright 2007-2015 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPBASE_COMPILER_MACROS_H
9 #define IMPBASE_COMPILER_MACROS_H
10 
11 #include <boost/config.hpp>
12 #include <boost/version.hpp>
13 #if defined(BOOST_NO_CXX11_RANGE_BASED_FOR) || BOOST_VERSION < 105300
14 #include <boost/foreach.hpp>
15 #define IMP_FOREACH(v, r) BOOST_FOREACH(v, r)
16 #else
17 /** Use C++11 range-based for if available or BOOST_FOREACH if not. */
18 #define IMP_FOREACH(v, r) for (v : r)
19 #endif
20 
21 #include <boost/detail/endian.hpp>
22 
23 #define IMP_STRINGIFY(x) #x
24 
25 // recommended by http://gcc.gnu.org/gcc/Function-Names.html
26 #if defined(_MSC_VER)
27 #define IMP_CURRENT_FUNCTION __FUNCTION__
28 #define IMP_CURRENT_PRETTY_FUNCTION __FUNCTION__
29 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
30 #define IMP_CURRENT_FUNCTION __FUNCTION__
31 #define IMP_CURRENT_PRETTY_FUNCTION __PRETTY_FUNCTION__
32 #else
33 #define IMP_CURRENT_FUNCTION __func__
34 #define IMP_CURRENT_PRETTY_FUNCTION __PRETTY_FUNCTION__
35 #endif
36 
37 #ifndef IMP_DOXYGEN
38 #ifdef __GNUC__
39 #define IMP_NO_SIDEEFFECTS __attribute__((pure))
40 #define IMP_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
41 #define IMP_RESTRICT __restrict__
42 
43 #else
44 #define IMP_NO_SIDEEFFECTS
45 #define IMP_WARN_UNUSED_RESULT
46 #define IMP_RESTRICT
47 #endif
48 
49 #else
50 //! Use this to label a function with no side effects
51 /** \advanced */
52 #define IMP_NO_SIDEEFFECTS
53 //! Use this to make the compiler (possibly) warn if the result is not used
54 /** \advanced */
55 #define IMP_WARN_UNUSED_RESULT
56 //! restrict means that a variable is not aliased with this function
57 #define IMP_RESTRICT
58 #endif
59 
60 #ifdef __clang__
61 #define IMP_COMPILER_HAS_OVERRIDE 1
62 #elif defined(__GNUC__) && __cplusplus >= 201103L
63 // probably should be finer here
64 #define IMP_COMPILER_HAS_OVERRIDE 1
65 #else
66 #define IMP_COMPILER_HAS_OVERRIDE 0
67 #endif
68 
69 #ifdef IMP_DOXYGEN
70 //! Cause a compile error if this method does not override a parent method
71 /** This is helpful to catch accidental mismatches of call signatures between
72  the method and the parent method, which would cause the method to be
73  overloaded rather than overridden. Usually this macro should be used
74  whenever implementing a method that is declared virtual in the parent. */
75 #define IMP_OVERRIDE
76 #else
77 #if IMP_COMPILER_HAS_OVERRIDE
78 #define IMP_OVERRIDE override
79 #else
80 #define IMP_OVERRIDE
81 #endif
82 #endif
83 
84 #if defined(IMP_SWIG_WRAPPER)
85 #define IMP_COMPILER_HAS_FINAL 0
86 #elif defined(__clang__)
87 #define IMP_COMPILER_HAS_FINAL 1
88 #elif defined(__GNUC__) && __cplusplus >= 201103L
89 // probably should be finer here
90 #define IMP_COMPILER_HAS_FINAL 1
91 #else
92 #define IMP_COMPILER_HAS_FINAL 0
93 #endif
94 
95 #ifdef IMP_DOXYGEN
96 //! Have the compiler report an error if anything overrides this method
97 #define IMP_FINAL
98 #else
99 #if IMP_COMPILER_HAS_FINAL
100 #define IMP_FINAL final
101 #else
102 #define IMP_FINAL
103 #endif
104 #endif
105 
106 #if defined(__GNUC__) && __cplusplus >= 201103L
107 #define IMP_HAS_NOEXCEPT 1
108 #elif defined(__clang__) && defined(__has_feature)
109 #define IMP_HAS_NOEXCEPT __has_feature(cxx_noexcept)
110 #else
111 #define IMP_HAS_NOEXCEPT 0
112 #endif
113 
114 #if IMP_HAS_NOEXCEPT
115 #define IMP_NOEXCEPT noexcept
116 #define IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Name) \
117  Name(const Name &) = default; \
118  Name &operator=(const Name &) = default
119 #else
120 // probably should be finer here
121 #define IMP_NOEXCEPT throw()
122 #define IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Name)
123 #endif
124 
125 #if defined(__clang__) || defined(__GNUC__)
126 #define IMP_PRAGMA(x) _Pragma(IMP_STRINGIFY(x))
127 
128 #if defined(__clang__)
129 #define IMP_CLANG_PRAGMA(x) IMP_PRAGMA(clang x)
130 #define IMP_GCC_PRAGMA(x)
131 #define IMP_VC_PRAGMA(x)
132 #else
133 #define IMP_CLANG_PRAGMA(x)
134 #define IMP_GCC_PRAGMA(x) IMP_PRAGMA(GCC x)
135 #define IMP_VC_PRAGMA(x)
136 #endif
137 
138 #elif defined(_MSC_VER)
139 #define IMP_PRAGMA(x) __pragma(x)
140 #define IMP_CLANG_PRAGMA(x)
141 #define IMP_GCC_PRAGMA(x)
142 #define IMP_VC_PRAGMA(x) IMP_PRAGMA(x)
143 
144 #else
145 #define IMP_PRAGMA(x)
146 #define IMP_CLANG_PRAGMA(x)
147 #define IMP_GCC_PRAGMA(x)
148 #define IMP_VC_PRAGMA(x)
149 #endif
150 
151 #ifdef __clang__
152 
153 #define IMP_GCC_PUSH_POP(x)
154 
155 #define IMP_COMPILER_ENABLE_WARNINGS \
156  IMP_CLANG_PRAGMA(diagnostic push) \
157  IMP_CLANG_PRAGMA(diagnostic warning "-Wall") \
158  IMP_CLANG_PRAGMA(diagnostic warning "-Wextra") \
159  IMP_CLANG_PRAGMA(diagnostic warning "-Weverything") \
160  IMP_CLANG_PRAGMA(diagnostic ignored "-Wconversion") \
161  IMP_CLANG_PRAGMA(diagnostic ignored "-Wc++11-extensions") \
162  IMP_CLANG_PRAGMA(diagnostic ignored "-Wc++11-compat") \
163  IMP_CLANG_PRAGMA(diagnostic warning "-Wsign-compare") \
164  IMP_CLANG_PRAGMA(diagnostic ignored "-Wunused-member-function")
165 
166 #define IMP_HELPER_MACRO_PUSH_WARNINGS IMP_CLANG_PRAGMA(diagnostic push)
167 
168 #define IMP_HELPER_MACRO_POP_WARNINGS IMP_CLANG_PRAGMA(diagnostic pop)
169 
170 #define IMP_COMPILER_DISABLE_WARNINGS IMP_CLANG_PRAGMA(diagnostic pop)
171 
172 #elif defined(__GNUC__)
173 
174 /*ret+=["-Wno-deprecated",
175  "-Wstrict-aliasing=2",
176  -fno-operator-names",]*/
177 #if __GNUC__ > 4 || __GNUC_MINOR__ >= 6
178 #define IMP_GCC_PUSH_POP(x) IMP_PRAGMA(x)
179 #define IMP_GCC_CXX0X_COMPAT \
180  IMP_GCC_PRAGMA(diagnostic ignored \
181  "-Wc++0x-compa" \
182  "t")
183 #define IMP_GCC_PROTOTYPES \
184  IMP_GCC_PRAGMA(diagnostic warning "-Wmissing-declarations")
185 
186 #define IMP_HELPER_MACRO_PUSH_WARNINGS IMP_GCC_PRAGMA(diagnostic push)
187 
188 #define IMP_HELPER_MACRO_POP_WARNINGS IMP_GCC_PRAGMA(diagnostic pop)
189 
190 #else
191 #define IMP_GCC_PUSH_POP(x)
192 #define IMP_GCC_CXX0X_COMPAT
193 #define IMP_GCC_PROTOTYPES
194 #define IMP_HELPER_MACRO_PUSH_WARNINGS
195 #define IMP_HELPER_MACRO_POP_WARNINGS
196 #endif
197 
198 #define IMP_COMPILER_ENABLE_WARNINGS \
199  IMP_GCC_PUSH_POP(GCC diagnostic push) \
200  IMP_GCC_PRAGMA(diagnostic warning "-Wall") \
201  IMP_GCC_PRAGMA(diagnostic warning "-Wextra") \
202  IMP_GCC_PRAGMA(diagnostic warning "-Winit-self") \
203  IMP_GCC_PRAGMA(diagnostic warning "-Wcast-align") \
204  IMP_GCC_PRAGMA(diagnostic warning "-Woverloaded-virtual") \
205  IMP_GCC_PRAGMA(diagnostic warning "-Wdeprecated-declarations") \
206  IMP_GCC_PRAGMA(diagnostic warning \
207  "-Wundef") IMP_GCC_PROTOTYPES IMP_GCC_CXX0X_COMPAT
208 
209 #define IMP_COMPILER_DISABLE_WARNINGS IMP_GCC_PUSH_POP(GCC diagnostic pop)
210 
211 #elif defined(_MSC_VER)
212 #define IMP_GCC_PUSH_POP(x)
213 
214 #define IMP_COMPILER_ENABLE_WARNINGS \
215  IMP_VC_PRAGMA(warning(push)) IMP_VC_PRAGMA(warning(disable : 4273)) \
216  IMP_VC_PRAGMA(warning(disable : 4244)) \
217  IMP_VC_PRAGMA(warning(disable : 4068)) \
218  IMP_VC_PRAGMA(warning(disable : 4297))
219 
220 #define IMP_COMPILER_DISABLE_WARNINGS IMP_VC_PRAGMA(warning(pop))
221 
222 #define IMP_HELPER_MACRO_PUSH_WARNINGS
223 #define IMP_HELPER_MACRO_POP_WARNINGS
224 
225 #else
226 #define IMP_COMPILER_ENABLE_WARNINGS
227 #define IMP_COMPILER_DISABLE_WARNINGS
228 #define IMP_HELPER_MACRO_PUSH_WARNINGS
229 #define IMP_HELPER_MACRO_POP_WARNINGS
230 #endif
231 
232 #if defined(__GNUC__) || defined(__clang__)
233 #define IMP_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
234 #else
235 #define IMP_DEPRECATED_ATTRIBUTE
236 #endif
237 
238 #endif /* IMPBASE_COMPILER_MACROS_H */