Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qassert.cpp
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
#
include
"qassert.h"
6
7
#
include
<
QtCore
/
qlogging
.
h
>
8
9
#
include
<
cstdlib
>
10
#
include
<
cstdio
>
11
#
include
<
exception
>
12
#
ifndef
QT_NO_EXCEPTIONS
13
#
include
<
new
>
14
#
endif
15
16
#
if
defined
(
Q_CC_MSVC
)
17
#
include
<
crtdbg
.
h
>
18
#
endif
19
#
ifdef
Q_OS_WIN
20
#
include
<
qt_windows
.
h
>
21
#
endif
22
23
QT_BEGIN_NAMESPACE
24
25
Q_NORETURN
void
qAbort
()
26
{
27
#
ifdef
Q_OS_WIN
28
// std::abort() in the MSVC runtime will call _exit(3) if the abort
29
// behavior is _WRITE_ABORT_MSG - see also _set_abort_behavior(). This is
30
// the default for a debug-mode build of the runtime. Worse, MinGW's
31
// std::abort() implementation (in msvcrt.dll) is basically a call to
32
// _exit(3) too. Unfortunately, _exit() and _Exit() *do* run the static
33
// destructors of objects in DLLs, a violation of the C++ standard (see
34
// [support.start.term]). So we bypass std::abort() and directly
35
// terminate the application.
36
37
#
if
defined
(
Q_CC_MSVC
)
38
if
(IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
39
__fastfail(FAST_FAIL_FATAL_APP_EXIT);
40
#
else
41
RaiseFailFastException(
nullptr
,
nullptr
, 0);
42
#
endif
43
44
// Fallback
45
TerminateProcess(GetCurrentProcess(), STATUS_FATAL_APP_EXIT);
46
#
else
// !Q_OS_WIN
47
std
::abort();
48
#
endif
49
50
// Tell the compiler the application has stopped.
51
Q_UNREACHABLE_IMPL();
52
}
53
54
/*!
55
\headerfile <QtAssert>
56
\inmodule QtCore
57
\ingroup funclists
58
\brief Macros for condition checks during development and debugging.
59
*/
60
61
/*!
62
\macro void Q_ASSERT(bool test)
63
\relates <QtAssert>
64
65
Prints a warning message containing the source code file name and
66
line number if \a test is \c false.
67
68
Q_ASSERT() is useful for testing pre- and post-conditions
69
during development. It does nothing if \c QT_NO_DEBUG was defined
70
during compilation.
71
72
Example:
73
74
\snippet code/src_corelib_global_qglobal.cpp 17&19_include_open
75
\snippet code/src_corelib_global_qglobal.cpp 17assert
76
\snippet code/src_corelib_global_qglobal.cpp 17&19_return_close
77
78
If \c b is zero, the Q_ASSERT statement will output the following
79
message using the qFatal() function:
80
81
\snippet code/src_corelib_global_qglobal.cpp 18
82
83
\sa Q_ASSERT_X(), qFatal(), {Debugging Techniques}
84
*/
85
86
/*!
87
\macro void Q_ASSERT_X(bool test, const char *where, const char *what)
88
\relates <QtAssert>
89
90
Prints the message \a what together with the location \a where,
91
the source file name and line number if \a test is \c false.
92
93
Q_ASSERT_X is useful for testing pre- and post-conditions during
94
development. It does nothing if \c QT_NO_DEBUG was defined during
95
compilation.
96
97
Example:
98
99
\snippet code/src_corelib_global_qglobal.cpp 17&19_include_open
100
\snippet code/src_corelib_global_qglobal.cpp 19assert
101
\snippet code/src_corelib_global_qglobal.cpp 17&19_return_close
102
103
If \c b is zero, the Q_ASSERT_X statement will output the following
104
message using the qFatal() function:
105
106
\snippet code/src_corelib_global_qglobal.cpp 20
107
108
\sa Q_ASSERT(), qFatal(), {Debugging Techniques}
109
*/
110
111
#
if
!
defined
(
QT_BOOTSTRAPPED
)
||
defined
(
QT_FORCE_ASSERTS
)
||
!
defined
(
QT_NO_DEBUG
)
112
/*
113
The Q_ASSERT macro calls this function when the test fails.
114
*/
115
void
qt_assert(
const
char
*assertion,
const
char
*file,
int
line)
noexcept
116
{
117
QMessageLogger(file, line,
nullptr
)
118
.fatal(
"ASSERT: \"%s\" in file %s, line %d"
, assertion, file, line);
119
}
120
121
/*
122
The Q_ASSERT_X macro calls this function when the test fails.
123
*/
124
void
qt_assert_x
(
const
char
*where,
const
char
*what,
const
char
*file,
int
line)
noexcept
125
{
126
QMessageLogger(file, line,
nullptr
)
127
.fatal(
"ASSERT failure in %s: \"%s\", file %s, line %d"
, where, what, file, line);
128
}
129
#
endif
// bootstrapped
130
131
/*!
132
\macro void Q_CHECK_PTR(void *pointer)
133
\relates <QtAssert>
134
135
If \a pointer is \nullptr, prints a message containing the source
136
code's file name and line number, saying that the program ran out
137
of memory and aborts program execution. It throws \c std::bad_alloc instead
138
if exceptions are enabled.
139
140
\note In Qt versions prior to 6.13, this macro was a no-op in certain
141
build configurations. Since 6.13, it always reliably terminates or
142
throws.
143
144
\sa qWarning(), {Debugging Techniques}
145
*/
146
147
/*!
148
\fn template <typename T> T *q_check_ptr(T *p)
149
\relates <QtAssert>
150
151
Uses Q_CHECK_PTR on \a p, then returns \a p.
152
153
This can be used as an inline version of Q_CHECK_PTR.
154
*/
155
156
/*!
157
\internal
158
The Q_CHECK_PTR macro calls this function if an allocation check
159
fails.
160
*/
161
void
qt_check_pointer
(
const
char
*n,
int
l)
noexcept
162
{
163
// make separate printing calls so that the first one may flush;
164
// the second one could want to allocate memory (fputs prints a
165
// newline and stderr auto-flushes).
166
fputs(
"Out of memory"
, stderr);
167
fprintf(stderr,
" in %s, line %d\n"
, n, l);
168
169
std
::terminate();
170
}
171
172
/*
173
\internal
174
Allows you to throw an exception without including <new>
175
Called internally from Q_CHECK_PTR on certain OS combinations
176
*/
177
void
qBadAlloc
()
178
{
179
#
ifndef
QT_NO_EXCEPTIONS
180
throw
std
::bad_alloc();
181
#
else
182
std::terminate();
183
#
endif
184
}
185
186
/*!
187
\macro void Q_ASSUME(bool expr)
188
\deprecated [6.6]
189
\relates <QtAssert>
190
\since 5.0
191
192
Causes the compiler to assume that \a expr is \c true.
193
194
This macro is known to produce worse code than when no assumption was
195
inserted in the code, with some compiler versions. The arguments passed to
196
it are always evaluated, even in release mode, with some compilers and not
197
others, so application code needs to be aware of those possible differences
198
in behavior.
199
200
Do not use it in new code. It is retained as-is for compatibility with old
201
code and will likely be removed in the next major version Qt.
202
203
\sa Q_ASSERT(), Q_UNREACHABLE(), Q_LIKELY(), Q_PRESUME()
204
*/
205
206
/*!
207
\macro void Q_PRESUME(bool expr)
208
\relates <QtAssert>
209
\since 6.11
210
211
Causes the compiler to assume that \a expr is \c true.
212
213
This macro emits Q_ASSERT() and a C++23-style \c{[[assume]]} attribute
214
when supported by the compiler. Otherwise it falls back to Q_ASSERT().
215
216
\sa Q_ASSERT(), Q_UNREACHABLE(), Q_LIKELY(), Q_ASSUME()
217
*/
218
219
/*!
220
\macro void Q_UNREACHABLE()
221
\relates <QtAssert>
222
\since 5.0
223
224
Tells the compiler that the current point cannot be reached by any
225
execution, so it may optimize any code paths leading here as dead code, as
226
well as code continuing from here.
227
228
This macro is useful to mark impossible conditions. For example, given the
229
following enum:
230
231
\snippet code/src_corelib_global_qglobal.cpp qunreachable-enum
232
233
One can write a switch table like so:
234
235
\snippet code/src_corelib_global_qglobal.cpp qunreachable-switch
236
237
The advantage of inserting Q_UNREACHABLE() at that point is that the
238
compiler is told not to generate code for a shape variable containing that
239
value. If the macro is missing, the compiler will still generate the
240
necessary comparisons for that value. If the case label were removed, some
241
compilers could produce a warning that some enum values were not checked.
242
243
By using this macro in impossible conditions, code coverage may be improved
244
as dead code paths may be eliminated.
245
246
In debug builds the condition is enforced by an assert to facilitate debugging.
247
248
\note Use the macro Q_UNREACHABLE_RETURN() to insert return statements for
249
compilers that need them, without causing warnings for compilers that
250
complain about its presence.
251
252
\sa Q_ASSERT(), qFatal(), Q_UNREACHABLE_RETURN(), Q_PRESUME()
253
*/
254
255
/*!
256
\macro void Q_UNREACHABLE_RETURN(...)
257
\relates <QtAssert>
258
\since 6.5
259
260
This is equivalent to
261
\code
262
Q_UNREACHABLE();
263
return __VA_ARGS__;
264
\endcode
265
except it omits the return on compilers that would warn about it.
266
267
\sa Q_UNREACHABLE(), Q_PRESUME()
268
*/
269
QT_END_NAMESPACE
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qt_assert_x
void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept
Definition
qassert.cpp:124
qAbort
QT_BEGIN_NAMESPACE Q_NORETURN void qAbort()
Definition
qassert.cpp:25
qt_check_pointer
void qt_check_pointer(const char *n, int l) noexcept
Definition
qassert.cpp:161
qBadAlloc
void qBadAlloc()
Definition
qassert.cpp:177
qtbase
src
corelib
global
qassert.cpp
Generated on
for Qt by
1.18.0