IMP  2.0.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-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPBASE_BASE_COMPILER_MACROS_H
9 #define IMPBASE_BASE_COMPILER_MACROS_H
10 
11 #include <boost/detail/endian.hpp>
12 
13 #define IMP_STRINGIFY(x) #x
14 
15 // recommended by http://gcc.gnu.org/gcc/Function-Names.html
16 #if defined(_MSC_VER)
17 # define __func__ __FUNCTION__
18 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
19 # if __GNUC__ >= 2
20 # define __func__ __FUNCTION__
21 # else
22 # define __func__ "<unknown>"
23 # endif
24 #endif
25 
26 
27 #ifndef IMP_DOXYGEN
28 #ifdef __GNUC__
29 //! Use this to label a function with no side effects
30 /** \advanced */
31 #define IMP_NO_SIDEEFFECTS __attribute__ ((pure))
32 //! Use this to make the compiler (possibly) warn if the result is not used
33 /** \advanced */
34 #define IMP_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
35 //! restrict means that a variable is not aliased with this function
36 #define IMP_RESTRICT __restrict__
37 
38 #else
39 #define IMP_NO_SIDEEFFECTS
40 #define IMP_WARN_UNUSED_RESULT
41 #define IMP_RESTRICT
42 #endif
43 
44 #endif
45 
46 #ifdef __clang__
47 #define IMP_COMPILER_HAS_OVERRIDE 1
48 #elif defined(__GNUC__) && __cplusplus >= 201103L
49 // probably should be finer here
50 #define IMP_COMPILER_HAS_OVERRIDE 1
51 #else
52 #define IMP_COMPILER_HAS_OVERRIDE 0
53 #endif
54 
55 #if IMP_COMPILER_HAS_OVERRIDE
56 #define IMP_OVERRIDE override
57 #else
58 #define IMP_OVERRIDE
59 #endif
60 
61 
62 #ifdef __clang__
63 #define IMP_COMPILER_HAS_FINAL 1
64 #elif defined(__GNUC__) && __cplusplus >= 201103L
65 // probably should be finer here
66 #define IMP_COMPILER_HAS_FINAL 1
67 #else
68 #define IMP_COMPILER_HAS_FINAL 0
69 #endif
70 
71 #if IMP_COMPILER_HAS_FINAL
72 #define IMP_FINAL final
73 #else
74 #define IMP_FINAL
75 #endif
76 
77 #if defined(__clang__) || defined(__GNUC__)
78 #define IMP_PRAGMA(x) _Pragma(IMP_STRINGIFY(x))
79 
80 #if defined(__clang__)
81 #define IMP_CLANG_PRAGMA(x) IMP_PRAGMA(clang x)
82 #define IMP_GCC_PRAGMA(x)
83 #define IMP_VC_PRAGMA(x)
84 #else
85 #define IMP_CLANG_PRAGMA(x)
86 #define IMP_GCC_PRAGMA(x) IMP_PRAGMA(GCC x)
87 #define IMP_VC_PRAGMA(x)
88 #endif
89 
90 #elif defined(_MSC_VER)
91 #define IMP_PRAGMA(x) __pragma(x)
92 #define IMP_CLANG_PRAGMA(x)
93 #define IMP_GCC_PRAGMA(x)
94 #define IMP_VC_PRAGMA(x) IMP_PRAGMA(x)
95 
96 #else
97 #define IMP_PRAGMA(x)
98 #define IMP_CLANG_PRAGMA(x)
99 #define IMP_GCC_PRAGMA(x)
100 #define IMP_VC_PRAGMA(x)
101 #endif
102 
103 #ifndef IMP_DOXYGEN
104 #if defined(BOOST_LITTLE_ENDIAN)
105 #define IMP_LITTLE_ENDIAN
106 #else
107 #define IMP_BIG_ENDIAN
108 #endif
109 #endif
110 
111 #ifdef __clang__
112 
113 #define IMP_GCC_PUSH_POP(x)
114 
115 #define IMP_COMPILER_ENABLE_WARNINGS \
116  IMP_CLANG_PRAGMA( diagnostic push) \
117  /*IMP_CLANG_PRAGMA( diagnostic warning "-Wall")*/ \
118  /*IMP_CLANG_PRAGMA( diagnostic warning "-Wextra") */ \
119  IMP_CLANG_PRAGMA( diagnostic warning "-Wabi") \
120  IMP_CLANG_PRAGMA( diagnostic warning "-Waddress-of-temporary") \
121  IMP_CLANG_PRAGMA( diagnostic warning "-Waddress") \
122  IMP_CLANG_PRAGMA( diagnostic warning "-Waggregate-return") \
123  IMP_CLANG_PRAGMA( diagnostic warning "-Wambiguous-member-template") \
124  IMP_CLANG_PRAGMA( diagnostic warning "-Warc-abi") \
125  IMP_CLANG_PRAGMA( diagnostic warning "-Warc-non-pod-memaccess") \
126  IMP_CLANG_PRAGMA( diagnostic warning "-Warc-retain-cycles") \
127  IMP_CLANG_PRAGMA( diagnostic warning "-Warc-unsafe-retained-assign") \
128  IMP_CLANG_PRAGMA( diagnostic warning "-Warc") \
129  IMP_CLANG_PRAGMA( diagnostic warning "-Watomic-properties") \
130  IMP_CLANG_PRAGMA( diagnostic warning "-Wattributes") \
131  IMP_CLANG_PRAGMA( diagnostic warning "-Wavailability") \
132  IMP_CLANG_PRAGMA( diagnostic warning "-Wbad-function-cast") \
133  IMP_CLANG_PRAGMA( diagnostic warning "-Wbind-to-temporary-copy") \
134  IMP_CLANG_PRAGMA( diagnostic warning "-Wbitwise-op-parentheses") \
135  IMP_CLANG_PRAGMA( diagnostic warning "-Wbool-conversions") \
136  IMP_CLANG_PRAGMA( diagnostic warning "-Wbuiltin-macro-redefined") \
137  IMP_CLANG_PRAGMA( diagnostic warning "-Wc++-compat") \
138  IMP_CLANG_PRAGMA( diagnostic warning "-Wc++0x-compat") \
139  IMP_CLANG_PRAGMA( diagnostic ignored "-Wc++11-extensions") \
140  IMP_CLANG_PRAGMA( diagnostic ignored "-Wc++0x-extensions") \
141  IMP_CLANG_PRAGMA( diagnostic warning "-Wcast-align") \
142  IMP_CLANG_PRAGMA( diagnostic warning "-Wcast-qual") \
143  IMP_CLANG_PRAGMA( diagnostic warning "-Wchar-align") \
144  IMP_CLANG_PRAGMA( diagnostic warning "-Wchar-subscripts") \
145  IMP_CLANG_PRAGMA( diagnostic warning "-Wcomment") \
146  IMP_CLANG_PRAGMA( diagnostic warning "-Wcomments") \
147  /* Too many false positives */ \
148  /*IMP_CLANG_PRAGMA( diagnostic warning "-Wconditional-uninitialized")*/ \
149  IMP_CLANG_PRAGMA( diagnostic warning "-Wctor-dtor-privacy") \
150  IMP_CLANG_PRAGMA( diagnostic warning "-Wcustom-atomic-properties") \
151  IMP_CLANG_PRAGMA( diagnostic warning "-Wdeclaration-after-statement") \
152  IMP_CLANG_PRAGMA( diagnostic warning "-Wdefault-arg-special-member") \
153  IMP_CLANG_PRAGMA( diagnostic warning "-Wdelegating-ctor-cycles") \
154  IMP_CLANG_PRAGMA( diagnostic warning "-Wdelete-non-virtual-dtor") \
155  IMP_CLANG_PRAGMA( diagnostic warning "-Wdeprecated-declarations") \
156  IMP_CLANG_PRAGMA( diagnostic warning "-Wdeprecated-implementations") \
157  IMP_CLANG_PRAGMA( diagnostic warning "-Wdeprecated-writable-strings") \
158  IMP_CLANG_PRAGMA( diagnostic warning "-Wdeprecated") \
159  IMP_CLANG_PRAGMA( diagnostic warning "-Wdisabled-optimization") \
160  IMP_CLANG_PRAGMA( diagnostic warning "-Wdiscard-qual") \
161  IMP_CLANG_PRAGMA( diagnostic warning "-Wdiv-by-zero") \
162  IMP_CLANG_PRAGMA( diagnostic warning "-Wduplicate-method-arg") \
163  IMP_CLANG_PRAGMA( diagnostic warning "-Weffc++") \
164  IMP_CLANG_PRAGMA( diagnostic warning "-Wempty-body") \
165  IMP_CLANG_PRAGMA( diagnostic warning "-Wendif-labels") \
166  IMP_CLANG_PRAGMA( diagnostic ignored "-Wexit-time-destructors") \
167  IMP_CLANG_PRAGMA( diagnostic warning "-Wextra-tokens") \
168  IMP_CLANG_PRAGMA( diagnostic warning "-Wformat-extra-args") \
169  IMP_CLANG_PRAGMA( diagnostic warning "-Wformat-nonliteral") \
170  IMP_CLANG_PRAGMA( diagnostic warning "-Wformat-zero-length") \
171  IMP_CLANG_PRAGMA( diagnostic warning "-Wformat") \
172  IMP_CLANG_PRAGMA( diagnostic warning "-Wformat=2") \
173  IMP_CLANG_PRAGMA( diagnostic warning "-Wfour-char-constants") \
174  IMP_CLANG_PRAGMA( diagnostic ignored "-Wglobal-constructors") \
175  IMP_CLANG_PRAGMA( diagnostic warning "-Wgnu-designator") \
176  IMP_CLANG_PRAGMA( diagnostic warning "-Wgnu") \
177  IMP_CLANG_PRAGMA( diagnostic warning "-Wheader-hygiene") \
178  IMP_CLANG_PRAGMA( diagnostic warning "-Widiomatic-parentheses") \
179  IMP_CLANG_PRAGMA( diagnostic warning "-Wignored-qualifiers") \
180  IMP_CLANG_PRAGMA( diagnostic warning "-Wimplicit-atomic-properties") \
181  IMP_CLANG_PRAGMA( diagnostic warning "-Wimplicit-function-declaration") \
182  IMP_CLANG_PRAGMA( diagnostic warning "-Wimplicit-int") \
183  IMP_CLANG_PRAGMA( diagnostic warning "-Wimplicit") \
184  IMP_CLANG_PRAGMA( diagnostic warning "-Wimport") \
185  IMP_CLANG_PRAGMA( diagnostic warning "-Wincompatible-pointer-types") \
186  IMP_CLANG_PRAGMA( diagnostic warning "-Winit-self") \
187  IMP_CLANG_PRAGMA( diagnostic warning "-Winitializer-overrides") \
188  IMP_CLANG_PRAGMA( diagnostic warning "-Winline") \
189  IMP_CLANG_PRAGMA( diagnostic warning "-Wint-to-pointer-cast") \
190  IMP_CLANG_PRAGMA( diagnostic warning "-Winvalid-offsetof") \
191  IMP_CLANG_PRAGMA( diagnostic warning "-Winvalid-pch") \
192  IMP_CLANG_PRAGMA( diagnostic warning "-Wlarge-by-value-copy") \
193  IMP_CLANG_PRAGMA( diagnostic warning "-Wliteral-range") \
194  IMP_CLANG_PRAGMA( diagnostic warning "-Wlocal-type-template-args") \
195  IMP_CLANG_PRAGMA( diagnostic warning "-Wlogical-op-parentheses") \
196  IMP_CLANG_PRAGMA( diagnostic warning "-Wlong-long") \
197  IMP_CLANG_PRAGMA( diagnostic warning "-Wmain") \
198  IMP_CLANG_PRAGMA( diagnostic warning "-Wmicrosoft") \
199  IMP_CLANG_PRAGMA( diagnostic warning "-Wmismatched-tags") \
200  IMP_CLANG_PRAGMA( diagnostic warning "-Wmissing-braces") \
201  IMP_CLANG_PRAGMA( diagnostic warning "-Wmissing-declarations") \
202  IMP_CLANG_PRAGMA( diagnostic warning "-Wmissing-field-initializers") \
203  IMP_CLANG_PRAGMA( diagnostic warning "-Wmissing-format-attribute") \
204  IMP_CLANG_PRAGMA( diagnostic warning "-Wmissing-include-dirs") \
205  IMP_CLANG_PRAGMA( diagnostic ignored "-Wmissing-noreturn") \
206  IMP_CLANG_PRAGMA( diagnostic warning "-Wmost") \
207  IMP_CLANG_PRAGMA( diagnostic warning "-Wmultichar") \
208  IMP_CLANG_PRAGMA( diagnostic warning "-Wnested-externs") \
209  IMP_CLANG_PRAGMA( diagnostic warning "-Wnewline-eof") \
210  IMP_CLANG_PRAGMA( diagnostic warning "-Wnon-gcc") \
211  IMP_CLANG_PRAGMA( diagnostic warning "-Wnon-virtual-dtor") \
212  /*IMP_CLANG_PRAGMA( diagnostic ignored "-Wnonfragile-abi2")*/ \
213  IMP_CLANG_PRAGMA( diagnostic warning "-Wnonnull") \
214  IMP_CLANG_PRAGMA( diagnostic warning "-Wnonportable-cfstrings") \
215  IMP_CLANG_PRAGMA( diagnostic warning "-Wnull-dereference") \
216  IMP_CLANG_PRAGMA( diagnostic warning "-Wobjc-nonunified-exceptions") \
217  IMP_CLANG_PRAGMA( diagnostic warning "-Wold-style-cast") \
218  IMP_CLANG_PRAGMA( diagnostic warning "-Wold-style-definition") \
219  IMP_CLANG_PRAGMA( diagnostic warning "-Wout-of-line-declaration") \
220  IMP_CLANG_PRAGMA( diagnostic warning "-Woverflow") \
221  IMP_CLANG_PRAGMA( diagnostic warning "-Woverlength-strings") \
222  IMP_CLANG_PRAGMA( diagnostic warning "-Woverloaded-virtual") \
223  IMP_CLANG_PRAGMA( diagnostic warning "-Wpacked") \
224  IMP_CLANG_PRAGMA( diagnostic ignored "-Wpadded") \
225  IMP_CLANG_PRAGMA( diagnostic warning "-Wparentheses") \
226  IMP_CLANG_PRAGMA( diagnostic warning "-Wpointer-arith") \
227  IMP_CLANG_PRAGMA( diagnostic warning "-Wpointer-to-int-cast") \
228  IMP_CLANG_PRAGMA( diagnostic warning "-Wprotocol") \
229  IMP_CLANG_PRAGMA( diagnostic warning "-Wreadonly-setter-attrs") \
230  IMP_CLANG_PRAGMA( diagnostic warning "-Wredundant-decls") \
231  IMP_CLANG_PRAGMA( diagnostic warning "-Wreorder") \
232  IMP_CLANG_PRAGMA( diagnostic warning "-Wreturn-type") \
233  IMP_CLANG_PRAGMA( diagnostic warning "-Wself-assign") \
234  IMP_CLANG_PRAGMA( diagnostic warning "-Wsemicolon-before-method-body") \
235  IMP_CLANG_PRAGMA( diagnostic warning "-Wsequence-point") \
236  /* We should turn these on, but there are too may warnings.*/ \
237  /*IMP_CLANG_PRAGMA( diagnostic warning "-Wshadow")*/ \
238  IMP_CLANG_PRAGMA( diagnostic ignored "-Wshorten-64-to-32") \
239  IMP_CLANG_PRAGMA( diagnostic warning "-Wsign-compare") \
240  IMP_CLANG_PRAGMA( diagnostic ignored "-Wsign-conversion") \
241  IMP_CLANG_PRAGMA( diagnostic warning "-Wsign-promo") \
242  IMP_CLANG_PRAGMA( diagnostic warning "-Wsizeof-array-argument") \
243  IMP_CLANG_PRAGMA( diagnostic warning "-Wstack-protector") \
244  IMP_CLANG_PRAGMA( diagnostic warning "-Wstrict-aliasing") \
245  IMP_CLANG_PRAGMA( diagnostic warning "-Wstrict-overflow") \
246  IMP_CLANG_PRAGMA( diagnostic warning "-Wstrict-prototypes") \
247  IMP_CLANG_PRAGMA( diagnostic warning "-Wstrict-selector-match") \
248  IMP_CLANG_PRAGMA( diagnostic warning "-Wsuper-class-method-mismatch") \
249  IMP_CLANG_PRAGMA( diagnostic warning "-Wswitch-default") \
250 /* This is just a dumb warning, provided for gcc compat.*/ \
251 IMP_CLANG_PRAGMA( diagnostic ignored "-Wswitch-enum") \
252  IMP_CLANG_PRAGMA( diagnostic warning "-Wswitch") \
253  IMP_CLANG_PRAGMA( diagnostic warning "-Wsynth") \
254  IMP_CLANG_PRAGMA( diagnostic warning "-Wtautological-compare") \
255  IMP_CLANG_PRAGMA( diagnostic warning "-Wtrigraphs") \
256  IMP_CLANG_PRAGMA( diagnostic warning "-Wtype-limits") \
257  IMP_CLANG_PRAGMA( diagnostic warning "-Wundeclared-selector") \
258  IMP_CLANG_PRAGMA( diagnostic warning "-Wuninitialized") \
259  IMP_CLANG_PRAGMA( diagnostic warning "-Wunknown-pragmas") \
260  IMP_CLANG_PRAGMA( diagnostic warning "-Wunnamed-type-template-args") \
261  IMP_CLANG_PRAGMA( diagnostic warning "-Wunneeded-internal-declaration") \
262  IMP_CLANG_PRAGMA( diagnostic warning "-Wunneeded-member-function") \
263  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-argument") \
264  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-exception-parameter") \
265  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-function") \
266  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-label") \
267  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-member-function") \
268  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-parameter") \
269  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-value") \
270  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused-variable") \
271  IMP_CLANG_PRAGMA( diagnostic warning "-Wunused") \
272  IMP_CLANG_PRAGMA( diagnostic warning "-Wused-but-marked-unused") \
273  IMP_CLANG_PRAGMA( diagnostic warning "-Wvariadic-macros") \
274  IMP_CLANG_PRAGMA( diagnostic warning "-Wvector-conversions") \
275  IMP_CLANG_PRAGMA( diagnostic warning "-Wvla") \
276  IMP_CLANG_PRAGMA( diagnostic warning "-Wvolatile-register-var") \
277  IMP_CLANG_PRAGMA( diagnostic warning "-Wwrite-strings") \
278  /* Most of these are stupid uses of floats instead of doubles. I don't
279  want to fix them all now. For some reason this needs to be last.*/ \
280 IMP_CLANG_PRAGMA( diagnostic ignored "-Wconversion") \
281 IMP_CLANG_PRAGMA( diagnostic ignored "-Wc++11-compat")
282 
283 
284 #define IMP_HELPER_MACRO_PUSH_WARNINGS \
285  IMP_CLANG_PRAGMA( diagnostic push) \
286  IMP_CLANG_PRAGMA( diagnostic ignored "-Wunused-member-function")
287 
288 #define IMP_HELPER_MACRO_POP_WARNINGS \
289  IMP_CLANG_PRAGMA( diagnostic pop)
290 
291 /* IMP_CLANG_PRAGMA( diagnostic warning "-Wall") \
292  IMP_CLANG_PRAGMA( diagnostic warning "-Weverything") \
293  IMP_CLANG_PRAGMA( diagnostic ignored "-Wpadded") \
294  IMP_CLANG_PRAGMA( diagnostic ignored "-Wc++11-extensions") \
295  IMP_CLANG_PRAGMA( diagnostic ignored "-Wunknown-pragmas") \
296  IMP_CLANG_PRAGMA( diagnostic ignored "-Wc++98-compat")*/
297 
298 #define IMP_COMPILER_DISABLE_WARNINGS \
299  IMP_CLANG_PRAGMA( diagnostic pop)
300 
301 #elif defined(__GNUC__)
302 
303 /*ret+=["-Wno-deprecated",
304  "-Wstrict-aliasing=2",
305  -fno-operator-names",]*/
306 #if __GNUC__ > 4 || __GNUC_MINOR__ >=6
307 #define IMP_GCC_PUSH_POP(x) IMP_PRAGMA(x)
308 #define IMP_GCC_CXX0X_COMPAT \
309  IMP_GCC_PRAGMA( diagnostic ignored "-Wc++0x-compat")
310 #define IMP_GCC_PROTOTYPES \
311  IMP_GCC_PRAGMA( diagnostic warning "-Wmissing-declarations")
312 
313 #define IMP_HELPER_MACRO_PUSH_WARNINGS \
314  IMP_GCC_PRAGMA( diagnostic push)
315 
316 #define IMP_HELPER_MACRO_POP_WARNINGS \
317  IMP_GCC_PRAGMA( diagnostic pop)
318 
319 
320 #else
321 #define IMP_GCC_PUSH_POP(x)
322 #define IMP_GCC_CXX0X_COMPAT
323 #define IMP_GCC_PROTOTYPES
324 #define IMP_HELPER_MACRO_PUSH_WARNINGS
325 #define IMP_HELPER_MACRO_POP_WARNINGS
326 #endif
327 
328 #define IMP_COMPILER_ENABLE_WARNINGS \
329  IMP_GCC_PUSH_POP(GCC diagnostic push) \
330  IMP_GCC_PRAGMA( diagnostic warning "-Wall") \
331  IMP_GCC_PRAGMA( diagnostic warning "-Wextra") \
332  IMP_GCC_PRAGMA( diagnostic warning "-Winit-self") \
333  IMP_GCC_PRAGMA( diagnostic warning "-Wcast-align") \
334  IMP_GCC_PRAGMA( diagnostic warning "-Woverloaded-virtual") \
335  IMP_GCC_PRAGMA( diagnostic warning "-Wundef") \
336  IMP_GCC_PROTOTYPES \
337  IMP_GCC_CXX0X_COMPAT
338 
339 #define IMP_COMPILER_DISABLE_WARNINGS \
340  IMP_GCC_PUSH_POP(GCC diagnostic pop)
341 
342 #elif defined(_MSC_VER)
343 #define IMP_GCC_PUSH_POP(x)
344 
345 #define IMP_COMPILER_ENABLE_WARNINGS\
346  IMP_VC_PRAGMA(warning(push)) \
347  IMP_VC_PRAGMA(warning( disable: 4273 ))
348 
349 #define IMP_COMPILER_DISABLE_WARNINGS\
350  IMP_VC_PRAGMA(warning(pop))
351 
352 #define IMP_HELPER_MACRO_PUSH_WARNINGS
353 #define IMP_HELPER_MACRO_POP_WARNINGS
354 
355 #else
356 #define IMP_COMPILER_ENABLE_WARNINGS
357 #define IMP_COMPILER_DISABLE_WARNINGS
358 #define IMP_HELPER_MACRO_PUSH_WARNINGS
359 #define IMP_HELPER_MACRO_POP_WARNINGS
360 #endif
361 
362 #endif /* IMPBASE_BASE_COMPILER_MACROS_H */