1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #ifndef SkMacros_DEFINED 8 #define SkMacros_DEFINED 9 10 #include "include/private/base/SkTo.h" // IWYU pragma: keep 11 12 /* 13 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab 14 * 15 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly 16 * 17 */ 18 #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) 19 #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y 20 21 /* 22 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current 23 * line number. Easy way to construct 24 * unique names for local functions or 25 * variables. 26 */ 27 #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) 28 29 #define SK_MACRO_APPEND_COUNTER(name) SK_MACRO_CONCAT(name, __COUNTER__) 30 31 //////////////////////////////////////////////////////////////////////////////// 32 33 // Can be used to bracket data types that must be dense/packed, e.g. hash keys. 34 #if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work! 35 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \ 36 _Pragma("GCC diagnostic error \"-Wpadded\"") 37 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop") 38 #else 39 #define SK_BEGIN_REQUIRE_DENSE 40 #define SK_END_REQUIRE_DENSE 41 #endif 42 43 #if defined(__clang__) && defined(__has_feature) 44 // Some compilers have a preprocessor that does not appear to do short-circuit 45 // evaluation as expected 46 #if __has_feature(leak_sanitizer) || __has_feature(address_sanitizer) 47 // Chrome had issues if we tried to include lsan_interface.h ourselves. 48 // https://github.com/llvm/llvm-project/blob/10a35632d55bb05004fe3d0c2d4432bb74897ee7/compiler-rt/include/sanitizer/lsan_interface.h#L26 49 extern "C" { 50 void __lsan_ignore_object(const void *p); 51 } 52 #define SK_INTENTIONALLY_LEAKED(X) __lsan_ignore_object(X) 53 #else 54 #define SK_INTENTIONALLY_LEAKED(X) ((void)0) 55 #endif 56 #else 57 #define SK_INTENTIONALLY_LEAKED(X) ((void)0) 58 #endif 59 60 #define SK_INIT_TO_AVOID_WARNING = 0 61 62 //////////////////////////////////////////////////////////////////////////////// 63 64 /** 65 * Defines overloaded bitwise operators to make it easier to use an enum as a 66 * bitfield. 67 */ 68 #define SK_MAKE_BITFIELD_OPS(X) \ 69 inline X operator ~(X a) { \ 70 using U = std::underlying_type_t<X>; \ 71 return (X) (~static_cast<U>(a)); \ 72 } \ 73 inline X operator |(X a, X b) { \ 74 using U = std::underlying_type_t<X>; \ 75 return (X) (static_cast<U>(a) | static_cast<U>(b)); \ 76 } \ 77 inline X& operator |=(X& a, X b) { \ 78 return (a = a | b); \ 79 } \ 80 inline X operator &(X a, X b) { \ 81 using U = std::underlying_type_t<X>; \ 82 return (X) (static_cast<U>(a) & static_cast<U>(b)); \ 83 } \ 84 inline X& operator &=(X& a, X b) { \ 85 return (a = a & b); \ 86 } 87 88 #define SK_DECL_BITFIELD_OPS_FRIENDS(X) \ 89 friend X operator ~(X a); \ 90 friend X operator |(X a, X b); \ 91 friend X& operator |=(X& a, X b); \ 92 \ 93 friend X operator &(X a, X b); \ 94 friend X& operator &=(X& a, X b); 95 96 /** 97 * Wraps a C++11 enum that we use as a bitfield, and enables a limited amount of 98 * masking with type safety. Instantiated with the ~ operator. 99 */ 100 template<typename TFlags> class SkTFlagsMask { 101 public: SkTFlagsMask(TFlags value)102 constexpr explicit SkTFlagsMask(TFlags value) : SkTFlagsMask(static_cast<int>(value)) {} SkTFlagsMask(int value)103 constexpr explicit SkTFlagsMask(int value) : fValue(value) {} value()104 constexpr int value() const { return fValue; } 105 private: 106 const int fValue; 107 }; 108 109 /** 110 * Defines bitwise operators that make it possible to use an enum class as a 111 * basic bitfield. 112 */ 113 #define SK_MAKE_BITFIELD_CLASS_OPS(X) \ 114 [[maybe_unused]] constexpr SkTFlagsMask<X> operator~(X a) { \ 115 return SkTFlagsMask<X>(~static_cast<int>(a)); \ 116 } \ 117 [[maybe_unused]] constexpr X operator|(X a, X b) { \ 118 return static_cast<X>(static_cast<int>(a) | static_cast<int>(b)); \ 119 } \ 120 [[maybe_unused]] inline X& operator|=(X& a, X b) { \ 121 return (a = a | b); \ 122 } \ 123 [[maybe_unused]] constexpr bool operator&(X a, X b) { \ 124 return SkToBool(static_cast<int>(a) & static_cast<int>(b)); \ 125 } \ 126 [[maybe_unused]] constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X> a, SkTFlagsMask<X> b) { \ 127 return SkTFlagsMask<X>(a.value() | b.value()); \ 128 } \ 129 [[maybe_unused]] constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X> a, X b) { \ 130 return SkTFlagsMask<X>(a.value() | static_cast<int>(b)); \ 131 } \ 132 [[maybe_unused]] constexpr SkTFlagsMask<X> operator|(X a, SkTFlagsMask<X> b) { \ 133 return SkTFlagsMask<X>(static_cast<int>(a) | b.value()); \ 134 } \ 135 [[maybe_unused]] constexpr X operator&(SkTFlagsMask<X> a, SkTFlagsMask<X> b) { \ 136 return static_cast<X>(a.value() & b.value()); \ 137 } \ 138 [[maybe_unused]] constexpr X operator&(SkTFlagsMask<X> a, X b) { \ 139 return static_cast<X>(a.value() & static_cast<int>(b)); \ 140 } \ 141 [[maybe_unused]] constexpr X operator&(X a, SkTFlagsMask<X> b) { \ 142 return static_cast<X>(static_cast<int>(a) & b.value()); \ 143 } \ 144 [[maybe_unused]] inline X& operator&=(X& a, SkTFlagsMask<X> b) { \ 145 return (a = a & b); \ 146 } \ 147 148 #define SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(X) \ 149 friend constexpr SkTFlagsMask<X> operator ~(X); \ 150 friend constexpr X operator |(X, X); \ 151 friend X& operator |=(X&, X); \ 152 friend constexpr bool operator &(X, X); \ 153 friend constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X>, SkTFlagsMask<X>); \ 154 friend constexpr SkTFlagsMask<X> operator|(SkTFlagsMask<X>, X); \ 155 friend constexpr SkTFlagsMask<X> operator|(X, SkTFlagsMask<X>); \ 156 friend constexpr X operator&(SkTFlagsMask<X>, SkTFlagsMask<X>); \ 157 friend constexpr X operator&(SkTFlagsMask<X>, X); \ 158 friend constexpr X operator&(X, SkTFlagsMask<X>); \ 159 friend X& operator &=(X&, SkTFlagsMask<X>) 160 161 #endif // SkMacros_DEFINED 162