模板化构造函数游标和模板化成员游标在 libclang 中具有种类 FUNCTION_TEMPLATE

A templated constructor cursor and a templated member cursor have kind FUNCTION_TEMPLATE in libclang

一些背景

我对 libclang 不是很熟悉。我只是在修改一个使用 the python bindings to libclang.

的 vim 插件

有一个 python 函数接收游标参数。当前 C++ 缓冲区的 AST 中的几乎每个节点都会调用此方法。

问题

cursor.kind用于获取游标的种类。一切正常,除了

都是同类:FUNCTION_TEMPLATE。我需要区分它们。

更多见识

例如上面的非模板版本有以下几种:

我搜索了 cindex.py 的来源,但没有 CXX_METHOD_TEMPLATECONSTRUCTOR_TEMPLATE 或类似的。

我曾尝试以某种方式获取我需要的信息,但没有成功,例如cursor.get_definition()cursor.underlying_typedef_type.get_declaration().

我获得的唯一部分成功是,对于方法和构造函数,语义和词法父级是 STRUCT_DECL

我真的不在乎它是否是模板化的。我只关心它是构造函数、成员还是自由函数。

总结一下

给定一个游标,我如何判断它是方法(甚至是模板化的)、构造函数(甚至是模板化的)还是自由函数声明?

tl;博士

cindex.CursorKind.from_id(cindex.conf.lib.clang_getTemplateCursorKind(cursor))

正在浏览 C libclang online doxygen documentation we find in the C++ AST introspection the function clang_getTemplateCursorKind

CINDEX_LINKAGE enum CXCursorKind  clang_getTemplateCursorKind (CXCursor C)

Given a cursor that represents a template, determine the cursor kind of the specializations would be generated by instantiating the template.

这正是我想要的。不幸的是,在 Python 中调用它并不那么简单。

环顾四周,这就是我们调用 Python 中未明确实现的 C 函数的方式:

tk = cindex.conf.lib.clang_getTemplateCursorKind(cursor)

别问了,黑魔法。

下一个障碍是这个函数 returns 很长。如果我们真的想要 CursorKind 对象,我们需要这个:

cindex.CursorKind.from_id(tk)