繁体   English   中英

我可以在 Python 中使用 clang.cindex 解析打印 c++ 代码的所有行吗?

[英]Can I print all lines of c++ code using clang.cindex parsing in Python?

我使用clang.cindex库来解析 C++ 源代码。

使用get_children() function,我尝试打印所有解析的行。

这是我的 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++源码解析库?

您可以使用 cursor 中的令牌来获取相应的代码:

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

但是,请记住,标记“只是”来自词法分析器的结果。 因此,诸如宏扩展之类的事情将无法正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM