如何使用python从C ++头中检索类名及其方法名

时间:2019-07-17 08:53:23

标签: python c++ clang

我正在使用Python中的Clang模块来解析C ++头文件。我正在尝试列出所有类名及其成员函数。

这是我尝试过的

我在 https://stackoverflow.com/a/40328378/6223628

import clang
import clang.cindex as cl
from clang.cindex import CursorKind

def fully_qualified(c):
    if c is None:
        return ''
    elif c.kind == CursorKind.TRANSLATION_UNIT:
        return ''
    else:
        res = fully_qualified(c.semantic_parent)
        if res != '':
            return res + '::' + c.spelling
    return c.spelling

cl.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
idx = clang.cindex.Index.create()
tu = idx.parse('C:/Repository/myproject/myHeader.h', args='-xc++ --std=c++11'.split())
for c in tu.cursor.walk_preorder():
    if c.kind == CursorKind.CALL_EXPR:
        print (fully_qualified(c.referenced))

我只能检索某些类名,其余的或结果来自std名称空间。

还有其他可用选项吗?

1 个答案:

答案 0 :(得分:0)

从@Predelnik的注释中,我将ENV["REDIS_URL"]调整为Class_decl和CXX_methods并获得了必需的内容。

CursorKind
相关问题