| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Handy macros |
| 4 | * |
| 5 | * (c) 2003 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef MLIB_MACROS_H |
| 29 | #define MLIB_MACROS_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <assert.h> |
| 38 | |
| 39 | #ifndef MLIB_COMPILER_H |
| 40 | # include "compiler.h" |
| 41 | #endif |
| 42 | |
| 43 | /*----- Miscellaneous utility macros --------------------------------------*/ |
| 44 | |
| 45 | #define N(v) (sizeof(v)/sizeof(*(v))) |
| 46 | |
| 47 | #define MLIB__STR(x) #x |
| 48 | #define STR(x) MLIB__STR(x) |
| 49 | |
| 50 | #define MLIB__GLUE(x, y) x##y |
| 51 | #define GLUE(x, y) MLIB__GLUE(x, y) |
| 52 | |
| 53 | #ifdef static_assert |
| 54 | # define STATIC_ASSERT(cond, msg) static_assert(cond, msg) |
| 55 | #else |
| 56 | # define STATIC_ASSERT(cond, msg) \ |
| 57 | IGNORABLE extern char static_assert_failed[2*!!(cond) - 1] |
| 58 | #endif |
| 59 | |
| 60 | /*----- String and character hacks ----------------------------------------*/ |
| 61 | |
| 62 | #define CTYPE_HACK(func, ch) (func((unsigned char)(ch))) |
| 63 | |
| 64 | #define ISALNUM(ch) CTYPE_HACK(isalnum, ch) |
| 65 | #define ISALPHA(ch) CTYPE_HACK(isalpha, ch) |
| 66 | #define ISASCII(ch) CTYPE_HACK(isascii, ch) |
| 67 | #define ISBLANK(ch) CTYPE_HACK(isblank, ch) |
| 68 | #define ISCNTRL(ch) CTYPE_HACK(iscntrl, ch) |
| 69 | #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch) |
| 70 | #define ISGRAPH(ch) CTYPE_HACK(isgraph, ch) |
| 71 | #define ISLOWER(ch) CTYPE_HACK(islower, ch) |
| 72 | #define ISPRINT(ch) CTYPE_HACK(isprint, ch) |
| 73 | #define ISPUNCT(ch) CTYPE_HACK(ispunct, ch) |
| 74 | #define ISSPACE(ch) CTYPE_HACK(isspace, ch) |
| 75 | #define ISUPPER(ch) CTYPE_HACK(isupper, ch) |
| 76 | #define ISXDIGIT(ch) CTYPE_HACK(isxdigit, ch) |
| 77 | |
| 78 | #define TOASCII(ch) CTYPE_HACK(toascii, ch) |
| 79 | #define TOLOWER(ch) CTYPE_HACK(tolower, ch) |
| 80 | #define TOUPPER(ch) CTYPE_HACK(toupper, ch) |
| 81 | |
| 82 | #define MEMCMP(x, op, y, n) (memcmp((x), (y), (n)) op 0) |
| 83 | #define STRCMP(x, op, y) (strcmp((x), (y)) op 0) |
| 84 | #define STRNCMP(x, op, y, n) (strncmp((x), (y), (n)) op 0) |
| 85 | |
| 86 | /*----- Compiler diagnostics ----------------------------------------------*/ |
| 87 | |
| 88 | /* --- Compiler-specific definitions --- */ |
| 89 | |
| 90 | #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3) |
| 91 | # define NORETURN __attribute__((__noreturn__)) |
| 92 | # define PRINTF_LIKE(fix, aix) __attribute__((__format__(printf, fix, aix))) |
| 93 | # define SCANF_LIKE(fix, aix) __attribute__((__format__(scanf, fix, aix))) |
| 94 | # define IGNORABLE __attribute__((__unused__)) |
| 95 | #endif |
| 96 | |
| 97 | #if GCC_VERSION_P(4, 5) || CLANG_VERSION_P(3, 3) |
| 98 | # define DEPRECATED(msg) __attribute__((__deprecated__(msg))) |
| 99 | #elif GCC_VERSION_P(3, 1) |
| 100 | # define DEPRECATED(msg) __attribute__((__deprecated__)) |
| 101 | #endif |
| 102 | |
| 103 | #if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3) |
| 104 | # define EXECL_LIKE(ntrail) __attribute__((__sentinel__(ntrail))) |
| 105 | #endif |
| 106 | |
| 107 | #if CLANG_VERSION_P(3, 3) |
| 108 | |
| 109 | # define MLIB__PRAGMA_HACK(x) _Pragma(#x) |
| 110 | # define MLIB__MUFFLE_WARNINGS(warns, body) \ |
| 111 | _Pragma("clang diagnostic push") \ |
| 112 | warns \ |
| 113 | body \ |
| 114 | _Pragma("clang diagnostic pop") |
| 115 | # define CLANG_WARNING(warn) \ |
| 116 | MLIB__PRAGMA_HACK(clang diagnostic ignored warn) |
| 117 | # define MUFFLE_WARNINGS_DECL(warns, body) \ |
| 118 | MLIB__MUFFLE_WARNINGS(warns, body) |
| 119 | # define MUFFLE_WARNINGS_EXPR(warns, body) \ |
| 120 | __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) }) |
| 121 | # define MUFFLE_WARNINGS_STMT(warns, body) \ |
| 122 | do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0) |
| 123 | |
| 124 | #elif GCC_VERSION_P(4, 6) |
| 125 | |
| 126 | /* --- Diagnostic suppression in GCC: a tale of woe --- * |
| 127 | * |
| 128 | * This is extremely unpleasant, largely as a result of bugs in the GCC |
| 129 | * preprocessor's handling of @_Pragma@. The fundamental problem is |
| 130 | * that it's the preprocessor, and not the compiler proper, which |
| 131 | * detects @_Pragma@, emitting @#pragma@ lines into its output; and it |
| 132 | * does it during macro expansion, even if the macro is being expanded |
| 133 | * during argument collection. Since arguments are expanded before |
| 134 | * replacing the macro's invocation with its body, a pragma in an |
| 135 | * argument will be emitted %%\emph{before}%% any pragmata in the body, |
| 136 | * even if they appear before the argument in the body -- and even if |
| 137 | * the argument doesn't actually appear anywhere at all in the body. |
| 138 | * |
| 139 | * Another, rather less significant, problem is that @_Pragma@'s |
| 140 | * argument is a single string literal, recognized in translation phase |
| 141 | * 4, before string-literal concatenation in phase 6, so we must build |
| 142 | * pragma bodies as token lists and then stringify them. |
| 143 | * |
| 144 | * As a result, we need some subterfuge here. The @MLIB__PRAGMA_HACK@ |
| 145 | * macro issues a @_Pragma@ on its argument token list, which it |
| 146 | * stringifies; this deals with the second problem. The first is |
| 147 | * trickier: we must delay expansion of @MLIB__PRAGMA_HACK@ from the |
| 148 | * argument collection phase to the body rescanning phase, and we do |
| 149 | * this by splitting the invocations between @GCC_WARNING@ macro calls: |
| 150 | * the name is left hanging from the previous call (or from |
| 151 | * @MLIB__MUFFLE_WARNINGS@, in the first case) and the body is supplied |
| 152 | * by @GCC_WARNING@, which also supplies the next @MLIB__PRAGMA_HACK@. |
| 153 | * The remaining problem is to make sure we can dispose of the final |
| 154 | * trailing @MLIB__PRAGMA_HACK@ harmlessly, which we do by introducing |
| 155 | * an extra argument @emitp@, which may be either @t@ or @nil@; this |
| 156 | * dispatches to an appropriate helper macro by means of token-pasting. |
| 157 | * |
| 158 | * I'm so sorry. |
| 159 | */ |
| 160 | |
| 161 | # define MLIB__PRAGMA_HACK_t(x) _Pragma(#x) |
| 162 | # define MLIB__PRAGMA_HACK_nil(x) |
| 163 | # define MLIB__PRAGMA_HACK(emitp, x) MLIB__PRAGMA_HACK_##emitp(x) |
| 164 | # define MLIB__MUFFLE_WARNINGS(warns, body) \ |
| 165 | _Pragma("GCC diagnostic push") MLIB__PRAGMA_HACK \ |
| 166 | warns \ |
| 167 | (nil, nil) \ |
| 168 | body \ |
| 169 | _Pragma("GCC diagnostic pop") |
| 170 | # define GCC_WARNING(warn) \ |
| 171 | (t, GCC diagnostic ignored warn) MLIB__PRAGMA_HACK |
| 172 | # define MUFFLE_WARNINGS_DECL(warns, body) \ |
| 173 | MLIB__MUFFLE_WARNINGS(warns, body) |
| 174 | # define MUFFLE_WARNINGS_EXPR(warns, body) \ |
| 175 | __extension__ ({ MLIB__MUFFLE_WARNINGS(warns, (body);) }) |
| 176 | # define MUFFLE_WARNINGS_STMT(warns, body) \ |
| 177 | do { MLIB__MUFFLE_WARNINGS(warns, body) } while (0) |
| 178 | #endif |
| 179 | |
| 180 | /* --- Fallback definitions, mostly trivial --- */ |
| 181 | |
| 182 | #ifndef DEPRECATED |
| 183 | # define DEPRECATED(msg) |
| 184 | #endif |
| 185 | |
| 186 | #ifndef EXECL_LIKE |
| 187 | # define EXECL_LIKE(ntrail) |
| 188 | #endif |
| 189 | |
| 190 | #ifndef DISCARD |
| 191 | # define DISCARD(x) do if (x); while (0) |
| 192 | #endif |
| 193 | |
| 194 | #ifndef IGNORE |
| 195 | # define IGNORE(x) ((void)(x)) |
| 196 | #endif |
| 197 | |
| 198 | #ifndef MUFFLE_WARNINGS_DECL |
| 199 | # define MUFFLE_WARNINGS_DECL(warns, body) body |
| 200 | #endif |
| 201 | |
| 202 | #ifndef MUFFLE_WARNINGS_EXPR |
| 203 | # define MUFFLE_WARNINGS_EXPR(warns, body) (body) |
| 204 | #endif |
| 205 | |
| 206 | #ifndef MUFFLE_WARNINGS_STMT |
| 207 | # define MUFFLE_WARNINGS_STMT(warns, body) do { body } while (0) |
| 208 | #endif |
| 209 | |
| 210 | #ifndef PRINTF_LIKE |
| 211 | # define PRINF_LIKE(fmtix, argix) |
| 212 | #endif |
| 213 | |
| 214 | #ifndef SCANF_LIKE |
| 215 | # define SCANF_LIKE(fmtix, argix) |
| 216 | #endif |
| 217 | |
| 218 | #ifndef NORETURN |
| 219 | # define NORETURN |
| 220 | #endif |
| 221 | |
| 222 | #ifndef IGNORABLE |
| 223 | # define IGNORABLE |
| 224 | #endif |
| 225 | |
| 226 | #ifndef GCC_WARNING |
| 227 | # define GCC_WARNING(warn) |
| 228 | #endif |
| 229 | |
| 230 | #ifndef CLANG_WARNING |
| 231 | # define CLANG_WARNING(warn) |
| 232 | #endif |
| 233 | |
| 234 | /*----- That's all, folks -------------------------------------------------*/ |
| 235 | |
| 236 | #ifdef __cplusplus |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | #endif |