AST Matching Template Functions

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

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
::boost::shared_ptr TestFunc() {
::boost::shared_ptr ptr = ::boost::make_shared();
std::cout << ::boost::static_pointer_cast(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).