chromium/third_party/skia/include/private/base/SkAssert.h

/*
 * Copyright 2022 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkAssert_DEFINED
#define SkAssert_DEFINED

#include "include/private/base/SkAPI.h"
#include "include/private/base/SkAttributes.h"
#include "include/private/base/SkDebug.h" // IWYU pragma: keep

#include <cstddef>
#include <limits>

#if defined(__clang__) && defined(__has_attribute)
    #if __has_attribute(likely)
        #define SK_LIKELY
        #define SK_UNLIKELY
    #else
        #define SK_LIKELY
        #define SK_UNLIKELY
    #endif
#else
    #define SK_LIKELY
    #define SK_UNLIKELY
#endif

// c++23 will give us [[assume]] -- until then we're stuck with various other options:
#if defined(__clang__)
    #define SK_ASSUME(cond)
#elif defined(__GNUC__)
    #if __GNUC__ >= 13
        #define SK_ASSUME
    #else
        // NOTE: This implementation could actually evaluate `cond`, which is not desirable.
        #define SK_ASSUME
    #endif
#elif defined(_MSC_VER)
    #define SK_ASSUME
#else
    #define SK_ASSUME
#endif

/** Called internally if we hit an unrecoverable error.
    The platform implementation must not return, but should either throw
    an exception or otherwise exit.
*/
[[noreturn]] SK_API extern void sk_abort_no_print(void);

#if defined(SK_BUILD_FOR_GOOGLE3)
    void SkDebugfForDumpStackTrace(const char* data, void* unused);
    namespace base {
        void DumpStackTrace(int skip_count, void w(const char*, void*), void* arg);
    }
#define SK_DUMP_GOOGLE3_STACK
#else
#define SK_DUMP_GOOGLE3_STACK()
#endif

#if !defined(SK_ABORT)
#  if defined(SK_BUILD_FOR_WIN)
     // This style lets Visual Studio follow errors back to the source file.
#define SK_DUMP_LINE_FORMAT
#  else
#define SK_DUMP_LINE_FORMAT
#  endif
#define SK_ABORT
#endif

// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as standalone assertion expressions, e.g.
//    uint32_t foo(int x) {
//        SkASSERT(x > 4);
//        return x - 4;
//    }
// and are also written to be compatible with constexpr functions:
//    constexpr uint32_t foo(int x) {
//        return SkASSERT(x > 4),
//               x - 4;
//    }
#if defined(__clang__)
#define SkASSERT_RELEASE(cond)

#define SkASSERTF_RELEASE(cond, fmt, ...)
#else
#define SkASSERT_RELEASE

#define SkASSERTF_RELEASE
#endif

#if defined(SK_DEBUG)
    #define SkASSERT(cond)
    #define SkASSERTF(cond, fmt, ...)
    #define SkDEBUGFAIL(message)
    #define SkDEBUGFAILF(fmt, ...)
    #define SkAssertResult(cond)
#else
    #define SkASSERT
    #define SkASSERTF
    #define SkDEBUGFAIL
    #define SkDEBUGFAILF

    // unlike SkASSERT, this macro executes its condition in the non-debug build.
    // The if is present so that this can be used with functions marked [[nodiscard]].
    #define SkAssertResult
#endif

#if !defined(SkUNREACHABLE)
#  if defined(_MSC_VER) && !defined(__clang__)
#    include <intrin.h>
#define FAST_FAIL_INVALID_ARG
// See https://developercommunity.visualstudio.com/content/problem/1128631/code-flow-doesnt-see-noreturn-with-extern-c.html
// for why this is wrapped. Hopefully removable after msvc++ 19.27 is no longer supported.
[[noreturn]] static inline void sk_fast_fail() { __fastfail(FAST_FAIL_INVALID_ARG); }
#define SkUNREACHABLE
#  else
#define SkUNREACHABLE
#  endif
#endif

[[noreturn]] SK_API inline void sk_print_index_out_of_bounds(size_t i, size_t size) {}

template <typename T> SK_API inline T sk_collection_check_bounds(T i, T size) {}

[[noreturn]] SK_API inline void sk_print_length_too_big(size_t i, size_t size) {}

template <typename T> SK_API inline T sk_collection_check_length(T i, T size) {}

SK_API inline void sk_collection_not_empty(bool empty) {}

[[noreturn]] SK_API inline void sk_print_size_too_big(size_t size, size_t maxSize) {}

template <typename T>
SK_ALWAYS_INLINE size_t check_size_bytes_too_big(size_t size) {}

#endif  // SkAssert_DEFINED