I’m having a difficult time with what, in my mind, should be a fairly simple task: Using the python bindings to libclang
I want to get the dimensions for a multi-dimensional array field of a POD C++ structure. When I traverse the AST, I can drill down the cursor containing the array field declaration, and I can even see that it has two child nodes containing, what I assume, are the sizes of each of its dimensions… but I’ve had no luck accessing the size as a value. Below is a minimal example of my attempt at accessing the size:
test.hpp
#pragma once
struct my_struct {
int mdarr[10][20];
};
test.py
import clang.cindex as cl
def process(c):
if c.kind in [cl.CursorKind.STRUCT_DECL, cl.CursorKind.CLASS_DECL]:
print("Found struct: ", c.spelling)
for field in c.type.get_fields():
print("Found field: ", field.spelling)
# Returns size of first dimension but not the second dimension
print("Array size: ", field.type.get_array_size())
for child in field.get_children():
# Prints an empty string
print("Found child: ", child.spelling)
# How do I extract the value from the `INTEGER_LITERAL`?
print("Child cursor kind: ", child.kind)
return
for child in c.get_children():
process(child)
idx = cl.Index.create()
tu = idx.parse("test.hpp")
process(tu.cursor)
Output
Found struct: my_struct
Found field: mdarr
Array size: 10
Found child:
Child cursor kind: CursorKind.INTEGER_LITERAL
Found child:
Child cursor kind: CursorKind.INTEGER_LITERAL
Is there an easy way to extract each dimension’s size using libclang
?