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
74
75 If \c b is zero, the Q_ASSERT statement will output the following
76 message using the qFatal() function:
77
78 \snippet code/src_corelib_global_qglobal.cpp 18
79
80 \sa Q_ASSERT_X(), qFatal(), {Debugging Techniques}
81*/
82
83/*!
84 \macro void Q_ASSERT_X(bool test, const char *where, const char *what)
85 \relates <QtAssert>
86
87 Prints the message \a what together with the location \a where,
88 the source file name and line number if \a test is \c false.
89
90 Q_ASSERT_X is useful for testing pre- and post-conditions during
91 development. It does nothing if \c QT_NO_DEBUG was defined during
92 compilation.
93
94 Example:
95
96 \snippet code/src_corelib_global_qglobal.cpp 19
97
98 If \c b is zero, the Q_ASSERT_X statement will output the following
99 message using the qFatal() function:
100
101 \snippet code/src_corelib_global_qglobal.cpp 20
102
103 \sa Q_ASSERT(), qFatal(), {Debugging Techniques}
104*/
105
106#if !defined(QT_BOOTSTRAPPED) || defined(QT_FORCE_ASSERTS) || !defined(QT_NO_DEBUG)
107/*
108 The Q_ASSERT macro calls this function when the test fails.
109*/
110void qt_assert(const char *assertion, const char *file, int line) noexcept
111{
112 QMessageLogger(file, line, nullptr)
113 .fatal(msg: "ASSERT: \"%s\" in file %s, line %d", assertion, file, line);
114}
115
116/*
117 The Q_ASSERT_X macro calls this function when the test fails.
118*/
119void qt_assert_x(const char *where, const char *what, const char *file, int line) noexcept
120{
121 QMessageLogger(file, line, nullptr)
122 .fatal(msg: "ASSERT failure in %s: \"%s\", file %s, line %d", where, what, file, line);
123}
124#endif // bootstrapped
125
126/*!
127 \macro void Q_CHECK_PTR(void *pointer)
128 \relates <QtAssert>
129
130 If \a pointer is \nullptr, prints a message containing the source
131 code's file name and line number, saying that the program ran out
132 of memory and aborts program execution. It throws \c std::bad_alloc instead
133 if exceptions are enabled.
134
135 Q_CHECK_PTR does nothing if \c QT_NO_DEBUG and \c QT_NO_EXCEPTIONS were
136 defined during compilation. Therefore you must not use Q_CHECK_PTR to check
137 for successful memory allocations because the check will be disabled in
138 some cases.
139
140 Example:
141
142 \snippet code/src_corelib_global_qglobal.cpp 21
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*/
161void 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(s: "Out of memory", stderr);
167 fprintf(stderr, format: " 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*/
177void 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
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()
204*/
205
206/*!
207 \macro void Q_UNREACHABLE()
208 \relates <QtAssert>
209 \since 5.0
210
211 Tells the compiler that the current point cannot be reached by any
212 execution, so it may optimize any code paths leading here as dead code, as
213 well as code continuing from here.
214
215 This macro is useful to mark impossible conditions. For example, given the
216 following enum:
217
218 \snippet code/src_corelib_global_qglobal.cpp qunreachable-enum
219
220 One can write a switch table like so:
221
222 \snippet code/src_corelib_global_qglobal.cpp qunreachable-switch
223
224 The advantage of inserting Q_UNREACHABLE() at that point is that the
225 compiler is told not to generate code for a shape variable containing that
226 value. If the macro is missing, the compiler will still generate the
227 necessary comparisons for that value. If the case label were removed, some
228 compilers could produce a warning that some enum values were not checked.
229
230 By using this macro in impossible conditions, code coverage may be improved
231 as dead code paths may be eliminated.
232
233 In debug builds the condition is enforced by an assert to facilitate debugging.
234
235 \note Use the macro Q_UNREACHABLE_RETURN() to insert return statements for
236 compilers that need them, without causing warnings for compilers that
237 complain about its presence.
238
239 \sa Q_ASSERT(), qFatal(), Q_UNREACHABLE_RETURN()
240*/
241
242/*!
243 \macro void Q_UNREACHABLE_RETURN(...)
244 \relates <QtAssert>
245 \since 6.5
246
247 This is equivalent to
248 \code
249 Q_UNREACHABLE();
250 return __VA_ARGS__;
251 \endcode
252 except it omits the return on compilers that would warn about it.
253
254 \sa Q_UNREACHABLE()
255*/
256QT_END_NAMESPACE
257

source code of qtbase/src/corelib/global/qassert.cpp