Clang AST Matcher:没有CXXMethodDecl用于`operator()`吗?

时间:2018-10-03 05:19:00

标签: clang clang-ast-matchers

我在文件中具有以下struct定义:

template <class... EventArgs>
struct banana {
    template <class... Args>
    void operator()(Args&&... args) const {
        _f(std::forward<Args>(args)...);
    }

private:
    std::function<void(EventArgs...)> _f;
};

使用-ast-dump -fsyntax-only,我可以清楚地看到转储中引用了CXXRecordDecl的多个banana和引用了CXXMethodDecl的{​​{1}} :

operator()

我的`-ClassTemplateDecl 0x7fb23c11e028 <./test_files/templates.cpp:12:1, line:21:1> line:13:8 banana |-TemplateTypeParmDecl 0x7fb23c11df08 <line:12:11, col:20> col:20 referenced class depth 0 index 0 ... EventArgs |-CXXRecordDecl 0x7fb23c11df90 <line:13:1, line:21:1> line:13:8 struct banana definition | |-CXXRecordDecl 0x7fb23c11e300 <line:13:1, col:8> col:8 implicit struct banana | |-FunctionTemplateDecl 0x7fb23c11e668 <line:14:5, line:17:5> line:15:10 operator() | | |-TemplateTypeParmDecl 0x7fb23c11e398 <line:14:15, col:24> col:24 referenced class depth 1 index 0 ... Args | | `-CXXMethodDecl 0x7fb23c11e5d0 <line:15:5, line:17:5> line:15:10 operator() 'void (Args &&...) const' | | etc., etc. 子类正在MatchFinder::MatchCallback上运行以进行事件,但是CXXRecordDecl返回的范围是空的:

methods()

我想念什么?

1 个答案:

答案 0 :(得分:1)

好吧,clas->methods()只返回CXXMethodDecl顶层的struct。 但是在您的struct中,只有FunctionTemplateDecl。 因此,您可能想要执行以下操作:

for (const auto &decl : clas->decls()) {
  if (auto *templ = dyn_cast<FunctionTemplateDecl>(&decl)) {
    // Use templ->getTemplatedDecl to get FunctionDecl.
  }
}

注意。我尚未实际测试或编译该代码,但希望您能从中得到启发。