[cfe-dev] Statements appear twice in CFG

Rachel HaCohen (rahacohe) via cfe-dev cfe-dev at lists.llvm.org
Wed Jun 7 07:17:38 PDT 2017


Hello all,
I'm writing a tool that uses the CFG with different functions.
I have a problem that when iterating over the elements of a CFGBlock, some of the statements appear twice.
It is most prevalent in function calls but I saw it in some other cases as well.
For example, the following code:

double i = pow(2.0, 3);

will create two elements in the CFGBlock:
[B1]
   1: pow(2., 3)
   2: double i = pow(2., 3);

And when I try to reproduce the code from this block (by iterating over the elements and pretty print them), I get two function calls:
pow(2., 3);
double i = pow(2., 3);

I saw the same behavior also in binary operator when the opcode is a comma and in conditional operator.
Here are some examples:
1)
int i, k = 10;
for (i = 0; i < 10; i++, k--){
// some code
}

The block that corresponds to the step of the for loop looks like that:
[B2]
   1: i++
   2: k--
   3: [B2.1], [B2.2]

Which creates the code:
i++;
k--;
i++, k--;

2)
return a > 0? a : -a;

Creates this block:
1: [B4.2] ? [B2.1] : [B3.1]
   2: return [B1.1];

Which pretty prints the following code:
a > 0 ? a : -a;
return a > 0 ? a : -a;


I suspect that the reason for this behavior is that the CFG is built recursively and some of the statements are visited and appended twice to the block.
In the case of the binary operator with comma, I solved this problem by removing the call to appendStmt in the code below (Which is in CFGBuilder::VisitBinaryOperator() in CFG.cpp file)

if (B->getOpcode() == BO_Comma) { // ,
    autoCreateBlock();
    //appendStmt(Block, B);
    addStmt(B->getRHS());
    return addStmt(B->getLHS());
  }

I tried to solve the problem also by changing VisitCallExpr() and VisitConditioalOperator() but couldn't solve it there.
I would appreciate any help and insights about this issue.
Thanks,
Rachel.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170607/d6fd57d7/attachment.html>


More information about the cfe-dev mailing list