用于模板的模板方法的 Clang AST class

Clang AST for a template method of a template class

我有如下一段代码:

template <typename T>
class Foo
{
  template <typename U>
  void bar() const;
};

[[template <typename T>]]
template <typename U>
void Foo<T>::bar() const
{
}

转储方法 foo 的 AST 树给出了以下方法定义(为清楚起见,删除了不相关的细节):

`-FunctionTemplateDecl <line:10:1, line:14:1> col:11:14 bar
 |-TemplateTypeParmDecl <line:10:11, col:20> col:20 typename depth 1 index 0 U
 `-CXXMethodDecl <line:9:1, line:14:1> line:11:14 bar 'void () const'
  `-CompoundStmt <line:12:1, line:14:1>

我不知道如何找到 template <typename T> 部分(在 [[...]] 块中的那个)的位置。我没能在 clang AST API 中找到任何相关内容。任何帮助将不胜感激。

最后,我找到了自己问题的解决方案,所以发布了答案。希望它可能对其他人有所帮助。

可以使用DeclaratorDecl class 的getNumTemplateParameterLists()getTemplateParameterList() 方法获取外部模板参数的位置信息。 有关详细信息,请参阅 documentation