Code

d76c8f6b968a2269ad9c5f5ccda846cb573fd853
[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_MAKE_VERSION(major, minor, patchlevel) ((major) * 10000 + (minor) * 100 + patchlevel)
25 #ifdef __GNUC__
26 #  define GCC_VERSION GCC_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
27 #else
28 #  define GCC_VERSION 0
29 #endif
31 #ifdef __clang__
32 #  define CLANG_VERSION GCC_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
33 #else
34 #  define CLANG_VERSION 0
35 #endif
37 #define GCC_CHECK_VERSION(major, minor) \
38   (GCC_VERSION >= GCC_MAKE_VERSION(major, minor, 0))
40 /**
41  * Are we building with clang (any version) or at least the specified
42  * gcc version?
43  */
44 #define CLANG_OR_GCC_VERSION(major, minor) \
45   (CLANG_VERSION || GCC_CHECK_VERSION(major, minor))
47 /**
48  * Are we building with gcc (not clang or any other compiler) and a
49  * version older than the specified one?
50  */
51 #define GCC_OLDER_THAN(major, minor) \
52   (GCC_VERSION && !CLANG_VERSION && \
53    GCC_VERSION < GCC_MAKE_VERSION(major, minor, 0))
55 #if CLANG_OR_GCC_VERSION(4,0)
57 /* GCC 4.x */
59 #define gcc_const __attribute__((const))
60 #define gcc_deprecated __attribute__((deprecated))
61 #define gcc_may_alias __attribute__((may_alias))
62 #define gcc_malloc __attribute__((malloc))
63 #define gcc_noreturn __attribute__((noreturn))
64 #define gcc_packed __attribute__((packed))
65 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
66 #define gcc_pure __attribute__((pure))
67 #define gcc_sentinel __attribute__((sentinel))
68 #define gcc_unused __attribute__((unused))
69 #define gcc_warn_unused_result __attribute__((warn_unused_result))
71 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
72 #define gcc_nonnull_all __attribute__((nonnull))
74 #define gcc_likely(x) __builtin_expect (!!(x), 1)
75 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
77 #define gcc_aligned(n) __attribute__((aligned(n)))
79 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
80 #define gcc_visibility_default __attribute__((visibility("default")))
82 #define gcc_always_inline __attribute__((always_inline))
84 #else
86 /* generic C compiler */
88 #define gcc_const
89 #define gcc_deprecated
90 #define gcc_may_alias
91 #define gcc_malloc
92 #define gcc_noreturn
93 #define gcc_packed
94 #define gcc_printf(a,b)
95 #define gcc_pure
96 #define gcc_sentinel
97 #define gcc_unused
98 #define gcc_warn_unused_result
100 #define gcc_nonnull(...)
101 #define gcc_nonnull_all
103 #define gcc_likely(x) (x)
104 #define gcc_unlikely(x) (x)
106 #define gcc_aligned(n)
108 #define gcc_visibility_hidden
109 #define gcc_visibility_default
111 #define gcc_always_inline inline
113 #endif
115 #if CLANG_OR_GCC_VERSION(4,3)
117 #define gcc_hot __attribute__((hot))
118 #define gcc_cold __attribute__((cold))
120 #else /* ! GCC_UNUSED >= 40300 */
122 #define gcc_hot
123 #define gcc_cold
125 #endif /* ! GCC_UNUSED >= 40300 */
127 #if GCC_CHECK_VERSION(4,6) && !defined(__clang__)
128 #define gcc_flatten __attribute__((flatten))
129 #else
130 #define gcc_flatten
131 #endif
133 #ifndef __cplusplus
134 /* plain C99 has "restrict" */
135 #define gcc_restrict restrict
136 #elif CLANG_OR_GCC_VERSION(4,0)
137 /* "__restrict__" is a GCC extension for C++ */
138 #define gcc_restrict __restrict__
139 #else
140 /* disable it on other compilers */
141 #define gcc_restrict
142 #endif
144 /* C++11 features */
146 #if defined(__cplusplus)
148 /* support for C++11 "override" was added in gcc 4.7 */
149 #if GCC_OLDER_THAN(4,7)
150 #define override
151 #define final
152 #endif
154 #if CLANG_OR_GCC_VERSION(4,8)
155 #define gcc_alignas(T, fallback) alignas(T)
156 #else
157 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
158 #endif
160 #endif
162 #ifndef __has_feature
163   // define dummy macro for non-clang compilers
164   #define __has_feature(x) 0
165 #endif
167 #if __has_feature(attribute_unused_on_fields)
168 #define gcc_unused_field gcc_unused
169 #else
170 #define gcc_unused_field
171 #endif
173 #if defined(__GNUC__) || defined(__clang__)
174 #define gcc_unreachable() __builtin_unreachable()
175 #else
176 #define gcc_unreachable()
177 #endif
179 #endif