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// Qt-Security score:significant reason:default
4
5#ifndef QASSERT_H
6#define QASSERT_H
7
8#include <QtCore/qcompilerdetection.h>
9#include <QtCore/qtconfigmacros.h>
10#include <QtCore/qtcoreexports.h>
11#include <QtCore/qtnoop.h>
12
13#if 0
14#pragma qt_class(QtAssert)
15#pragma qt_sync_stop_processing
16#endif
17
18QT_BEGIN_NAMESPACE
19
20#if defined(__cplusplus)
21
22#if !defined(Q_CC_MSVC_ONLY)
23Q_NORETURN
24#endif
25Q_DECL_COLD_FUNCTION
26Q_CORE_EXPORT void qt_assert(const char *assertion, const char *file, int line) noexcept;
27
28#if !defined(Q_ASSERT)
29# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
30# define Q_ASSERT(cond) static_cast<void>(false && (cond))
31# else
32# define Q_ASSERT(cond) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert)(#cond, __FILE__, __LINE__))
33# endif
34#endif
35
36#if !defined(Q_CC_MSVC_ONLY)
37Q_NORETURN
38#endif
39Q_DECL_COLD_FUNCTION
40Q_CORE_EXPORT
41void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept;
42inline bool qt_no_assert_x(bool, const char *, const char *) noexcept { return false; }
43
44#if !defined(Q_ASSERT_X)
45# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
46# define Q_ASSERT_X(cond, where, what)
47 static_cast<void>(false && QT_PREPEND_NAMESPACE(qt_no_assert_x)(bool(cond), where, what))
48# else
49# define Q_ASSERT_X(cond, where, what) ((cond) ? static_cast<void>(0) : QT_PREPEND_NAMESPACE(qt_assert_x)(where, what, __FILE__, __LINE__))
50# endif
51#endif
52
53#ifndef Q_PRE
54# define Q_PRE(cond)
55 Q_ASSERT(cond) /* for now... */
56#endif
57
58#ifndef Q_PRE_X
59# define Q_PRE_X(cond, what)
60 Q_ASSERT_X(cond, Q_FUNC_INFO, what) /* for now... */
61#endif
62
63Q_NORETURN Q_CORE_EXPORT void qt_check_pointer(const char *, int) noexcept;
64Q_NORETURN Q_DECL_COLD_FUNCTION
65Q_CORE_EXPORT void qBadAlloc();
66
67#ifdef QT_NO_EXCEPTIONS
68# if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)
69# define Q_CHECK_PTR(p) qt_noop()
70# else
71# define Q_CHECK_PTR(p) do {if (!(p)) QT_PREPEND_NAMESPACE(qt_check_pointer)(__FILE__,__LINE__);} while (false)
72# endif
73#else
74# define Q_CHECK_PTR(p) do { if (!(p)) QT_PREPEND_NAMESPACE(qBadAlloc)(); } while (false)
75#endif
76
77template <typename T>
78inline T *q_check_ptr(T *p) { Q_CHECK_PTR(p); return p; }
79
80// Q_UNREACHABLE_IMPL() and Q_ASSUME_IMPL() used below are defined in qcompilerdetection.h
81#define Q_UNREACHABLE()
82 do {
83 Q_ASSERT_X(false, "Q_UNREACHABLE()", "Q_UNREACHABLE was reached");
84 Q_UNREACHABLE_IMPL();
85 } while (false)
86
87#ifndef Q_UNREACHABLE_RETURN
88# ifdef Q_COMPILER_COMPLAINS_ABOUT_RETURN_AFTER_UNREACHABLE
89# define Q_UNREACHABLE_RETURN(...) Q_UNREACHABLE()
90# else
91# define Q_UNREACHABLE_RETURN(...) do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)
92# endif
93#endif
94
95Q_DECL_DEPRECATED_X("Q_ASSUME() is deprecated because it can produce worse code than when it's absent; "
96 "use C++23 [[assume]] instead")
97inline bool qt_assume_is_deprecated(bool cond) noexcept { return cond; }
98#define Q_ASSUME(Expr)
99 [] (bool valueOfExpression) {
100 Q_ASSERT_X(valueOfExpression, "Q_ASSUME()", "Assumption in Q_ASSUME(\"" #Expr "\") was not correct");
101 Q_ASSUME_IMPL(valueOfExpression);
102 }(qt_assume_is_deprecated(Expr))
103
104
105#if __has_builtin(__builtin_assume)
106// Clang has this intrinsic and won't warn about its use in C++20 mode
107# define Q_PRESUME_IMPL(assumption) __builtin_assume(assumption)
108#elif __has_cpp_attribute(assume)
109// GCC has implemented this attribute and allows its use in C++20 mode
110# define Q_PRESUME_IMPL(assumption) [[assume(assumption)]]
111#elif defined(Q_CC_MSVC)
112# define Q_PRESUME_IMPL(assumption) __assume(assumption)
113#else
114# define Q_PRESUME_IMPL(assumption) (void)0
115#endif
116
117#define Q_PRESUME(assumption)
118 [&] {
119 Q_ASSERT(assumption);
120 Q_PRESUME_IMPL(assumption);
121 }()
122
123// Don't use these in C++ mode, use static_assert directly.
124// These are here only to keep old code compiling.
125# define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition)
126# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
127
128#elif defined(Q_COMPILER_STATIC_ASSERT)
129// C11 mode - using the _S version in case <assert.h> doesn't do the right thing
130# define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition)
131# define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message)
132#else
133// C89 & C99 version
134# define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
135# define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
136# ifdef __COUNTER__
137# define Q_STATIC_ASSERT(Condition)
138 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1];
139# else
140# define Q_STATIC_ASSERT(Condition)
141 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1];
142# endif /* __COUNTER__ */
143# define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
144#endif // __cplusplus
145
146QT_END_NAMESPACE
147
148#endif // QASSERT_H
void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept
Definition qassert.cpp:124
QT_BEGIN_NAMESPACE Q_NORETURN void qAbort()
Definition qassert.cpp:25
void qt_check_pointer(const char *n, int l) noexcept
Definition qassert.cpp:166
void qBadAlloc()
Definition qassert.cpp:182
#define __has_builtin(x)
#define Q_ASSERT(cond)
Definition qrandom.cpp:48
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:49