Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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
Python clang绑定:识别节点的大小并获取类型_Python_Clang_Sizeof - Fatal编程技术网

Python clang绑定:识别节点的大小并获取类型

Python clang绑定:识别节点的大小并获取类型,python,clang,sizeof,Python,Clang,Sizeof,我正在一些C代码上使用libclang python绑定。我不知道如何获取有关sizeof节点的信息。我找不到任何相关文件。实验上: sizeof节点的种类为CursorKind.CXX\u-uniary\u-EXPR sizeof(type)没有子项sizeof expression有一个子项,即expression 到目前为止是有道理的,但是接下来呢?如何区分sizeof与其他一元表达式?如何在sizeof(type)中获取类型 我使用的是libclang 10.0.0和Ubuntu 1

我正在一些C代码上使用libclang python绑定。我不知道如何获取有关
sizeof
节点的信息。我找不到任何相关文件。实验上:

  • sizeof
    节点的种类为
    CursorKind.CXX\u-uniary\u-EXPR
  • sizeof(type)
    没有子项
    sizeof expression
    有一个子项,即
    expression
到目前为止是有道理的,但是接下来呢?如何区分
sizeof
与其他一元表达式?如何在
sizeof(type)
中获取类型

我使用的是libclang 10.0.0和Ubuntu 18.04中附带的相应Python绑定(
python3-clang-10

下面是一个小的C源文件
foo.C
,供您试用:

#包括
尺寸\u t foo(空){
返回sizeof(无符号短)+sizeof 1234;
}
来自ClangAST解析器的转储显示了我期望的所有信息

$clang-Xclang-ast dump-fsyntax only foo.c| tail-n 5
`-返回STMT 0x97ad48
`-二进制运算符0x97ad28'无符号长'+'
|-UnaryExprOrTypeTraitExpr 0x97acc8“无符号长”大小“无符号短”
`-UnaryExprOrTypeTraitExpr 0x97ad08“无符号长”大小为
`-集成文件0x97ace8'内部'1234
下面是我用来探索Python数据结构的Python代码

#/usr/bin/env蟒蛇3
进口clang.cindex
从clang.cindex导入CursorKind
def explore_attrs(节点):
对于排序中的属性(目录(节点)):
尝试:
value=getattr(节点,attr)
除了AssertionError作为e:#跳过其访问器仅适用于特定类型的属性
持续
如果可调用(值):继续
如果值为“无”:是否继续
打印(属性、报告(值))
def explore(节点):
“”“打印有关AST节点的信息。”“”
为节点中的子节点打印('Children:',[(child.kind,child.spelling))。获取_Children()
打印('Arguments:',list(node.get_Arguments()))
打印('Definition:',node.get_Definition())
浏览属性(节点)
def main():
index=clang.cindex.index.create()
tu=index.parse('foo.c')
对于tu.cursor.walk_preorder()中的节点:
如果node.kind==CursorKind.RETURN\u STMT:
children=list(node.get\u children())
#预期sizeof(…)+sizeof(…)
断言子项[0]。种类==CursorKind.BINARY\u运算符
lhs,rhs=子项[0]。获取子项()
探索(lhs)
打印()
探索(rhs)
main()