python - 如何将 compile_commands.json 与 llvm clang(版本 7.0.1)python 绑定(bind)一起使用?

标签 python c++ clang llvm libclang

我正在尝试打印给定 C++ 文件中的所有 AST 节点。我提供了一个有效的 C++ 文件,该文件在其父目录中有一个 compile_commands.json 文件。所有步骤就像 How to use compile_commands.json with clang python bindings?我的 clang 版本是:“clang 版本 7.0.1 (tags/RELEASE_701/final)
目标:x86_64-pc-windows-msvc
线程模型:posix
InstalledDir:C:\Program Files\LLVM\bin“。操作系统是windows10,python版本是2.7。但是,当我在下面运行这个脚本时。

在这里输入代码

import clang.cindex
from clang.cindex import *

libclang_path = r'C:\Program Files\LLVM\bin\libclang.dll'

if Config.loaded == True:
    pass
else:
    Config.set_library_file(libclang_path)


def node_info(node):
    return {'kind': node.kind,
            'usr': node.get_usr(),
            'spelling': node.spelling,
            'location': node.location,
            'file': node.location.file.name,
            'extent.start': node.extent.start,
            'extent.end': node.extent.end,
            'is_definition': node.is_definition()
            }


def get_nodes_in_file(node, filename, ls=None):
    ls = ls if ls is not None else []
    for n in node.get_children():
        if n.location.file is not None and n.location.file.name == filename:
            ls.append(n)
            get_nodes_in_file(n, filename, ls)
    return ls


def main():
    compilation_database_path = 'C:/Users/liqiu/Desktop/md/gn/build/win/x64'

    source_file_path = 'C:/Users/liqiu/Desktop/md/.../video_camera_source.cpp'

    index = clang.cindex.Index.create()
    compdb = clang.cindex.CompilationDatabase.fromDirectory(compilation_database_path)

    try:
        file_args = compdb.getCompileCommands(source_file_path)
        translation_unit = index.parse(source_file_path, file_args)
        file_nodes = get_nodes_in_file(translation_unit.cursor, source_file_path)
        print [p.spelling for p in file_nodes]
    except clang.cindex.CompilationDatabaseError:
        print 'Could not load compilation flags for', source_file_path


if __name__ == '__main__':
    main()

有一些错误,如下所示。我认为这个错误的原因是“file_args = compdb.GetCompileCommands(source_file_path)”这行返回了一个CompileCommand实例而不是字符串或整数类型,所以index.parse()不能直接接受这个实例作为参数。但这似乎在旧版本的 libclang 中返回了某种字符串类型的“命令”。这让我现在很困惑。
D:\Python27\python.exe C:/Users/liqiu/Desktop/libclang_parse/parser.py
Traceback (most recent call last):
  File "C:/Users/liqiu/Desktop/libclang_parse/parser.py", line 51, in <module>
    main()
  File "C:/Users/liqiu/Desktop/libclang_parse/parser.py", line 43, in main
    translation_unit = index.parse(source_file_path, file_args)
  File "C:\Users\liqiu\Desktop\libclang_parse\clang\cindex.py", line 2689, in parse
    self)
  File "C:\Users\liqiu\Desktop\libclang_parse\clang\cindex.py", line 2783, in from_source
    args_array = (c_char_p * len(args))(*[b(x) for x in args])
TypeError: string or integer address expected instead of CompileCommand instance

Process finished with exit code 1


最佳答案

因为它是一个listiterator,所以我们应该像下面这样处理:

  file_args = compdb.getCompileCommands(source_file_path)
  arguments = list(iter(file_args).next().arguments)

关于python - 如何将 compile_commands.json 与 llvm clang(版本 7.0.1)python 绑定(bind)一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59083241/

相关文章:

python - 如何在 ubuntu 上转储 postgresql 数据库?

python - eventlet 与 celery 并行执行任务吗?

python - 在Python线程内运行子进程实时读取输出

c++ - 编译cocos2d-x v3 IOS项目时出错,关于集成AdMob(IOS)框架

Clang、Microsoft 链接器和标准库

python - 如何在Python中向整数添加尾随零?或者如何在 python 中的任何整数后面添加零?

c++ - 如何使用 STL 正确跟踪地址?

c++ - OpenGL + SDL 有时会脱离坐标

ios - 哪个是抑制 "unused variable"警告的最佳方法

c++ - 打印 reference_wrapper<int> 的 vector