Fix QString/QByteArray literals for MSVC2010 (compilation of tests).
authorFriedemann Kleint <Friedemann.Kleint@nokia.com>
Tue, 2 Aug 2011 13:58:48 +0000 (15:58 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 2 Aug 2011 15:14:02 +0000 (17:14 +0200)
- Specify return type of QByteArrayLiteral/QStringLiteral lambdas.
- Define QT_UNICODE_LITERAL instead of QT_UNICODE_MARKER.

Change-Id: I8a53506887d2736b093798220b088f645f05e415
Reviewed-on: http://codereview.qt.nokia.com/2514
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
src/corelib/tools/qbytearray.h
src/corelib/tools/qstring.h
tests/auto/qstring/tst_qstring.cpp

index 5cf4c15..b70dba4 100644 (file)
@@ -146,7 +146,7 @@ template<int N> struct QConstByteArrayDataPtr
 
 
 #if defined(Q_COMPILER_LAMBDA)
-#  define QByteArrayLiteral(str) ([]() { \
+#  define QByteArrayLiteral(str) ([]() -> QConstByteArrayDataPtr<sizeof(str) - 1> { \
         enum { Size = sizeof(str) - 1 }; \
         static const QConstByteArrayData<Size> qbytearray_literal = \
         { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, str }; \
index 4471da4..209994d 100644 (file)
@@ -102,7 +102,8 @@ template<int N> struct QConstStringData
     const QStringData str;
     const char16_t data[N + 1];
 };
-#define QT_QSTRING_UNICODE_MARKER   u""
+
+#define QT_UNICODE_LITERAL_II(str) u"" str
 
 #elif defined(Q_OS_WIN) || (defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2) || defined(WCHAR_MAX) && (WCHAR_MAX - 0 < 65536)
 // wchar_t is 2 bytes
@@ -111,7 +112,12 @@ template<int N> struct QConstStringData
     const QStringData str;
     const wchar_t data[N + 1];
 };
-#define QT_QSTRING_UNICODE_MARKER   L""
+
+#if defined(Q_CC_MSVC)
+#    define QT_UNICODE_LITERAL_II(str) L##str
+#else
+#    define QT_UNICODE_LITERAL_II(str) L"" str
+#endif
 
 #else
 template<int N> struct QConstStringData
@@ -121,12 +127,13 @@ template<int N> struct QConstStringData
 };
 #endif
 
-#if defined(QT_QSTRING_UNICODE_MARKER)
+#if defined(QT_UNICODE_LITERAL_II)
+#  define QT_UNICODE_LITERAL(str) QT_UNICODE_LITERAL_II(str)
 # if defined(Q_COMPILER_LAMBDA)
-#  define QStringLiteral(str) ([]() { \
-        enum { Size = sizeof(QT_QSTRING_UNICODE_MARKER str)/2 - 1 }; \
+#  define QStringLiteral(str) ([]() -> QConstStringDataPtr<sizeof(QT_UNICODE_LITERAL(str))/2 - 1> { \
+        enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
         static const QConstStringData<Size> qstring_literal = \
-        { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_QSTRING_UNICODE_MARKER str }; \
+        { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
         QConstStringDataPtr<Size> holder = { &qstring_literal }; \
     return holder; }())
 
@@ -137,9 +144,9 @@ template<int N> struct QConstStringData
 
 #  define QStringLiteral(str) \
     __extension__ ({ \
-        enum { Size = sizeof(QT_QSTRING_UNICODE_MARKER str)/2 - 1 }; \
+        enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \
         static const QConstStringData<Size> qstring_literal = \
-        { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_QSTRING_UNICODE_MARKER str }; \
+        { { Q_REFCOUNT_INITIALIZER(-1), Size, 0, 0, { 0 } }, QT_UNICODE_LITERAL(str) }; \
         QConstStringDataPtr<Size> holder = { &qstring_literal }; \
         holder; })
 # endif
index 8c725fa..f7a725c 100644 (file)
@@ -5110,7 +5110,7 @@ void tst_QString::toUpperLower_icu()
 
 void tst_QString::literals()
 {
-#if defined(QT_QSTRING_UNICODE_MARKER) && (defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU))
+#if defined(QT_UNICODE_LITERAL) && (defined(Q_COMPILER_LAMBDA) || defined(Q_CC_GNU))
     QString str(QStringLiteral("abcd"));
 
     QVERIFY(str.length() == 4);