[cfe-dev] CLang Python Bindings getCond(), getElse(), etc...

Joxean Koret via cfe-dev cfe-dev at lists.llvm.org
Thu May 3 01:50:28 PDT 2018


Hi guys!

I'm using the CLang Python Bindings for getting 'artifacts' from C/C++
source codes. One of the easiest things I have to do is to just get the
number of conditions, however, considering that getCond() is not
exposed in the Python bindings, I don't know how to do that.

For example, consider the following example C code:

if ( a == 1 && b == 2 )
  ...

I need to get '2', as there are 2 conditions. My Python code basically
traverses all elements in a translation unit similarly to the following
code:

(...)
def recurse_cursor(cur):
  for children in cur.get_children():
    if
children.kind == CursorKind.IF_STMT
    do_stuff(children)
   
recurse_cursor(children)

idx = clang.cindex.Index.create()
tu = idx.parse('my_source.c')

recurse_cursor(tu)
(...)

Then, in the function I named here 'do_stuff' I check if the element is
a CursorKind.IF_STMT and then I should do 'something' to count the
number of conditions in that IF_STMT. However, as getCond() is not
published, I don't know how to do that. I can try to get the tokens by
calling get_tokens() and iterating them to check if some comparison
operator is used (==, !=, etc...), but it will not work for expressions
like the following:

if ( some_bool )
  do_something()

So, how can I get the number of conditional expressions? Or,
alternatively, is there any way I can call IF_STMT.getCond() in Python
or other non exposed functions?

Thanks & regards,
Joxean Koret




More information about the cfe-dev mailing list