1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef LIBPANDABASE_MACROS_H 17 #define LIBPANDABASE_MACROS_H 18 19 #include <cassert> 20 #include <iostream> 21 #include "os/stacktrace.h" 22 #include "utils/debug.h" 23 24 // Inline (disabled for DEBUG) 25 #ifndef NDEBUG 26 #define ALWAYS_INLINE // NOLINT(cppcoreguidelines-macro-usage) 27 #else // NDEBUG 28 #define ALWAYS_INLINE __attribute__((always_inline)) // NOLINT(cppcoreguidelines-macro-usage) 29 #endif // !NDEBUG 30 31 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 32 #define NO_INLINE __attribute__((noinline)) 33 34 #ifdef __clang__ 35 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 36 #define NO_OPTIMIZE [[clang::optnone]] 37 #else 38 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 39 #define NO_OPTIMIZE __attribute__((optimize("O0"))) 40 #endif 41 42 #ifdef __clang__ 43 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 44 #define FIELD_UNUSED __attribute__((__unused__)) 45 #else 46 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 47 #define FIELD_UNUSED 48 #endif 49 50 #ifndef PANDA_TARGET_WINDOWS 51 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 52 #define PANDA_PUBLIC_API __attribute__((visibility ("default"))) 53 #else 54 #define PANDA_PUBLIC_API __declspec(dllexport) 55 #endif 56 57 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 58 #define MEMBER_OFFSET(T, F) offsetof(T, F) 59 60 #if defined(__cplusplus) 61 62 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 63 #define NO_COPY_CTOR(TypeName) \ 64 TypeName(const TypeName&) = delete; 65 66 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 67 #define NO_COPY_OPERATOR(TypeName) \ 68 void operator=(const TypeName&) = delete 69 70 // Disabling copy ctor and copy assignment operator. 71 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 72 #define NO_COPY_SEMANTIC(TypeName) \ 73 NO_COPY_CTOR(TypeName) \ 74 NO_COPY_OPERATOR(TypeName) 75 76 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 77 #define NO_MOVE_CTOR(TypeName) \ 78 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 79 TypeName(TypeName&&) = delete; 80 81 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 82 #define NO_MOVE_OPERATOR(TypeName) \ 83 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 84 TypeName& operator=(TypeName&&) = delete 85 86 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 87 #define NO_MOVE_SEMANTIC(TypeName) \ 88 NO_MOVE_CTOR(TypeName) \ 89 NO_MOVE_OPERATOR(TypeName) 90 91 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 92 #define DEFAULT_MOVE_CTOR(TypeName) \ 93 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 94 TypeName(TypeName&&) = default; 95 96 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 97 #define DEFAULT_MOVE_OPERATOR(TypeName) \ 98 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 99 TypeName& operator=(TypeName&&) = default 100 101 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 102 #define DEFAULT_MOVE_SEMANTIC(TypeName) \ 103 DEFAULT_MOVE_CTOR(TypeName) \ 104 DEFAULT_MOVE_OPERATOR(TypeName) 105 106 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 107 #define DEFAULT_COPY_CTOR(TypeName) \ 108 TypeName(const TypeName&) = default; \ 109 110 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 111 #define DEFAULT_COPY_OPERATOR(TypeName) \ 112 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 113 TypeName& operator=(const TypeName&) = default 114 115 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 116 #define DEFAULT_COPY_SEMANTIC(TypeName) \ 117 DEFAULT_COPY_CTOR(TypeName) \ 118 DEFAULT_COPY_OPERATOR(TypeName) 119 120 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 121 #define DEFAULT_NOEXCEPT_MOVE_CTOR(TypeName) \ 122 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 123 TypeName(TypeName&&) noexcept = default; 124 125 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 126 #define DEFAULT_NOEXCEPT_MOVE_OPERATOR(TypeName) \ 127 /* NOLINTNEXTLINE(misc-macro-parentheses) */ \ 128 TypeName& operator=(TypeName&&) noexcept = default 129 130 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 131 #define DEFAULT_NOEXCEPT_MOVE_SEMANTIC(TypeName) \ 132 DEFAULT_NOEXCEPT_MOVE_CTOR(TypeName) \ 133 DEFAULT_NOEXCEPT_MOVE_OPERATOR(TypeName) 134 135 #endif // defined(__cplusplus) 136 137 #define LIKELY(exp) (__builtin_expect((exp) != 0, true)) // NOLINT(cppcoreguidelines-macro-usage) 138 #define UNLIKELY(exp) (__builtin_expect((exp) != 0, false)) // NOLINT(cppcoreguidelines-macro-usage) 139 140 // NOLINT(cppcoreguidelines-macro-usage) 141 #define ABORT_AND_UNREACHABLE() \ 142 do { \ 143 std::cerr << "This line should be unreachable" << std::endl; \ 144 std::abort(); \ 145 __builtin_unreachable(); \ 146 } while (0) 147 148 #if !defined(NDEBUG) 149 150 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 151 #define ASSERT_FAIL(expr) panda::debug::AssertionFail(expr, __FILE__, __LINE__, __FUNCTION__) 152 153 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 154 #define ASSERT_OP(lhs, op, rhs) do { \ 155 auto __lhs = lhs; \ 156 auto __rhs = rhs; \ 157 if (UNLIKELY(!(__lhs op __rhs))) { \ 158 std::cerr << "CHECK FAILED: " << #lhs << " " #op " " #rhs << std::endl; \ 159 std::cerr << " VALUES: " << __lhs << " " #op " " << __rhs << std::endl; \ 160 std::cerr << " IN: " << __FILE__ << ":" << __LINE__ << ": " << __FUNCTION__ << std::endl; \ 161 panda::PrintStack(std::cerr); \ 162 std::abort(); \ 163 } \ 164 } while (0) 165 166 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 167 #define CHECK_LE(lhs, rhs) ASSERT_OP(lhs, <=, rhs) 168 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 169 #define CHECK_LT(lhs, rhs) ASSERT_OP(lhs, <, rhs) 170 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 171 #define CHECK_GE(lhs, rhs) ASSERT_OP(lhs, >=, rhs) 172 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 173 #define CHECK_GT(lhs, rhs) ASSERT_OP(lhs, >, rhs) 174 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 175 #define CHECK_EQ(lhs, rhs) ASSERT_OP(lhs, ==, rhs) 176 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 177 #define CHECK_NE(lhs, rhs) ASSERT_OP(lhs, !=, rhs) 178 179 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 180 #define ASSERT(cond) \ 181 if (UNLIKELY(!(cond))) { \ 182 ASSERT_FAIL(#cond); \ 183 } 184 185 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 186 #define ASSERT_DO(cond, func) \ 187 do { \ 188 if (auto cond_val = cond; UNLIKELY(!(cond_val))) { \ 189 func; \ 190 ASSERT_FAIL(#cond); \ 191 } \ 192 } while (0) 193 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 194 #define ASSERT_PRINT(cond, message) \ 195 do { \ 196 if (auto cond_val = cond; UNLIKELY(!(cond_val))) { \ 197 std::cerr << message << std::endl; \ 198 ASSERT_FAIL(#cond); \ 199 } \ 200 } while (0) 201 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 202 #define ASSERT_RETURN(cond) assert(cond) 203 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 204 #define UNREACHABLE() \ 205 do { \ 206 ASSERT_PRINT(false, "This line should be unreachable"); /* NOLINT(misc-static-assert) */ \ 207 __builtin_unreachable(); \ 208 } while (0) 209 #else // NDEBUG 210 #define ASSERT(cond) static_cast<void>(0) // NOLINT(cppcoreguidelines-macro-usage) 211 #define ASSERT_DO(cond, func) static_cast<void>(0) // NOLINT(cppcoreguidelines-macro-usage) 212 #define ASSERT_PRINT(cond, message) static_cast<void>(0) // NOLINT(cppcoreguidelines-macro-usage) 213 #define ASSERT_RETURN(cond) static_cast<void>(cond) // NOLINT(cppcoreguidelines-macro-usage) 214 #define UNREACHABLE() ABORT_AND_UNREACHABLE() // NOLINT(cppcoreguidelines-macro-usage) 215 #define ASSERT_OP(lhs, op, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 216 #define CHECK_LE(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 217 #define CHECK_LT(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 218 #define CHECK_GE(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 219 #define CHECK_GT(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 220 #define CHECK_EQ(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 221 #define CHECK_NE(lhs, rhs) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 222 #endif // !NDEBUG 223 224 // Due to the impossibility of using asserts in constexpr methods 225 // we need an extra version of UNREACHABLE macro that can be used in such situations 226 #define UNREACHABLE_CONSTEXPR() ABORT_AND_UNREACHABLE() // NOLINT(cppcoreguidelines-macro-usage) 227 228 #define MERGE_WORDS_X(A, B) A ## B // NOLINT(cppcoreguidelines-macro-usage) 229 #define MERGE_WORDS(A, B) MERGE_WORDS_X(A, B) // NOLINT(cppcoreguidelines-macro-usage) 230 231 #if defined(__has_feature) 232 # if __has_feature(thread_sanitizer) 233 #define NO_THREAD_SANITIZE __attribute__((no_sanitize("thread"))) // NOLINT(cppcoreguidelines-macro-usage) 234 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 235 #define TSAN_ANNOTATE_HAPPENS_BEFORE(addr) \ 236 AnnotateHappensBefore(__FILE__, __LINE__, reinterpret_cast<void *>(addr)) 237 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 238 #define TSAN_ANNOTATE_HAPPENS_AFTER(addr) \ 239 AnnotateHappensAfter(__FILE__, __LINE__, reinterpret_cast<void *>(addr)) 240 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 241 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 242 #define TSAN_ANNOTATE_IGNORE_WRITES_BEGIN() \ 243 AnnotateIgnoreWritesBegin(__FILE__, __LINE__) 244 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 245 #define TSAN_ANNOTATE_IGNORE_WRITES_END() \ 246 AnnotateIgnoreWritesEnd(__FILE__, __LINE__) 247 extern "C" void AnnotateHappensBefore(const char* f, int l, void* addr); 248 extern "C" void AnnotateHappensAfter(const char* f, int l, void* addr); 249 extern "C" void AnnotateIgnoreWritesBegin(const char* f, int l); 250 extern "C" void AnnotateIgnoreWritesEnd(const char* f, int l); 251 # else 252 #define NO_THREAD_SANITIZE 253 #define TSAN_ANNOTATE_HAPPENS_BEFORE(addr) 254 #define TSAN_ANNOTATE_HAPPENS_AFTER(addr) 255 #define TSAN_ANNOTATE_IGNORE_WRITES_BEGIN() 256 #define TSAN_ANNOTATE_IGNORE_WRITES_END() 257 # endif 258 #else 259 # ifdef __SANITIZE_THREAD__ 260 #define NO_THREAD_SANITIZE __attribute__((no_sanitize("thread"))) // NOLINT(cppcoreguidelines-macro-usage) 261 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 262 #define TSAN_ANNOTATE_HAPPENS_BEFORE(addr) \ 263 AnnotateHappensBefore(__FILE__, __LINE__, reinterpret_cast<void *>(addr)) 264 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 265 #define TSAN_ANNOTATE_HAPPENS_AFTER(addr) \ 266 AnnotateHappensAfter(__FILE__, __LINE__, reinterpret_cast<void *>(addr)) 267 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 268 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 269 #define TSAN_ANNOTATE_IGNORE_WRITES_BEGIN() \ 270 AnnotateIgnoreWritesBegin(__FILE__, __LINE__) 271 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 272 #define TSAN_ANNOTATE_IGNORE_WRITES_END() \ 273 AnnotateIgnoreWritesEnd(__FILE__, __LINE__) 274 extern "C" void AnnotateHappensBefore(const char* f, int l, void* addr); 275 extern "C" void AnnotateHappensAfter(const char* f, int l, void* addr); 276 extern "C" void AnnotateIgnoreWritesBegin(const char* f, int l); 277 extern "C" void AnnotateIgnoreWritesEnd(const char* f, int l); 278 # else 279 #define NO_THREAD_SANITIZE 280 #define TSAN_ANNOTATE_HAPPENS_BEFORE(addr) 281 #define TSAN_ANNOTATE_HAPPENS_AFTER(addr) 282 #define TSAN_ANNOTATE_IGNORE_WRITES_BEGIN() 283 #define TSAN_ANNOTATE_IGNORE_WRITES_END() 284 # endif 285 #endif 286 287 #if defined(__has_feature) 288 # if __has_feature(undefined_behavior_sanitizer) 289 #define NO_UB_SANITIZE __attribute__((no_sanitize("undefined"))) // NOLINT(cppcoreguidelines-macro-usage) 290 # else 291 #define NO_UB_SANITIZE 292 # endif 293 #else 294 # ifdef __SANITIZE_UNDEFINED__ 295 #define NO_UB_SANITIZE __attribute__((no_sanitize("undefined"))) // NOLINT(cppcoreguidelines-macro-usage) 296 # else 297 #define NO_UB_SANITIZE 298 # endif 299 #endif 300 301 #if defined(__has_feature) 302 # if __has_feature(address_sanitizer) 303 #define USE_ADDRESS_SANITIZER 304 # endif 305 #elif defined(__SANITIZE_ADDRESS__) 306 #define USE_ADDRESS_SANITIZER 307 #endif 308 309 #ifdef USE_ADDRESS_SANITIZER 310 #define NO_ADDRESS_SANITIZE __attribute__((no_sanitize("address"))) // NOLINT(cppcoreguidelines-macro-usage) 311 #else 312 #define NO_ADDRESS_SANITIZE 313 #endif // USE_ADDRESS_SANITIZER 314 315 #if defined(__has_include) 316 #if __has_include(<filesystem>) 317 #define USE_STD_FILESYSTEM 318 #endif 319 #endif 320 321 // WEAK_SYMBOLS_FOR_LTO is defined if compiling arkbase_lto and unset otherwise 322 #ifdef WEAK_SYMBOLS_FOR_LTO 323 #define WEAK_FOR_LTO_START _Pragma("clang attribute push(__attribute__((weak)), apply_to = function)") 324 #define WEAK_FOR_LTO_END _Pragma("clang attribute pop") 325 #else 326 #define WEAK_FOR_LTO_START 327 #define WEAK_FOR_LTO_END 328 #endif 329 330 #endif // LIBPANDABASE_MACROS_H 331