Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qassert.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QASSERT_H
5#define QASSERT_H
6
7#include <QtCore/qcompilerdetection.h>
8#include <QtCore/qtconfigmacros.h>
9#include <QtCore/qtcoreexports.h>
10#include <QtCore/qtnoop.h>
11
12#if 0
13#pragma qt_class(QtAssert)
14#pragma qt_sync_stop_processing
15#endif
16
17QT_BEGIN_NAMESPACE
18
19#if defined(__cplusplus)
20
21#if !defined(Q_CC_MSVC_ONLY)
22Q_NORETURN
23#endif
24Q_DECL_COLD_FUNCTION
25Q_CORE_EXPORT void qt_assert(const char *assertion, const char *file, int line) noexcept;
26
27#if !defined(Q_ASSERT)
28# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
29# define Q_ASSERT(cond) static_cast<void>(false && (cond))
30# else
31# define Q_ASSERT(cond) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert)(#cond, __FILE__, __LINE__))
32# endif
33#endif
34
35#if !defined(Q_CC_MSVC_ONLY)
36Q_NORETURN
37#endif
38Q_DECL_COLD_FUNCTION
39Q_CORE_EXPORT
40void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept;
41inline bool qt_no_assert_x(bool, const char *, const char *) noexcept { return false; }
42
43#if !defined(Q_ASSERT_X)
44# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
45# define Q_ASSERT_X(cond, where, what)
46 static_cast<void>(false && QT_PREPEND_NAMESPACE(qt_no_assert_x)(bool(cond), where, what))
47# else
48# define Q_ASSERT_X(cond, where, what) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert_x)(where, what, __FILE__, __LINE__))
49# endif
50#endif
51
52#ifndef Q_PRE
53# define Q_PRE(cond)
54 Q_ASSERT(cond) /* for now... */
55#endif
56
57#ifndef Q_PRE_X
58# define Q_PRE_X(cond, what)
59 Q_ASSERT_X(cond, Q_FUNC_INFO, what) /* for now... */
60#endif
61
62Q_NORETURN Q_CORE_EXPORT void qt_check_pointer(const char *, int) noexcept;
63Q_NORETURN Q_DECL_COLD_FUNCTION
64Q_CORE_EXPORT void qBadAlloc();
65
66#ifdef QT_NO_EXCEPTIONS
67# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
68# define Q_CHECK_PTR(p) qt_noop()
69# else
70# define Q_CHECK_PTR(p) do {if (!(p)) QT_PREPEND_NAMESPACE(qt_check_pointer)(__FILE__,__LINE__);} while (false)
71# endif
72#else
73# define Q_CHECK_PTR(p) do { if (!(p)) QT_PREPEND_NAMESPACE(qBadAlloc)(); } while (false)
74#endif
75
76template <typename T>
77inline T *q_check_ptr(T *p) { Q_CHECK_PTR(p); return p; }
78
79// Q_UNREACHABLE_IMPL() and Q_ASSUME_IMPL() used below are defined in qcompilerdetection.h
80#define Q_UNREACHABLE()
81 do {
82 Q_ASSERT_X(false, "Q_UNREACHABLE()", "Q_UNREACHABLE was reached");
83 Q_UNREACHABLE_IMPL();
84 } while (false)
85
86#ifndef Q_UNREACHABLE_RETURN
87# ifdef Q_COMPILER_COMPLAINS_ABOUT_RETURN_AFTER_UNREACHABLE
88# define Q_UNREACHABLE_RETURN(...) Q_UNREACHABLE()
89# else
90# define Q_UNREACHABLE_RETURN(...) do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)
91# endif
92#endif
93
94Q_DECL_DEPRECATED_X("Q_ASSUME() is deprecated because it can produce worse code than when it's absent; "
95 "use C++23 [[assume]] instead")
96inline bool qt_assume_is_deprecated(bool cond) noexcept { return cond; }
97#define Q_ASSUME(Expr)
98 [] (bool valueOfExpression) {
99 Q_ASSERT_X(valueOfExpression, "Q_ASSUME()", "Assumption in Q_ASSUME(\"" #Expr "\") was not correct");
100 Q_ASSUME_IMPL(valueOfExpression);
101 }(qt_assume_is_deprecated(Expr))
102
103// Don't use these in C++ mode, use static_assert directly.
104// These are here only to keep old code compiling.
105# define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition)
106# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
107
108#elif defined(Q_COMPILER_STATIC_ASSERT)
109// C11 mode - using the _S version in case <assert.h> doesn't do the right thing
110# define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition)
111# define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message)
112#else
113// C89 & C99 version
114# define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
115# define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
116# ifdef __COUNTER__
117# define Q_STATIC_ASSERT(Condition)
118 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1];
119# else
120# define Q_STATIC_ASSERT(Condition)
121 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1];
122# endif /* __COUNTER__ */
123# define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
124#endif // __cplusplus
125
126QT_END_NAMESPACE
127
128#endif // QASSERT_H
void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept
Definition qassert.cpp:123
QT_BEGIN_NAMESPACE Q_NORETURN void qAbort()
Definition qassert.cpp:24
void qt_check_pointer(const char *n, int l) noexcept
Definition qassert.cpp:165
void qBadAlloc()
Definition qassert.cpp:181
#define Q_ASSERT(cond)
Definition qrandom.cpp:48
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:49