From d12d2949d1e4ac08a47928ef27bc45459b3fb104 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Mon, 31 Jul 2017 17:20:53 +0300 Subject: [PATCH] uic: Add -no-stringliteral option ... and use it when building shared libraries and plugins. It prevents application crashes in cases when libraries and plugins are unloaded and their strings are still used by the main application. Task-number: QTBUG-51602 Change-Id: I4af79183f18c5ed6142d55af02a36fe4334f3fee Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira Reviewed-by: Friedemann Kleint --- mkspecs/features/uic.prf | 4 +++- qmake/doc/src/qmake-manual.qdoc | 8 +++++++ src/tools/uic/cpp/cppwriteinitialization.cpp | 23 ++++++++++++++++---- src/tools/uic/cpp/cppwriteinitialization.h | 3 +++ src/tools/uic/main.cpp | 5 +++++ src/tools/uic/option.h | 2 ++ src/tools/uic/utils.h | 13 ----------- 7 files changed, 40 insertions(+), 18 deletions(-) diff --git a/mkspecs/features/uic.prf b/mkspecs/features/uic.prf index ca8283920a..134d3b4acb 100644 --- a/mkspecs/features/uic.prf +++ b/mkspecs/features/uic.prf @@ -3,8 +3,10 @@ qtPrepareTool(QMAKE_UIC, uic, _DEP) isEmpty(UI_DIR):UI_DIR = . isEmpty(QMAKE_MOD_UIC):QMAKE_MOD_UIC = ui_ +contains(TEMPLATE, .*lib):dll: QMAKE_UIC_FLAGS += -no-stringliteral + uic.depends += $$QMAKE_UIC_EXE -uic.commands = $$QMAKE_UIC ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT} +uic.commands = $$QMAKE_UIC $$QMAKE_UIC_FLAGS ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT} uic.depend_command = $$QMAKE_UIC_DEP -d ${QMAKE_FILE_IN} uic.output = $$UI_DIR/$${QMAKE_MOD_UIC}${QMAKE_FILE_BASE}$${first(QMAKE_EXT_H)} uic.input = FORMS diff --git a/qmake/doc/src/qmake-manual.qdoc b/qmake/doc/src/qmake-manual.qdoc index 27abec596f..c9759fa517 100644 --- a/qmake/doc/src/qmake-manual.qdoc +++ b/qmake/doc/src/qmake-manual.qdoc @@ -2266,6 +2266,14 @@ For more information, see \l {Expressing Supported iOS Versions}. + \section1 QMAKE_UIC_FLAGS + + This variable is used to customize the list of options passed to the + \l{uic}{User Interface Compiler} in each of the build rules where it is + used. For example, \c{-no-stringliteral} can be passed to use QLatin1String + instead of QStringLiteral in generated code (which is the default for + dynamic libraries). + \section1 QMAKE_WATCHOS_DEPLOYMENT_TARGET \note This variable is used on the watchOS platform only. diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index cc7e8fc9e3..e599bc8f08 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -475,6 +475,21 @@ WriteInitialization::WriteInitialization(Uic *uic) : { } +QString WriteInitialization::writeString(const QString &s, const QString &indent) const +{ + unsigned flags = 0; + const QString ret = fixString(s, indent, &flags); + if (flags & Utf8String) + return QLatin1String("QString::fromUtf8(") + ret + QLatin1Char(')'); + // MSVC cannot concat L"foo" "bar" (C2308: concatenating mismatched strings), + // use QLatin1String instead (all platforms to avoid cross-compiling issues). + if (flags & MultiLineString) + return QLatin1String("QLatin1String(") + ret + QLatin1Char(')'); + const QLatin1String stringWrapper = m_uic->option().stringLiteral ? + QLatin1String("QStringLiteral(") : QLatin1String("QLatin1String("); + return stringWrapper + ret + QLatin1Char(')'); +} + void WriteInitialization::acceptUI(DomUI *node) { m_actionGroupChain.push(0); @@ -1601,10 +1616,10 @@ QString WriteInitialization::writeFontProperties(const DomFont *f) } // Post 4.4 write resource icon -static void writeResourceIcon(QTextStream &output, - const QString &iconName, - const QString &indent, - const DomResourceIcon *i) +void WriteInitialization::writeResourceIcon(QTextStream &output, + const QString &iconName, + const QString &indent, + const DomResourceIcon *i) const { if (i->hasElementNormalOff()) output << indent << iconName << ".addFile(" << writeString(i->elementNormalOff()->text(), indent) << ", QSize(), QIcon::Normal, QIcon::Off);\n"; diff --git a/src/tools/uic/cpp/cppwriteinitialization.h b/src/tools/uic/cpp/cppwriteinitialization.h index 187ba8378e..b26f51875d 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.h +++ b/src/tools/uic/cpp/cppwriteinitialization.h @@ -139,6 +139,8 @@ struct WriteInitialization : public TreeWalker private: static QString domColor2QString(const DomColor *c); + QString writeString(const QString &s, const QString &indent) const; + QString iconCall(const DomProperty *prop); QString pixCall(const DomProperty *prop) const; QString pixCall(const QString &type, const QString &text) const; @@ -228,6 +230,7 @@ private: private: QString writeFontProperties(const DomFont *f); + void writeResourceIcon(QTextStream &output, const QString &iconName, const QString &indent, const DomResourceIcon *i) const; QString writeIconProperties(const DomResourceIcon *i); QString writeSizePolicy(const DomSizePolicy *sp); QString writeBrushInitialization(const DomBrush *brush); diff --git a/src/tools/uic/main.cpp b/src/tools/uic/main.cpp index 3599470403..46a1e66bcc 100644 --- a/src/tools/uic/main.cpp +++ b/src/tools/uic/main.cpp @@ -75,6 +75,10 @@ int runUic(int argc, char *argv[]) noImplicitIncludesOption.setDescription(QStringLiteral("Disable generation of #include-directives.")); parser.addOption(noImplicitIncludesOption); + QCommandLineOption noStringLiteralOption(QStringList() << QStringLiteral("s") << QStringLiteral("no-stringliteral")); + noStringLiteralOption.setDescription(QStringLiteral("Use QLatin1String instead of QStringLiteral in generated code.")); + parser.addOption(noStringLiteralOption); + QCommandLineOption postfixOption(QStringLiteral("postfix")); postfixOption.setDescription(QStringLiteral("Postfix to add to all generated classnames.")); postfixOption.setValueName(QStringLiteral("postfix")); @@ -107,6 +111,7 @@ int runUic(int argc, char *argv[]) driver.option().outputFile = parser.value(outputOption); driver.option().headerProtection = !parser.isSet(noProtOption); driver.option().implicitIncludes = !parser.isSet(noImplicitIncludesOption); + driver.option().stringLiteral = !parser.isSet(noStringLiteralOption); driver.option().idBased = parser.isSet(idBasedOption); driver.option().postfix = parser.value(postfixOption); driver.option().translateFunction = parser.value(translateOption); diff --git a/src/tools/uic/option.h b/src/tools/uic/option.h index a5b14abc5f..c7278393fb 100644 --- a/src/tools/uic/option.h +++ b/src/tools/uic/option.h @@ -51,6 +51,7 @@ struct Option unsigned int limitXPM_LineLength : 1; unsigned int implicitIncludes: 1; unsigned int idBased: 1; + unsigned int stringLiteral: 1; Generator generator; QString inputFile; @@ -76,6 +77,7 @@ struct Option limitXPM_LineLength(0), implicitIncludes(1), idBased(0), + stringLiteral(1), generator(CppGenerator), prefix(QLatin1String("Ui_")) { indent.fill(QLatin1Char(' '), 4); } diff --git a/src/tools/uic/utils.h b/src/tools/uic/utils.h index 7c5918cab9..18b361fb81 100644 --- a/src/tools/uic/utils.h +++ b/src/tools/uic/utils.h @@ -107,19 +107,6 @@ inline QString fixString(const QString &str, const QString &indent, return rc; } -inline QString writeString(const QString &s, const QString &indent) -{ - unsigned flags = 0; - const QString ret = fixString(s, indent, &flags); - if (flags & Utf8String) - return QLatin1String("QString::fromUtf8(") + ret + QLatin1Char(')'); - // MSVC cannot concat L"foo" "bar" (C2308: concatenating mismatched strings), - // use QLatin1String instead (all platforms to avoid cross-compiling issues). - if (flags & MultiLineString) - return QLatin1String("QLatin1String(") + ret + QLatin1Char(')'); - return QLatin1String("QStringLiteral(") + ret + QLatin1Char(')'); -} - inline QHash propertyMap(const QList &properties) { QHash map;