0

I would like to print out the if/switch statement in c++ source code, currently I'm using the libclang python API. I figured that the .spelling is not the correct way to print out the statement, any idea how to solve it?

python scripts:

def print_if(nodes):
    for node in nodes:
        if node.kind == clang.cindex.CursorKind.IF_STMT:
            print("if_stmt is here.")
            print(node.spelling)
        print_if(node.get_children())
def main(argv):
    tu = index.parse(argv[1], args=['-std=c++17'])
    print_if(tu.cursor.get_children())
if __name__ == "__main__":
    main(sys.argv)

test case:

int func(int a, int b)
{
    if (a+b) { return 1; }
    return 0;
}

result:

$ python get_enum_info.py small.cpp 
if_stmt is here.

So here the program got an if statement but .spelling does not print the string "if (a+b)", the same in the return statement, any idea on this?

Crystal
  • 85
  • 9

0 Answers0