问题描述
我想要的
给定一个 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 (将#修改为@)