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
qssgassert.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
6#include "qssgassert_p.h"
7
9
10/*!
11 Collection of assert checks that causes a soft or hard assert depending on the build. Unlike Q_ASSERT(),
12 which is a no-op for non-debug build, QSSG_ASSERT() etc., will print a warning in non-developer builds (soft assert)
13 or terminate on developer-build (hard assert).
14
15 \macro QSSG_ASSERT(condition, action)
16 \internal
17
18 The assert will be fatal in developer builds if \a condition is not met. In non-developer builds
19 the assert is "soft" and will instead print a warning with the reason and location of the assert
20 before execution \a action. The \a action can be for example be: \c return, \c break or \c continue.
21
22 For example, writing:
23
24 \badcode
25 QSSG_ASSERT(ptr != nullptr, return);
26 \endcode
27
28 other actions are of course possible, e.g., in a loop it might be better to do:
29
30 \badcode
31 QSSG_ASSERT(ptr != nullptr, continue);
32 \endcode
33
34 is the equivalent to:
35
36 \badcode
37 Q_ASSERT(ptr != nullptr);
38 if (ptr != nullptr) {
39 qWarning() << "Something unexpected here, proceeding will be fatal!";
40 return;
41 }
42 \endcode
43
44 \sa QSSG_ASSERT_X
45*/
46
47/*!
48 \macro QSSG_ASSERT_X(condition, message, action)
49 \internal
50
51 Same as \l QSSG_ASSERT() but with a custom \a message that will be print if \a condition is not met.
52*/
53
54/*!
55 \macro QSSG_CHECK(condition)
56 \internal
57
58 Similar to \l QSSG_ASSERT but without an action. Convenient when the \a condition is expected to be valid,
59 but it's not immediately fatal if the current code path continues.
60
61 \badcode
62 QSSG_CHECK(ptr != nullptr);
63 \endcode
64
65 is the equivalent to:
66
67 \badcode
68 Q_ASSERT(ptr != nullptr);
69 if (ptr != nullptr)
70 qWarning() << "Something unexpected here, will probably not work as expected!";
71 \endcode
72
73 \sa QSSG_CHECK_X
74*/
75
76/*!
77 \macro QSSG_CHECK_X(condition, message)
78 \internal
79
80 Same as \l QSSG_CHECK() but with a custom \a message that will be print if \a condition is not met.
81*/
82
83/*!
84 \macro QSSG_GUARD(condition)
85 \internal
86
87 Check that returns the result of \a condition. As with the other assert functions, a call to QSSG_GUARD, when \a condition
88 is not met, is fatal for developer builds.
89
90 \badcode
91
92 if (QSSG_GUARD(ptr != nullptr)) {
93 ... // OK
94 } else {
95 ... // We shouldn't be here!
96 }
97
98 \endcode
99
100 is the equivalent to:
101
102 \badcode
103 if (ptr != nullptr) {
104 ... // OK
105 } else {
106 Q_ASSERT(ptr != nullptr);
107 qWarning() << "Something unexpected here!";
108 }
109 \endcode
110
111 \sa QSSG_GUARD_X
112*/
113
114/*!
115 \macro QSSG_GUARD_X(condition, message)
116 \internal
117
118 Same as \l QSSG_GUARD() but with a custom \a message that will be print if \a condition is not met.
119*/
120
121/*!
122 \macro QSSG_DEBUG_COND(condition)
123 \internal
124
125 Macro for condition that should only be run in debug builds. In releases build the macro
126 produces an "almost-no-op" condition (always true) and the \a condition is never run.
127 Can e.g., be combined with the assert checks to add potentially expensive sanity checks
128 that should only be run in debug builds.
129
130 \badcode
131 QSSG_CEHCK(QSSG_DEBUG_COND(!list.contains(...)));
132 \endcode
133
134 In a release build the \c QSSG_DEBUG_COND will never return \c false and the \a condition will never
135 be evaluated.
136
137 \note DO NOT make surrounding code depend on the \a condition being evaluated or called!.
138
139 \note Unlike the assert checks, this macro does not change behavior in relation to developer-builds.
140*/
141
142void qssgWriteAssertLocation(const char *msg)
143{
144#if defined(QT_BUILD_INTERNAL)
145 qFatal("ASSERT: %s", msg);
146#else
147 qWarning("Unexpected condition met: %s", msg);
148#endif
149}
150
151QT_END_NAMESPACE
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE void qssgWriteAssertLocation(const char *msg)
Collection of assert checks that causes a soft or hard assert depending on the build.