Code

ef13a546c7666b9211f66cc521d3e3e0243ff219
[ncmpc.git] / src / Compiler.h
1 /*
2  * Copyright (C) 2003-2013 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  */
20 #ifndef COMPILER_H
21 #define COMPILER_H
23 #define GCC_CHECK_VERSION(major, minor) \
24   (defined(__GNUC__) &&                                                 \
25    (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))))
27 #define GCC_MAKE_VERSION(major, minor, patchlevel) ((major) * 10000 + (minor) * 100 + patchlevel)
29 #ifdef __GNUC__
30 #  define GCC_VERSION GCC_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
31 #else
32 #  define GCC_VERSION 0
33 #endif
35 #ifdef __clang__
36 #  define CLANG_VERSION GCC_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
37 #else
38 #  define CLANG_VERSION 0
39 #endif
41 /**
42  * Are we building with clang (any version) or at least the specified
43  * gcc version?
44  */
45 #define CLANG_OR_GCC_VERSION(major, minor) \
46   (CLANG_VERSION || GCC_CHECK_VERSION(major, minor))
48 /**
49  * Are we building with gcc (not clang or any other compiler) and a
50  * version older than the specified one?
51  */
52 #define GCC_OLDER_THAN(major, minor) \
53   (GCC_VERSION && !CLANG_VERSION && \
54    GCC_VERSION < GCC_MAKE_VERSION(major, minor, 0))
56 #if CLANG_OR_GCC_VERSION(4,0)
58 /* GCC 4.x */
60 #define gcc_const __attribute__((const))
61 #define gcc_deprecated __attribute__((deprecated))
62 #define gcc_may_alias __attribute__((may_alias))
63 #define gcc_malloc __attribute__((malloc))
64 #define gcc_noreturn __attribute__((noreturn))
65 #define gcc_packed __attribute__((packed))
66 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
67 #define gcc_pure __attribute__((pure))
68 #define gcc_sentinel __attribute__((sentinel))
69 #define gcc_unused __attribute__((unused))
70 #define gcc_warn_unused_result __attribute__((warn_unused_result))
72 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
73 #define gcc_nonnull_all __attribute__((nonnull))
75 #define gcc_likely(x) __builtin_expect (!!(x), 1)
76 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
78 #define gcc_aligned(n) __attribute__((aligned(n)))
80 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
81 #define gcc_visibility_default __attribute__((visibility("default")))
83 #define gcc_always_inline __attribute__((always_inline))
85 #else
87 /* generic C compiler */
89 #define gcc_const
90 #define gcc_deprecated
91 #define gcc_may_alias
92 #define gcc_malloc
93 #define gcc_noreturn
94 #define gcc_packed
95 #define gcc_printf(a,b)
96 #define gcc_pure
97 #define gcc_sentinel
98 #define gcc_unused
99 #define gcc_warn_unused_result
101 #define gcc_nonnull(...)
102 #define gcc_nonnull_all
104 #define gcc_likely(x) (x)
105 #define gcc_unlikely(x) (x)
107 #define gcc_aligned(n)
109 #define gcc_visibility_hidden
110 #define gcc_visibility_default
112 #define gcc_always_inline inline
114 #endif
116 #if CLANG_OR_GCC_VERSION(4,3)
118 #define gcc_hot __attribute__((hot))
119 #define gcc_cold __attribute__((cold))
121 #else /* ! GCC_UNUSED >= 40300 */
123 #define gcc_hot
124 #define gcc_cold
126 #endif /* ! GCC_UNUSED >= 40300 */
128 #if GCC_CHECK_VERSION(4,6) && !defined(__clang__)
129 #define gcc_flatten __attribute__((flatten))
130 #else
131 #define gcc_flatten
132 #endif
134 #ifndef __cplusplus
135 /* plain C99 has "restrict" */
136 #define gcc_restrict restrict
137 #elif CLANG_OR_GCC_VERSION(4,0)
138 /* "__restrict__" is a GCC extension for C++ */
139 #define gcc_restrict __restrict__
140 #else
141 /* disable it on other compilers */
142 #define gcc_restrict
143 #endif
145 /* C++11 features */
147 #if defined(__cplusplus)
149 /* support for C++11 "override" was added in gcc 4.7 */
150 #if GCC_OLDER_THAN(4,7)
151 #define override
152 #define final
153 #endif
155 #if CLANG_OR_GCC_VERSION(4,8)
156 #define gcc_alignas(T, fallback) alignas(T)
157 #else
158 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
159 #endif
161 #endif
163 #ifndef __has_feature
164   // define dummy macro for non-clang compilers
165   #define __has_feature(x) 0
166 #endif
168 #if __has_feature(attribute_unused_on_fields)
169 #define gcc_unused_field gcc_unused
170 #else
171 #define gcc_unused_field
172 #endif
174 #if defined(__GNUC__) || defined(__clang__)
175 #define gcc_unreachable() __builtin_unreachable()
176 #else
177 #define gcc_unreachable()
178 #endif
180 #endif