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_DECL_COLD_FUNCTION
64Q_CORE_EXPORT void qt_check_pointer(const char *, int) noexcept;
65Q_NORETURN Q_DECL_COLD_FUNCTION
66Q_CORE_EXPORT void qBadAlloc();
67
68#ifdef QT_NO_EXCEPTIONS
69# define Q_CHECK_PTR(p) do {if (!(p)) QT_PREPEND_NAMESPACE(qt_check_pointer)(__FILE__,__LINE__);} while (false)
70#else
71# define Q_CHECK_PTR(p) do { if (!(p)) QT_PREPEND_NAMESPACE(qBadAlloc)(); } while (false)
72#endif
73
74template <typename T>
75inline T *q_check_ptr(T *p) { Q_CHECK_PTR(p); return p; }
76
77// Q_UNREACHABLE_IMPL() and Q_ASSUME_IMPL() used below are defined in qcompilerdetection.h
78#define Q_UNREACHABLE()
79 do {
80 Q_ASSERT_X(false, "Q_UNREACHABLE()", "Q_UNREACHABLE was reached");
81 Q_UNREACHABLE_IMPL();
82 } while (false)
83
84#ifndef Q_UNREACHABLE_RETURN
85# ifdef Q_COMPILER_COMPLAINS_ABOUT_RETURN_AFTER_UNREACHABLE
86# define Q_UNREACHABLE_RETURN(...) Q_UNREACHABLE()
87# else
88# define Q_UNREACHABLE_RETURN(...) do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)
89# endif
90#endif
91
92Q_DECL_DEPRECATED_X("Q_ASSUME() is deprecated because it can produce worse code than when it's absent; "
93 "use C++23 [[assume]] instead")
94inline bool qt_assume_is_deprecated(bool cond) noexcept { return cond; }
95#define Q_ASSUME(Expr)
96 [] (bool valueOfExpression) {
97 Q_ASSERT_X(valueOfExpression, "Q_ASSUME()", "Assumption in Q_ASSUME(\"" #Expr "\") was not correct");
98 Q_ASSUME_IMPL(valueOfExpression);
99 }(qt_assume_is_deprecated(Expr))
100
101
102#if __has_builtin(__builtin_assume)
103// Clang has this intrinsic and won't warn about its use in C++20 mode
104# define Q_PRESUME_IMPL(assumption) __builtin_assume(assumption)
105#elif defined(Q_CC_MSVC)
106// MSVC claims to support [[assume]], but optimizations do not seem to be applied
107# define Q_PRESUME_IMPL(assumption) __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#else
112# define Q_PRESUME_IMPL(assumption) (void)0
113#endif
114
115#define Q_PRESUME(assumption)
116 do {
117 Q_ASSERT(assumption);
118 Q_PRESUME_IMPL(assumption);
119 } while (false)
120
121// Don't use these in C++ mode, use static_assert directly.
122// These are here only to keep old code compiling.
123# define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition)
124# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
125
126#elif defined(Q_COMPILER_STATIC_ASSERT)
127// C11 mode - using the _S version in case <assert.h> doesn't do the right thing
128# define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition)
129# define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message)
130#else
131// C89 & C99 version
132# define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
133# define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
134# ifdef __COUNTER__
135# define Q_STATIC_ASSERT(Condition)
136 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1];
137# else
138# define Q_STATIC_ASSERT(Condition)
139 typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1];
140# endif /* __COUNTER__ */
141# define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
142#endif // __cplusplus
143
144QT_END_NAMESPACE
145
146#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:161
void qBadAlloc()
Definition qassert.cpp:177
#define __has_builtin(x)
#define Q_ASSERT(cond)
Definition qrandom.cpp:52
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:53