Code

*: add "pure" attributes
[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 #ifdef __GNUC__
28 #define GCC_VERSION (__GNUC__ * 10000 \
29                      + __GNUC_MINOR__ * 100 \
30                      + __GNUC_PATCHLEVEL__)
31 #else
32 #define GCC_VERSION 0
33 #endif
35 #if GCC_CHECK_VERSION(4,0)
37 /* GCC 4.x */
39 #define gcc_const __attribute__((const))
40 #define gcc_deprecated __attribute__((deprecated))
41 #define gcc_may_alias __attribute__((may_alias))
42 #define gcc_malloc __attribute__((malloc))
43 #define gcc_noreturn __attribute__((noreturn))
44 #define gcc_packed __attribute__((packed))
45 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
46 #define gcc_pure __attribute__((pure))
47 #define gcc_sentinel __attribute__((sentinel))
48 #define gcc_unused __attribute__((unused))
49 #define gcc_warn_unused_result __attribute__((warn_unused_result))
51 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
52 #define gcc_nonnull_all __attribute__((nonnull))
54 #define gcc_likely(x) __builtin_expect (!!(x), 1)
55 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
57 #define gcc_aligned(n) __attribute__((aligned(n)))
59 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
60 #define gcc_visibility_default __attribute__((visibility("default")))
62 #define gcc_always_inline __attribute__((always_inline))
64 #else
66 /* generic C compiler */
68 #define gcc_const
69 #define gcc_deprecated
70 #define gcc_may_alias
71 #define gcc_malloc
72 #define gcc_noreturn
73 #define gcc_packed
74 #define gcc_printf(a,b)
75 #define gcc_pure
76 #define gcc_sentinel
77 #define gcc_unused
78 #define gcc_warn_unused_result
80 #define gcc_nonnull(...)
81 #define gcc_nonnull_all
83 #define gcc_likely(x) (x)
84 #define gcc_unlikely(x) (x)
86 #define gcc_aligned(n)
88 #define gcc_visibility_hidden
89 #define gcc_visibility_default
91 #define gcc_always_inline inline
93 #endif
95 #if GCC_CHECK_VERSION(4,3)
97 #define gcc_hot __attribute__((hot))
98 #define gcc_cold __attribute__((cold))
100 #else /* ! GCC_UNUSED >= 40300 */
102 #define gcc_hot
103 #define gcc_cold
105 #endif /* ! GCC_UNUSED >= 40300 */
107 #if GCC_CHECK_VERSION(4,6) && !defined(__clang__)
108 #define gcc_flatten __attribute__((flatten))
109 #else
110 #define gcc_flatten
111 #endif
113 #ifndef __cplusplus
114 /* plain C99 has "restrict" */
115 #define gcc_restrict restrict
116 #elif GCC_CHECK_VERSION(4,0)
117 /* "__restrict__" is a GCC extension for C++ */
118 #define gcc_restrict __restrict__
119 #else
120 /* disable it on other compilers */
121 #define gcc_restrict
122 #endif
124 /* C++11 features */
126 #if defined(__cplusplus)
128 /* support for C++11 "override" was added in gcc 4.7 */
129 #if !defined(__clang__) && !GCC_CHECK_VERSION(4,7)
130 #define override
131 #define final
132 #endif
134 #if defined(__clang__) || GCC_CHECK_VERSION(4,8)
135 #define gcc_alignas(T, fallback) alignas(T)
136 #else
137 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
138 #endif
140 #endif
142 #ifndef __has_feature
143   // define dummy macro for non-clang compilers
144   #define __has_feature(x) 0
145 #endif
147 #if __has_feature(attribute_unused_on_fields)
148 #define gcc_unused_field gcc_unused
149 #else
150 #define gcc_unused_field
151 #endif
153 #if defined(__GNUC__) || defined(__clang__)
154 #define gcc_unreachable() __builtin_unreachable()
155 #else
156 #define gcc_unreachable()
157 #endif
159 #endif