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