macros_defs.h (5620B)
1 #pragma once 2 3 #ifndef NVIM_NLUA0 4 # include "auto/config.h" 5 #endif 6 7 // EXTERN is only defined in main.c. That's where global variables are 8 // actually defined and initialized. 9 #ifndef EXTERN 10 # define EXTERN extern 11 # define INIT(...) 12 #else 13 # ifndef INIT 14 # define INIT(...) __VA_ARGS__ 15 # endif 16 #endif 17 18 #ifndef MIN 19 # define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 20 #endif 21 #ifndef MAX 22 # define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 23 #endif 24 25 /// String with length 26 /// 27 /// For use in functions which accept (char *s, size_t len) pair in arguments. 28 /// 29 /// @param[in] s Static string. 30 /// 31 /// @return `s, sizeof(s) - 1` 32 #define S_LEN(s) (s), (sizeof(s) - 1) 33 34 // toupper() and tolower() that use the current locale. 35 // Careful: Only call TOUPPER_LOC() and TOLOWER_LOC() with a character in the 36 // range 0 - 255. toupper()/tolower() on some systems can't handle others. 37 // Note: It is often better to use mb_tolower() and mb_toupper(), because many 38 // toupper() and tolower() implementations only work for ASCII. 39 #define TOUPPER_LOC toupper 40 #define TOLOWER_LOC tolower 41 42 // toupper() and tolower() for ASCII only and ignore the current locale. 43 #define TOUPPER_ASC(c) (((c) < 'a' || (c) > 'z') ? (c) : (c) - ('a' - 'A')) 44 #define TOLOWER_ASC(c) (((c) < 'A' || (c) > 'Z') ? (c) : (c) + ('a' - 'A')) 45 46 // Like isalpha() but reject non-ASCII characters. Can't be used with a 47 // special key (negative value). 48 #define ASCII_ISLOWER(c) ((unsigned)(c) >= 'a' && (unsigned)(c) <= 'z') 49 #define ASCII_ISUPPER(c) ((unsigned)(c) >= 'A' && (unsigned)(c) <= 'Z') 50 #define ASCII_ISALPHA(c) (ASCII_ISUPPER(c) || ASCII_ISLOWER(c)) 51 #define ASCII_ISALNUM(c) (ASCII_ISALPHA(c) || ascii_isdigit(c)) 52 53 // Returns empty string if it is NULL. 54 #define EMPTY_IF_NULL(x) ((x) ? (x) : "") 55 56 #define WRITEBIN "wb" // no CR-LF translation 57 #define READBIN "rb" 58 #define APPENDBIN "ab" 59 60 #define REPLACE_NORMAL(s) (((s)& REPLACE_FLAG) && !((s)& VREPLACE_FLAG)) 61 62 #define RESET_BINDING(wp) \ 63 do { \ 64 (wp)->w_p_scb = false; \ 65 (wp)->w_p_crb = false; \ 66 } while (0) 67 68 /// Calculate the length of a C array 69 /// 70 /// This should be called with a real array. Calling this with a pointer is an 71 /// error. A mechanism to detect many (though not all) of those errors at 72 /// compile time is implemented. It works by the second division producing 73 /// a division by zero in those cases (-Wdiv-by-zero in GCC). 74 #define ARRAY_SIZE(arr) \ 75 ((sizeof(arr)/sizeof((arr)[0])) \ 76 / ((size_t)(!(sizeof(arr) % sizeof((arr)[0]))))) 77 78 /// Get last array entry 79 /// 80 /// This should be called with a real array. Calling this with a pointer is an 81 /// error. 82 #define ARRAY_LAST_ENTRY(arr) (arr)[ARRAY_SIZE(arr) - 1] 83 84 // Duplicated in os/win_defs.h to avoid include-order sensitivity. 85 #define RGB_(r, g, b) (((r) << 16) | ((g) << 8) | (b)) 86 87 #define STR_(x) #x 88 #define STR(x) STR_(x) 89 90 #ifndef __has_include 91 # define NVIM_HAS_INCLUDE(x) 0 92 #else 93 # define NVIM_HAS_INCLUDE __has_include 94 #endif 95 96 #ifndef __has_attribute 97 # define NVIM_HAS_ATTRIBUTE(x) 0 98 #elif defined(__clang__) && __clang__ == 1 \ 99 && (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ <= 5)) 100 // Starting in Clang 3.6, __has_attribute was fixed to only report true for 101 // GNU-style attributes. Prior to that, it reported true if _any_ backend 102 // supported the attribute. 103 # define NVIM_HAS_ATTRIBUTE(x) 0 104 #else 105 # define NVIM_HAS_ATTRIBUTE __has_attribute 106 #endif 107 108 #if NVIM_HAS_ATTRIBUTE(fallthrough) \ 109 && (!defined(__apple_build_version__) || __apple_build_version__ >= 7000000) 110 # define FALLTHROUGH {} __attribute__((fallthrough)) 111 #else 112 # define FALLTHROUGH 113 #endif 114 115 #if defined(__clang__) || defined(__GNUC__) 116 # define EXPECT(cond, value) __builtin_expect((cond), (value)) 117 # define UNREACHABLE __builtin_unreachable() 118 #elif defined(_MSC_VER) 119 # define EXPECT(cond, value) (cond) 120 # define UNREACHABLE __assume(false) 121 #else 122 # define EXPECT(cond, value) (cond) 123 # define UNREACHABLE 124 #endif 125 126 // Type of uv_buf_t.len is platform-dependent. 127 // Related: https://github.com/libuv/libuv/pull/1236 128 #if defined(MSWIN) 129 # define UV_BUF_LEN(x) (ULONG)(x) 130 #else 131 # define UV_BUF_LEN(x) (x) 132 #endif 133 134 // Type of read()/write() `count` param is platform-dependent. 135 #if defined(MSWIN) 136 # define IO_COUNT(x) (unsigned)(x) 137 #else 138 # define IO_COUNT(x) (x) 139 #endif 140 141 /// 142 /// PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES 143 /// 144 #if defined(__clang__) && __clang__ == 1 145 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \ 146 _Pragma("clang diagnostic push") \ 147 _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"") 148 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG 149 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \ 150 _Pragma("clang diagnostic push") \ 151 _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"") 152 # else 153 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \ 154 _Pragma("clang diagnostic push") 155 # endif 156 # define PRAGMA_DIAG_POP \ 157 _Pragma("clang diagnostic pop") 158 #elif defined(__GNUC__) 159 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES \ 160 _Pragma("GCC diagnostic push") \ 161 _Pragma("GCC diagnostic ignored \"-Wmissing-prototypes\"") 162 # ifdef HAVE_WIMPLICIT_FALLTHROUGH_FLAG 163 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \ 164 _Pragma("GCC diagnostic push") \ 165 _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"") 166 # else 167 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH \ 168 _Pragma("GCC diagnostic push") 169 # endif 170 # define PRAGMA_DIAG_POP \ 171 _Pragma("GCC diagnostic pop") 172 #else 173 # define PRAGMA_DIAG_PUSH_IGNORE_MISSING_PROTOTYPES 174 # define PRAGMA_DIAG_PUSH_IGNORE_IMPLICIT_FALLTHROUGH 175 # define PRAGMA_DIAG_POP 176 #endif 177 178 #define EMPTY_POS(a) ((a).lnum == 0 && (a).col == 0 && (a).coladd == 0)