| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* attributes.h: A collection of useful compiler attribute macros, |
| 4 |
* backported from Atheme IRC Services to Mowgli. |
| 5 |
* |
| 6 |
* Copyright (C) 2018 Atheme Development Group (https://atheme.github.io/) |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice is present in all copies. |
| 11 |
* |
| 12 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 14 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 16 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 17 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 18 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 19 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 20 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 21 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 |
* POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef MOWGLI_SRC_LIBMOWGLI_PLATFORM_ATTRIBUTES_H_INCLUDE_GUARD |
| 26 |
#define MOWGLI_SRC_LIBMOWGLI_PLATFORM_ATTRIBUTES_H_INCLUDE_GUARD 1 |
| 27 |
|
| 28 |
/* Woe is me for spending many hours looking up in the GCC documentation |
| 29 |
* which old versions introduced which attributes. |
| 30 |
* |
| 31 |
* <https://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/> |
| 32 |
* <https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/> |
| 33 |
* <https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/> |
| 34 |
* |
| 35 |
* We don't test for the GCC patchlevel. If e.g. v4.9.1 introduced an |
| 36 |
* attribute that v4.9.0 lacks, too bad; use >= v5 instead if you want the |
| 37 |
* attribute that badly. |
| 38 |
* |
| 39 |
* Clang >= v3.7 and GCC >= v5 support the __has_attribute() function-like |
| 40 |
* macro used below, so we just use that instead. This is why there are no |
| 41 |
* links above to the GCC documentation for newer versions. |
| 42 |
* |
| 43 |
* -- amdj |
| 44 |
*/ |
| 45 |
|
| 46 |
#ifdef __has_attribute |
| 47 |
# if __has_attribute(__alloc_size__) |
| 48 |
# define MOWGLI_ATTR_HAS_ALLOC_SIZE 1 |
| 49 |
# endif |
| 50 |
# if __has_attribute(__deprecated__) |
| 51 |
# define MOWGLI_ATTR_HAS_DEPRECATED 1 |
| 52 |
# endif |
| 53 |
# if __has_attribute(__diagnose_if__) |
| 54 |
# define MOWGLI_ATTR_HAS_DIAGNOSE_IF 1 |
| 55 |
# endif |
| 56 |
# if __has_attribute(__format__) |
| 57 |
# define MOWGLI_ATTR_HAS_FORMAT 1 |
| 58 |
# endif |
| 59 |
# if __has_attribute(__malloc__) |
| 60 |
# define MOWGLI_ATTR_HAS_MALLOC 1 |
| 61 |
# endif |
| 62 |
# if __has_attribute(__maybe_unused__) |
| 63 |
# define MOWGLI_ATTR_HAS_MAYBE_UNUSED 1 |
| 64 |
# endif |
| 65 |
# if __has_attribute(__noreturn__) |
| 66 |
# define MOWGLI_ATTR_HAS_NORETURN 1 |
| 67 |
# endif |
| 68 |
# if __has_attribute(__packed__) |
| 69 |
# define MOWGLI_ATTR_HAS_PACKED 1 |
| 70 |
# endif |
| 71 |
# if __has_attribute(__returns_nonnull__) |
| 72 |
# define MOWGLI_ATTR_HAS_RETURNS_NONNULL 1 |
| 73 |
# endif |
| 74 |
# if __has_attribute(__unused__) |
| 75 |
# define MOWGLI_ATTR_HAS_UNUSED 1 |
| 76 |
# endif |
| 77 |
# if __has_attribute(__warn_unused_result__) |
| 78 |
# define MOWGLI_ATTR_HAS_WARN_UNUSED_RESULT 1 |
| 79 |
# endif |
| 80 |
#else |
| 81 |
# if !defined(__clang__) && defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) |
| 82 |
# if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))) |
| 83 |
# define MOWGLI_ATTR_HAS_DEPRECATED 1 |
| 84 |
# define MOWGLI_ATTR_HAS_FORMAT 1 |
| 85 |
# define MOWGLI_ATTR_HAS_MALLOC 1 |
| 86 |
# define MOWGLI_ATTR_HAS_NORETURN 1 |
| 87 |
# define MOWGLI_ATTR_HAS_PACKED 1 |
| 88 |
# define MOWGLI_ATTR_HAS_UNUSED 1 |
| 89 |
# define MOWGLI_ATTR_HAS_WARN_UNUSED_RESULT 1 |
| 90 |
# endif |
| 91 |
# if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))) |
| 92 |
# define MOWGLI_ATTR_HAS_ALLOC_SIZE 1 |
| 93 |
# endif |
| 94 |
# if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9))) |
| 95 |
# define MOWGLI_ATTR_HAS_RETURNS_NONNULL 1 |
| 96 |
# endif |
| 97 |
# endif |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
/* Informs the compiler that the size of memory reachable by the pointer returned from this function is x (or x * y) |
| 103 |
* bytes, where x and y are positional function arguments (starting at 1). This aids static analysis and makes e.g. |
| 104 |
* __builtin_object_size(ptr) possible. |
| 105 |
* |
| 106 |
* Example: |
| 107 |
* void *my_debug_malloc(const char *file, int line, size_t len) __attribute__((alloc_size(3))); |
| 108 |
* #define DEBUG_MALLOC(len) my_debug_malloc(__FILE__, __LINE__, (len)) |
| 109 |
*/ |
| 110 |
#ifdef MOWGLI_ATTR_HAS_ALLOC_SIZE |
| 111 |
# define MOWGLI_FATTR_ALLOC_SIZE(x) __attribute__((__alloc_size__((x)))) |
| 112 |
# define MOWGLI_FATTR_ALLOC_SIZE_PRODUCT(x, y) __attribute__((__alloc_size__((x), (y)))) |
| 113 |
#else |
| 114 |
# define MOWGLI_FATTR_ALLOC_SIZE(x) /* No 'alloc_size' function attribute support */ |
| 115 |
# define MOWGLI_FATTR_ALLOC_SIZE_PRODUCT(x, y) /* No 'alloc_size' function attribute support */ |
| 116 |
#endif |
| 117 |
|
| 118 |
// Diagnose when calls or references to this function are made |
| 119 |
#ifdef MOWGLI_ATTR_HAS_DEPRECATED |
| 120 |
# define MOWGLI_FATTR_DEPRECATED __attribute__((__deprecated__)) |
| 121 |
#else |
| 122 |
# define MOWGLI_FATTR_DEPRECATED /* No 'deprecated' function attribute support */ |
| 123 |
#endif |
| 124 |
|
| 125 |
/* Informs the compiler to generate a diagnostic (type=="warning") or compilation failure (type=="error") when |
| 126 |
* calling this function with !cond; cond has access to its parameters by name. msg is the message to emit. Can |
| 127 |
* only be used with conditions that can be evaluated at compilation time. |
| 128 |
* |
| 129 |
* Example: |
| 130 |
* void foo(void *ptr) __attribute__((diagnose_if(!ptr, "calling foo() with NULL 'ptr'", "error"))); |
| 131 |
*/ |
| 132 |
#ifdef MOWGLI_ATTR_HAS_DIAGNOSE_IF |
| 133 |
# define MOWGLI_FATTR_DIAGNOSE_IF(cond, msg, type) __attribute__((__diagnose_if__((cond), (msg), (type)))) |
| 134 |
#else |
| 135 |
# define MOWGLI_FATTR_DIAGNOSE_IF(cond, msg, type) /* No 'diagnose_if' function attribute support */ |
| 136 |
#endif |
| 137 |
|
| 138 |
/* Have the compiler verify printf(3) or scanf(3) format tokens in the parameter position given by 'fmt' against the |
| 139 |
* function arguments starting at position 'start'. Also quenches warnings about passing 'fmt' itself to the |
| 140 |
* printf(3) or scanf(3) family of functions (because it is not a string literal). If the format string is available |
| 141 |
* but the list of arguments isn't (as in the case of e.g. stdarg(3) variadic functions), use 0 for 'start'. |
| 142 |
* |
| 143 |
* Examples: |
| 144 |
* void log_message(int lvl, const char *fmt, ...) __attribute__((format(__printf__, 2, 3))); |
| 145 |
* void vlog_message(int lvl, const char *fmt, va_args argv) __attribute__((format(__printf__, 2, 0))); |
| 146 |
*/ |
| 147 |
#ifdef MOWGLI_ATTR_HAS_FORMAT |
| 148 |
# define MOWGLI_FATTR_PRINTF(fmt, start) __attribute__((__format__(__printf__, (fmt), (start)))) |
| 149 |
# define MOWGLI_FATTR_SCANF(fmt, start) __attribute__((__format__(__scanf__, (fmt), (start)))) |
| 150 |
#else |
| 151 |
# define MOWGLI_FATTR_PRINTF(fmt, start) /* No 'format' function attribute support */ |
| 152 |
# define MOWGLI_FATTR_SCANF(fmt, start) /* No 'format' function attribute support */ |
| 153 |
#endif |
| 154 |
|
| 155 |
/* Inform the compiler that this function allocates memory; in particular, that the pointer it returns cannot |
| 156 |
* possibly alias or overlap another object that existed at the time the function was called; and also that if the |
| 157 |
* return value is a pointer to a structure, that any of the structure's pointer members also do not point to such |
| 158 |
* objects. This aids static analysis and some compiler optimisations. |
| 159 |
* |
| 160 |
* The warn_unused_result attribute is also present (if supported) because ignoring the return value could lead to |
| 161 |
* a memory leak. |
| 162 |
*/ |
| 163 |
#ifdef MOWGLI_ATTR_HAS_MALLOC |
| 164 |
# ifdef MOWGLI_ATTR_HAS_WARN_UNUSED_RESULT |
| 165 |
# define MOWGLI_FATTR_MALLOC __attribute__((__malloc__, __warn_unused_result__)) |
| 166 |
# else |
| 167 |
# define MOWGLI_FATTR_MALLOC __attribute__((__malloc__)) |
| 168 |
# endif |
| 169 |
#else |
| 170 |
# define MOWGLI_FATTR_MALLOC /* No 'malloc' function attribute support */ |
| 171 |
#endif |
| 172 |
|
| 173 |
/* Inform the compiler that a variable may be unused (don't warn, whether it's used or not). This is distinct |
| 174 |
* from __attribute__((unused)) because some compilers (e.g. Clang) will warn you if you use such a variable. |
| 175 |
*/ |
| 176 |
#ifdef MOWGLI_ATTR_HAS_MAYBE_UNUSED |
| 177 |
# define MOWGLI_VATTR_MAYBE_UNUSED __attribute__((__maybe_unused__)) |
| 178 |
#else |
| 179 |
# define MOWGLI_VATTR_MAYBE_UNUSED /* No 'maybe_unused' variable attribute support */ |
| 180 |
#endif |
| 181 |
|
| 182 |
/* Inform the compiler that this function does not return to its caller. This aids some compiler optimisations, |
| 183 |
* such as not inserting return instructions and stack unwinding/validating logic into the end of the function, or |
| 184 |
* time spent evaluating whether a function can be tail-call-recursively optimised. It also aids static analysis |
| 185 |
* because an analyser can assume this function is an 'assertion handler' (i.e. that it terminates the program), |
| 186 |
* which lets it assume that certain code paths are unreachable. |
| 187 |
* |
| 188 |
* Example: |
| 189 |
* void my_debug_abort(const char *reason) __attribute__((noreturn)); |
| 190 |
*/ |
| 191 |
#ifdef MOWGLI_ATTR_HAS_NORETURN |
| 192 |
# define MOWGLI_FATTR_NORETURN __attribute__((__noreturn__)) |
| 193 |
#else |
| 194 |
# define MOWGLI_FATTR_NORETURN /* No 'noreturn' function attribute support */ |
| 195 |
#endif |
| 196 |
|
| 197 |
// Inform the compiler that we don't want structure padding on this object |
| 198 |
#ifdef MOWGLI_ATTR_HAS_PACKED |
| 199 |
# define MOWGLI_SATTR_PACKED __attribute__((__packed__)) |
| 200 |
#else |
| 201 |
# define MOWGLI_SATTR_PACKED /* No 'packed' structure attribute support */ |
| 202 |
#endif |
| 203 |
|
| 204 |
/* Inform the compiler that the pointer returned by this function is never NULL. |
| 205 |
* This aids compiler optimisations and static analysis. |
| 206 |
*/ |
| 207 |
#ifdef MOWGLI_ATTR_HAS_RETURNS_NONNULL |
| 208 |
# define MOWGLI_FATTR_RETURNS_NONNULL __attribute__((__returns_nonnull__)) |
| 209 |
#else |
| 210 |
# define MOWGLI_FATTR_RETURNS_NONNULL /* No 'returns_nonnull' function attribute support */ |
| 211 |
#endif |
| 212 |
|
| 213 |
// Inform the compiler that a variable is unused (don't warn if it isn't used, and maybe warn if it is used) |
| 214 |
#ifdef MOWGLI_ATTR_HAS_UNUSED |
| 215 |
# define MOWGLI_VATTR_UNUSED __attribute__((__unused__)) |
| 216 |
#else |
| 217 |
# define MOWGLI_VATTR_UNUSED /* No 'unused' variable attribute support */ |
| 218 |
#endif |
| 219 |
|
| 220 |
// Diagnose if the return value of the function is unused by the caller (or under Clang, not explicitly discarded). |
| 221 |
#ifdef MOWGLI_ATTR_HAS_WARN_UNUSED_RESULT |
| 222 |
# define MOWGLI_FATTR_WUR __attribute__((__warn_unused_result__)) |
| 223 |
#else |
| 224 |
# define MOWGLI_FATTR_WUR /* No 'warn_unused_result' function attribute support */ |
| 225 |
#endif |
| 226 |
|
| 227 |
#endif /* MOWGLI_SRC_LIBMOWGLI_PLATFORM_ATTRIBUTES_H_INCLUDE_GUARD */ |