MPD  0.19.20
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 #ifdef __clang__
32 # define CLANG_VERSION GCC_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
33 #elif defined(__GNUC__)
34 # define CLANG_VERSION 0
35 #endif
36 
41 #define GCC_CHECK_VERSION(major, minor) \
42  (CLANG_VERSION == 0 && \
43  GCC_VERSION >= GCC_MAKE_VERSION(major, minor, 0))
44 
49 #define CLANG_OR_GCC_VERSION(major, minor) \
50  (CLANG_VERSION > 0 || GCC_CHECK_VERSION(major, minor))
51 
56 #define GCC_OLDER_THAN(major, minor) \
57  (GCC_VERSION > 0 && CLANG_VERSION == 0 && \
58  GCC_VERSION < GCC_MAKE_VERSION(major, minor, 0))
59 
60 #ifdef __clang__
61 # if __clang_major__ < 3
62 # error Sorry, your clang version is too old. You need at least version 3.1.
63 # endif
64 #elif defined(__GNUC__)
65 # if GCC_OLDER_THAN(4,7)
66 # error Sorry, your gcc version is too old. You need at least version 4.6.
67 # endif
68 #else
69 # warning Untested compiler. Use at your own risk!
70 #endif
71 
75 #define CLANG_CHECK_VERSION(major, minor) \
76  (CLANG_VERSION >= GCC_MAKE_VERSION(major, minor, 0))
77 
78 #if CLANG_OR_GCC_VERSION(4,0)
79 
80 /* GCC 4.x */
81 
82 #define gcc_const __attribute__((const))
83 #define gcc_deprecated __attribute__((deprecated))
84 #define gcc_may_alias __attribute__((may_alias))
85 #define gcc_malloc __attribute__((malloc))
86 #define gcc_noreturn __attribute__((noreturn))
87 #define gcc_packed __attribute__((packed))
88 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
89 #define gcc_pure __attribute__((pure))
90 #define gcc_sentinel __attribute__((sentinel))
91 #define gcc_unused __attribute__((unused))
92 #define gcc_warn_unused_result __attribute__((warn_unused_result))
93 
94 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
95 #define gcc_nonnull_all __attribute__((nonnull))
96 
97 #define gcc_likely(x) __builtin_expect (!!(x), 1)
98 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
99 
100 #define gcc_aligned(n) __attribute__((aligned(n)))
101 
102 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
103 #define gcc_visibility_default __attribute__((visibility("default")))
104 
105 #define gcc_always_inline __attribute__((always_inline))
106 
107 #else
108 
109 /* generic C compiler */
110 
111 #define gcc_const
112 #define gcc_deprecated
113 #define gcc_may_alias
114 #define gcc_malloc
115 #define gcc_noreturn
116 #define gcc_packed
117 #define gcc_printf(a,b)
118 #define gcc_pure
119 #define gcc_sentinel
120 #define gcc_unused
121 #define gcc_warn_unused_result
122 
123 #define gcc_nonnull(...)
124 #define gcc_nonnull_all
125 
126 #define gcc_likely(x) (x)
127 #define gcc_unlikely(x) (x)
128 
129 #define gcc_aligned(n)
130 
131 #define gcc_visibility_hidden
132 #define gcc_visibility_default
133 
134 #define gcc_always_inline inline
135 
136 #endif
137 
138 #if CLANG_OR_GCC_VERSION(4,3)
139 
140 #define gcc_hot __attribute__((hot))
141 #define gcc_cold __attribute__((cold))
142 
143 #else /* ! GCC_UNUSED >= 40300 */
144 
145 #define gcc_hot
146 #define gcc_cold
147 
148 #endif /* ! GCC_UNUSED >= 40300 */
149 
150 #if GCC_CHECK_VERSION(4,6)
151 #define gcc_flatten __attribute__((flatten))
152 #else
153 #define gcc_flatten
154 #endif
155 
156 #ifndef __cplusplus
157 /* plain C99 has "restrict" */
158 #define gcc_restrict restrict
159 #elif CLANG_OR_GCC_VERSION(4,0)
160 /* "__restrict__" is a GCC extension for C++ */
161 #define gcc_restrict __restrict__
162 #else
163 /* disable it on other compilers */
164 #define gcc_restrict
165 #endif
166 
167 /* C++11 features */
168 
169 #if defined(__cplusplus)
170 
171 /* support for C++11 "override" was added in gcc 4.7 */
172 #if GCC_OLDER_THAN(4,7)
173 #define override
174 #define final
175 #endif
176 
177 #if CLANG_OR_GCC_VERSION(4,8)
178 #define gcc_alignas(T, fallback) alignas(T)
179 #else
180 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
181 #endif
182 
183 #endif
184 
185 #ifndef __has_feature
186  // define dummy macro for non-clang compilers
187  #define __has_feature(x) 0
188 #endif
189 
190 #if __has_feature(attribute_unused_on_fields)
191 #define gcc_unused_field gcc_unused
192 #else
193 #define gcc_unused_field
194 #endif
195 
196 #if defined(__GNUC__) || defined(__clang__)
197 #define gcc_unreachable() __builtin_unreachable()
198 #else
199 #define gcc_unreachable()
200 #endif
201 
202 #endif