【问题标题】:Can I print all lines of c++ code using clang.cindex parsing in Python?我可以在 Python 中使用 clang.cindex 解析打印所有 C++ 代码行吗?
【发布时间】:2021-02-03 08:41:55
【问题描述】:

我使用 clang.cindex 库来解析 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++源码解析库?

【问题讨论】:

    标签: python c++ clang


    【解决方案1】:

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

    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
    

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

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多