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