Is there any way to get the caller of the CallExpr* in VisitCallExpr method with clang?

abstract-syntax-tree, c++, clang, recursion

Solution

Better way to deal with this is to use AST matchers. you can basically look for all callExpr nodes in an AST matcher and bind them and at the same time bind the corresponding caller (CXXRecordDecl) nodes as well with a different string.

For Example:

CallBackFunc callBackFunc;

Matchers.addMatcher(callExpr(isExpansionInMainFile(), callee(), hasAncestor(recordDecl().bind("caller"))).bind("callee"), &callBackFunc);

Then in the callBack function you can retrieve theses callee and caller functions like this:

class CallBackFunc : public MatchFinder::MatchCallBack {
  public:
     virtual void run(const MatcherFinder::MatchResult &Results) {
        auto callee = Results.Nodes.getNodeAs<clang::CallExpr>("callee");
        auto caller = Results.Nodes.getNodeAs<clang::CXXRecordDecl>("caller"); 

       // Do what is required with callee and caller.
    }
};

(I can give more information if required)

Problem

The method `getDirectCallee()` can get the callee (be called method/function) of the call expression, but is there any way to get the caller (the method/ function who called it) of the `CallExpr*` in `VisitCallExpr()` method? Are there any other ways to know the caller of one call expression?

Original source

Related problems