This is an archive of the discontinued LLVM Phabricator instance.

Qt (version 4 or 5) signal/method checker
Needs ReviewPublic

Authored by Dushistov on Nov 11 2015, 3:30 PM.

Details

Summary

In Qt 4/5 it is possible connect classes methods in such way:

connect(ObjectPointer1, SIGNAL(methodOfObject1()), ObjectPointer2, SLOT(methodOfObject2());

and when you call this method -> ObjectPointer1->methodOfObject1() method of ObjectPointer2->methodOfObject2() will be called automatically.

The only problem that you can check of correctness of method name and its argument only
at runtime (more details can be found here http://doc.qt.io/qt-4.8/signalsandslots.html).

So I implement this checker to help re factoring large Qt based projects.

Note: Qt 4 have only such method to connect signals and slots.
Qt 5 uses two methods, described above and based on function pointers, but because of SIGNAL/SLOT syntax with disadvantages
have also advantages (see http://doc.qt.io/qt-5/signalsandslots-syntaxes.html) sometime Qt5 also
have not compile time checked signal/slot connection.

What this checker do exactly:
1)It checks that such methods with such signatures exists in both classes
2)It checks that slot subset of parameters match the first parameters of signal
3)It checks that signature of method written in proper way (normalized), this improve performance,
see https://marcmutz.wordpress.com/effective-qt/prefer-to-use-normalised-signalslot-signatures/ and http://doc.qt.io/qt-5/qmetaobject.html#normalizedSignature

Diff Detail

Event Timeline

Dushistov updated this revision to Diff 39980.Nov 11 2015, 3:30 PM
Dushistov retitled this revision from to Qt (version 4 or 5) signal/method checker.
Dushistov updated this object.
Dushistov added a subscriber: cfe-commits.
jroelofs added inline comments.
lib/StaticAnalyzer/Checkers/QtSignalSlotChecker.cpp
114

Why are you throwing away all the work that the parser did to build up the type signature as a QualType, pretty-printing it, and the re-parsing it by hand? ISTM it would be much better if qtNormalizeSignature had this type signature: QualType qtNormalizeSignature(QualType).

Dushistov added inline comments.Nov 11 2015, 4:43 PM
lib/StaticAnalyzer/Checkers/QtSignalSlotChecker.cpp
114

Looks like reviews have no SMTP interface, so duplicate answer here:

First of all this code compare two things:
1)QualType from clang,
2)string that I get from
connect(obj1, "1f(unsgined int)", obj2, "2g(uint)");

^                          ^                                                                                                   
here                       and here

so to simplify things I need compare QualType and data from
StringLiteral.

The second thing, that should be menntioned,that
"Qt normalization" and converting output to string
(this normalization used by Qt when it compare at runtime
that signal match slot)

not match exactly to already existing in clang
"print type functionality":

  • it remove spaces every where, where possible
  • it print "uint" instead of "unsigned int"

so I glad to have inteface like this
QualType qtNormalizeSignature(QualType)
but to use such interface I need convert StringLiteral conent
to function signature, in other words run parser inside parser,
plus I have to hack PrintType from AST to make possible
print function signature in Qt way.

There are dedicated project to Qt checks: https://github.com/KDE/clazy.