c++ - Clang AST 将对 make_unique 的调用与特定类匹配

标签 c++ clang abstract-syntax-tree clang-ast-matchers

我正在尝试使用 clang 的 AST 匹配器来定位如下代码:

#include<memory>

namespace Demo {
    class Widget {};
}

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

在 clang-query 中,我尝试了以下方法:

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")))
)))

我也试过将 refersToType(...) 换成

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

这也给出了零匹配。我可以使用什么来定位对特定类型模板化的 std::make_unique 的调用?

最佳答案

通过模板参数的实际类型与 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.

关于c++ - Clang AST 将对 make_unique 的调用与特定类匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443058/

相关文章:

c++ - 使用指针或引用访问 vector 然后遍历它缓存不友好吗?

c++ - 这是 C++ 中的 typedef 函数吗?

c++ - 分配给 C++ 类方法?

java - 从 Eclipse 的 JDT 访问者模式中提取数据

c++ - 变量周围的 VS 报告堆栈已损坏

c - 符号可见性未按预期工作

c - 为什么 LLVM IR 结构中的函数指针字段被 {}* 替换?

c - 没有明显段错误的编译器警告

java - JDT ASTParser - 如何解析常量值?

c++ - 为解析树实现制作一个通用头文件