如何将compile_commands.json与clang python绑定一起使用?

2023-11-27

我有以下脚本尝试打印给定 C++ 文件中的所有 AST 节点。当在具有简单包含的简单文件(同一目录中的头文件等)上使用它时,效果很好。

#!/usr/bin/env python
from argparse import ArgumentParser, FileType
from clang import cindex


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():
    arg_parser = ArgumentParser()
    arg_parser.add_argument('source_file', type=FileType('r+'),
                            help='C++ source file to parse.')
    arg_parser.add_argument('compilation_database', type=FileType('r+'),
                            help='The compile_commands.json to use to parse the source file.')
    args = arg_parser.parse_args()
    compilation_database_path = args.compilation_database.name
    source_file_path = args.source_file.name
    clang_args = ['-x', 'c++', '-std=c++11', '-p', compilation_database_path]
    index = cindex.Index.create()
    translation_unit = index.parse(source_file_path, clang_args)
    file_nodes = get_nodes_in_file(translation_unit.cursor, source_file_path)
    print [p.spelling for p in file_nodes]


if __name__ == '__main__':
    main()

但是,我得到了clang.cindex.TranslationUnitLoadError: Error parsing translation unit.当我运行脚本并提供一个有效的 C++ 文件时,该文件的父目录中有一个compile_commands.json 文件。这段代码使用 CMake 和 clang 可以正常运行和构建,但我似乎无法弄清楚如何正确传递指向compile_commands.json的参数。

我也很难在 clang 文档中找到这个选项,并且无法得到-ast-dump上班。然而,clang-check 只需传递文件路径就可以正常工作!


您自己接受的答案是不正确的。libclang 是否支持编译数据库 and cindex.py 也是如此,libclang python 绑定。

混乱的主要来源可能是 libclang 知道/使用的编译标志只是可以传递到 clang 前端的所有参数的子集。支持编译数据库,但不会自动工作:必须手动加载和查询。像这样的东西应该有效:

#!/usr/bin/env python
from argparse import ArgumentParser, FileType
from clang import cindex

compilation_database_path = args.compilation_database.name
source_file_path = args.source_file.name
index = cindex.Index.create()

# Step 1: load the compilation database
compdb = cindex.CompilationDatabase.fromDirectory(compilation_database_path)

# Step 2: query compilation flags
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 CompilationDatabaseError:
    print 'Could not load compilation flags for', source_file_path
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将compile_commands.json与clang python绑定一起使用? 的相关文章

随机推荐

  • 根据路线动态插入 CSS 类到导航栏

    Vue 2 和 Vue Router 2 我正在尝试根据访问的路线更改应用程序导航栏的颜色 这是我所拥有的 main js import App from components App vue const app new Vue route
  • Excel VBA 打开 Word 模板,填充,然后在其他位置另存为 .docx 文件

    我创建了一个带有占位符 例如 的 Word 模板 然后我可以将其自动替换为 Excel 宏 当我再次尝试此过程时 Word 文档现在打开 显示它是只读文档 我该如何保存我的 Word 模板以便对其进行编辑 另外 当我通过excel宏打开wo
  • 尝试使用 GNU GMP 库中的类型作为 Bison 的 yylval 类型时出错

    我正在尝试使用该类型mpz t来自 GMP 库的类型yylval通过在 Bison 文件中包含以下内容 define api value type mpz t 我检查了生成的解析器 它正确生成了该行typedef mpz t YYSTYPE
  • Django 1.4 - {{ request.user.username}} 不在模板中呈现

    在我看来 我可以打印request user username 但是在模板中 request user username 不会出现 为了简单起见 我从函数中删除了逻辑 并导入 render to response 和 RequestCont
  • 错误“超出最大更新深度。当组件在 useEffect 中调用 setState 时可能会发生这种情况”

    每当调用我的购物车组件 也如下所示 时 我都会遇到此错误 重复数千次 直到页面崩溃 index js 1 Warning Maximum update depth exceeded This can happen when a compon
  • 如何根据依赖关系进行排序?

    我有一个类 其中包含指向相同基类型的其他类的 依赖项 列表 class Foo Base dependencies class Bar Base dependencies Foo class Baz Base dependencies Ba
  • PyInstaller 文件无法执行脚本 - DistributionNotFound

    我正在尝试使用 PyInstaller 将 python 文件转换为可执行文件 该程序使用谷歌云翻译 API在语言之间翻译给定文本 跑步时python quicktrans py在终端中 程序运行良好 然后我跑了pyinstaller qu
  • Xcode - 未连接配置的 iOS 设备

    今天下午我正在使用 Xcode 并在我的设备上调试应用程序 效果很好 当我从办公室回到家并插入手机继续工作时 XCode 将不再让我在设备上进行调试 我收到的错误是 Error Starting Executable No provisio
  • Pandas 最大值指数

    我有一个混合了屏幕名称 推文 收藏夹等的 Pandas DataFrame 我想找到 favcount 的最大值 我已经完成了 并返回该 推文 的屏幕名称 df pd DataFrame df timestamp timestamp df
  • SSRS如何添加新行

    我正在从这样的存储过程创建一个字符串Name1 Name2 Name 3 等等 该字符串位于一列中 我想在 SSRS 报告中的新行中显示这些名称 例如 Name1 Name2 Name3 我尝试将字符串更改为 Name1 VbCrlf Na
  • 从 http 基本身份验证中排除特定的 cakephp 控制器

    我试图排除路径 URI 被基本 http 身份验证阻止 路径是 rest http example com rest 并代表 cakephp 3 应用程序的控制器 它不是一个真实的文件 而是一个由重写条件重写并由 webroot 目录中的
  • r sf包多边形内的质心

    我需要向多边形添加标签 并且通常使用质心 但是质心不会落在多边形内 我发现这个问题计算 SpatialPolygon 内 内部的质心但我正在使用 sf 包 下面是玩具数据 rm list ls all TRUE start with emp
  • 绘制矩形和Interface Builder之间的颜色差异?

    简而言之 我在界面生成器中有 2 个视图 其中一个使用界面生成器中的 RGB 滑块设置为颜色 99 99 99 另一个视图以编程方式着色以实现某种形状 我使用以下方式填充它 Obviously this is in drawRect UIC
  • 如何修改非常大的 zip 中的单个文件而不重写整个 zip?

    我有包含巨大文件的大型 zip 文件 zip 存档中有一些需要修改的 元数据 文本文件 但是 无法提取整个 zip 并重新压缩它 我需要在 zip 中找到目标文本文件 对其进行编辑 并可能将更改附加到 zip 文件中 文本文件的文件名始终相
  • 从 Option>> 解开并访问 T

    我正在尝试用 Rust 解决一些 Leetcode 问题 然而 我在使用 LeetCode 时遇到了一些困难TreeNode执行 use std cell RefCell use std rc Rc TreeNode data struct
  • Android:WebView的方法goBack()显示空白页面

    我有一个 Android 应用程序 它使用 WebView 在活动中加载网页 我正在使用手动检索页面并使用 WebView 的使用BaseURL加载数据将其显示在屏幕上 那里的一切都很好 现在 我尝试覆盖 后退 按钮按下以模拟在 WebVi
  • 如何将 iOS React Native 模板转换为 Swift?

    我一直在阅读有关 React Native 的内容 并决定尝试一下 我在 Swift 方面很有经验 但从未尝试过 Objective C 所以我想将模板项目转换为使用 AppDelegate swift 我按照这个接受的答案中的解决方案进行
  • Fetch API 与 XMLHttpRequest

    我知道 Fetch API 使用Promises 并且它们都允许您向服务器发出 AJAX 请求 我读到 Fetch API 有一些额外的功能 这些功能在XMLHttpRequest 以及在 Fetch API polyfill 中 因为它基
  • 什么时候有人会使用工会?这是纯 C 时代的残余吗?

    我学到了 但并没有真正加入工会 我读过的每本 C 或 C 文本都会介绍它们 有时是顺便介绍 但它们往往很少给出关于为什么或在哪里使用它们的实际示例 工会何时在现代 甚至遗留 情况下有用 我唯一的两个猜测是 当您的工作空间非常有限时 或者当您
  • 如何将compile_commands.json与clang python绑定一起使用?

    我有以下脚本尝试打印给定 C 文件中的所有 AST 节点 当在具有简单包含的简单文件 同一目录中的头文件等 上使用它时 效果很好 usr bin env python from argparse import ArgumentParser