Clean up QStringLiteral definition

We require lambda support in the compiler since Qt 5.7, so drop
the guard macro.

Apart from MSVC 2013, all our supported compiler also support
char16_t:

- VxWorks uses GCC 4.8, which supports char16_t since 4.5.
- ICC supports char16_t since v12.1; we require v14+.
- ONX uses a GCC which supports char16_t in the compiler,
  but is equipped with a stdlib that does not sport char16_t
  support, which is why we revoke its Q_C_UNICODE_STRINGS in
  qcompilerdetection.h. But we don't need stdlib support, we
  only need the core language feature.
  This is the only platform where this patch actually changes
  something. It removes the, rather unfair, pessimization of
  the platform which could have supported the static-storage-
  duration implementation of QStringLiteral instead of the
  fallback, which uses dynamic memory (QString::fromUtf8()).
- GCC and Clang support char16_t since 4.5 and 3.0, resp.,
  which is far below our minimum compiler requirements in
  effect since Qt 5.7.
- On Windows, MSVC supports char16_t since 2015 only, and we
  still support 2013, but on Windows, wchar_t is the same size
  as char16_t, so instead of u"str" we can fall back to L"str".

So simplify the implementation of QStringLiteral by assuming
that all these feature are present, adding noexcept to the
lambda to make noexcept() queries return true.

This allows us to guarantee:

[ChangeLog][QtCore] QStringLiteral is now guaranteed to be
evaluated at compile-time. The last platform (QNX) to use the
QString::fromUtf8() fallback has been ported to allocate the
string data statically, too.

Change-Id: I7920d7a77001e5c5550e7c7d57ceb7c51c9eb443
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2017-02-09 10:09:09 +01:00
parent 04d6495bf7
commit bf2160e72c

View File

@ -144,15 +144,10 @@ typedef QLatin1String QLatin1Literal;
typedef QTypedArrayData<ushort> QStringData;
#if defined(Q_COMPILER_UNICODE_STRINGS)
#define QT_UNICODE_LITERAL_II(str) u"" str
typedef char16_t qunicodechar;
#elif defined(Q_OS_WIN) \
|| (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) \
|| (!defined(__SIZEOF_WCHAR_T__) && defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536))
// wchar_t is 2 bytes
#if defined(Q_OS_WIN) && !defined(Q_COMPILER_UNICODE_STRINGS)
// fall back to wchar_t if the a Windows compiler does not
// support Unicode string literals, assuming wchar_t is 2 bytes
// on that platform (sanity-checked by static_assert further below)
#if defined(Q_CC_MSVC)
# define QT_UNICODE_LITERAL_II(str) L##str
@ -162,21 +157,22 @@ typedef char16_t qunicodechar;
typedef wchar_t qunicodechar;
#else
// all our supported compilers support Unicode string literals,
// even if their Q_COMPILER_UNICODE_STRING has been revoked due
// to lacking stdlib support. But QStringLiteral only needs the
// core language feature, so just use u"" here unconditionally:
#define QT_NO_UNICODE_LITERAL
typedef ushort qunicodechar;
#define QT_UNICODE_LITERAL_II(str) u"" str
typedef char16_t qunicodechar;
#endif
Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2,
"qunicodechar must typedef an integral type of size 2");
#ifndef QT_NO_UNICODE_LITERAL
# define QT_UNICODE_LITERAL(str) QT_UNICODE_LITERAL_II(str)
# if defined(Q_COMPILER_LAMBDA)
# define QStringLiteral(str) \
([]() -> QString { \
#define QT_UNICODE_LITERAL(str) QT_UNICODE_LITERAL_II(str)
#define QStringLiteral(str) \
([]() Q_DECL_NOEXCEPT -> QString { \
enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
static const QStaticStringData<Size> qstring_literal = { \
Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \
@ -187,17 +183,6 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2,
}()) \
/**/
# endif
#endif // QT_NO_UNICODE_LITERAL
#ifndef QStringLiteral
// no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t
// fallback, return a temporary QString
// source code is assumed to be encoded in UTF-8
# define QStringLiteral(str) QString::fromUtf8("" str "", sizeof(str) - 1)
#endif
#define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
{ Q_REFCOUNT_INITIALIZE_STATIC, size, 0, 0, offset } \
/**/