0

我正在尝试使用 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特定类型的模板化调用?

4

2 回答 2

1

通过模板参数的实际类型适用于 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.
于 2019-10-19T13:38:59.230 回答
1

你需要refersToType(asString("class Demo::Widget")))

不幸的是,这不是很容易发现。我在这里展示了一些工具来使这个发现:https ://steveire.wordpress.com/2019/04/30/the-future-of-ast-matching-refactoring-tools-eurollvm-and-accu/但通常不是还可以。

于 2019-10-20T09:27:42.147 回答