summaryrefslogtreecommitdiffstats
path: root/src/tools
diff options
context:
space:
mode:
authorMartin Storsjö <martin@martin.st>2017-11-24 10:10:23 +0200
committerMartin Storsjö <martin@martin.st>2018-01-09 18:36:46 +0000
commit74118a4784569046d5fdf5e08c99f8b1b43e9710 (patch)
treef1e51ebf76e6397445900c6455446f9d83a76e79 /src/tools
parent50deb8cf70f61e21fb0c35182341477af11adbc1 (diff)
moc: Initialize staticMetaObject with the highest user-settable priority
The referenced static meta object for the superclass might be in a different DLL. In this case, the whole QMetaObject can't be initialized all via preinitialized data in the data section of the binary, but must run code at runtime to fill in the value of the dllimported pointer. In these cases, both GCC and MSVC initialize as much as possible statically, while only filling in the dllimported values (QMetaObject::d::superdata) at runtime. Clang, on the other side, initializes the whole struct at runtime if some part of it needs runtime initialization, leaving the struct completely uninitialized before constructors are run. In C++, there are no guarantees for in what order constructors in different translation units are executed. This in particular means that there are no guarantees as to whether qRegisterWidgetsVariant() in qwidgetsvariants.cpp runs before or after the runtime initialization of QWidget::staticMetaObject. With GCC and MSVC, this doesn't seem to have mattered since only the superdata pointer of the staticMetaObject was uninitialized - everything else was initialized, and the superdata pointer doesn't seem to be accessed during qRegisterWidgetsVariant. With clang, the whole staticMetaObject is uninitialized, unless the staticMetaObject has been initialized before (and the initialization order is undefined). By setting a manual priority (which is a GCC extension that also clang supports) for the staticMetaObjects, we can be sure that these are initialized before the actual explicit constructor invocations (without any explicit initialization priority) that can access the staticMetaObjects. Change-Id: I64a82f12d690528567509791bae088b6304e189b Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/moc/generator.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp
index 139f7328af..1e17e00050 100644
--- a/src/tools/moc/generator.cpp
+++ b/src/tools/moc/generator.cpp
@@ -523,9 +523,9 @@ void Generator::generateCode()
// Finally create and initialize the static meta object
//
if (isQt)
- fprintf(out, "const QMetaObject QObject::staticQtMetaObject = {\n");
+ fprintf(out, "QT_INIT_METAOBJECT const QMetaObject QObject::staticQtMetaObject = {\n");
else
- fprintf(out, "const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
+ fprintf(out, "QT_INIT_METAOBJECT const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
if (isQObject)
fprintf(out, " { nullptr, ");