diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index dc15ec831f..50b9009f8d 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -54,6 +54,7 @@ qt_internal_add_module(Core global/qcontainerinfo.h global/qendian.cpp global/qendian.h global/qendian_p.h global/qenvironmentvariables.cpp global/qenvironmentvariables.h + global/qexceptionhandling.cpp global/qexceptionhandling.h global/qflags.h global/qfloat16.cpp global/qfloat16.h global/qforeach.h diff --git a/src/corelib/global/qexceptionhandling.cpp b/src/corelib/global/qexceptionhandling.cpp new file mode 100644 index 0000000000..f74eb49546 --- /dev/null +++ b/src/corelib/global/qexceptionhandling.cpp @@ -0,0 +1,20 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qexceptionhandling.h" + +#include + +QT_BEGIN_NAMESPACE + +/* + \internal + Allows you to call std::terminate() without including . + Called internally from QT_TERMINATE_ON_EXCEPTION +*/ +Q_NORETURN void qTerminate() noexcept +{ + std::terminate(); +} + +QT_END_NAMESPACE diff --git a/src/corelib/global/qexceptionhandling.h b/src/corelib/global/qexceptionhandling.h new file mode 100644 index 0000000000..aca8de9003 --- /dev/null +++ b/src/corelib/global/qexceptionhandling.h @@ -0,0 +1,45 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QEXCEPTIONHANDLING_H +#define QEXCEPTIONHANDLING_H + +#include +#include + +#if 0 +#pragma qt_class(QtExceptionHandling) +#pragma qt_sync_stop_processing +#endif + +QT_BEGIN_NAMESPACE + +/* These wrap try/catch so we can switch off exceptions later. + + Beware - do not use more than one QT_CATCH per QT_TRY, and do not use + the exception instance in the catch block. + If you can't live with those constraints, don't use these macros. + Use the QT_NO_EXCEPTIONS macro to protect your code instead. +*/ +Q_NORETURN Q_DECL_COLD_FUNCTION Q_CORE_EXPORT void qTerminate() noexcept; +#ifdef QT_NO_EXCEPTIONS +# define QT_TRY if (true) +# define QT_CATCH(A) else +# define QT_THROW(A) qt_noop() +# define QT_RETHROW qt_noop() +# define QT_TERMINATE_ON_EXCEPTION(expr) do { expr; } while (false) +#else +# define QT_TRY try +# define QT_CATCH(A) catch (A) +# define QT_THROW(A) throw A +# define QT_RETHROW throw +# ifdef Q_COMPILER_NOEXCEPT +# define QT_TERMINATE_ON_EXCEPTION(expr) do { expr; } while (false) +# else +# define QT_TERMINATE_ON_EXCEPTION(expr) do { try { expr; } catch (...) { qTerminate(); } } while (false) +# endif +#endif + +QT_END_NAMESPACE + +#endif // QEXCEPTIONHANDLING_H diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 6acfefd771..de28cd87af 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -22,11 +22,6 @@ #include #include -#include // For std::terminate -#ifndef QT_NO_EXCEPTIONS -#include // For std::bad_alloc -#endif - #include #if defined(Q_CC_MSVC) # include @@ -2640,16 +2635,6 @@ QByteArray QSysInfo::bootUniqueId() If this macro is used outside a function, the behavior is undefined. */ -/* - \internal - Allows you to call std::terminate() without including . - Called internally from QT_TERMINATE_ON_EXCEPTION -*/ -Q_NORETURN void qTerminate() noexcept -{ - std::terminate(); -} - /* Dijkstra's bisection algorithm to find the square root of an integer. Deliberately not exported as part of the Qt API, but used in both diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index f0295b70f5..13452bf22a 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -225,32 +225,6 @@ private: #endif // Q_OS_DARWIN -/* These wrap try/catch so we can switch off exceptions later. - - Beware - do not use more than one QT_CATCH per QT_TRY, and do not use - the exception instance in the catch block. - If you can't live with those constraints, don't use these macros. - Use the QT_NO_EXCEPTIONS macro to protect your code instead. -*/ -Q_NORETURN Q_DECL_COLD_FUNCTION Q_CORE_EXPORT void qTerminate() noexcept; -#ifdef QT_NO_EXCEPTIONS -# define QT_TRY if (true) -# define QT_CATCH(A) else -# define QT_THROW(A) qt_noop() -# define QT_RETHROW qt_noop() -# define QT_TERMINATE_ON_EXCEPTION(expr) do { expr; } while (false) -#else -# define QT_TRY try -# define QT_CATCH(A) catch (A) -# define QT_THROW(A) throw A -# define QT_RETHROW throw -# ifdef Q_COMPILER_NOEXCEPT -# define QT_TERMINATE_ON_EXCEPTION(expr) do { expr; } while (false) -# else -# define QT_TERMINATE_ON_EXCEPTION(expr) do { try { expr; } catch (...) { qTerminate(); } } while (false) -# endif -#endif - Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qSharedBuild() noexcept; /* @@ -407,6 +381,7 @@ QT_END_NAMESPACE #include #include +#include #include #include #include