我适应锵工具模板(如描述这里)来搜索我的代码的特定方法调用。为了以后重写该调用,我想获取调用该方法的参数的类型,以及调用该方法的对象的类型。
我设法找到一个匹配项,该匹配项可以回调以下内容:
class AddListenerPrinter : public MatchFinder::MatchCallback
{
public :
virtual void run(const MatchFinder::MatchResult &Result) {
if (const auto *FS = Result.Nodes.getNodeAs<clang::MemberExpr>("ListeningBound"))
{
FS->dump();
}
}
};
输出:
MemberExpr 0x7fb05b07b948 '<bound member function type>' .addListener 0x7fb05b077670
`-MemberExpr 0x7fb05b07b918 'class MyCore' lvalue ->mCore 0x7fb05b078e30
`-CXXThisExpr 0x7fb05b07b900 'class MyComponent *' this
现在,我找不到任何方法来检索调用了该方法的对象的类型(在此处class MyCore
)或方法参数的类型(在此class MyComponent
)。
我怎样才能做到这一点?
我通过浏览现有匹配器的代码找到了答案。
使用 matcher = memberCallExpr( callee(methodDecl(hasName("addListener"))) )
我能够检索一个CXXMemberCallExpr
节点。然后获取调用该方法的对象的类型:
// FS is the CXXMemberCallExpr
// Prints out the type of x in x.method()
llvm::outs() << FS->getRecordDecl()->getName();
可以通过访问方法参数FS->getArg(n)
。
底线是:首先找到包含您要查找的内容的CXX对象(例如,哪个类具有访问函数参数的方法?),然后在ASTMatchers.h中找到将返回相同类型对象的匹配器。
希望这可以帮助其他有相同问题的人。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句