如何解决Clang AST:从 CXXRecordDecl 访问成员函数模板
class Stack
{
public:
template<typename T>
void push(T val)
{ (void)val; }
template<typename T>
T pop()
{ return 0; }
bool empty() const
{ return true; }
};
AST 转储看起来像:
...
|-CXXRecordDecl 0x5610df147d60 <col:1,col:7> col:7 implicit class Stack
|-AccessspecDecl 0x5610df147df0 <line:3:1,col:7> col:1 public
|-FunctionTemplateDecl 0x5610df1480e0 <line:4:3,line:6:16> line:5:8 push
| |-TemplateTypeParmDecl 0x5610df147e18 <line:4:12,col:21> col:21 referenced typename depth 0 index 0 T
| `-CXXMethodDecl 0x5610df148040 <line:5:3,line:6:16> line:5:8 push 'void (T)'
| |-ParmVarDecl 0x5610df147f00 <col:13,col:15> col:15 referenced val 'T'
| `-CompoundStmt 0x5610df1484f0 <line:6:3,col:16>
| `-CStyleCastExpr 0x5610df1484c8 <col:5,col:11> 'void' <ToVoid>
| `-DeclRefExpr 0x5610df148498 <col:11> 'T' lvalue ParmVar 0x5610df147f00 'val' 'T'
|-FunctionTemplateDecl 0x5610df148300 <line:8:3,line:10:15> line:9:5 pop
| |-TemplateTypeParmDecl 0x5610df148140 <line:8:12,col:21> col:21 referenced typename depth 0 index 0 T
| `-CXXMethodDecl 0x5610df148260 <line:9:3,line:10:15> line:9:5 pop 'T ()'
| `-CompoundStmt 0x5610df148538 <line:10:3,col:15>
| `-ReturnStmt 0x5610df148528 <col:5,col:12>
| `-IntegerLiteral 0x5610df148508 <col:12> 'int' 0
`-CXXMethodDecl 0x5610df1483e0 <line:12:3,line:13:18> line:12:8 empty 'bool () const'
`-CompoundStmt 0x5610df148570 <line:13:3,col:18>
`-ReturnStmt 0x5610df148560 <col:5,col:12>
`-CXXBoolLiteralExpr 0x5610df148550 <col:12> 'bool' true
如果可以访问 CXXRecordDecl
的 Stack
节点,我该如何访问 FunctionTemplateDecl
和 push
的 pop
对象? CXXRecorDecl::methods
只返回 empty
的方法声明,我没有看到任何其他可能有用的函数。我是否必须单独匹配函数模板声明,然后将它们与类声明关联?
解决方法
我相信我现在已经弄清楚了,如果这对其他人有用,我会回答我自己的问题:
这样做的方法是首先将给定的 CXXRecordDecl
转换为 DeclContext
,迭代包含的声明并尝试将它们转换为 FunctionTemplateDecl
,伪代码:
clang::CXXRecordDecl const *record = // ...
for (auto const *inner : static_cast<clang::DeclContext>(record)->decls()) {
auto const *member_function_template = llvm::dyn_cast<clang::FunctionTemplateDecl>(inner);
if (!member_function_template)
continue;
// do something useful
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。