简体   繁体   中英

pass a member function as argument in libclang visitor

I am using LibClang to traverse AST in a C++ program as below:

CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData data) {
  // some code
}

int main() {
  // some code
  clang_visitChildren(rootCursor, visitor, 0);
}

I do not know how to pass visitor to clang_visitChildren() if visitor() is a member of a class. I tried using ClassName::visitor instead of visitor, but I get the following compiler error:

error: invalid use of non-static member function ‘CXChildVisitResult ClassName::visitor(CXCursor, CXCursor, CXClientData)’

clang_visitChildren only accepts regular functions, not member functions. It order to use a member function with it you will have to create a helper static member function that will call a non-static one:

class MyVisitor
{
public:
  CXChildVisitResult visitor(CXCursor, CXCursor);
  static CXChildVisitResult visitorHelper(CXCursor cursor, CXCursor parent, CXClientData client_data) {
    return static_cast<MyVisitor *>(client_data)->visitor(cursor, parent);
  }
};

int main() {
  MyVisitor myVisitor;
  // some code
  clang_visitChildren(rootCursor, &MyVisitor::visitorHelper, &myVisitor);
}

Note the use of client_data parameter to pass a pointer to an object whose method you want to call. This is a very common pattern for callback in C API.

Alternatively you may want to choose a different library to work with Clang (more info here ) such as LibTooling which provides a C++ interface.

Passing member functions as arguments is easy. The trick is the syntax. You don't need static functions you need a pointer to the member function and a pointer to the calling object.

See this example

#include <iostream>

using namespace std;

class Foo {
    public:
    virtual void MemberFunc() {
        std::cout << "MemberFunc called" << std::endl;
    }

    virtual void MemberFuncWithArgs(int a, std::string b, double c) {
        std::cout << "MemberFuncWithArgs called with a = " << a << " b = " << b << " c = " << c << std::endl;
    }
};

class Bar : public Foo {
    public:
    virtual void MemberFunc() {
        std::cout << "Bar MemberFunc called" << std::endl;
    }
};

// funPtr is a pointer to the function in the class Foo:: scope that takes zero arguments and returns void
void CallClassFunction(void (Foo::*funPtr)(), Foo* obj) {
    // We deference the function pointer and invoke it on the caller
    (obj->*funPtr)();  
}

// Same as above but funPtr takes 3 arguments
void CallClassFunctionWithArgs(void (Foo::*funPtr)(int a, std::string b, double c), int a, std::string b, double c, Foo* obj) {
    (obj->*funPtr)(a, b, c);
}

int main()
{
    cout<<"Hello World";

    Foo obj;

    // We take the address of the class member function (makes a pointer)
    // We take a pointer to the obj variable
    CallClassFunction(&Foo::MemberFunc, &obj);
    CallClassFunctionWithArgs(&Foo::MemberFuncWithArgs, 34, "hello", 65.87, &obj);


    // Works with inheritance too! This will now call Bar::MemberFunc
    Bar bar;

    CallClassFunction(&Foo::MemberFunc, &bar);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM