Using compiler plugins
l.lunak at suse.cz
Fri Aug 3 06:05:23 PDT 2012
as a followup to building LO with the Clang compiler, I tried to have a look
at writing plugins for it during the last SUSE Hackweek. It turned out to be
quite straightforward, after one gets into it, and it seems to open
possibilities to do quite powerful things.
As a demo, attached is a plugin that can do two actions:
- Convert postfix operator++ to prefix operator++ (I think we have an EasyHack
for this). This can be run as a separate step from compilation, it analyzes
the given source file, checks where the return value fo the expression is
unused, in which case it modifies the source and prints it out.
- Check arguments to
com::sun::star::lang::XMultiServiceFactory::createInstance() calls. This is
to be run during normal compilation and it could be checked that the argument
is correct (i.e. prevent typos). Unlike manual checking, this can more easily
identify the right places in the code (and, once written, it's not
manual :) ).
If you have clang including devel files installed, it should be enough to
just run make in the demo, which will also run the two actions on two testing
files (but I've also run them manually on some real LO code).
It's just a proof of concept, but I think it shows the possibilities.
Rewriting actions could be used to handle presumably quite a number of
EasyHack-like changes. Whether that would be done in less time and could be
more complicated depends on what the changes exactly would be. Something like
renaming a class is probably much better done with plain sed, and way too ...
sophisticated changes would be probably easier to do manually rather than
trying to find the AST manipulations, but I think there are still enough
changes that would be better done this way. And this could also allow more
LO4 changes than we'd be able to do manually.
The checking actions could be normally incorporated into our build when one
builds with Clang, and Clang-based tinderbox(es) could check it for the rest
Upstream ccache and icecream both have a working support for this in their
repositories (and my SUSE packages in home:llunak:clang have it too).
In case somebody asks, I haven't really looked into GCC plugins, but I doubt
the support there matches Clang's. I think GCC does not have any support for
rewriting of the source code, and GCC plugins use C API as opposed to Clang's
C++ API (which is reasonably easy, 'clang++ a.cpp -Xclang -ast-dump' gives an
overview of the AST and browsing clang/include/clang/AST/ gives the API for
So, the remaining things to do would be:
- See if we actually want to use this in practice (I guess yes :) )
- Integrate with our build system. Should be fairly easy, Makefile to build
the plugin, gbuild changes to use it, gbuild changes to make it easy to
figure out exact compilation commands for each .cxx and a script to use a
- Find out what we can actually use it for. Some EasyHacks are a good start,
but the list might get quite long.
l.lunak at suse.cz
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 6537 bytes
Desc: not available
More information about the LibreOffice