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?
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)
My answer might not be perfect but it works.
There is no direct method, which gives you direct caller of the call expression. But if we look at the way AST traverse, while entering into callee function, if we somehow store last visited FunctionDecl
name, it will give you the direct caller of that CallExpr*
.
Example
string CallerFunc = ""; //declared in class (private/public)
virtual bool VisitFunctionDecl(FunctionDecl *func)
{
CallerFunc = func->getNameInfo().getName().getAsString();
return true;
}
virtual bool VisitCallExpr(CallExpr *E)
{
if (E != NULL){
QualType q = E->getType();
const Type *t = q.getTypePtrOrNull();
if(t != NULL)
{
FunctionDecl *func = E->getDirectCallee(); //gives you callee function
string callee = func->getNameInfo().getName().getAsString();
cout << callee << " is called by " << CallerFunc;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With