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.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
23QT_BEGIN_NAMESPACE
24
25Q_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*/
115void 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*/
124void 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 Q_CHECK_PTR does nothing if \c QT_NO_DEBUG and \c QT_NO_EXCEPTIONS were
141 defined during compilation. Therefore you must not use Q_CHECK_PTR to check
142 for successful memory allocations because the check will be disabled in
143 some cases.
144
145 Example:
146
147 \snippet code/src_corelib_global_qglobal.cpp 21
148
149 \sa qWarning(), {Debugging Techniques}
150*/
151
152/*!
153 \fn template <typename T> T *q_check_ptr(T *p)
154 \relates <QtAssert>
155
156 Uses Q_CHECK_PTR on \a p, then returns \a p.
157
158 This can be used as an inline version of Q_CHECK_PTR.
159*/
160
161/*!
162 \internal
163 The Q_CHECK_PTR macro calls this function if an allocation check
164 fails.
165*/
166void qt_check_pointer(const char *n, int l) noexcept
167{
168 // make separate printing calls so that the first one may flush;
169 // the second one could want to allocate memory (fputs prints a
170 // newline and stderr auto-flushes).
171 fputs("Out of memory", stderr);
172 fprintf(stderr, " in %s, line %d\n", n, l);
173
174 std::terminate();
175}
176
177/*
178 \internal
179 Allows you to throw an exception without including <new>
180 Called internally from Q_CHECK_PTR on certain OS combinations
181*/
183{
184#ifndef QT_NO_EXCEPTIONS
185 throw std::bad_alloc();
186#else
187 std::terminate();
188#endif
189}
190
191/*!
192 \macro void Q_ASSUME(bool expr)
193 \deprecated
194 \relates <QtAssert>
195 \since 5.0
196
197 Causes the compiler to assume that \a expr is \c true.
198
199 This macro is known to produce worse code than when no assumption was
200 inserted in the code, with some compiler versions. The arguments passed to
201 it are always evaluated, even in release mode, with some compilers and not
202 others, so application code needs to be aware of those possible differences
203 in behavior.
204
205 Do not use it in new code. It is retained as-is for compatibility with old
206 code and will likely be removed in the next major version Qt.
207
208 \sa Q_ASSERT(), Q_UNREACHABLE(), Q_LIKELY(), Q_PRESUME()
209*/
210
211/*!
212 \macro void Q_PRESUME(bool expr)
213 \relates <QtAssert>
214 \since 6.11
215
216 Causes the compiler to assume that \a expr is \c true.
217
218 This macro emits Q_ASSERT() and a C++23-style \c{[[assume]]} attribute
219 when supported by the compiler. Otherwise it falls back to Q_ASSERT().
220
221 \sa Q_ASSERT(), Q_UNREACHABLE(), Q_LIKELY(), Q_ASSUME()
222*/
223
224/*!
225 \macro void Q_UNREACHABLE()
226 \relates <QtAssert>
227 \since 5.0
228
229 Tells the compiler that the current point cannot be reached by any
230 execution, so it may optimize any code paths leading here as dead code, as
231 well as code continuing from here.
232
233 This macro is useful to mark impossible conditions. For example, given the
234 following enum:
235
236 \snippet code/src_corelib_global_qglobal.cpp qunreachable-enum
237
238 One can write a switch table like so:
239
240 \snippet code/src_corelib_global_qglobal.cpp qunreachable-switch
241
242 The advantage of inserting Q_UNREACHABLE() at that point is that the
243 compiler is told not to generate code for a shape variable containing that
244 value. If the macro is missing, the compiler will still generate the
245 necessary comparisons for that value. If the case label were removed, some
246 compilers could produce a warning that some enum values were not checked.
247
248 By using this macro in impossible conditions, code coverage may be improved
249 as dead code paths may be eliminated.
250
251 In debug builds the condition is enforced by an assert to facilitate debugging.
252
253 \note Use the macro Q_UNREACHABLE_RETURN() to insert return statements for
254 compilers that need them, without causing warnings for compilers that
255 complain about its presence.
256
257 \sa Q_ASSERT(), qFatal(), Q_UNREACHABLE_RETURN(), Q_PRESUME()
258*/
259
260/*!
261 \macro void Q_UNREACHABLE_RETURN(...)
262 \relates <QtAssert>
263 \since 6.5
264
265 This is equivalent to
266 \code
267 Q_UNREACHABLE();
268 return __VA_ARGS__;
269 \endcode
270 except it omits the return on compilers that would warn about it.
271
272 \sa Q_UNREACHABLE(), Q_PRESUME()
273*/
274QT_END_NAMESPACE
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