Skip to content
Myr

macros.h

This file

Path

macros.h

151 lines · 4.1 KiB · C

Last changed by

Not known.

History of this file

Take it

1#pragma once23#define ALEN(v) (sizeof(v) / sizeof((v)[0]))4#define DO_PRAGMA(x) _Pragma(#x)5#define VERCMP(x, y, cx, cy) ((cx > x) || ((cx == x) && (cy >= y)))67#ifdef __has_include8    #define HAS_INCLUDE(x) __has_include(x)9#else10    #define HAS_INCLUDE(x) 011#endif1213#ifdef __has_attribute14    #define HAS_ATTRIBUTE(x) __has_attribute(x)15#else16    #define HAS_ATTRIBUTE(x) 017#endif1819#ifdef __has_feature20    #define HAS_FEATURE(x) __has_feature(x)21#else22    #define HAS_FEATURE(x) 023#endif2425#ifdef __has_builtin26    #define HAS_BUILTIN(x) __has_builtin(x)27#else28    #define HAS_BUILTIN(x) 029#endif3031// __has_extension() is a Clang macro used to determine if a feature is32// available even if not standardized in the current "-std" mode.33#ifdef __has_extension34    #define HAS_EXTENSION(x) __has_extension(x)35#else36    // Clang versions prior to 3.0 only supported __has_feature()37    #define HAS_EXTENSION(x) HAS_FEATURE(x)38#endif3940#if defined(__GNUC__) && defined(__GNUC_MINOR__)41    #define GNUC_AT_LEAST(x, y) VERCMP(x, y, __GNUC__, __GNUC_MINOR__)42#else43    #define GNUC_AT_LEAST(x, y) 044#endif4546#if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(unused) || defined(__TINYC__)47    #define UNUSED __attribute__((__unused__))48#else49    #define UNUSED50#endif5152#if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(malloc)53    #define MALLOC WARN_UNUSED_RESULT __attribute__((__malloc__))54#else55    #define MALLOC WARN_UNUSED_RESULT56#endif5758#if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(pure)59    #define PURE WARN_UNUSED_RESULT __attribute__((__pure__))60#else61    #define PURE WARN_UNUSED_RESULT62#endif6364#if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(const)65    #define CONST_FN WARN_UNUSED_RESULT __attribute__((__const__))66#else67    #define CONST_FN WARN_UNUSED_RESULT68#endif6970#if GNUC_AT_LEAST(3, 0) || HAS_ATTRIBUTE(format)71    #define PRINTF(x) __attribute__((__format__(__printf__, (x), (x + 1))))72    #define VPRINTF(x) __attribute__((__format__(__printf__, (x), 0)))73#else74    #define PRINTF(x)75    #define VPRINTF(x)76#endif7778#if (GNUC_AT_LEAST(3, 0) || HAS_BUILTIN(__builtin_expect)) && defined(__OPTIMIZE__)79    #define likely(x) __builtin_expect(!!(x), 1)80    #define unlikely(x) __builtin_expect(!!(x), 0)81#else82    #define likely(x) (x)83    #define unlikely(x) (x)84#endif8586#if __STDC_VERSION__ >= 201112L87    #define noreturn _Noreturn88#elif GNUC_AT_LEAST(3, 0)89    #define noreturn __attribute__((__noreturn__))90#else91    #define noreturn92#endif9394#if GNUC_AT_LEAST(4, 3) || HAS_ATTRIBUTE(cold)95    #define COLD __attribute__((__cold__))96#else97    #define COLD98#endif99100#if GNUC_AT_LEAST(3, 3) || HAS_ATTRIBUTE(nonnull)101    #define NONNULL_ARGS __attribute__((__nonnull__))102    #define NONNULL_ARG(...) __attribute__((__nonnull__(__VA_ARGS__)))103#else104    #define NONNULL_ARGS105    #define NONNULL_ARG(...)106#endif107108#if GNUC_AT_LEAST(3, 4) || HAS_ATTRIBUTE(warn_unused_result)109    #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))110#else111    #define WARN_UNUSED_RESULT112#endif113114#if GNUC_AT_LEAST(4, 3) || HAS_ATTRIBUTE(hot)115    #define HOT __attribute__((__hot__))116#else117    #define HOT118#endif119120#if GNUC_AT_LEAST(4, 5) || HAS_BUILTIN(__builtin_unreachable)121    #define UNREACHABLE() __builtin_unreachable()122#else123    #define UNREACHABLE()124#endif125126#if GNUC_AT_LEAST(5, 0) || HAS_ATTRIBUTE(returns_nonnull)127    #define RETURNS_NONNULL __attribute__((__returns_nonnull__))128#else129    #define RETURNS_NONNULL130#endif131132#define XMALLOC MALLOC RETURNS_NONNULL133#define XSTRDUP XMALLOC NONNULL_ARGS134135#ifdef __clang__136    #define IGNORE_WARNING(wflag) \137        DO_PRAGMA(clang diagnostic push) \138        DO_PRAGMA(clang diagnostic ignored "-Wunknown-pragmas") \139        DO_PRAGMA(clang diagnostic ignored "-Wunknown-warning-option") \140        DO_PRAGMA(clang diagnostic ignored wflag)141    #define UNIGNORE_WARNINGS DO_PRAGMA(clang diagnostic pop)142#elif GNUC_AT_LEAST(4, 6)143    #define IGNORE_WARNING(wflag) \144        DO_PRAGMA(GCC diagnostic push) \145        DO_PRAGMA(GCC diagnostic ignored "-Wpragmas") \146        DO_PRAGMA(GCC diagnostic ignored wflag)147    #define UNIGNORE_WARNINGS DO_PRAGMA(GCC diagnostic pop)148#else149    #define IGNORE_WARNING(wflag)150    #define UNIGNORE_WARNINGS151#endif