使用libclang查找匿名枚举 [英] Finding anonymous enums with libclang

查看:269
本文介绍了使用libclang查找匿名枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用libclang检测匿名枚举,而不依赖于拼写名称中的文本?

Is there a way to detect anonymous enumerations using libclang without relying on the text in the spelling name?

python绑定到 libclang 包含使用clang.cindex.Cursor.is_anonymous ,最后调用 clang_Cursor_isAnonymous

The python bindings to libclang include functionality to detect whether C/C++ structs or unions are anonymous using clang.cindex.Cursor.is_anonymous, which ends up calling clang_Cursor_isAnonymous.

以下示例演示了此问题。

The following sample demonstrates the issue.

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上运行的时候

Which when run on sample.cpp

enum
{
    VAL = 1
};

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

赠送:

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 只有结构和联合,你可以从cl源代码中看到 tools / libclang / CXType.cpp

Unfortunately clang_Cursor_isAnonymous works only with structs and unions as you can see from clang source code in tools/libclang/CXType.cpp

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 ,位于 clang。 cindex.Cursor.is_anonymous 没有做任何新的,因为已经针对FIELD_DECL检查了游标类型(仅对结构和联合来说是true)

So fallback to the conf.lib.clang_Cursor_isAnonymous in clang.cindex.Cursor.is_anonymous does nothing new as cursor type has been already checked against FIELD_DECL (which is true only for structs and unions)

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 ),然后检查其是否存在

You can try to extract identifier of current element (n in your sample) and check if it exists or is null

这篇关于使用libclang查找匿名枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆