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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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