Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将compile_commands.json与llvm clang(7.0.1版)python绑定一起使用?_Python_C++_Clang_Llvm_Libclang - Fatal编程技术网

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

如何将compile_commands.json与llvm clang(7.0.1版)python绑定一起使用?,python,c++,clang,llvm,libclang,Python,C++,Clang,Llvm,Libclang,我试图打印出一个给定C++文件中的所有AST节点。我提供了一个有效的C++文件,它的父目录中有一个CixIyCurd7.j森文件。所有步骤都和我的clang版本一样:“clang版本7.0.1(标签/发行版/最终版) 目标:x86_64-pc-windows-msvc 线程模型:posix InstalledDir:C:\Program Files\LLVM\bin”。操作系统是windows10,python版本是2.7。但是,当我运行下面的脚本时 在这里输入代码 import clang.c

我试图打印出一个给定C++文件中的所有AST节点。我提供了一个有效的C++文件,它的父目录中有一个CixIyCurd7.j森文件。所有步骤都和我的clang版本一样:“clang版本7.0.1(标签/发行版/最终版) 目标: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.getCompileCommand(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


D:\Python27\python.exe C:/Users/liqiu/Desktop/libclang\u parse/parser.py
回溯(最近一次呼叫最后一次):
文件“C:/Users/liqiu/Desktop/libclang_parse/parser.py”,第51行,在
main()
文件“C:/Users/liqiu/Desktop/libclang_parse/parser.py”,第43行,在main中
translation\u unit=index.parse(源文件路径,文件参数)
文件“C:\Users\liqiu\Desktop\libclang\u parse\clang\cindex.py”,第2689行,在parse中
(自我)
文件“C:\Users\liqiu\Desktop\libclang\u parse\clang\cindex.py”,第2783行,在from\u source中
args_数组=(c_char_p*len(args))(*[b(x)表示args中的x])
TypeError:需要字符串或整数地址,而不是CompileCommand实例
进程已完成,退出代码为1

因为它是一个listiterator,所以我们应该如下处理:

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

由于它是一个listiterator,因此我们应该如下处理:

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