Skip to contentSkip to conversation

problem with the order of added statements

Cocci Nic Volanschi (R&D) 2 participants 2 messages Aug 28, 2013

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to instrument a program with traces, by adding calls to trace() at the beginning of each block. In particular, at the beginning of an "if" branch and after the "if". So my rule is very simple:

--- trace.cocci:
@@ statement s1; @@
if(...)
+{trace("ifth");
s1
+}
+trace("endif");

The rule works as expected except when the "then" branch contains a control flow statement such as return or break. For example, on the following file:

--- trace.c:
short VerDate(char *pcDate)
{
short sRetour = 0;

  if ( pcDate == 0 ) {
    sRetour = 1;
  }
  if ( pcDate == 0 )
    sRetour = 1;
  if ( pcDate == 0 ) {
    sRetour = 1;
    return sRetour;

}
return sRetour;
}

the rule generates the following instrumented program:

--- trace.c.cocci-res:
short VerDate(char *pcDate)
{
short sRetour = 0;
if ( pcDate == 0 ) { trace("ifth");

  {
    sRetour = 1;

}
}
trace("endif");
if ( pcDate == 0 ) { trace("ifth");
sRetour = 1;
}
trace("endif");
if ( pcDate == 0 ) { trace("ifth");
{
}

trace("endif");
    sRetour = 1;
    return sRetour;

}
return sRetour;
}

That is:
- the trace("ifth") is correctly added in all three cases at the beginning of the "if-then" branch
- the trace("endif") is correcty added in the first two cases after the "if", BUT in the middle of the third "if"!

Is the problem in my rule or is in its application?

Cheers,
Nic.

PS. I sort of turned around the problem by splitting my rule in two:

--- trace2.cocci:
@@ statement s1; @@
if(...)
+{trace("ifth");
s1
+}

@@ statement s1; @@
if(...) s1
+trace("endif");

In this case, the third trace("endif") is correctly added after the "if", but the produced code is still a bit strange, as it contains an emtpy block:

if ( pcDate == 0 ) { trace("ifth");
{
}

    sRetour = 1;
    return sRetour;

}
trace("endif");

#1 lore raw
Quoted 9 lines

PS. I sort of turned around the problem by splitting my rule in two:

--- trace2.cocci:
@@ statement s1; @@
if(...)

(quoted diff) +2 -0
+{trace("ifth");
  s1
+}
Quoted 3 lines

@@ statement s1; @@
if(...) s1
+trace("endif");

I was going to suggest this solution. The empty block, though seems strange. I will look into it.

julia

Quoted 18 lines

In this case, the third trace("endif") is correctly added after the "if", but the produced code is still a bit strange, as it contains an emtpy block:

if ( pcDate == 0 ) { trace("ifth");
{
}

    sRetour = 1;
    return sRetour;

}
trace("endif");

_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci