[cfe-dev] Where to put upcoming refcatoring tools
Stephen Kelly
steveire at gmail.com
Wed Apr 25 06:53:05 PDT 2012
Manuel Klimek wrote:
>> I got the example running and working, so now I will see what more I can
>> learn about the tooling APIs. I've had a read through ASTMatchers.h, so
>> that looks like a good start.
>
> Fixed in mainline and merged the tooling branch. Should all work like
> expected now.
Great, thanks.
I haven't tried it yet, but will do so soon.
Meanwhile, I have hit a few problems trying to modify remove-cstr-calls to
port my Qt API calls.
Firstly, I'm using some printf debugging and trial and error to try to get
the code working. Is there a better way?
My testcase is this:
Qt::escape("Foo"); // Use implicit QString(const char *)
QString foo("bar");
Qt::escape(foo); // Use QString copy ctor.
Qt::escape(qApp->objectName()); // Use QString copy ctor.
// Use implicit QString(QByteArray) :
Qt::escape(qApp->objectName().toLatin1());
My first attempt at a matcher was:
Finder.addMatcher(
Id("call",
Call(
Callee(Function(HasName("escape")))
, HasArgument(
0, Id("arg", Expression())
)
)
),
&Callback);
However, I need to handle the calls differently based on whether or what
kind of implicit cast is used to call Qt::escape(). I tried to refine my
matcher to match only the calls where the expression is already of type
QString (ie, needs no implicit cast):
0, Id("arg", Expression(HasType(Class(HasName("QString")))))
This refinement didn't work, giving the same matches as before. Is this the
wrong approach entirely? Is it not possible to match like that? Should I
just let it match everything and conditionally change what I replace the
calls with inside the MatchCallback::run implementation instead?
Additionally, the third and fourth uses of escape are excluded by the
getText function because it determines that it starts and ends in different
files. The reason for that seems to be that qApp is actually a macro, which
can expand to QCoreApplication::instance() or QApplication::instance();
Using this matches as expected:
QObject o;
Qt::escape(o.objectName()); // Use QString copy ctor.
// Use implicit QString(QByteArray) :
Qt::escape(o.objectName().toLatin1());
So, really the only thing I need to find out is how to determine whether an
implicit cast is being used in the Call or not.
Thanks,
Steve.
More information about the cfe-dev
mailing list