Compiler.h (4170B)
1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // Copyright The Music Player Daemon Project 3 4 #ifndef COMPILER_H 5 #define COMPILER_H 6 7 #define GCC_MAKE_VERSION(major, minor, patchlevel) ((major) * 10000 + (minor) * 100 + patchlevel) 8 9 #ifdef __GNUC__ 10 #define GCC_VERSION GCC_MAKE_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) 11 #else 12 #define GCC_VERSION 0 13 #endif 14 15 #ifdef __clang__ 16 # define CLANG_VERSION GCC_MAKE_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) 17 #else 18 # define CLANG_VERSION 0 19 #endif 20 21 /** 22 * Are we building with the specified version of gcc (not clang or any 23 * other compiler) or newer? 24 */ 25 #define GCC_CHECK_VERSION(major, minor) \ 26 (!CLANG_VERSION && \ 27 GCC_VERSION >= GCC_MAKE_VERSION(major, minor, 0)) 28 29 /** 30 * Are we building with clang (any version) or at least the specified 31 * gcc version? 32 */ 33 #define CLANG_OR_GCC_VERSION(major, minor) \ 34 (CLANG_VERSION || GCC_CHECK_VERSION(major, minor)) 35 36 /** 37 * Are we building with gcc (not clang or any other compiler) and a 38 * version older than the specified one? 39 */ 40 #define GCC_OLDER_THAN(major, minor) \ 41 (GCC_VERSION && !CLANG_VERSION && \ 42 GCC_VERSION < GCC_MAKE_VERSION(major, minor, 0)) 43 44 /** 45 * Are we building with the specified version of clang or newer? 46 */ 47 #define CLANG_CHECK_VERSION(major, minor) \ 48 (CLANG_VERSION >= GCC_MAKE_VERSION(major, minor, 0)) 49 50 #if CLANG_OR_GCC_VERSION(4,0) 51 52 /* GCC 4.x */ 53 54 #define gcc_const __attribute__((const)) 55 #define gcc_deprecated __attribute__((deprecated)) 56 #define gcc_may_alias __attribute__((may_alias)) 57 #define gcc_malloc __attribute__((malloc)) 58 #define gcc_noreturn __attribute__((noreturn)) 59 #define gcc_packed __attribute__((packed)) 60 #define gcc_printf(a,b) __attribute__((format(printf, a, b))) 61 #define gcc_pure __attribute__((pure)) 62 #define gcc_sentinel __attribute__((sentinel)) 63 #define gcc_unused __attribute__((unused)) 64 #define gcc_warn_unused_result __attribute__((warn_unused_result)) 65 66 #define gcc_nonnull(...) __attribute__((nonnull(__VA_ARGS__))) 67 #define gcc_nonnull_all __attribute__((nonnull)) 68 69 #define gcc_likely(x) __builtin_expect (!!(x), 1) 70 #define gcc_unlikely(x) __builtin_expect (!!(x), 0) 71 72 #define gcc_aligned(n) __attribute__((aligned(n))) 73 74 #define gcc_visibility_hidden __attribute__((visibility("hidden"))) 75 #define gcc_visibility_default __attribute__((visibility("default"))) 76 77 #define gcc_always_inline __attribute__((always_inline)) 78 79 #else 80 81 /* generic C compiler */ 82 83 #define gcc_const 84 #define gcc_deprecated 85 #define gcc_may_alias 86 #define gcc_malloc 87 #define gcc_noreturn 88 #define gcc_packed 89 #define gcc_printf(a,b) 90 #define gcc_pure 91 #define gcc_sentinel 92 #define gcc_unused 93 #define gcc_warn_unused_result 94 95 #define gcc_nonnull(...) 96 #define gcc_nonnull_all 97 98 #define gcc_likely(x) (x) 99 #define gcc_unlikely(x) (x) 100 101 #define gcc_aligned(n) 102 103 #define gcc_visibility_hidden 104 #define gcc_visibility_default 105 106 #define gcc_always_inline inline 107 108 #endif 109 110 #if CLANG_OR_GCC_VERSION(4,3) 111 112 #define gcc_hot __attribute__((hot)) 113 #define gcc_cold __attribute__((cold)) 114 115 #else /* ! GCC_UNUSED >= 40300 */ 116 117 #define gcc_hot 118 #define gcc_cold 119 120 #endif /* ! GCC_UNUSED >= 40300 */ 121 122 #if GCC_CHECK_VERSION(4,6) 123 #define gcc_flatten __attribute__((flatten)) 124 #else 125 #define gcc_flatten 126 #endif 127 128 #ifndef __cplusplus 129 /* plain C99 has "restrict" */ 130 #define gcc_restrict restrict 131 #elif CLANG_OR_GCC_VERSION(4,0) 132 /* "__restrict__" is a GCC extension for C++ */ 133 #define gcc_restrict __restrict__ 134 #else 135 /* disable it on other compilers */ 136 #define gcc_restrict 137 #endif 138 139 /* C++11 features */ 140 141 #if defined(__cplusplus) 142 143 /* support for C++11 "override" was added in gcc 4.7 */ 144 #if GCC_OLDER_THAN(4,7) 145 #define override 146 #define final 147 #endif 148 149 #if CLANG_OR_GCC_VERSION(4,8) 150 #define gcc_alignas(T, fallback) alignas(T) 151 #else 152 #define gcc_alignas(T, fallback) gcc_aligned(fallback) 153 #endif 154 155 #endif 156 157 #ifndef __has_feature 158 // define dummy macro for non-clang compilers 159 #define __has_feature(x) 0 160 #endif 161 162 #if __has_feature(attribute_unused_on_fields) 163 #define gcc_unused_field gcc_unused 164 #else 165 #define gcc_unused_field 166 #endif 167 168 #if defined(__GNUC__) || defined(__clang__) 169 #define gcc_unreachable() __builtin_unreachable() 170 #else 171 #define gcc_unreachable() 172 #endif 173 174 #endif