Search code examples
clangclang-ast-matchers

Clang AST Matcher: No CXXMethodDecl for `operator()`?


I have the following struct definition in a file:

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

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

Using -ast-dump -fsyntax-only, I can clearly see multiple CXXRecordDecls in the dump that refer to banana, as well as CXXMethodDecls that refer to 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.

My MatchFinder::MatchCallback subclass is being run over the CXXRecordDecl for event, however methods() is coming back with an empty range:

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?
    }
}

What am I missing?


Solution

  • Well, clas->methods() only returns CXXMethodDecls in the top level of the struct. But in your struct, there is only FunctionTemplateDecl. So, you may wanna do something like this:

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

    Note. I haven't actually tested nor compiled that code, but hopefully, you get the idea from it.