Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:07:16

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