如何解决Python - 检查 TypeKinds 是否匹配指针的最便携方法
我想要的
给定一个 AST,我想识别所有包含指向特定整数类型的指针的游标。例如,以下所有内容都应标记为至少某种类型的 char *
。
unsigned char arrayBob[][3];
char arrayFred[];
char *arrayAlice;
我有什么
import clang.cindex as clx
def checkMatchingSpecificPointerType(cursor,setofExpectedTypeKinds):
"""
Checks that cursor is AT LEAST of a particular pointer kind,for example char *
"""
objType = cursor.canonical.type.get_canonical()
if objType.kind in setofexpectedTypeKinds: return
if objType.kind == clx.TypeKind.INCOMPLETEARRAY:
objType = objType.element_type
# get rid of any nested array types
while (objType.kind == clx.TypeKind.INCOMPLETEARRAY or objType.kind == clx.TypeKind.CONSTANTARRAY):
objType = objType.element_type
if objType.kind == TypeKind.POINTER:
objType = objType.get_pointee()
while objType.kind == clx.TypeKind.POINTER:
objType = objType.get_pointee()
if objType.kind not in setofexpectedTypeKinds:
print("\n".join(["Argument type {otype} is not in expected types:".format(otype=objType),"\n".join(expectedTypeKinds)]))
return
setofExpectedTypeKinds = set([clx.TypeKind.CHAR_S])
exSrc = """
int main(int argc,char *argv[])
{
unsigned char arrayBob[][3];
char arrayFred[];
char *arrayAlice;
return 0;
}
"""
index = clx.Index.create()
tu = index.parse("tmpSrc.cpp",args=["-I/usr/local/include"],unsaved_files=[("tmpSrc.cpp",exSrc)])
for cursor in tu.cursor.walk_preorder():
checkMatchingSpecificPointerType(cursor,setofExpectedTypeKinds)
这对 arrayFred[]
和 *arrayAlice
正常工作,但不适用于 arrayBob
。 arrayBob
触发打印语句,因为结果 objType.kind
是 TypeKind.UCHAR
。
这是没有正确提取指针类型的问题吗?还是我的 expectedType 空间不够宽?假设 TypeKind.CHAR_S
代表 char *
类型是否错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。