Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-09 08:53:40

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QASSERT_H
0005 #define QASSERT_H
0006 
0007 #include <QtCore/qcompilerdetection.h>
0008 #include <QtCore/qtconfigmacros.h>
0009 #include <QtCore/qtcoreexports.h>
0010 #include <QtCore/qtnoop.h>
0011 
0012 #if 0
0013 #pragma qt_class(QtAssert)
0014 #pragma qt_sync_stop_processing
0015 #endif
0016 
0017 QT_BEGIN_NAMESPACE
0018 
0019 #if defined(__cplusplus)
0020 
0021 #if !defined(Q_CC_MSVC_ONLY)
0022 Q_NORETURN
0023 #endif
0024 Q_DECL_COLD_FUNCTION
0025 Q_CORE_EXPORT void qt_assert(const char *assertion, const char *file, int line) noexcept;
0026 
0027 #if !defined(Q_ASSERT)
0028 #  if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0029 #    define Q_ASSERT(cond) static_cast<void>(false && (cond))
0030 #  else
0031 #    define Q_ASSERT(cond) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert)(#cond, __FILE__, __LINE__))
0032 #  endif
0033 #endif
0034 
0035 #if !defined(Q_CC_MSVC_ONLY)
0036 Q_NORETURN
0037 #endif
0038 Q_DECL_COLD_FUNCTION
0039 Q_CORE_EXPORT
0040 void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept;
0041 inline bool qt_no_assert_x(bool, const char *, const char *) noexcept { return false; }
0042 
0043 #if !defined(Q_ASSERT_X)
0044 #  if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0045 #    define Q_ASSERT_X(cond, where, what) \
0046         static_cast<void>(false && QT_PREPEND_NAMESPACE(qt_no_assert_x)(bool(cond), where, what))
0047 #  else
0048 #    define Q_ASSERT_X(cond, where, what) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert_x)(where, what, __FILE__, __LINE__))
0049 #  endif
0050 #endif
0051 
0052 #ifndef Q_PRE
0053 # define Q_PRE(cond) \
0054     Q_ASSERT(cond) /* for now... */
0055 #endif
0056 
0057 #ifndef Q_PRE_X
0058 # define Q_PRE_X(cond, what) \
0059     Q_ASSERT_X(cond, Q_FUNC_INFO, what) /* for now... */
0060 #endif
0061 
0062 Q_NORETURN Q_CORE_EXPORT void qt_check_pointer(const char *, int) noexcept;
0063 Q_NORETURN Q_DECL_COLD_FUNCTION
0064 Q_CORE_EXPORT void qBadAlloc();
0065 
0066 #ifdef QT_NO_EXCEPTIONS
0067 #  if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0068 #    define Q_CHECK_PTR(p) qt_noop()
0069 #  else
0070 #    define Q_CHECK_PTR(p) do {if (!(p)) QT_PREPEND_NAMESPACE(qt_check_pointer)(__FILE__,__LINE__);} while (false)
0071 #  endif
0072 #else
0073 #  define Q_CHECK_PTR(p) do { if (!(p)) QT_PREPEND_NAMESPACE(qBadAlloc)(); } while (false)
0074 #endif
0075 
0076 template <typename T>
0077 inline T *q_check_ptr(T *p) { Q_CHECK_PTR(p); return p; }
0078 
0079 // Q_UNREACHABLE_IMPL() and Q_ASSUME_IMPL() used below are defined in qcompilerdetection.h
0080 #define Q_UNREACHABLE() \
0081     do {\
0082         Q_ASSERT_X(false, "Q_UNREACHABLE()", "Q_UNREACHABLE was reached");\
0083         Q_UNREACHABLE_IMPL();\
0084     } while (false)
0085 
0086 #ifndef Q_UNREACHABLE_RETURN
0087 #  ifdef Q_COMPILER_COMPLAINS_ABOUT_RETURN_AFTER_UNREACHABLE
0088 #    define Q_UNREACHABLE_RETURN(...) Q_UNREACHABLE()
0089 #  else
0090 #    define Q_UNREACHABLE_RETURN(...) do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)
0091 #  endif
0092 #endif
0093 
0094 Q_DECL_DEPRECATED_X("Q_ASSUME() is deprecated because it can produce worse code than when it's absent; "
0095                     "use C++23 [[assume]] instead")
0096 inline bool qt_assume_is_deprecated(bool cond) noexcept { return cond; }
0097 #define Q_ASSUME(Expr) \
0098     [] (bool valueOfExpression) {\
0099         Q_ASSERT_X(valueOfExpression, "Q_ASSUME()", "Assumption in Q_ASSUME(\"" #Expr "\") was not correct");\
0100         Q_ASSUME_IMPL(valueOfExpression);\
0101     }(qt_assume_is_deprecated(Expr))
0102 
0103 
0104 #if __has_builtin(__builtin_assume)
0105 // Clang has this intrinsic and won't warn about its use in C++20 mode
0106 #  define Q_PRESUME_IMPL(assumption) __builtin_assume(assumption)
0107 #elif __has_cpp_attribute(assume)
0108 // GCC has implemented this attribute and allows its use in C++20 mode
0109 #  define Q_PRESUME_IMPL(assumption) [[assume(assumption)]]
0110 #elif defined(Q_CC_MSVC)
0111 #  define Q_PRESUME_IMPL(assumption) __assume(assumption)
0112 #else
0113 #  define Q_PRESUME_IMPL(assumption) (void)0
0114 #endif
0115 
0116 #define Q_PRESUME(assumption)       \
0117     [&] {                            \
0118         Q_ASSERT(assumption);       \
0119         Q_PRESUME_IMPL(assumption); \
0120     }()
0121 
0122 // Don't use these in C++ mode, use static_assert directly.
0123 // These are here only to keep old code compiling.
0124 #  define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition)
0125 #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
0126 
0127 #elif defined(Q_COMPILER_STATIC_ASSERT)
0128 // C11 mode - using the _S version in case <assert.h> doesn't do the right thing
0129 #  define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition)
0130 #  define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message)
0131 #else
0132 // C89 & C99 version
0133 #  define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
0134 #  define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
0135 #  ifdef __COUNTER__
0136 #  define Q_STATIC_ASSERT(Condition) \
0137     typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1];
0138 #  else
0139 #  define Q_STATIC_ASSERT(Condition) \
0140     typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1];
0141 #  endif /* __COUNTER__ */
0142 #  define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
0143 #endif // __cplusplus
0144 
0145 QT_END_NAMESPACE
0146 
0147 #endif // QASSERT_H