0

我在文件中有以下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转储中引用的多个 sbanana以及CXXMethodDecl引用的 s 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子类正在CXXRecordDeclfor 事件中运行,但是methods()返回一个空范围:

void ClassInfo::run(const MatchFinder::MatchResult& Result) {
    auto clas = Result.Nodes.getNodeAs<clang::CXXRecordDecl>("class");

    for (const auto& method : clas->methods()) {
        // Not getting run - methods() is empty?
    }
}

我错过了什么?

4

1 回答 1

1

好吧,clas->methods()只返回CXXMethodDecls 的顶层struct。但在你的struct,只有FunctionTemplateDecl。所以,你可能想做这样的事情:

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

笔记。我实际上并没有测试或编译过该代码,但希望你能从中得到灵感。

于 2018-10-03T07:42:12.603 回答