diff --git a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp index 672242d665..0c35632e7b 100644 --- a/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp +++ b/src/tools/qdbusxml2cpp/qdbusxml2cpp.cpp @@ -37,6 +37,7 @@ static bool verbose; static bool includeMocs; static QString commandLine; static QStringList includes; +static QStringList globalIncludes; static QStringList wantedInterfaces; static const char includeList[] = @@ -459,6 +460,12 @@ static void writeProxy(const QString &filename, const QDBusIntrospection::Interf cs << "#include \"" << include << "\"" << Qt::endl; } + for (const QString &include : qAsConst(globalIncludes)) { + hs << "#include <" << include << ">" << Qt::endl; + if (headerName.isEmpty()) + cs << "#include <" << include << ">" << Qt::endl; + } + hs << Qt::endl; if (cppName != headerName) { @@ -772,6 +779,12 @@ static void writeAdaptor(const QString &filename, const QDBusIntrospection::Inte cs << "#include \"" << include << "\"" << Qt::endl; } + for (const QString &include : qAsConst(globalIncludes)) { + hs << "#include <" << include << ">" << Qt::endl; + if (headerName.isEmpty()) + cs << "#include <" << include << ">" << Qt::endl; + } + if (cppName != headerName) { if (!headerName.isEmpty() && headerName != "-"_L1) cs << "#include \"" << headerName << "\"" << Qt::endl; @@ -1058,9 +1071,13 @@ int main(int argc, char **argv) parser.addOption(classNameOption); QCommandLineOption addIncludeOption(QStringList() << QStringLiteral("i") << QStringLiteral("include"), - QStringLiteral("Add #include to the output"), QStringLiteral("filename")); + QStringLiteral("Add #include \"filename\" to the output"), QStringLiteral("filename")); parser.addOption(addIncludeOption); + QCommandLineOption addGlobalIncludeOption(QStringList() << QStringLiteral("I") << QStringLiteral("global-include"), + QStringLiteral("Add #include to the output"), QStringLiteral("filename")); + parser.addOption(addGlobalIncludeOption); + QCommandLineOption adapterParentOption(QStringLiteral("l"), QStringLiteral("When generating an adaptor, use as the parent class"), QStringLiteral("classname")); parser.addOption(adapterParentOption); @@ -1086,6 +1103,7 @@ int main(int argc, char **argv) adaptorFile = parser.value(adapterCodeOption); globalClassName = parser.value(classNameOption); includes = parser.values(addIncludeOption); + globalIncludes = parser.values(addGlobalIncludeOption); parentClassName = parser.value(adapterParentOption); includeMocs = parser.isSet(mocIncludeOption); skipNamespaces = parser.isSet(noNamespaceOption); diff --git a/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp b/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp index 6cbea1b328..db0c8af0fd 100644 --- a/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp +++ b/tests/auto/tools/qdbusxml2cpp/tst_qdbusxml2cpp.cpp @@ -19,6 +19,8 @@ private slots: void initTestCase_data(); void process_data(); void process(); + void includeStyle_data(); + void includeStyle(); }; struct BasicTypeList { @@ -235,6 +237,53 @@ void tst_qdbusxml2cpp::process() QVERIFY2(output.count(adaptorSearch) == 1, qPrintable(adaptorSearch.pattern() + "\nin\n" + output)); } +void tst_qdbusxml2cpp::includeStyle_data() +{ + QTest::addColumn("isGlobal"); + QTest::addColumn("expected"); + + QTest::newRow("localInclude") << false << QByteArray("#include \"test.hpp\""); + QTest::newRow("globalInclude") << true << QByteArray("#include "); +} + +void tst_qdbusxml2cpp::includeStyle() +{ + QFETCH(bool, isGlobal); + QFETCH(QByteArray, expected); + QFETCH_GLOBAL(QString, commandLineArg); + + // feed it our XML data + static const char xml[] = + "\n" + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE // \n is included + "\n" + " \n" + " \n" + "\n"; + + // Run the tool + const QString binpath = QLibraryInfo::path(QLibraryInfo::BinariesPath); + const QString command = binpath + QLatin1String("/qdbusxml2cpp"); + QProcess process; + process.start(command, QStringList() << commandLineArg << "-" << "-N" << (isGlobal ? "-I" : "-i") << "test.hpp"); + QVERIFY2(process.waitForStarted(), qPrintable(process.errorString())); + + process.write(xml, int(sizeof xml) - 1); + while (process.bytesToWrite()) + QVERIFY2(process.waitForBytesWritten(), qPrintable(process.errorString())); + + process.closeWriteChannel(); + QVERIFY2(process.waitForFinished(), qPrintable(process.errorString())); + + QByteArray errOutput = process.readAllStandardError(); + QVERIFY2(errOutput.isEmpty(), errOutput); + QCOMPARE(process.exitCode(), 0); + + QByteArray fullOutput = process.readAll(); + QVERIFY(!fullOutput.isEmpty()); + QVERIFY(fullOutput.contains(expected)); +} + QTEST_MAIN(tst_qdbusxml2cpp) #include "tst_qdbusxml2cpp.moc"