Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-07 09:21:43

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