我使用<强> CLAN.cEng/Stult>库解析C++源代码。
使用get_children()
函数,我试图打印所有解析的行
这是我的Python代码
import clang.cindex
def print_all(cursor, i):
print('\t' * i, cursor.kind, ':', cursor.spelling, '(', cursor.location, ')')
for child in cursor.get_children():
print_all(child, i+1)
if __name__=='__main__':
tu = clang.cindex.Index.create().parse(filename)
print_all(tu.cursor, 0)
这是目标C++源代码。
void bad()
{
char * data;
data = NULL;
data = new char[50];
data[0] = '\0';
const int& baseObject = char_ncpy_81_bad();
baseObject.action(data);
}
但是,它不会打印所有行。 打印行如下
Cursor.Kind.Function_DECL : bad ( <..., line 9, column 6> )
CursorKind.COMPOUND_STMT : ( <..., line 10, column 1> )
CursorKind.DECL_STMT : ( <..., line 11, column 5> )
CursorKind.VAR_DECL : data ( <..., line 11, column 12> )
CursorKind.BINARY_OPERATOR : ( <..., line 14, column 5> )
CursorKind.DECL_REF_EXPR : data ( <..., line 14, column 5> )
CursorKind.CXX_DELETE_EXPR : ( <..., line 14, column 12> )
CursorKind.UNEXPOSED_EXPR : ( <..., line 14, column 21> )
CursorKind.Integer_LITERAL : ( <..., line 14, column 21> )
CursorKind.BINARY_OPERATOR : ( <..., line 15, column 5> )
CursorKind.ARRAY_SUBSCRIPT_EXPR : ( <..., line 15, column 5> )
CursorKind.UNEXPOSED_EXPR : data ( <..., line 15, column 5> )
CursorKind.DECL_REF_EXPR : data ( <..., line 15, column 5> )
CursorKind.INTEGER_LITERAL : ( <..., line 15, column 5> )
CursorKind.CHARACTER_LITERAL : ( <..., line 15, column 5> )
CursorKind.DECL_STMT : ( <..., line 16, column 5> )
CursorKind.VAR_DECL : baseObject ( <..., line 16, column 16> )
如您所见,某些行没有出现在结果中
如何打印所有的C++代码行? 或者Python上有C++源代码解析库?
您可以使用光标中的标记来获取相应的代码:
但是,请记住,标记“只是”来自lexer的结果。因此,像宏观扩张这样的事情将无法正常工作
相关问题 更多 >
编程相关推荐