[cfe-dev] Where to put upcoming refcatoring tools

Manuel Klimek klimek at google.com
Wed Apr 25 08:21:24 PDT 2012

On Wed, Apr 25, 2012 at 3:53 PM, Stephen Kelly <steveire at gmail.com> wrote:
> 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?

Unfortunately not - I'm open to ideas :D
One thing I'm usually doing is to create small unit tests that execute
particular cases and bootstrap from "obviously working" towards a more
and more restrictive filter.

> 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();

Yea, the getText function needs some love - the problem is that you
need either the spelling or the expansion location based on what you
want to look at - in this particular instance you'll want the
expansion location.

Let me know if you have more questions!

> 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.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list