Clang Cindex:涉及到模板时检索类层次结构的问题

2024-09-28 05:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用clangpython绑定检索类的完整层次结构。在

基本上我要做的是:

  • 找到声明类的CLASS_DECL游标。
  • 迭代它的子节点,寻找CXX_BASE_SPECIFIER节点。
  • 通过get_definition()type.get_declaration()获取父声明

然而,当父类是模板时,我遇到了问题。我附上了一个相当简单的例子来说明这个问题。在

import clang.cindex

clang.cindex.Config.set_library_file('/usr/lib/llvm-6.0/lib/libclang.so')


def get_template_args(node):
    t = node.type
    if t.get_num_template_arguments() < 0:
        return []
    for i in range(t.get_num_template_arguments()):
        yield t.get_template_argument_type(i)


def str_node_type(node):
    if isinstance(node, clang.cindex.Cursor):
        return '[K={}] [S={}] [DN={}] [ISDEF: {}]'.format(node.kind, node.spelling, node.displayname,
                                                          node.is_definition())
    else:
        return '[K={}] [Type:{}]'.format(node.kind, node.spelling)


def find_parent2(node):
    print('\tFind parent2')
    print('\t\tNode {}'.format(str_node_type(node)))
    print('\t\tNum template args: {}'.format(node.canonical.get_num_template_arguments()))
    for c in node.get_children():
        if c.kind == clang.cindex.CursorKind.CXX_BASE_SPECIFIER:
            print('\t\tFound base: {}'.format(str_node_type(c)))


def find_parent(classdecl_node):
    for c in classdecl_node.get_children():
        if c.kind == clang.cindex.CursorKind.CXX_BASE_SPECIFIER:
            print('\tFound base: {}'.format(str_node_type(c)))
            definition = c.get_definition()
            print('\tTemplate arguments for type {}: {}'.format(str_node_type(definition.type),
                                                                definition.type.get_num_template_arguments()))
            for a in get_template_args(c):
                print('\t\tArg: {}'.format(str_node_type(a)))
            find_parent2(c.type.get_declaration())  # or find_parent2(definition)


def main():
    tu = clang.cindex.TranslationUnit.from_source('class.cpp', ['-std=c++11', '-O0'],
                                                  options=clang.cindex.TranslationUnit.PARSE_INCOMPLETE | clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES)

    for node in tu.cursor.get_children():
        if (node.kind == clang.cindex.CursorKind.CLASS_DECL or
                node.kind == clang.cindex.CursorKind.STRUCT_DECL):
            print('Found class declaration:{}'.format(str_node_type(node)))
            find_parent(node)


if __name__ == "__main__":
    main()

正在解析的相应C++文件:

^{pr2}$

通过读取输出,我们可以看到我们能够找到VerySimple类,它是一个非模板的祖父母。但是,我们看不到任何关于GrandParent的提及,它是一个模板类。在

Found class declaration:[K=CursorKind.CLASS_DECL] [S=VerySimple] [DN=VerySimple] [ISDEF: True]
Found class declaration:[K=CursorKind.CLASS_DECL] [S=Simple] [DN=Simple] [ISDEF: True]
        Found base: [K=CursorKind.CXX_BASE_SPECIFIER] [S=class VerySimple] [DN=class VerySimple] [ISDEF: False]
        Template arguments for type [K=TypeKind.RECORD] [Type:VerySimple]: -1
        Find parent2
                Node [K=CursorKind.CLASS_DECL] [S=VerySimple] [DN=VerySimple] [ISDEF: True]
                Num template args: -1
Found class declaration:[K=CursorKind.CLASS_DECL] [S=Child] [DN=Child] [ISDEF: True]
        Found base: [K=CursorKind.CXX_BASE_SPECIFIER] [S=Parent<int>] [DN=Parent<int>] [ISDEF: False]
        Template arguments for type [K=TypeKind.RECORD] [Type:Parent<int>]: 1
                Arg: [K=TypeKind.INT] [Type:int]
        Find parent2
                Node [K=CursorKind.CLASS_DECL] [S=Parent] [DN=Parent<int>] [ISDEF: True]
                Num template args: -1
        Found base: [K=CursorKind.CXX_BASE_SPECIFIER] [S=class Simple] [DN=class Simple] [ISDEF: False]
        Template arguments for type [K=TypeKind.RECORD] [Type:Simple]: -1
        Find parent2
                Node [K=CursorKind.CLASS_DECL] [S=Simple] [DN=Simple] [ISDEF: True]
                Num template args: -1
                Found base: [K=CursorKind.CXX_BASE_SPECIFIER] [S=class VerySimple] [DN=class VerySimple] [ISDEF: False]

我不知道我错过了什么或者误解了什么。在


Tags: nodeformatforgettypetemplateclassclang

热门问题