Search code examples
c++clangabstract-syntax-treeclang-ast-matchers

Clang AST Match call to make_unique with specific class


I'm trying to use clang's AST matchers to target code like the following:

#include<memory>

namespace Demo {
    class Widget {};
}

int main () {
    auto w = std::make_unique<Demo::Widget>();
}

In clang-query, I've tried the following:

callExpr(callee(functionDecl(
  // including only this arg gives matches
  hasName("make_unique"),
  // adding this second arg produces zero matches
  hasTemplateArgument(0, refersToType(asString("Demo::Widget")))
)))

I've also tried swapping out refersToType(...) for

refersToDeclaration(cxxRecordDecl(isSameOrDerivedFrom("Demo::Widget")))

which also gives zero matches. What can I use to target calls to std::make_unique templated on a particular type?


Solution

  • Going via the template argument's actual type works with clang-10.0.0,

    clang-query> match callExpr(callee(functionDecl(hasName("make_unique"),
                                 hasAnyTemplateArgument(refersToType(hasDeclaration(
                                     namedDecl(hasName("Demo::Widget"))))))))
    
    Match #1:
    
    /tmp/test.cpp:8:18: note: "root" binds here
            auto w = std::make_unique<Demo::Widget>();
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 match.