Bug 389994 - new-check: Avoid explicit qRegisterMetaType / Q_DECLARE_METATYPE
Summary: new-check: Avoid explicit qRegisterMetaType / Q_DECLARE_METATYPE
Status: CONFIRMED
Alias: None
Product: clazy
Classification: Developer tools
Component: general (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: Unassigned bugs mailing-list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-02-07 08:20 UTC by aklitzing
Modified: 2018-04-24 10:17 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
Example for QMetaObject::RegisterMethodArgumentMetaType (359 bytes, text/plain)
2018-04-24 10:14 UTC, aklitzing
Details

Note You need to log in before you can comment on or make changes to this bug.
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. :-(