In the previous episode we presented how to uncover 32 Qt best practices at compile time with clazy. Today it's time to show 5 more and other new goodies present in the freshly released clazy v1.2.
New checks
1. connect-not-normalized
Warns when the content of SIGNAL()
, SLOT()
, Q_ARG()
and Q_RETURN_ARG()
is not normalized. Using normalized signatures allows to avoid unneeded memory allocations.
Example:
See QMetaObject::normalizedSignature()
for more information.
2. returning-data-from-temporary
Warns when returning the data from a QByteArray
that will soon be destroyed. Accessing such data usually results in a crash. For example:
3. install-event-filter
Warns on potential misuse of QObject::installEventFilter()
.
To install an event filter on obj1
you should call obj1->installEventFilter(this)
, but sometimes you'll write installEventFilter(obj1)
by mistake, which compiles fine.
4. qcolor-from-literal
Warns when a QColor
is being constructed from a string literal such as "#RRGGBB".
This is less performant than calling the ctor that takes int
s, since it creates temporary QString
s.
Example:
5. strict-iterators
Warns when iterator
objects are implicitly cast to const_iterator
.
This is mostly equivalent to passing -DQT_STRICT_ITERATORS
to the compiler, except that it also works for QString
. This prevents detachments but also caches subtle bugs such as:
New features
1. clazy-standalone
Many people asked for clang-tidy support. This can't be done due to clang-tidy not supporting plugins yet. Thus, clazy-standalone was born. It works in a similar way as clang-tidy, by operating on a json compilation database instead of being loaded as a compiler plugin. It can be invoked as:
2. ASTMatchers
If you want to contribute a new check to clazy you can now use the AST Matchers API instead of the low level AST way. AST Matchers are gaining popularity and used in many clang tooling already.
3. Pre-built Windows binaries
Since it's an hassle to build LLVM on your own on Windows, we've made a pre-built ready to use
clazy v1.2 package.
The End
That's all folks. You can get clazy from https://phabricator.kde.org/source/clazy/. Be sure to check the README.md file, it should have answers to most (clazy related) questions.
Please try it and report bugs!
6 Comments
27 - Jul - 2017
frederic
"Warns when a QColor is being constructed from a string literal such as “#RRGGBB”. This is less performant"
I'm wondering how did come about? How is has this become flagged as a big performance issue to warn about?
I mean.. in UI there is tons and tons of things that you do for readabilty, code searchability and maintenance. If you use Qt's CSS support, it makes sense to specify colors with the same notation.
Under which scenario did constructing QColor become a performance bottleneck people need to avoid?
One could avoid all of QML and do everythign in C++/QWidget, or avoid the CSS part of Qt to avoid "less performant" code, or every using QString and using const char* instead. There is no end "less performant" alternatives.
27 - Jul - 2017
Sérgio Martins
Although, to be fair, many of clazy's checks have been seen under a profiler too, being bottlenecks.
You can disable the qcolor check if it annoys you, real performance benefits can add up when you enable all 40-50 checks.
25 - Jan - 2018
zack
I have Visual Studio 2013, how can I use clazy?
25 - Jan - 2018
Sérgio Martins
I've never tried it. Clazy supports anything clang supports, if you can get clang >= 3.8 building with VS 2013 it will probably work.
26 - Jan - 2018
zack
ahh I think you have to clarify your tool better. I though its just some standalone tool which I can run on every source code.
But it is a compiler replacement for clang.
26 - Jan - 2018
Sérgio Martins
It comes in two forms: a plugin for clang, and clazy-standalone.exe, which you can run on any source that's buildable with clang. It's explained in the readme.