使用 libclang 查找匿名枚举

2023-12-28

有没有一种方法可以使用 libclang 检测匿名枚举而不依赖于拼写名称中的文本?

python 绑定到libclang https://raw.githubusercontent.com/llvm-mirror/clang/release_36/bindings/python/clang/cindex.py包含检测 C/C++ 结构或联合是否是匿名的功能clang.cindex.Cursor.is_anonymous https://github.com/llvm-mirror/clang/blob/release_37/bindings/python/clang/cindex.py#L1483,最终调用clang_Cursor_isAnonymous http://clang.llvm.org/doxygen/group__CINDEX__TYPES.html#ga6e0d2674d126fd43816ce3a80b592373.

以下示例演示了该问题。

import sys
from clang.cindex import *

def nodeinfo(n):
    return (n.kind, n.is_anonymous(), n.spelling, n.type.spelling)

idx = Index.create()

# translation unit parsed correctly
tu = idx.parse(sys.argv[1], ['-std=c++11'])
assert(len(tu.diagnostics) == 0)

for n in tu.cursor.walk_preorder():
    if n.kind == CursorKind.STRUCT_DECL and n.is_anonymous():
        print nodeinfo(n)
    if n.kind == CursorKind.UNION_DECL and n.is_anonymous():
        print nodeinfo(n)
    if n.kind == CursorKind.ENUM_DECL:
        if n.is_anonymous():
            print nodeinfo(n)
        else:
            print 'INCORRECT', nodeinfo(n)

当在sample.cpp上运行时

enum
{
    VAL = 1
};

struct s
{
    struct {};
    union
    {
        int x;
        float y;
    };
};

Gives:

INCORRECT (CursorKind.ENUM_DECL, False, '', '(anonymous enum at sample1.cpp:1:1)')
(CursorKind.STRUCT_DECL, True, '', 's::(anonymous struct at sample1.cpp:8:5)')
(CursorKind.UNION_DECL, True, '', 's::(anonymous union at sample1.cpp:9:5)')

很遗憾clang_Cursor_isAnonymous仅适用于结构和联合,正如您可以从 clang 源代码中看到的那样工具/libclang/CXType.cpp https://github.com/llvm-mirror/clang/blob/master/tools/libclang/CXType.cpp#L978

unsigned clang_Cursor_isAnonymous(CXCursor C){
  if (!clang_isDeclaration(C.kind))
    return 0;
  const Decl *D = cxcursor::getCursorDecl(C);
  if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D))
    return FD->isAnonymousStructOrUnion();
  return 0;
}

所以回退到conf.lib.clang_Cursor_isAnonymous in clang.cindex.Cursor.is_anonymous https://github.com/llvm-mirror/clang/blob/release_37/bindings/python/clang/cindex.py#L1483没有做任何新的事情,因为游标类型已经根据FIELD_DECL进行了检查(仅适用于结构和联合)

def is_anonymous(self):
        """
        Check if the record is anonymous.
        """
        if self.kind == CursorKind.FIELD_DECL:
            return self.type.get_declaration().is_anonymous()
        return conf.lib.clang_Cursor_isAnonymous(self)

您可以尝试提取当前元素的标识符(n在您的示例中)并检查它是否存在或为空

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 libclang 查找匿名枚举 的相关文章

  • 不屈不挠的野兽:一个二维字符数组,位于结构内部,位于非托管 dll 的内部

    我束紧腰 冒险进入了遗产之地 砍倒 召唤并集结了各种野兽 现在我站在了一个如此凶猛的生物面前 据我对我的弟兄们进行的详尽调查来看 我现在所面对的生物是如此凶猛 武器中 没有一个代码战士能够幸存 以下是详细信息 我试图将结构内部的二维字符数组
  • WPF:BinaryFormatter 可以序列化 FlowDocument 实例吗?

    我喜欢使用binaryformatter来序列化流文档 但这是例外 Serializable public class BinFlow public FlowDocument my get set BinFlow myBF new BinF
  • 如何使用包含 \n 的 .txt 创建一维列表?

    我想读取一个文本文件并将文件的每个元素放入一个列表中 而不是为文件中的每一行都有一个单独的列表 例如 如果文件是 你好我的名字 Is Joe 我希望列表是 你好 我的名字是 Joe 而不是 你好 我的名字 是乔 这是我到目前为止所拥有的 d
  • django 创建多类型用户的最佳方法

    我想在 django 中创建多个用户 我想知道哪种方法是最好的 class Teachers models Model user models ForeignKey User is teacher models BooleanField d
  • 了解C/C++中函数调用的堆栈框架? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我也是 C C 和汇编语言的新手 这
  • allocator.construct 循环是否等于 std::uninitialized_copy?

    在此背景下T是某种类型并且allocator是该类型的分配器对象 默认情况下是std allocator
  • cygwin $'\r':命令未找到错误

    我稍微修改了一个项目 在调试下它运行得很好 当我尝试在不调试的情况下构建它时 它显示错误 无法修复它 make Making all in third party make 1 Entering directory cygdrive c U
  • OpenXML 如何获取范围内的单元格

    请帮助我获取范围内的单元格 例如从 A 1 到 E 11 都是矩形单元格 目前我的理想是 Worksheet worksheet GetWorksheet document sheetName SheetData sheetData wor
  • 熊猫:SettingWithCopyWarning:[重复]

    这个问题在这里已经有答案了 我尝试使用以下代码将列转换为 日期 df DATE pd to datetime df DATE or df DATE pd to datetime df DATE 但我收到以下错误 Users xyz anac
  • 如何防止用户生成的 Sql 查询上的 Sql 注入

    我有一个项目 私有的 ASP net 网站 受 https 密码保护 其中要求之一是用户能够输入直接查询数据库的 Sql 查询 我需要能够允许这些查询 同时防止它们对数据库本身造成损坏 以及访问或更新它们不应该访问 更新的数据 我制定了以下
  • Python Pandas 系列失败日期时间

    我认为这一定是 pandas 的失败 有一个 pandas 系列 v 18 1 和 19 如果我为该系列分配一个日期 第一次将其添加为 int 错误 第二次将其添加为 int 错误 添加为日期时间 正确 我无法理解原因 例如使用以下代码 i
  • Python代码检测OS X El Capitan中的暗模式以更改状态栏菜单图标

    我有目标 C 代码来检测暗模式以更改状态栏 NSDistributedNotificationCenter defaultCenter addObserver self selector selector darkModeChanged n
  • 在一个数据库请求中连接 IQueryable 集合

    我使用实体框架 我需要连接两个集合 例如 IQueryable
  • Django - 以表单形式访问 request.session

    我按如下方式调用表单 然后将其传递给模板 f UserProfileConfig request 我需要能够访问表单中的 request session 所以首先我尝试了这个 class UserProfileConfig forms Fo
  • 需要帮助编写扭曲的代理

    我想编写一个简单的代理 可以对请求页面正文中的文本进行打乱 我已经阅读了 stackoverflow 上的部分扭曲文档和其他一些类似的问题 但我有点菜鸟 所以我仍然不明白 我现在就是这样 不知道如何访问和修改页面 from twisted
  • 将base64字符串转换为图像c#时出错

    我想在我的网页上显示图像 并单击应该下载的链接按钮 存储的图像文件以二进制格式存储在db中 将 base64 字符串转换为图像时显示错误 详细信息如下 帮助我找到合适的解决方案 谢谢 Error Code protected void Pa
  • 如何在google colaboratory上使用GPU升级tensorflow

    目前google colaboratory使用tensorflow 1 4 1 我想升级到1 5 0版本 每次当我执行时 pip install upgrade tensorflow命令 notebook实例成功将tensorflow版本升
  • python散景中的反转轴

    我正在尝试反转 y 轴并在散景散点图中设置 x 和 y 的范围 我在用 BokehPlot bokeh scatter data df x range min utc max utc y range min val max val 我收到错
  • 从多个 .csv 文件创建混淆矩阵

    我有很多具有以下格式的 csv 文件 338 800 338 550 339 670 340 600 327 500 301 430 299 350 284 339 284 338 283 335 283 330 283 310 282 3
  • Microsoft Graph API 调用无限期挂起

    我正在尝试使用 Microsoft Graph 查询 Azure Active Directory 用户信息 我可以很好地进行身份验证 但是当我尝试查询用户信息时client Users我的应用程序无限期挂起 没有超时 没有错误 只是挂起

随机推荐