[cfe-dev] AST Matching Template Functions

Manuel Klimek klimek at google.com
Mon Jul 20 02:39:21 PDT 2015


On Sat, Jul 18, 2015 at 12:32 AM Aaron Mertz <mertz.ac at gmail.com> wrote:

> Hello all,
>
> I've been having some trouble writing the correct matcher to deal with a
> rather sticky case of mine.
>
> I have some code which serves as a kind of catch all so that I can refactor
> boost::shared_ptr (and friends) into the std version. The code below is
> meaningless, but lets me test to make sure I can refactor in real code.
>
> class Test {
>   public:
>     Test() {}
>
>     std::string toString() {
>       return std::string("Test");
>     }
> };
>
>
> template <class T>
> ::boost::shared_ptr<T> TestFunc() {
>   ::boost::shared_ptr<T> ptr = ::boost::make_shared<T>();
>   std::cout << ::boost::static_pointer_cast<T>(txn)->toString();
>

Where does txn come from?

Generally, it helps me if you have a code snippet that I can use without
having boost around (just forward declare the boost functions necessary, as
the template is not actually instantiated).


>   return ptr ;
> }
>
>
> If I call TestFunc() inside main, my matcher works perfectly fine and
> everything inside the template matches. However, if I don't call the
> function, then I get 0 matches.
>
> My matchers look like:
>
> // For make_shared, static_pointer_cast, etc...
>
> callExpr(callee(functionDecl(matchesName("make_shared|static_pointer_cast")))).bind(k_BoostStmtTokenId);
>
> // For shared_ptr, weak_ptr
>
> typeLoc(loc(qualType(hasDeclaration(recordDecl(matchesName("shared_ptr|weak_ptr"),
> isFromBoostNamespace()))))).bind(k_BoostTypeLocTokenId);
>
> I've been poring over the AST-dump, but I can't seem to figure out exactly
> how to construct a matcher that will let me match the template code without
> it being actually called somewhere.
>
> A general callExpr() catches the two expressions inside the template, but
> I'm not sure how to narrow them down from there.
>
> Any help is very much appreciated.
>
> Thanks,
> Aaron
>
>
>
>
>
> --
> View this message in context:
> http://clang-developers.42468.n3.nabble.com/AST-Matching-Template-Functions-tp4046680.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150720/cf43e53b/attachment.html>


More information about the cfe-dev mailing list