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) && \
50  defined(__has_cpp_attribute) && \
51  __cplusplus >= 201103L
52 # define AV_HAS_STD_ATTRIBUTE(x) __has_cpp_attribute(x)
53 #elif !defined(__cplusplus) && \
54  defined(__has_c_attribute) && \
55  defined(__STDC_VERSION__) && \
56  __STDC_VERSION__ >= 202311L
57 # define AV_HAS_STD_ATTRIBUTE(x) __has_c_attribute(x)
58 #else
59 # define AV_HAS_STD_ATTRIBUTE(x) 0
60 #endif
61 
62 #if AV_HAS_STD_ATTRIBUTE(fallthrough)
63 # define av_fallthrough [[fallthrough]]
64 #elif AV_HAS_ATTRIBUTE(fallthrough)
65 # define av_fallthrough __attribute__((fallthrough))
66 #else
67 # define av_fallthrough do {} while(0)
68 #endif
69 
70 #ifndef av_always_inline
71 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
72 # define av_always_inline __attribute__((always_inline)) inline
73 #elif defined(_MSC_VER)
74 # define av_always_inline __forceinline
75 #else
76 # define av_always_inline inline
77 #endif
78 #endif
79 
80 #ifndef av_extern_inline
81 #if defined(__ICL) && __ICL >= 1210 || defined(__GNUC_STDC_INLINE__)
82 # define av_extern_inline extern inline
83 #else
84 # define av_extern_inline inline
85 #endif
86 #endif
87 
88 #if AV_HAS_STD_ATTRIBUTE(nodiscard)
89 # define av_warn_unused_result [[nodiscard]]
90 #elif AV_GCC_VERSION_AT_LEAST(3,4) || defined(__clang__)
91 # define av_warn_unused_result __attribute__((warn_unused_result))
92 #else
93 # define av_warn_unused_result
94 #endif
95 
96 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
97 # define av_noinline __attribute__((noinline))
98 #elif defined(_MSC_VER)
99 # define av_noinline __declspec(noinline)
100 #else
101 # define av_noinline
102 #endif
103 
104 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
105 # define av_pure __attribute__((pure))
106 #else
107 # define av_pure
108 #endif
109 
110 #if AV_GCC_VERSION_AT_LEAST(2,6) || defined(__clang__)
111 # define av_const __attribute__((const))
112 #else
113 # define av_const
114 #endif
115 
116 #if AV_GCC_VERSION_AT_LEAST(4,3) || defined(__clang__)
117 # define av_cold __attribute__((cold))
118 #else
119 # define av_cold
120 #endif
121 
122 #if (AV_GCC_VERSION_AT_LEAST(4,1) && !defined(__clang__ )) || AV_HAS_ATTRIBUTE(flatten)
123 # define av_flatten __attribute__((flatten))
124 #else
125 # define av_flatten
126 #endif
127 
128 #if AV_HAS_STD_ATTRIBUTE(deprecated)
129 # define attribute_deprecated [[deprecated]]
130 #elif AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
131 # define attribute_deprecated __attribute__((deprecated))
132 #elif defined(_MSC_VER)
133 # define attribute_deprecated __declspec(deprecated)
134 #else
135 # define attribute_deprecated
136 #endif
137 
138 /**
139  * Disable warnings about deprecated features
140  * This is useful for sections of code kept for backward compatibility and
141  * scheduled for removal.
142  */
143 #ifndef AV_NOWARN_DEPRECATED
144 #if AV_GCC_VERSION_AT_LEAST(4,6) || defined(__clang__)
145 # define AV_NOWARN_DEPRECATED(code) \
146  _Pragma("GCC diagnostic push") \
147  _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \
148  code \
149  _Pragma("GCC diagnostic pop")
150 #elif defined(_MSC_VER)
151 # define AV_NOWARN_DEPRECATED(code) \
152  __pragma(warning(push)) \
153  __pragma(warning(disable : 4996)) \
154  code; \
155  __pragma(warning(pop))
156 #else
157 # define AV_NOWARN_DEPRECATED(code) code
158 #endif
159 #endif
160 
161 #if AV_HAS_STD_ATTRIBUTE(maybe_unused)
162 # define av_unused [[maybe_unused]]
163 #elif defined(__GNUC__) || defined(__clang__)
164 # define av_unused __attribute__((unused))
165 #else
166 # define av_unused
167 #endif
168 
169 /**
170  * Mark a variable as used and prevent the compiler from optimizing it
171  * away. This is useful for variables accessed only from inline
172  * assembler without the compiler being aware.
173  */
174 #if AV_GCC_VERSION_AT_LEAST(3,1) || defined(__clang__)
175 # define av_used __attribute__((used))
176 #else
177 # define av_used
178 #endif
179 
180 #if AV_GCC_VERSION_AT_LEAST(3,3) || defined(__clang__)
181 # define av_alias __attribute__((may_alias))
182 #else
183 # define av_alias
184 #endif
185 
186 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__INTEL_COMPILER)
187 # define av_uninit(x) x=x
188 #else
189 # define av_uninit(x) x
190 #endif
191 
192 #if defined(__GNUC__) || defined(__clang__)
193 # define av_builtin_constant_p __builtin_constant_p
194 #else
195 # define av_builtin_constant_p(x) 0
196 #endif
197 
198 // for __MINGW_PRINTF_FORMAT and __MINGW_SCANF_FORMAT
199 #ifdef __MINGW32__
200 # include <stdio.h>
201 #endif
202 
203 #ifdef __MINGW_PRINTF_FORMAT
204 # define AV_PRINTF_FMT __MINGW_PRINTF_FORMAT
205 #elif AV_HAS_ATTRIBUTE(format)
206 # define AV_PRINTF_FMT __printf__
207 #endif
208 
209 #ifdef __MINGW_SCANF_FORMAT
210 # define AV_SCANF_FMT __MINGW_SCANF_FORMAT
211 #elif AV_HAS_ATTRIBUTE(format)
212 # define AV_SCANF_FMT __scanf__
213 #endif
214 
215 #ifdef AV_PRINTF_FMT
216 # define av_printf_format(fmtpos, attrpos) __attribute__((format(AV_PRINTF_FMT, fmtpos, attrpos)))
217 #else
218 # define av_printf_format(fmtpos, attrpos)
219 #endif
220 
221 #ifdef AV_SCANF_FMT
222 # define av_scanf_format(fmtpos, attrpos) __attribute__((format(AV_SCANF_FMT, fmtpos, attrpos)))
223 #else
224 # define av_scanf_format(fmtpos, attrpos)
225 #endif
226 
227 #if AV_HAS_STD_ATTRIBUTE(noreturn)
228 # define av_noreturn [[noreturn]]
229 #elif AV_GCC_VERSION_AT_LEAST(2,5) || defined(__clang__)
230 # define av_noreturn __attribute__((noreturn))
231 #else
232 # define av_noreturn
233 #endif
234 
235 #endif /* AVUTIL_ATTRIBUTES_H */