[cfe-users] Parsing C code looking for Basic Blocks

Anna Zaks ganna at apple.com
Wed Mar 20 10:43:30 PDT 2013


On Mar 20, 2013, at 1:35 AM, Harald Servat <harald.servat at bsc.es> wrote:

> El dt 19 de 03 de 2013 a les 10:42 -0700, en/na Anna Zaks va escriure:
>> 
>> On Mar 19, 2013, at 8:43 AM, Harald Servat <harald.servat at bsc.es>
>> wrote:
>> 
>>> El dt 19 de 03 de 2013 a les 12:43 +0100, en/na Harald Servat va
>>> escriure:
>>>> Dear list,
>>>> 
>>>> I'm a newcommer to Clang/LLVM and I'm not sure whether this is
>>>> the
>>>> correct place to ask this topic. If it's not, would you mind to
>>>> point me
>>>> to the correct place/list?
>>>> 
>>>> I'm looking for a mechanism to detect basic blocks in C codes
>>>> (optionally C++, and eventually Fortran, in a future). I'm
>>>> wondering
>>>> whether how can Clang help me? Is it an appropriate tool for doing
>>>> such
>>>> thing? I'm not looking for something very difficult (I think). 
>>>> 
>>>> Imagine I have the following code (line numbering on the left,
>>>> source
>>>> code on the right)
>>>> 
>>>> 01: void foo(void)
>>>> 02: {
>>>> 03:    int a = 10;
>>>> 04:    int b = 11;
>>>> 05:    if (a > b)
>>>> 06:    {
>>>> 07:       printf ("a is greater than b\n");
>>>> 08:    }
>>>> 09:    else
>>>> 10:   {
>>>> 11:      printf ("b is greater or equal than a\n");
>>>> 12:   }
>>>> 13: }
>>>> 
>>>> I'd like to have a obtain a list of pairs that delimit the basic
>>>> blocks of the code. For the previous example, for instance it
>>>> should
>>>> return something like : [01-05], [06-08], [10-12]. 
>>>> 
>>>> Is there any Clang mechanism to provide such information? If not,
>>>> is
>>>> it possible to do it through Clang?
>>>> 
>>>> Thank you very much!
>>>> 
>>>> 
>>>> 
>>>> WARNING / LEGAL TEXT: This message is intended only for the use of
>>>> the
>>>> individual or entity to which it is addressed and may contain
>>>> information which is privileged, confidential, proprietary, or
>>>> exempt
>>>> from disclosure under applicable law. If you are not the intended
>>>> recipient or the person responsible for delivering the message to
>>>> the
>>>> intended recipient, you are strictly prohibited from disclosing,
>>>> distributing, copying, or in any way using this message. If you
>>>> have
>>>> received this communication in error, please notify the sender and
>>>> destroy and delete any copies you may have received.
>>>> 
>>>> http://www.bsc.es/disclaimer
>>>> _______________________________________________
>>>> cfe-users mailing list
>>>> cfe-users at cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
>>> 
>>> Hello,
>>> 
>>> I found the following command exceptionally useful for the purposes
>>> I
>>> wanted:
>>> 
>>>   clang -cc1 -ast-dump file.c
>>> 
>>> 
>> This would dump AST, not Basic Blocks from Control Flow Graph. Clang
>> does have CFG as well. 
>> To view/dump CFG use debug.ViewCFG or debug.DumpCFG checkers: 
>> $ clang -cc1 -analyze -analyzer-checker=debug.Dump/CFG test.c
>> 
>> 
>> Note, this is a debugging facility and not a user feature of clang, so
>> the interface is not guaranteed to be stable.
> 
>  Thank you for pointing that Anna. I've tried what you mention, and now
> I realize that I was looking for the AST tree instead of basic blocks.
> To generate a list of basic blocks you need to compile the file and I
> don't want to compile it fully (i.e. I don't want to generate an object
> file).

Just to make it clear, the CFG used by the dump method is produced by the clang front end and references elements of the AST (no object file generation is done).

Anna.

> I simply want to do some kind of code blocks breakdown, and thus
> -ast-dump fulfills my needs.
> 
> Best regards.
> 
> 
> 
> 
> 
> WARNING / LEGAL TEXT: This message is intended only for the use of the
> individual or entity to which it is addressed and may contain
> information which is privileged, confidential, proprietary, or exempt
> from disclosure under applicable law. If you are not the intended
> recipient or the person responsible for delivering the message to the
> intended recipient, you are strictly prohibited from disclosing,
> distributing, copying, or in any way using this message. If you have
> received this communication in error, please notify the sender and
> destroy and delete any copies you may have received.
> 
> http://www.bsc.es/disclaimer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20130320/8cff4bdb/attachment.html>


More information about the cfe-users mailing list