MPD  0.18.14
src/Compiler.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2013 The Music Player Daemon Project
00003  * http://www.musicpd.org
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018  */
00019 
00020 #ifndef COMPILER_H
00021 #define COMPILER_H
00022 
00023 #define GCC_CHECK_VERSION(major, minor) \
00024   (defined(__GNUC__) &&                                                 \
00025    (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))))
00026 
00027 #ifdef __GNUC__
00028 #define GCC_VERSION (__GNUC__ * 10000 \
00029                      + __GNUC_MINOR__ * 100 \
00030                      + __GNUC_PATCHLEVEL__)
00031 #else
00032 #define GCC_VERSION 0
00033 #endif
00034 
00035 #ifdef __clang__
00036 #  define CLANG_VERSION (__clang_major__ * 10000 \
00037                          + __clang_minor__ * 100 \
00038                          + __clang_patchlevel__)
00039 #  if __clang_major__ < 3
00040 #    error Sorry, your clang version is too old.  You need at least version 3.1.
00041 #  endif
00042 #elif defined(__GNUC__)
00043 #  if !GCC_CHECK_VERSION(4,6)
00044 #    error Sorry, your gcc version is too old.  You need at least version 4.6.
00045 #  endif
00046 #else
00047 #  warning Untested compiler.  Use at your own risk!
00048 #endif
00049 
00050 #if GCC_CHECK_VERSION(4,0)
00051 
00052 /* GCC 4.x */
00053 
00054 #define gcc_const __attribute__((const))
00055 #define gcc_deprecated __attribute__((deprecated))
00056 #define gcc_may_alias __attribute__((may_alias))
00057 #define gcc_malloc __attribute__((malloc))
00058 #define gcc_noreturn __attribute__((noreturn))
00059 #define gcc_packed __attribute__((packed))
00060 #define gcc_printf(a,b) __attribute__((format(printf, a, b)))
00061 #define gcc_pure __attribute__((pure))
00062 #define gcc_sentinel __attribute__((sentinel))
00063 #define gcc_unused __attribute__((unused))
00064 #define gcc_warn_unused_result __attribute__((warn_unused_result))
00065 
00066 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
00067 #define gcc_nonnull_all __attribute__((nonnull))
00068 
00069 #define gcc_likely(x) __builtin_expect (!!(x), 1)
00070 #define gcc_unlikely(x) __builtin_expect (!!(x), 0)
00071 
00072 #define gcc_aligned(n) __attribute__((aligned(n)))
00073 
00074 #define gcc_visibility_hidden __attribute__((visibility("hidden")))
00075 #define gcc_visibility_default __attribute__((visibility("default")))
00076 
00077 #define gcc_always_inline __attribute__((always_inline))
00078 
00079 #else
00080 
00081 /* generic C compiler */
00082 
00083 #define gcc_const
00084 #define gcc_deprecated
00085 #define gcc_may_alias
00086 #define gcc_malloc
00087 #define gcc_noreturn
00088 #define gcc_packed
00089 #define gcc_printf(a,b)
00090 #define gcc_pure
00091 #define gcc_sentinel
00092 #define gcc_unused
00093 #define gcc_warn_unused_result
00094 
00095 #define gcc_nonnull(...)
00096 #define gcc_nonnull_all
00097 
00098 #define gcc_likely(x) (x)
00099 #define gcc_unlikely(x) (x)
00100 
00101 #define gcc_aligned(n)
00102 
00103 #define gcc_visibility_hidden
00104 #define gcc_visibility_default
00105 
00106 #define gcc_always_inline inline
00107 
00108 #endif
00109 
00110 #if GCC_CHECK_VERSION(4,3)
00111 
00112 #define gcc_hot __attribute__((hot))
00113 #define gcc_cold __attribute__((cold))
00114 
00115 #else /* ! GCC_UNUSED >= 40300 */
00116 
00117 #define gcc_hot
00118 #define gcc_cold
00119 
00120 #endif /* ! GCC_UNUSED >= 40300 */
00121 
00122 #if GCC_CHECK_VERSION(4,6) && !defined(__clang__)
00123 #define gcc_flatten __attribute__((flatten))
00124 #else
00125 #define gcc_flatten
00126 #endif
00127 
00128 #ifndef __cplusplus
00129 /* plain C99 has "restrict" */
00130 #define gcc_restrict restrict
00131 #elif GCC_CHECK_VERSION(4,0)
00132 /* "__restrict__" is a GCC extension for C++ */
00133 #define gcc_restrict __restrict__
00134 #else
00135 /* disable it on other compilers */
00136 #define gcc_restrict
00137 #endif
00138 
00139 /* C++11 features */
00140 
00141 #if defined(__cplusplus)
00142 
00143 /* support for C++11 "override" was added in gcc 4.7 */
00144 #if !defined(__clang__) && !GCC_CHECK_VERSION(4,7)
00145 #define override
00146 #define final
00147 #endif
00148 
00149 #if defined(__clang__) || GCC_CHECK_VERSION(4,8)
00150 #define gcc_alignas(T, fallback) alignas(T)
00151 #else
00152 #define gcc_alignas(T, fallback) gcc_aligned(fallback)
00153 #endif
00154 
00155 #endif
00156 
00157 #ifndef __has_feature
00158   // define dummy macro for non-clang compilers
00159   #define __has_feature(x) 0
00160 #endif
00161 
00162 #if __has_feature(attribute_unused_on_fields)
00163 #define gcc_unused_field gcc_unused
00164 #else
00165 #define gcc_unused_field
00166 #endif
00167 
00168 #if defined(__GNUC__) || defined(__clang__)
00169 #define gcc_unreachable() __builtin_unreachable()
00170 #else
00171 #define gcc_unreachable()
00172 #endif
00173 
00174 #endif