我能用Python中的CLAN.cEnter解析打印C++代码的所有行吗?

2024-09-27 09:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用<强> 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++源代码解析库?


Tags: data源代码linecolumnallcursorbadprint
1条回答
网友
1楼 · 发布于 2024-09-27 09:27:07

您可以使用光标中的标记来获取相应的代码:

def code_from_cursor(cursor: Cursor) -> List[str]:
    code = []
    line = ""
    prev_token = None
    for tok in cursor.get_tokens():
        if prev_token is None:
            prev_token = tok
        prev_location = prev_token.location
        prev_token_end_col = prev_location.column + len(prev_token.spelling)
        cur_location = tok.location
        if cur_location.line > prev_location.line:
            code.append(line)
            line = " " * (cur_location.column - 1)
        else:
            if cur_location.column > (prev_token_end_col):
                line += " "
        line += tok.spelling
        prev_token = tok
    if len(line.strip()) > 0:
        code.append(line)
    return code

但是,请记住,标记“只是”来自lexer的结果。因此,像宏观扩张这样的事情将无法正常工作

相关问题 更多 >

    热门问题