[cfe-dev] AST Matching Template Functions

Aaron Mertz mertz.ac at gmail.com
Fri Jul 17 15:25:54 PDT 2015


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



More information about the cfe-dev mailing list