<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>I don’t think there’s a good way to do this in general from the CFG. Here’s another example for you:</div><div><br></div><div>if (condition())</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>doSomething()</div><div><br></div><div>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.</div><div><br></div><div>Another problem is that the CFG contains implicit statements inserted by the compiler, which may not have valid source locations.</div><div><br></div><div>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.</div><div><br></div><div>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?</div><div><br></div><div>Jordan</div><div><br></div><br><div><div>On Nov 4, 2013, at 9:47 , Shuai Wang <<a href="mailto:wangshuai901@gmail.com">wangshuai901@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div><span style="color: rgb(34, 34, 34); font-family: arial; font-size: small; background-color: rgb(255, 255, 255);">Hi Jordan,</span></div><div><font color="#222222" face="arial" size="2"><span style="background-color: rgb(255, 255, 255);"><br></span></font></div><div><font color="#222222" face="arial" size="2"><span style="background-color: rgb(255, 255, 255);">Thank you for your kindly reply!<br></span></font><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;"><br></div><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;">What I am trying to do is a source to source transformation of C/CPP code.</div><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;"><br></div><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;">I want to find all the basic block and insert certain code to check the integrity of this block.</div><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;"><br></div><div style="color: rgb(34, 34, 34); font-family: arial; font-size: small;">See, for Loop statement:</div></div><div><br></div><div> for (int a = 0, int i = 0; i< 5; i++)</div><div>{</div><div> a++; </div><div>}</div><div><br></div><div>what my transform goal is :</div><div><br></div><div>Check code;</div><div>for (int a = 0, int i = 0; i < 5; i++)</div><div>{</div><div> Check code;</div><div> a++;</div><div>} </div><div>Check code;</div><div><br></div><div>but when I just try to insert certain check code directly to the fist line of each basic block, this is what I got:</div><div><br></div><div>Check code;</div><div>for (int a = , int i= 0; check code; i < 5; check code; i++)</div><div>{</div><div> check code;</div><div> a++;</div><div>}</div><div>check code;</div><div><br></div><div>See, the grammar of C/CPP have been broken by me… </div><div><br></div><div>Could you give me some advice on how to achieve my transform goal? I really appreciate it..</div><div><br></div><div>THanks</div><div>SHuai</div><div><br></div><div><br></div><br><div><div>On Nov 4, 2013, at 12:33 PM, Jordan Rose <<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">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.<br><br>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.<br><br>All of that said, what do you need this for? It's possible there's a better way.<br>Jordan<br><br><br>On Nov 4, 2013, at 8:10 , Shuai Wang <<a href="mailto:wangshuai901@gmail.com">wangshuai901@gmail.com</a>> wrote:<br><br><blockquote type="cite">Hello,<br><br>I have got the CFG and I am analysing the CFGBlock of it.<br><br>I notice that in Clang, a for loop will be divide into several basic blocks like :<br><br><br> for (int a = 0, int i = 0; i< 5; i++)<br> a++; <br><br>Then we can got:<br> B1: int a=0, int i =0;<br> B2: i < 5;<br> B3: i ++;<br> B4: a++;<br><br>and so on by using clang -cc1 -analyze -analyzer-checker=debug.DumpCFG xxx.c<br><br>My question is , when a got the first CFGElement of CFGBlock by using <br>CFGBlock.front();<br>How can I know this CFGElement is in the for loop?<br>(Which means my current analyzing block is B2 or B3 )<br><br>Thanks,<br>Shuai <br><br>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br></blockquote><br></blockquote></div><br></div></blockquote></div><br></body></html>