Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:09:07

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_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 
0042 #if !defined(Q_ASSERT_X)
0043 #  if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0044 #    define Q_ASSERT_X(cond, where, what) static_cast<void>(false && (cond))
0045 #  else
0046 #    define Q_ASSERT_X(cond, where, what) ((cond) ? static_cast<void>(0) : qt_assert_x(where, what, __FILE__, __LINE__))
0047 #  endif
0048 #endif
0049 
0050 #ifndef Q_PRE
0051 # define Q_PRE(cond) \
0052     Q_ASSERT(cond) /* for now... */
0053 #endif
0054 
0055 #ifndef Q_PRE_X
0056 # define Q_PRE_X(cond, what) \
0057     Q_ASSERT_X(cond, Q_FUNC_INFO, what) /* for now... */
0058 #endif
0059 
0060 Q_NORETURN Q_CORE_EXPORT void qt_check_pointer(const char *, int) noexcept;
0061 Q_NORETURN Q_DECL_COLD_FUNCTION
0062 Q_CORE_EXPORT void qBadAlloc();
0063 
0064 #ifdef QT_NO_EXCEPTIONS
0065 #  if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
0066 #    define Q_CHECK_PTR(p) qt_noop()
0067 #  else
0068 #    define Q_CHECK_PTR(p) do {if (!(p)) qt_check_pointer(__FILE__,__LINE__);} while (false)
0069 #  endif
0070 #else
0071 #  define Q_CHECK_PTR(p) do { if (!(p)) qBadAlloc(); } while (false)
0072 #endif
0073 
0074 template <typename T>
0075 inline T *q_check_ptr(T *p) { Q_CHECK_PTR(p); return p; }
0076 
0077 // Q_UNREACHABLE_IMPL() and Q_ASSUME_IMPL() used below are defined in qcompilerdetection.h
0078 #define Q_UNREACHABLE() \
0079     do {\
0080         Q_ASSERT_X(false, "Q_UNREACHABLE()", "Q_UNREACHABLE was reached");\
0081         Q_UNREACHABLE_IMPL();\
0082     } while (false)
0083 
0084 #ifndef Q_UNREACHABLE_RETURN
0085 #  ifdef Q_COMPILER_COMPLAINS_ABOUT_RETURN_AFTER_UNREACHABLE
0086 #    define Q_UNREACHABLE_RETURN(...) Q_UNREACHABLE()
0087 #  else
0088 #    define Q_UNREACHABLE_RETURN(...) do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)
0089 #  endif
0090 #endif
0091 
0092 Q_DECL_DEPRECATED_X("Q_ASSUME() is deprecated because it can produce worse code than when it's absent; "
0093                     "use C++23 [[assume]] instead")
0094 inline bool qt_assume_is_deprecated(bool cond) noexcept { return cond; }
0095 #define Q_ASSUME(Expr) \
0096     [] (bool valueOfExpression) {\
0097         Q_ASSERT_X(valueOfExpression, "Q_ASSUME()", "Assumption in Q_ASSUME(\"" #Expr "\") was not correct");\
0098         Q_ASSUME_IMPL(valueOfExpression);\
0099     }(qt_assume_is_deprecated(Expr))
0100 
0101 // Don't use these in C++ mode, use static_assert directly.
0102 // These are here only to keep old code compiling.
0103 #  define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition)
0104 #  define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
0105 
0106 #elif defined(Q_COMPILER_STATIC_ASSERT)
0107 // C11 mode - using the _S version in case <assert.h> doesn't do the right thing
0108 #  define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition)
0109 #  define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message)
0110 #else
0111 // C89 & C99 version
0112 #  define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
0113 #  define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
0114 #  ifdef __COUNTER__
0115 #  define Q_STATIC_ASSERT(Condition) \
0116     typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1];
0117 #  else
0118 #  define Q_STATIC_ASSERT(Condition) \
0119     typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1];
0120 #  endif /* __COUNTER__ */
0121 #  define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
0122 #endif // __cplusplus
0123 
0124 QT_END_NAMESPACE
0125 
0126 #endif // QASSERT_H