QString & QByteArray: ensure QT_NO_CAST_* are not defined on Windows

We can't have those macros defined while compiling the headers on
Windows (mainly MSVC, but there are some corner cases that apply to
MinGW too) because the inline functions must be emitted and added to the
DLL export table. Trying to undefine (as qchar.cpp did) won't work in
builds with PCH or in CMake Unified builds, because the headers in
question will have already been included. So instead just #error out.

qchar.cpp is #included in qstring.cpp so it no longer needs this. For Qt
7, we'll un-export the class (see next commit).

Note: it's possible we only need a single .cpp including the headers and
compiled without these macros for the export to kick in. That could be
removed_api.cpp, because that can never be part of PCH or Unified
sources. But I have not confirmed this is the case.

Task-number: QTBUG-121356
Change-Id: I5dd50a1a7ca5424d9e7afffd17acba32fb27f1ca
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Thiago Macieira 2024-01-22 09:04:46 -08:00
parent 4b3d6be5f5
commit 62de01bf17
3 changed files with 13 additions and 8 deletions

View File

@ -34,6 +34,14 @@
#include <algorithm>
#ifdef Q_OS_WIN
# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
// MSVC requires this, but let's apply it to MinGW compilers too, just in case
# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, " \
"otherwise some QByteArray functions will not get exported."
# endif
#endif
QT_BEGIN_NAMESPACE
Q_CONSTINIT const char QByteArray::_empty = '\0';

View File

@ -1,14 +1,6 @@
// 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
// Don't define it while compiling this module, or USERS of Qt will
// not be able to link.
#ifdef QT_NO_CAST_FROM_ASCII
# undef QT_NO_CAST_FROM_ASCII
#endif
#ifdef QT_NO_CAST_TO_ASCII
# undef QT_NO_CAST_TO_ASCII
#endif
#include "qchar.h"
#include "qdatastream.h"

View File

@ -51,6 +51,11 @@
#ifdef Q_OS_WIN
# include <qt_windows.h>
# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_TO_ASCII))
// MSVC requires this, but let's apply it to MinGW compilers too, just in case
# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, " \
"otherwise some QString functions will not get exported."
# endif
#endif
#ifdef truncate