MPD  0.19.10
Compiler.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2014 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef COMPILER_H
21 #define COMPILER_H
22 
23 #define GCC_MAKE_VERSION(major, minor, patchlevel) ((major) * 10000 + (minor) * 100 + patchlevel)
24 
25 #ifdef __GNUC__
26 #define GCC_VERSION GCC_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
27 #else
28 #define GCC_VERSION 0
29 #endif
30 
31 #define GCC_CHECK_VERSION(major, minor) \
32  (defined(__GNUC__) && GCC_VERSION >= GCC_MAKE_VERSION(major, minor, 0))
33 
38 #define GCC_OLDER_THAN(major, minor) \
39  (defined(__GNUC__) && !defined(__clang__) && \
40  GCC_VERSION < GCC_MAKE_VERSION(major, minor, 0))
41 
42 #ifdef __clang__
43 # define CLANG_VERSION GCC_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
44 # if __clang_major__ < 3
45 # error Sorry, your clang version is too old. You need at least version 3.1.
46 # endif
47 #elif defined(__GNUC__)
48 # if GCC_OLDER_THAN(4,6)
49 # error Sorry, your gcc version is too old. You need at least version 4.6.
50 # endif
51 #else
52 # warning Untested compiler. Use at your own risk!
53 #endif
54 
58 #define CLANG_CHECK_VERSION(major, minor) \
59  (defined(__clang__) && \
60  CLANG_VERSION >= GCC_MAKE_VERSION(major, minor, 0))
61 
62 #if GCC_CHECK_VERSION(4,0)
63 
64 /* GCC 4.x */
65 
66 #define gcc_const __attribute__((const))
67 #define gcc_deprecated __attribute__((deprecated))
68 #define gcc_may_alias __attribute__((may_alias))
69 #define gcc_malloc __attribute__((malloc))
70 #define gcc_noreturn __attribute__((noreturn))
71 #define gcc_packed __attribute__((packed))
72 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
73 #define gcc_pure __attribute__((pure))
74 #define gcc_sentinel __attribute__((sentinel))
75 #define gcc_unused __attribute__((unused))
76 #define gcc_warn_unused_result __attribute__((warn_unused_result))
77 
78 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
79 #define gcc_nonnull_all __attribute__((nonnull))
80 
81 #define gcc_likely(x) __builtin_expect (!!(x), 1)
82 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
83 
84 #define gcc_aligned(n) __attribute__((aligned(n)))
85 
86 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
87 #define gcc_visibility_default __attribute__((visibility("default")))
88 
89 #define gcc_always_inline __attribute__((always_inline))
90 
91 #else
92 
93 /* generic C compiler */
94 
95 #define gcc_const
96 #define gcc_deprecated
97 #define gcc_may_alias
98 #define gcc_malloc
99 #define gcc_noreturn
100 #define gcc_packed
101 #define gcc_printf(a,b)
102 #define gcc_pure
103 #define gcc_sentinel
104 #define gcc_unused
105 #define gcc_warn_unused_result
106 
107 #define gcc_nonnull(...)
108 #define gcc_nonnull_all
109 
110 #define gcc_likely(x) (x)
111 #define gcc_unlikely(x) (x)
112 
113 #define gcc_aligned(n)
114 
115 #define gcc_visibility_hidden
116 #define gcc_visibility_default
117 
118 #define gcc_always_inline inline
119 
120 #endif
121 
122 #if GCC_CHECK_VERSION(4,3)
123 
124 #define gcc_hot __attribute__((hot))
125 #define gcc_cold __attribute__((cold))
126 
127 #else /* ! GCC_UNUSED >= 40300 */
128 
129 #define gcc_hot
130 #define gcc_cold
131 
132 #endif /* ! GCC_UNUSED >= 40300 */
133 
134 #if GCC_CHECK_VERSION(4,6) && !defined(__clang__)
135 #define gcc_flatten __attribute__((flatten))
136 #else
137 #define gcc_flatten
138 #endif
139 
140 #ifndef __cplusplus
141 /* plain C99 has "restrict" */
142 #define gcc_restrict restrict
143 #elif GCC_CHECK_VERSION(4,0)
144 /* "__restrict__" is a GCC extension for C++ */
145 #define gcc_restrict __restrict__
146 #else
147 /* disable it on other compilers */
148 #define gcc_restrict
149 #endif
150 
151 /* C++11 features */
152 
153 #if defined(__cplusplus)
154 
155 /* support for C++11 "override" was added in gcc 4.7 */
156 #if GCC_OLDER_THAN(4,7)
157 #define override
158 #define final
159 #endif
160 
161 #if defined(__clang__) || GCC_CHECK_VERSION(4,8)
162 #define gcc_alignas(T, fallback) alignas(T)
163 #else
164 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
165 #endif
166 
167 #endif
168 
169 #ifndef __has_feature
170  // define dummy macro for non-clang compilers
171  #define __has_feature(x) 0
172 #endif
173 
174 #if __has_feature(attribute_unused_on_fields)
175 #define gcc_unused_field gcc_unused
176 #else
177 #define gcc_unused_field
178 #endif
179 
180 #if defined(__GNUC__) || defined(__clang__)
181 #define gcc_unreachable() __builtin_unreachable()
182 #else
183 #define gcc_unreachable()
184 #endif
185 
186 #endif