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

Jonathan Coe via cfe-dev cfe-dev at lists.llvm.org
Thu May 3 02:09:13 PDT 2018


I think you will have to expose the function(s) you need through libclang
and then clang.cindex.

Functions are generally exposed as and when they are needed.

You can look at https://reviews.llvm.org/D34091 to see how this is done.

let me know if you need any further input.

regards,

Jon

On 3 May 2018 at 09:50, Joxean Koret via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> 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
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180503/659a5ceb/attachment.html>


More information about the cfe-dev mailing list