Bug 389994

Summary: new-check: Avoid explicit qRegisterMetaType / Q_DECLARE_METATYPE
Product: [Developer tools] clazy Reporter: aklitzing
Component: generalAssignee: Unassigned bugs <unassigned-bugs-null>
Status: CONFIRMED ---    
Severity: wishlist CC: smartins
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Example for QMetaObject::RegisterMethodArgumentMetaType

Description aklitzing 2018-02-07 08:20:53 UTC
It would be helpful if clazy could check if a class is able to register itself in Qt's meta-object-system.

Sometimes it is a forgotten include (forward declared) or something like this that the moc cannot see the whole class and won't generate the "qRegisterMetaType" itself. Most times the developer will add qRegisterMetaType somewhere because Qt said it at runtime. But clazy could help here to warn if a class is used in signal/slot and moc is not able to generate the registration as it is the better way to let moc do the registration.

moc can generate q qt_static_metacall with stuff like this:

    } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
        switch (_id) {
        default: *reinterpret_cast<int*>(_a[0]) = -1; break;
        case 0:
            switch (*reinterpret_cast<int*>(_a[1])) {
            default: *reinterpret_cast<int*>(_a[0]) = -1; break;
            case 1:
                *reinterpret_cast<int*>(_a[0]) = qRegisterMetaType< QSharedPointer<CUSTOM_CLASS> >(); break;
            }
            break;


moc has some more possibilities to detect own types like Q_PROPERTY usage.

See:
https://code.qt.io/cgit/qt/qtbase.git/tree/src/tools/moc/generator.cpp
https://woboq.com/blog/qmetatype-knows-your-types.html
Comment 1 Sergio Martins 2018-03-24 12:49:30 UTC
Sounds interesting!

Can you attach a sample .cpp file where running moc on it would generate the qRegisterMEtaType ?
Comment 2 aklitzing 2018-04-24 10:14:42 UTC
Created attachment 112215 [details]
Example for QMetaObject::RegisterMethodArgumentMetaType
Comment 3 aklitzing 2018-04-24 10:17:26 UTC
This allows moc to generate "QMetaObject::RegisterMethodArgumentMetaType" with qRegisterMetaType. If you use a forward-declaration the moc is not enable to recognize it and won't generate qRegisterMetaType.

Sadly if you remove "QObject" of DeviceListEntry it is the same problem. Should be available for Q_GADGET, too. That should work but moc needs to be fixed for that. :-(