python libclang绑定,获取无效文件错误

问题描述

我正在尝试使用 python libclang 绑定解析一些 C++ 文件,为此我制作了这个脚本:

import clang.cindex


def GetDoxygenCommentTokens(translation_unit):
    tokens = []
    for token in translation_unit.get_tokens(extent=translation_unit.cursor.extent):
        if token.kind == clang.cindex.TokenKind.COMMENT:
            print(token.spelling)
            print(token.cursor.displayname)
            print(token.kind)
            print(token.cursor.kind)
            print(token.cursor.brief_comment)
            tokens.append(token)
    return tokens


def main():
    index = clang.cindex.Index.create()

    compdb = clang.cindex.CompilationDatabase.fromDirectory(
        "/home/makogan/neverengine/")

    source_file_path = '/home/makogan/neverengine/Src/Engine/Geometry/GeometryUtils.cpp'
    commands = compdb.getCompileCommands(source_file_path)

    file_args = []
    for command in commands:
        for argument in command.arguments:
            file_args.append(argument)
    file_args = file_args[3:-3]
    translation_unit = index.parse(source_file_path,args=file_args)

    comment_tokens = GetDoxygenCommentTokens(translation_unit)


if __name__ == "__main__":
    main()

我知道这个文件在它所属的项目中编译。 我得到的错误是打印 CursorKind.INVALID_FILE 时的 cursor.kind 和打印 cursor.displayname 时的空字符串。

这是我实际传递给解析器的参数列表(在删除了一些 g++ 特定标志之后):

['-DVULKAN_HPP_NO_EXCEPTIONS','-DVULKAN_HPP_TYPESAFE_CONVERSION','-DDEBUG','-DTESTING','-I../Src/Engine','-I../libraries/stb','-I../libraries/glm','-I../libraries/Eigen','-I../libraries/imgui','-I../libraries/incbin','-I../libraries/tinygltf','-I../libraries/harfbuzz','-I../libraries/backward-cpp','-I../libraries/libigl/include','-I../libraries/freetype2/include','-I../libraries/benchmark/include','-I../libraries/magic_get/include','-I../libraries/shaderc/libshaderc/include','-I../libraries/VulkanMemoryAllocator/include','-I../libraries/googletest/googletest/include','-I../libraries/shaderc/libshaderc_util/include','-I../libraries/vulkan-sdk/1.2.176.1/source/Vulkan-Headers/include','-Og','-g','-O2']

并非我尝试过的所有文件都有这些错误,只有一些。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)