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