Blender  V2.93
BLI_compiler_attrs.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 /* hint to make sure function result is actually used */
27 #ifdef __GNUC__
28 # define ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
29 #else
30 # define ATTR_WARN_UNUSED_RESULT
31 #endif
32 
33 /* hint to mark function arguments expected to be non-null
34  * if no arguments are given to the macro, all of pointer
35  * arguments would be expected to be non-null
36  */
37 #ifdef __GNUC__
38 # define ATTR_NONNULL(args...) __attribute__((nonnull(args)))
39 #else
40 # define ATTR_NONNULL(...)
41 #endif
42 
43 /* never returns NULL */
44 #if (__GNUC__ * 100 + __GNUC_MINOR__) >= 409 /* gcc4.9+ only */
45 # define ATTR_RETURNS_NONNULL __attribute__((returns_nonnull))
46 #else
47 # define ATTR_RETURNS_NONNULL
48 #endif
49 
50 /* hint to mark function as it wouldn't return */
51 #if defined(__GNUC__) || defined(__clang__)
52 # define ATTR_NORETURN __attribute__((noreturn))
53 #else
54 # define ATTR_NORETURN
55 #endif
56 
57 /* hint to treat any non-null function return value cannot alias any other pointer */
58 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 403))
59 # define ATTR_MALLOC __attribute__((malloc))
60 #else
61 # define ATTR_MALLOC
62 #endif
63 
64 /* the function return value points to memory (2 args for 'size * tot') */
65 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 403))
66 # define ATTR_ALLOC_SIZE(args...) __attribute__((alloc_size(args)))
67 #else
68 # define ATTR_ALLOC_SIZE(...)
69 #endif
70 
71 /* ensures a NULL terminating argument as the n'th last argument of a variadic function */
72 #ifdef __GNUC__
73 # define ATTR_SENTINEL(arg_pos) __attribute__((sentinel(arg_pos)))
74 #else
75 # define ATTR_SENTINEL(arg_pos)
76 #endif
77 
78 /* hint to compiler that function uses printf-style format string */
79 #ifdef __GNUC__
80 # define ATTR_PRINTF_FORMAT(format_param, dots_param) \
81  __attribute__((format(printf, format_param, dots_param)))
82 #else
83 # define ATTR_PRINTF_FORMAT(format_param, dots_param)
84 #endif
85 
86 /* Use to suppress '-Wimplicit-fallthrough' (in place of 'break'). */
87 #ifndef ATTR_FALLTHROUGH
88 # if defined(__GNUC__) && (__GNUC__ >= 7) /* gcc7.0+ only */
89 # define ATTR_FALLTHROUGH __attribute__((fallthrough))
90 # else
91 # define ATTR_FALLTHROUGH ((void)0)
92 # endif
93 #endif
94 
95 /* Declare the memory alignment in Bytes. */
96 #if defined(_WIN32) && !defined(FREE_WINDOWS)
97 # define ATTR_ALIGN(x) __declspec(align(x))
98 #else
99 # define ATTR_ALIGN(x) __attribute__((aligned(x)))
100 #endif