OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to parse multi-dimensional arrays with python libclang bindings

  • Thread starter Thread starter Dudeman McGuybro
  • Start date Start date
D

Dudeman McGuybro

Guest
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​


Code:
#pragma once

struct my_struct {
    int mdarr[10][20];
};

test.py​


Code:
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​


Code:
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?
<p>I'm having a difficult time with what, in my mind, should be a fairly simple task: Using the python bindings to <code>libclang</code> 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:</p>
<h3>test.hpp</h3>
<pre class="lang-cpp prettyprint-override"><code>#pragma once

struct my_struct {
int mdarr[10][20];
};

</code></pre>
<h3>test.py</h3>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<h3>Output</h3>
<pre><code>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
</code></pre>
<p>Is there an easy way to extract each dimension's size using <code>libclang</code>?</p>
 
Top