diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-04-23 11:54:49 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2021-04-25 15:24:45 +0200 |
commit | 5fabad9a61d7fe7eadbd602723f39514932d7eaa (patch) | |
tree | 84d27c718eb1a120c8960e9e02c828ddae81a1d5 /tests | |
parent | 7c9597ef56f4df7bf2201a880906256b483d01c5 (diff) |
Long live PRI*Qdatatypes
Qt defines some integral datatypes (qsizetype, qintptr, quintptr,
qptrdiff) not in terms of the corresponding language datatypes (resp.
make_signed_t<size_t>, intptr_t, uintptr_t, ptrdiff_t) but as "integer
types with the same bit size of the corresponding language type" (and of
course the corret correct signedness for the target type).
This makes the Qt datatypes not printable via printf-like formatted
output, incl. qDebug, qWarning, QString::asprintf and so on; that's
because there isn't a format modifier that would universally work
with the Qt definitions.
For instance, on a 32 bit platform, ptrdiff_t may be a typedef for long,
while qptrdiff is a typedef for _int_ instead. Both long and int would
indeed be 32 bits, but they still are different types, and this means
that the ptrdiff_t-specific 't' length modifier would be wrong for
qptrdiff:
qptrdiff p;
printf("%td", p); // WARNING: -Wformat: wanted long, got int
Similarly, not using 't' breaks on 64 bits, and so on and so forth.
There isn't a way out, short of inserting casts on every print
statement.
So, let's adopt the same solution C/C++ use for their own integer
typedefs: the PRIx macros. This allows one to always use the correct
formatting specifier without the need of a cast.
I'm not adding the macros for the qintXX datatypes, as they already
exist in the Standard Library.
[ChangeLog][QtCore][QtGlobal] A series of PRIxQTDATATYPE macros have
been added. They make it possible to print some Qt type aliases
(qsizetype, qintptr, etc.) via a formatted output facility such as
printf() or qDebug() without raising formatting warnings and without
the need of a type cast.
Change-Id: I473226a661868aed9514d793c8e6e4d391ab5055
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 5b10049144..d2a449bb58 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -32,6 +32,7 @@ #include <QPair> #include <QSysInfo> #include <QLatin1String> +#include <QString> #include <cmath> @@ -58,6 +59,7 @@ private slots: void qRoundFloats(); void qRoundDoubles_data(); void qRoundDoubles(); + void PRImacros(); }; extern "C" { // functions in qglobal.c @@ -667,5 +669,34 @@ void tst_QGlobal::qRoundDoubles() { QCOMPARE(qRound64(actual), expected); } +void tst_QGlobal::PRImacros() +{ + // none of these calls must generate a -Wformat warning + { + quintptr p = 123u; + (void)QString::asprintf("The value %" PRIuQUINTPTR " is nice", p); + (void)QString::asprintf("The value %" PRIoQUINTPTR " is nice", p); + (void)QString::asprintf("The value %" PRIxQUINTPTR " is nice", p); + (void)QString::asprintf("The value %" PRIXQUINTPTR " is nice", p); + } + + { + qintptr p = 123; + (void)QString::asprintf("The value %" PRIdQINTPTR " is nice", p); + (void)QString::asprintf("The value %" PRIiQINTPTR " is nice", p); + } + + { + qptrdiff d = 123; + (void)QString::asprintf("The value %" PRIdQPTRDIFF " is nice", d); + (void)QString::asprintf("The value %" PRIiQPTRDIFF " is nice", d); + } + { + qsizetype s = 123; + (void)QString::asprintf("The value %" PRIdQSIZETYPE " is nice", s); + (void)QString::asprintf("The value %" PRIiQSIZETYPE " is nice", s); + } +} + QTEST_APPLESS_MAIN(tst_QGlobal) #include "tst_qglobal.moc" |