Be less laissez-faire with implicit conversions to QChar

QChar currently is convertible from nearly every integral type. This
is bad code hygiene and should be fixed come Qt 6.

The present patch is the result of compile fixes from marking these
constructors explicit. As is clear from the distribution of fixes,
only low-level string handling code used these implicit conversions,
an indication that they're not in widespread use elsewhere.

Change-Id: Ief5336f21e6d181e03ab92893b3d13a14adc7cb0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Marc Mutz 2019-06-18 20:11:18 +02:00
parent 36f6bd7cf0
commit 60ca2f5f7c
17 changed files with 60 additions and 61 deletions

View File

@ -62,7 +62,7 @@ QByteArray QLatin1Codec::convertFromUnicode(const QChar *ch, int len, ConverterS
char *d = r.data();
int invalid = 0;
for (int i = 0; i < len; ++i) {
if (ch[i] > 0xff) {
if (ch[i] > QChar(0xff)) {
d[i] = replacement;
++invalid;
} else {
@ -112,28 +112,28 @@ QString QLatin15Codec::convertToUnicode(const char* chars, int len, ConverterSta
while(len--) {
switch(uc->unicode()) {
case 0xa4:
*uc = 0x20ac;
*uc = QChar(0x20ac);
break;
case 0xa6:
*uc = 0x0160;
*uc = QChar(0x0160);
break;
case 0xa8:
*uc = 0x0161;
*uc = QChar(0x0161);
break;
case 0xb4:
*uc = 0x017d;
*uc = QChar(0x017d);
break;
case 0xb8:
*uc = 0x017e;
*uc = QChar(0x017e);
break;
case 0xbc:
*uc = 0x0152;
*uc = QChar(0x0152);
break;
case 0xbd:
*uc = 0x0153;
*uc = QChar(0x0153);
break;
case 0xbe:
*uc = 0x0178;
*uc = QChar(0x0178);
break;
default:
break;

View File

@ -951,10 +951,10 @@ QString QUtf32::convertToUnicode(const char *chars, int len, QTextCodec::Convert
}
uint code = (endian == BigEndianness) ? qFromBigEndian<quint32>(tuple) : qFromLittleEndian<quint32>(tuple);
if (QChar::requiresSurrogates(code)) {
*qch++ = QChar::highSurrogate(code);
*qch++ = QChar::lowSurrogate(code);
*qch++ = QChar(QChar::highSurrogate(code));
*qch++ = QChar(QChar::lowSurrogate(code));
} else {
*qch++ = code;
*qch++ = QChar(code);
}
num = 0;
}

View File

@ -82,7 +82,7 @@ public:
static inline QChar getFilterSepChar(const QString &nameFilter);
static inline QStringList splitFilters(const QString &nameFilter, QChar sep = 0);
static inline QStringList splitFilters(const QString &nameFilter, QChar sep = {});
void setPath(const QString &path);

View File

@ -312,7 +312,7 @@ void toString(QString &appendTo, const IPv6Address address)
}
}
const QChar colon = ushort(':');
const QChar colon = u':';
if (zeroRunLength < 4)
zeroRunOffset = -1;
else if (zeroRunOffset == 0)

View File

@ -1010,7 +1010,7 @@ inline bool QUrlPrivate::setScheme(const QString &value, int len, bool doSetErro
for (int i = needsLowercasing; i >= 0; --i) {
ushort c = schemeData[i].unicode();
if (c >= 'A' && c <= 'Z')
schemeData[i] = c + 0x20;
schemeData[i] = QChar(c + 0x20);
}
}

View File

@ -2220,9 +2220,8 @@ Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString
bool skipped = false;
// copy all basic code points verbatim to output.
for (uint j = 0; j < (uint) ucLength; ++j) {
ushort js = s[j].unicode();
if (js < 0x80)
*d++ = js;
if (s[j].unicode() < 0x80)
*d++ = s[j];
else
skipped = true;
}

View File

@ -293,9 +293,9 @@ void QUrlQueryPrivate::setQuery(const QString &query)
const QChar *delimiter = nullptr;
while (pos != end) {
// scan for the component parts of this pair
if (!delimiter && pos->unicode() == valueDelimiter)
if (!delimiter && *pos == valueDelimiter)
delimiter = pos;
if (pos->unicode() == pairDelimiter)
if (*pos == pairDelimiter)
break;
++pos;
}
@ -584,8 +584,8 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const
*/
void QUrlQuery::setQueryDelimiters(QChar valueDelimiter, QChar pairDelimiter)
{
d->valueDelimiter = valueDelimiter.unicode();
d->pairDelimiter = pairDelimiter.unicode();
d->valueDelimiter = valueDelimiter;
d->pairDelimiter = pairDelimiter;
}
/*!

View File

@ -888,7 +888,7 @@ inline bool QTextStreamPrivate::getChar(QChar *ch)
if ((string && stringOffset == string->size())
|| (device && readBuffer.isEmpty() && !fillReadBuffer())) {
if (ch)
*ch = 0;
*ch = QChar();
return false;
}
if (ch)

View File

@ -1423,7 +1423,7 @@ inline int QXmlStreamReaderPrivate::fastScanNMTOKEN()
int n = 0;
uint c;
while ((c = getChar()) != StreamEOF) {
if (fastDetermineNameChar(c) == NotName) {
if (fastDetermineNameChar(QChar(c)) == NotName) {
putChar(c);
return n;
} else {

View File

@ -1885,11 +1885,11 @@ static void composeHelper(QString *str, QChar::UnicodeVersion version, int from)
QChar *d = s.data();
// ligatureHelper() never changes planes
if (QChar::requiresSurrogates(ligature)) {
d[starter] = QChar::highSurrogate(ligature);
d[starter + 1] = QChar::lowSurrogate(ligature);
d[starter] = QChar(QChar::highSurrogate(ligature));
d[starter + 1] = QChar(QChar::lowSurrogate(ligature));
s.remove(i, 2);
} else {
d[starter] = ligature;
d[starter] = QChar(ligature);
s.remove(i, 1);
}
continue;
@ -1962,16 +1962,16 @@ static void canonicalOrderHelper(QString *str, QChar::UnicodeVersion version, in
int p = pos;
// exchange characters
if (!QChar::requiresSurrogates(u2)) {
uc[p++] = u2;
uc[p++] = QChar(u2);
} else {
uc[p++] = QChar::highSurrogate(u2);
uc[p++] = QChar::lowSurrogate(u2);
uc[p++] = QChar(QChar::highSurrogate(u2));
uc[p++] = QChar(QChar::lowSurrogate(u2));
}
if (!QChar::requiresSurrogates(u1)) {
uc[p++] = u1;
uc[p++] = QChar(u1);
} else {
uc[p++] = QChar::highSurrogate(u1);
uc[p++] = QChar::lowSurrogate(u1);
uc[p++] = QChar(QChar::highSurrogate(u1));
uc[p++] = QChar(QChar::lowSurrogate(u1));
}
if (pos > 0)
--pos;

View File

@ -443,17 +443,17 @@ public:
#endif
inline unsigned char combiningClass() const noexcept { return QChar::combiningClass(ucs); }
inline QChar mirroredChar() const noexcept { return QChar::mirroredChar(ucs); }
inline QChar mirroredChar() const noexcept { return QChar(QChar::mirroredChar(ucs)); }
inline bool hasMirrored() const noexcept { return QChar::hasMirrored(ucs); }
QString decomposition() const;
inline Decomposition decompositionTag() const noexcept { return QChar::decompositionTag(ucs); }
inline int digitValue() const noexcept { return QChar::digitValue(ucs); }
inline QChar toLower() const noexcept { return QChar::toLower(ucs); }
inline QChar toUpper() const noexcept { return QChar::toUpper(ucs); }
inline QChar toTitleCase() const noexcept { return QChar::toTitleCase(ucs); }
inline QChar toCaseFolded() const noexcept { return QChar::toCaseFolded(ucs); }
inline QChar toLower() const noexcept { return QChar(QChar::toLower(ucs)); }
inline QChar toUpper() const noexcept { return QChar(QChar::toUpper(ucs)); }
inline QChar toTitleCase() const noexcept { return QChar(QChar::toTitleCase(ucs)); }
inline QChar toCaseFolded() const noexcept { return QChar(QChar::toCaseFolded(ucs)); }
inline Script script() const noexcept { return QChar::script(ucs); }

View File

@ -285,9 +285,9 @@ public:
quint16 m_language_id, m_script_id, m_country_id;
// FIXME QTBUG-69324: not all unicode code-points map to single-token UTF-16 :-(
quint16 m_decimal, m_group, m_list, m_percent, m_zero, m_minus, m_plus, m_exponential;
quint16 m_quotation_start, m_quotation_end;
quint16 m_alternate_quotation_start, m_alternate_quotation_end;
char16_t m_decimal, m_group, m_list, m_percent, m_zero, m_minus, m_plus, m_exponential;
char16_t m_quotation_start, m_quotation_end;
char16_t m_alternate_quotation_start, m_alternate_quotation_end;
quint16 m_list_pattern_part_start_idx, m_list_pattern_part_start_size;
quint16 m_list_pattern_part_mid_idx, m_list_pattern_part_mid_size;
@ -417,7 +417,7 @@ inline char QLocaleData::digitToCLocale(QChar in) const
if (in == m_group)
return ',';
if (in == m_exponential || in == QChar::toUpper(m_exponential))
if (in == m_exponential || in == QChar(QChar::toUpper(m_exponential)))
return 'e';
// In several languages group() is a non-breaking space (U+00A0) or its thin

View File

@ -2035,7 +2035,7 @@ bool QRegExpMatchState::matchHere()
#ifndef QT_NO_REGEXP_CCLASS
const QRegExpCharClass &cc = eng->cl.at(m ^ QRegExpEngine::CharClassBit);
if (eng->cs)
inside = cc.in(ch);
inside = cc.in(QChar(ch));
else if (cc.negative())
inside = cc.in(QChar(ch).toLower()) &&
cc.in(QChar(ch).toUpper());

View File

@ -3208,7 +3208,7 @@ QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs
return remove(ch, cs);
if (after.d->size == 1)
return replace(ch, after.d->data()[0], cs);
return replace(ch, after.front(), cs);
if (d->size == 0)
return *this;
@ -6666,7 +6666,7 @@ static QString detachAndConvertCase(T &str, QStringIterator it)
} else if (Q_UNLIKELY(QChar::requiresSurrogates(uc))) {
// so far, case convertion never changes planes (guaranteed by the qunicodetables generator)
pp++;
*pp++ = QChar::lowSurrogate(uc + caseDiff);
*pp++ = QChar(QChar::lowSurrogate(uc + caseDiff));
} else {
*pp++ = QChar(uc + caseDiff);
}

View File

@ -1002,11 +1002,11 @@ inline QString::QString(QLatin1String aLatin1) : d(fromLatin1_helper(aLatin1.lat
inline int QString::length() const
{ return d->size; }
inline const QChar QString::at(int i) const
{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
{ Q_ASSERT(uint(i) < uint(size())); return QChar(d->data()[i]); }
inline const QChar QString::operator[](int i) const
{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
{ Q_ASSERT(uint(i) < uint(size())); return QChar(d->data()[i]); }
inline const QChar QString::operator[](uint i) const
{ Q_ASSERT(i < uint(size())); return d->data()[i]; }
{ Q_ASSERT(i < uint(size())); return QChar(d->data()[i]); }
inline bool QString::isEmpty() const
{ return d->size == 0; }
inline const QChar *QString::unicode() const
@ -1118,11 +1118,11 @@ public:
{
using namespace QtPrivate::DeprecatedRefClassBehavior;
if (Q_LIKELY(i < s.d->size))
return s.d->data()[i];
return QChar(s.d->data()[i]);
#ifdef QT_DEBUG
warn(WarningType::OutOfRange, EmittingClass::QCharRef);
#endif
return 0;
return QChar();
}
inline QCharRef &operator=(QChar c)
{

View File

@ -231,7 +231,7 @@ void QEvdevKeyboardHandler::processKeyEvent(int nativecode, int unicode, int qtc
QWindowSystemInterface::handleExtendedKeyEvent(0, (isPress ? QEvent::KeyPress : QEvent::KeyRelease),
qtcode, modifiers, nativecode + 8, 0, int(modifiers),
(unicode != 0xffff ) ? QString(unicode) : QString(), autoRepeat);
(unicode != 0xffff ) ? QString(QChar(unicode)) : QString(), autoRepeat);
}
QEvdevKeyboardHandler::KeycodeAction QEvdevKeyboardHandler::processKeycode(quint16 keycode, bool pressed, bool autorepeat)

View File

@ -1161,12 +1161,12 @@ QChar QXmlInputSource::next()
d->nextReturnedEndOfData = false;
fetchData();
if (d->pos >= d->length) {
return EndOfDocument;
return QChar(EndOfDocument);
}
return next();
}
d->nextReturnedEndOfData = true;
return EndOfData;
return QChar(EndOfData);
}
// QXmlInputSource has no way to signal encoding errors. The best we can do
@ -1174,7 +1174,7 @@ QChar QXmlInputSource::next()
// will then just call this function again to get the next char.
QChar c = d->unicode[d->pos++];
if (c.unicode() == EndOfData)
c = EndOfDocument;
c = QChar(EndOfDocument);
return c;
}
@ -1313,8 +1313,8 @@ static QString extractEncodingDecl(const QString &text, bool *needMoreText)
return QString();
while (pos < endPos) {
ushort uc = text.at(pos).unicode();
if (uc == '\'' || uc == '"')
QChar uc = text.at(pos);
if (uc == u'\'' || uc == u'"')
break;
++pos;
}
@ -1325,8 +1325,8 @@ static QString extractEncodingDecl(const QString &text, bool *needMoreText)
QString encoding;
++pos;
while (pos < endPos) {
ushort uc = text.at(pos).unicode();
if (uc == '\'' || uc == '"')
QChar uc = text.at(pos);
if (uc == u'\'' || uc == u'"')
break;
encoding.append(uc);
++pos;
@ -7800,7 +7800,7 @@ void QXmlSimpleReaderPrivate::next()
c = inputSource->next();
// If we are not incremental parsing, we just skip over EndOfData chars to give the
// parser an uninterrupted stream of document chars.
if (c == QXmlInputSource::EndOfData && parseStack == nullptr)
if (c == QChar(QXmlInputSource::EndOfData) && parseStack == nullptr)
c = inputSource->next();
if (uc == '\n') {
lineNr++;
@ -7877,7 +7877,7 @@ void QXmlSimpleReaderPrivate::init(const QXmlInputSource *i)
*/
void QXmlSimpleReaderPrivate::initData()
{
c = QXmlInputSource::EndOfData;
c = QChar(QXmlInputSource::EndOfData);
xmlRefStack.clear();
next();
}
@ -7925,7 +7925,7 @@ void QXmlSimpleReaderPrivate::unexpectedEof(ParseFunction where, int state)
if (parseStack == nullptr) {
reportParseError(QLatin1String(XMLERR_UNEXPECTEDEOF));
} else {
if (c == QXmlInputSource::EndOfDocument) {
if (c == QChar(QXmlInputSource::EndOfDocument)) {
reportParseError(QLatin1String(XMLERR_UNEXPECTEDEOF));
} else {
pushParseState(where, state);