aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmltc
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2021-10-20 11:14:52 +0200
committerAndrei Golubev <andrei.golubev@qt.io>2021-11-16 09:02:57 +0100
commit6ec7953257ef1155aef8a0a4b8379205ee850283 (patch)
tree1dac0463d8500bb3a90c37095a09890607423ec7 /tools/qmltc
parent88dfbe0deaa780f1cb5793d644341367d02e3073 (diff)
qmltc: support user-specified namespaces in the generated code
Already during the prototyping phase, conflicts in class names were encountered within Qt code base. Those could be avoided by namespaces. Initial qmltc logic was using meaningless "q_qmltc" namespace, so let's improve that by allowing user-specified namespaces + making Qt's own QML files (compiled to C++) being available under QT_NAMESPACE. The latter is achieved by providing (and using) the internal version of a qmltc-invoking function Task-number: QTBUG-84368 Task-number: QTBUG-96040 Change-Id: I99cdf1baba8838c093b6b469f6744869f72af093 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tools/qmltc')
-rw-r--r--tools/qmltc/main.cpp6
-rw-r--r--tools/qmltc/qmltccodewriter.cpp29
-rw-r--r--tools/qmltc/qmltccodewriter.h4
-rw-r--r--tools/qmltc/qmltccompiler.cpp1
-rw-r--r--tools/qmltc/qmltccompiler.h1
-rw-r--r--tools/qmltc/qmltcoutputir.h1
6 files changed, 30 insertions, 12 deletions
diff --git a/tools/qmltc/main.cpp b/tools/qmltc/main.cpp
index 801200839c..f6feb9ce2d 100644
--- a/tools/qmltc/main.cpp
+++ b/tools/qmltc/main.cpp
@@ -91,6 +91,11 @@ int main(int argc, char **argv)
QCoreApplication::translate("main", "resource path")
};
parser.addOption(resourcePathOption);
+ QCommandLineOption namespaceOption {
+ u"namespace"_qs, QCoreApplication::translate("main", "Namespace of the generated C++ code"),
+ QCoreApplication::translate("main", "namespace")
+ };
+ parser.addOption(namespaceOption);
parser.process(app);
@@ -160,6 +165,7 @@ int main(int argc, char **argv)
QmltcCompilerInfo info;
info.outputCppFile = outputCppFile;
info.outputHFile = outputHFile;
+ info.outputNamespace = parser.value(namespaceOption);
info.resourcePath = parser.value(resourcePathOption);
QQmlJSImporter importer { importPaths, /* resource file mapper */ nullptr };
diff --git a/tools/qmltc/qmltccodewriter.cpp b/tools/qmltc/qmltccodewriter.cpp
index 9670f64667..7ce489c899 100644
--- a/tools/qmltc/qmltccodewriter.cpp
+++ b/tools/qmltc/qmltccodewriter.cpp
@@ -96,6 +96,7 @@ static std::pair<QString, QString> functionSignatures(const QmltcMethodBase &met
void QmltcCodeWriter::writeGlobalHeader(QmltcOutputWrapper &code, const QString &sourcePath,
const QString &hPath, const QString &cppPath,
+ const QString &outNamespace,
const QSet<QString> &requiredCppIncludes)
{
Q_UNUSED(cppPath);
@@ -137,21 +138,26 @@ void QmltcCodeWriter::writeGlobalHeader(QmltcOutputWrapper &code, const QString
code.rawAppendToCpp(u"#include <private/qobject_p.h>"); // NB: for private properties
code.rawAppendToCpp(u"#include <private/qqmlglobal_p.h>"); // QQml_setParent_noEvent()
- code.rawAppendToHeader(u""); // blank line
- code.rawAppendToHeader(u"namespace q_qmltc {");
-
code.rawAppendToCpp(u""); // blank line
code.rawAppendToCpp(u"QT_USE_NAMESPACE // avoid issues with QT_NAMESPACE");
- code.rawAppendToCpp(u"namespace q_qmltc {");
+ if (!outNamespace.isEmpty()) {
+ code.rawAppendToHeader(u""); // blank line
+ code.rawAppendToHeader(u"namespace %1 {"_qs.arg(outNamespace));
+ code.rawAppendToCpp(u""); // blank line
+ code.rawAppendToCpp(u"namespace %1 {"_qs.arg(outNamespace));
+ }
}
-void QmltcCodeWriter::writeGlobalFooter(QmltcOutputWrapper &code, const QString &sourcePath)
+void QmltcCodeWriter::writeGlobalFooter(QmltcOutputWrapper &code, const QString &sourcePath,
+ const QString &outNamespace)
{
- code.rawAppendToCpp(u"} // namespace q_qmltc");
- code.rawAppendToCpp(u""); // blank line
+ if (!outNamespace.isEmpty()) {
+ code.rawAppendToCpp(u"} // namespace %1"_qs.arg(outNamespace));
+ code.rawAppendToCpp(u""); // blank line
+ code.rawAppendToHeader(u"} // namespace %1"_qs.arg(outNamespace));
+ code.rawAppendToHeader(u""); // blank line
+ }
- code.rawAppendToHeader(u"} // namespace q_qmltc");
- code.rawAppendToHeader(u""); // blank line
code.rawAppendToHeader(u"#endif // %1_H"_qs.arg(urlToMacro(sourcePath)));
code.rawAppendToHeader(u""); // blank line
}
@@ -179,7 +185,8 @@ static void writeToFile(const QString &path, const QByteArray &data)
void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcProgram &program)
{
- writeGlobalHeader(code, program.url, program.hPath, program.cppPath, program.includes);
+ writeGlobalHeader(code, program.url, program.hPath, program.cppPath, program.outNamespace,
+ program.includes);
// TODO: keep the "NOT IMPLEMENTED" as long as we don't actually compile
// useful code
code.rawAppendToHeader(u"/* QMLTC: NOT IMPLEMENTED */");
@@ -191,7 +198,7 @@ void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcProgram &progra
// write all the types and their content
for (const QmltcType &type : qAsConst(program.compiledTypes))
write(code, type);
- writeGlobalFooter(code, program.url);
+ writeGlobalFooter(code, program.url, program.outNamespace);
writeToFile(program.hPath, code.code().header.toUtf8());
writeToFile(program.cppPath, code.code().cpp.toUtf8());
diff --git a/tools/qmltc/qmltccodewriter.h b/tools/qmltc/qmltccodewriter.h
index b34f198c88..a6f38da0a9 100644
--- a/tools/qmltc/qmltccodewriter.h
+++ b/tools/qmltc/qmltccodewriter.h
@@ -40,8 +40,10 @@ struct QmltcCodeWriter
{
static void writeGlobalHeader(QmltcOutputWrapper &code, const QString &sourcePath,
const QString &hPath, const QString &cppPath,
+ const QString &outNamespace,
const QSet<QString> &requiredCppIncludes);
- static void writeGlobalFooter(QmltcOutputWrapper &code, const QString &sourcePath);
+ static void writeGlobalFooter(QmltcOutputWrapper &code, const QString &sourcePath,
+ const QString &outNamespace);
static void write(QmltcOutputWrapper &code, const QmltcProgram &program);
static void write(QmltcOutputWrapper &code, const QmltcType &type);
static void write(QmltcOutputWrapper &code, const QmltcEnum &enumeration);
diff --git a/tools/qmltc/qmltccompiler.cpp b/tools/qmltc/qmltccompiler.cpp
index a2e1f47089..4654ecfd20 100644
--- a/tools/qmltc/qmltccompiler.cpp
+++ b/tools/qmltc/qmltccompiler.cpp
@@ -65,6 +65,7 @@ void QmltcCompiler::compile(const QmltcCompilerInfo &info)
program.url = m_url;
program.cppPath = m_info.outputCppFile;
program.hPath = m_info.outputHFile;
+ program.outNamespace = m_info.outputNamespace;
program.compiledTypes = compiledTypes;
program.includes = m_visitor->cppIncludeFiles();
diff --git a/tools/qmltc/qmltccompiler.h b/tools/qmltc/qmltccompiler.h
index bdd56a7579..e59cc116f4 100644
--- a/tools/qmltc/qmltccompiler.h
+++ b/tools/qmltc/qmltccompiler.h
@@ -45,6 +45,7 @@ struct QmltcCompilerInfo
{
QString outputCppFile;
QString outputHFile;
+ QString outputNamespace;
QString resourcePath;
};
diff --git a/tools/qmltc/qmltcoutputir.h b/tools/qmltc/qmltcoutputir.h
index e81caf14ed..c299f283a8 100644
--- a/tools/qmltc/qmltcoutputir.h
+++ b/tools/qmltc/qmltcoutputir.h
@@ -126,6 +126,7 @@ struct QmltcProgram
QString url; // QML file url
QString cppPath; // C++ output .cpp path
QString hPath; // C++ output .h path
+ QString outNamespace;
QSet<QString> includes; // non-default C++ include files
QList<QmltcType> compiledTypes; // all QML types that are compiled to C++