gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:16:47 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com