[cfe-dev] How can I judge whether a "CFGElement" is in the for or while loop?

Jordan Rose jordan_rose at apple.com
Fri Nov 15 09:43:14 PST 2013


I don’t think there’s a good way to do this in general from the CFG. Here’s another example for you:

if (condition())
	doSomething()

The “doSomething()” call is in its own basic block, but you can’t just add another statement there, because there are no parentheses. I suppose that could be against your style guidelines.

Another problem is that the CFG contains implicit statements inserted by the compiler, which may not have valid source locations.

If you don’t strictly need this on every basic block, you could instead do an AST walk, and insert this at the beginning of all brace statements. That’s not quite the same thing (most importantly it doesn’t check the “join” block after an if-else), but it could get you pretty far.

Another question: does this have to be done as a source-to-source transform, or could you accomplish this with an instrumentation pass at the LLVM IR level?

Jordan


On Nov 4, 2013, at 9:47 , Shuai Wang <wangshuai901 at gmail.com> wrote:

> Hi Jordan,
> 
> Thank you for your kindly reply!
> 
> What I am trying to do is a source to source transformation of C/CPP code.
> 
> I want to find all the basic block and insert certain code to check the integrity of this block.
> 
> See, for Loop statement:
> 
>      for (int a = 0, int i = 0; i< 5; i++)
> {
>               a++; 
> }
> 
> what my transform goal is :
> 
> Check code;
> for (int a = 0, int i = 0; i < 5; i++)
> {
>         Check code;
>         a++;
> } 
> Check code;
> 
> but when I just try to insert certain check code directly to the fist line of each basic block, this is what I got:
> 
> Check code;
> for (int a = , int i= 0;  check code; i < 5; check code; i++)
> {
>     check code;
>     a++;
> }
> check code;
> 
> See, the grammar of C/CPP have been broken by me… 
> 
> Could you give me some advice on how to achieve my transform goal? I really appreciate it..
> 
> THanks
> SHuai
> 
> 
> 
> On Nov 4, 2013, at 12:33 PM, Jordan Rose <jordan_rose at apple.com> wrote:
> 
>> Hi, Shuai. Unfortunately, there's not a great answer for this. Part of the philosophical reason for that is "goto" (or even "switch"), where you can jump "into" a loop, and then possibly leave it again before even evaluating the loop condition.
>> 
>> The analyzer diagnostics occasionally find it useful to know this, but they're just using the ParentMap of the AnalysisDeclContext for the function to walk up from the statement to see if there's an enclosing loop.
>> 
>> All of that said, what do you need this for? It's possible there's a better way.
>> Jordan
>> 
>> 
>> On Nov 4, 2013, at 8:10 , Shuai Wang <wangshuai901 at gmail.com> wrote:
>> 
>>> Hello,
>>> 
>>> I have got the CFG and I am analysing the CFGBlock of it.
>>> 
>>> I notice that in Clang, a for loop will be divide into several basic blocks like :
>>> 
>>> 
>>>       for (int a = 0, int i = 0; i< 5; i++)
>>>               a++; 
>>> 
>>> Then we can got:
>>>       B1:  int a=0, int i =0;
>>>       B2:  i < 5;
>>>       B3:  i ++;
>>>       B4:  a++;
>>> 
>>> and so on by using clang -cc1 -analyze -analyzer-checker=debug.DumpCFG xxx.c
>>> 
>>> My question is , when a got the first CFGElement of CFGBlock by using 
>>> CFGBlock.front();
>>> How can I know this CFGElement is in the for loop?
>>> (Which means my current analyzing block is B2 or B3 )
>>> 
>>> Thanks,
>>> Shuai 
>>> 
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131115/9f1b16dd/attachment.html>


More information about the cfe-dev mailing list