FFmpeg
attributes.h
Go to the documentation of this file.
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Macro definitions for various function/variable attributes
24  */
25 
26 #ifndef AVUTIL_ATTRIBUTES_H
27 #define AVUTIL_ATTRIBUTES_H
28 
29 #ifdef __GNUC__
30 # define AV_GCC_VERSION_AT_LEAST(x,y) (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
31 # define AV_GCC_VERSION_AT_MOST(x,y) (__GNUC__ < (x) || __GNUC__ == (x) && __GNUC_MINOR__ <= (y))
32 #else
33 # define AV_GCC_VERSION_AT_LEAST(x,y) 0
34 # define AV_GCC_VERSION_AT_MOST(x,y) 0
35 #endif
36 
37 #ifdef __has_builtin
38 # define AV_HAS_BUILTIN(x) __has_builtin(x)
39 #else
40 # define AV_HAS_BUILTIN(x) 0
41 #endif
42 
43 #ifdef __has_attribute
44 # define AV_HAS_ATTRIBUTE(x) __has_attribute(x)
45 #else
46 # define AV_HAS_ATTRIBUTE(x) 0
47 #endif
48 
49 #if defined(__cplusplus) && defined(__has_cpp_attribute)
50 # define AV_HAS_STD_ATTRIBUTE(x) __has_cpp_attribute(x)
51 #elif !defined(__cplusplus) && defined(__has_c_attribute)
52 # define AV_HAS_STD_ATTRIBUTE(x) __has_c_attribute(x)
53 #else
54 # define AV_HAS_STD_ATTRIBUTE(x) 0
55 #endif
56 
57 #ifndef av_always_inline
58 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
59 # define av_always_inline __attribute__((always_inline)) inline
60 #elif defined(_MSC_VER)
61 # define av_always_inline __forceinline
62 #else
63 # define av_always_inline inline
64 #endif
65 #endif
66 
67 #ifndef av_extern_inline
68 #if defined(__ICL) && __ICL >= 1210 || defined(__GNUC_STDC_INLINE__)
69 # define av_extern_inline extern inline
70 #else
71 # define av_extern_inline inline
72 #endif
73 #endif
74 
75 #if AV_HAS_STD_ATTRIBUTE(nodiscard)
76 # define av_warn_unused_result [[nodiscard]]
77 #elif AV_GCC_VERSION_AT_LEAST(3,4) || defined(__clang__)
78 # define av_warn_unused_result __attribute__((warn_unused_result))
79 #else
80 # define av_warn_unused_result
81 #endif
82 
83 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
84 # define av_noinline __attribute__((noinline))
85 #elif defined(_MSC_VER)
86 # define av_noinline __declspec(noinline)
87 #else
88 # define av_noinline
89 #endif
90 
91 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
92 # define av_pure __attribute__((pure))
93 #else
94 # define av_pure
95 #endif
96 
97 #if AV_GCC_VERSION_AT_LEAST(2,6) || defined(__clang__)
98 # define av_const __attribute__((const))
99 #else
100 # define av_const
101 #endif
102 
103 #if AV_GCC_VERSION_AT_LEAST(4,3) || defined(__clang__)
104 # define av_cold __attribute__((cold))
105 #else
106 # define av_cold
107 #endif
108 
109 #if AV_GCC_VERSION_AT_LEAST(4,1) && !defined(__llvm__)
110 # define av_flatten __attribute__((flatten))
111 #else
112 # define av_flatten
113 #endif
114 
115 #if AV_HAS_STD_ATTRIBUTE(deprecated)
116 # define attribute_deprecated [[deprecated]]
117 #elif AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
118 # define attribute_deprecated __attribute__((deprecated))
119 #elif defined(_MSC_VER)
120 # define attribute_deprecated __declspec(deprecated)
121 #else
122 # define attribute_deprecated
123 #endif
124 
125 /**
126  * Disable warnings about deprecated features
127  * This is useful for sections of code kept for backward compatibility and
128  * scheduled for removal.
129  */
130 #ifndef AV_NOWARN_DEPRECATED
131 #if AV_GCC_VERSION_AT_LEAST(4,6) || defined(__clang__)
132 # define AV_NOWARN_DEPRECATED(code) \
133  _Pragma("GCC diagnostic push") \
134  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \
135  code \
136  _Pragma("GCC diagnostic pop")
137 #elif defined(_MSC_VER)
138 # define AV_NOWARN_DEPRECATED(code) \
139  __pragma(warning(push)) \
140  __pragma(warning(disable : 4996)) \
141  code; \
142  __pragma(warning(pop))
143 #else
144 # define AV_NOWARN_DEPRECATED(code) code
145 #endif
146 #endif
147 
148 #if AV_HAS_STD_ATTRIBUTE(maybe_unused)
149 # define av_unused [[maybe_unused]]
150 #elif defined(__GNUC__) || defined(__clang__)
151 # define av_unused __attribute__((unused))
152 #else
153 # define av_unused
154 #endif
155 
156 /**
157  * Mark a variable as used and prevent the compiler from optimizing it
158  * away. This is useful for variables accessed only from inline
159  * assembler without the compiler being aware.
160  */
161 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
162 # define av_used __attribute__((used))
163 #else
164 # define av_used
165 #endif
166 
167 #if AV_GCC_VERSION_AT_LEAST(3,3) || defined(__clang__)
168 # define av_alias __attribute__((may_alias))
169 #else
170 # define av_alias
171 #endif
172 
173 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__INTEL_COMPILER)
174 # define av_uninit(x) x=x
175 #else
176 # define av_uninit(x) x
177 #endif
178 
179 #if defined(__GNUC__) || defined(__clang__)
180 # define av_builtin_constant_p __builtin_constant_p
181 #else
182 # define av_builtin_constant_p(x) 0
183 #endif
184 
185 // for __MINGW_PRINTF_FORMAT and __MINGW_SCANF_FORMAT
186 #ifdef __MINGW32__
187 # include <stdio.h>
188 #endif
189 
190 #ifdef __MINGW_PRINTF_FORMAT
191 # define AV_PRINTF_FMT __MINGW_PRINTF_FORMAT
192 #elif AV_HAS_ATTRIBUTE(format)
193 # define AV_PRINTF_FMT __printf__
194 #endif
195 
196 #ifdef __MINGW_SCANF_FORMAT
197 # define AV_SCANF_FMT __MINGW_SCANF_FORMAT
198 #elif AV_HAS_ATTRIBUTE(format)
199 # define AV_SCANF_FMT __scanf__
200 #endif
201 
202 #ifdef AV_PRINTF_FMT
203 # define av_printf_format(fmtpos, attrpos) __attribute__((format(AV_PRINTF_FMT, fmtpos, attrpos)))
204 #else
205 # define av_printf_format(fmtpos, attrpos)
206 #endif
207 
208 #ifdef AV_SCANF_FMT
209 # define av_scanf_format(fmtpos, attrpos) __attribute__((format(AV_SCANF_FMT, fmtpos, attrpos)))
210 #else
211 # define av_scanf_format(fmtpos, attrpos)
212 #endif
213 
214 #if AV_HAS_STD_ATTRIBUTE(noreturn)
215 # define av_noreturn [[noreturn]]
216 #elif AV_GCC_VERSION_AT_LEAST(2,5) || defined(__clang__)
217 # define av_noreturn __attribute__((noreturn))
218 #else
219 # define av_noreturn
220 #endif
221 
222 #endif /* AVUTIL_ATTRIBUTES_H */