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

如果可以访问 CXXRecordDeclStack 节点,我该如何访问 FunctionTemplateDeclpushpop 对象? 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
}