title: How can I visit my statements with vstmt_aux in Frama-C
body: <p>I am working on a plugin for scalar replacement. I have some difficulty with the statement visitor. I would like to analyze the instructions that are in loops, for each instruction I need to know if it is included in a loop, if this loop itself is included or not in another loop. Here is for example a simple example of my vstmt_aux:</p>
  <pre><code>method! vstmt_aux s = 
  match s.skind with
  | Loop(_, body, l, _, _) -&gt; begin
    let lp = (fst l).Lexing.pos_lnum in 
    Format.printf &quot;Find outer loop at line %d @.\n&quot; lp;
    let bst = body.bstmts in List.iter(fun vbody -&gt; 
      match vbody.skind with
      | Loop(_, subbody, lsub, _, _) -&gt; begin
        let ls = (fst lsub).Lexing.pos_lnum in 
        Format.printf &quot;Find inner loop at line %d @.\n&quot; ls;
        let sub_st = subbody.bstmts in List.iter(fun ss_body -&gt;
          match ss_body.skind with
          | Instr(ii) -&gt; Format.printf &quot;Find instruction %a inside inner loop. @.\n&quot; Printer.pp_instr ii 
          | Loop(_, l_sub, lss, _, _) -&gt; begin
            let nss = (fst lss).Lexing.pos_lnum in 
            Format.printf &quot;Find inner inner loop at line %d @.\n&quot; nss;
            let ss_st = l_sub.bstmts in List.iter(fun ss -&gt;
              match ss.skind with
              | Instr(iii) -&gt; Format.printf &quot;Find instruction %a inner inner loop. @.\n&quot; Printer.pp_instr iii
              | _ -&gt; ()
            ) ss_st
          end 
          | _ -&gt; ()
        ) sub_st;
      end
      | Instr(iloop) -&gt; Format.printf &quot;Find instruction %a inside outer loop. @.\n&quot; Printer.pp_instr iloop
      | _ -&gt; ()
    ) bst;
    Cil.SkipChildren
  end
  | Instr(i) -&gt; Format.printf &quot;Find instruction %a. @.\n&quot; Printer.pp_instr i; Cil.SkipChildren
  | _ -&gt; Cil.DoChildren
  </code></pre>
  <p>And a example of code I want to analyze:</p>
  <pre><code>void test(int ni, int nj, int nk, float alpha, float *tmp, float *A) {
    int i, j, k;
    for (i = 0; i &lt; ni; i++) 
    for (j = 0; j &lt; nk; j++) 
      A[i * nk + j] = (float) ((i*j+1) % ni) / ni;
  
    for (i = 0; i &lt; ni; i++) 
      for (j = 0; j &lt; nj; j++) {
        tmp[i * nj + j] = 0.0;
        for (k = 0; k &lt; nk; ++k) 
          tmp[i * nj + j] += alpha * A[i * nk + k] * A[i * nk + k];
      }
  }
  </code></pre>
  <p>When I run this script on this c code, only instruction <code>A[i * nk + j] = (float) ((i*j+1) % ni) / ni;</code> is detected.</p>
  <blockquote>
  <p>Find instruction *(A + (i * nk + j)) = (float)((i * j + 1) % ni) / (float)ni; inside inner loop.</p>
  </blockquote>
  <p>When I use <code>DoChildren</code> instead of <code>SkipChildren</code> in Loop pattern case, all instructions inside loops are detected more than one time. For example:</p>
  <ul>
  <li>instruction <code>A[i * nk + j] = (float) ((i*j+1) % ni) / ni;</code> is detected three times:</li>
  </ul>
  <blockquote>
  <p>Find instruction *(A + (i * nk + j)) = (float)((i * j + 1) % ni) / (float)ni; inside inner loop.</p>
  <p>Find instruction *(A + (i * nk + j)) = (float)((i * j + 1) % ni) / (float)ni; inside outer loop.</p>
  <p>Find instruction *(A + (i * nk + j)) = (float)((i * j + 1) % ni) / (float)ni;.</p>
  </blockquote>
  <p>where only the first message is true.</p>
  <ul>
  <li>instruction <code>tmp[i * nj + j] = 0.0;</code> is detected one time but output message is not correct (while this instruction is inside an inner loop)</li>
  </ul>
  <blockquote>
  <p>Find instruction *(tmp + (i * nj + j)) = (float)0.0;.</p>
  </blockquote>
  <ul>
  <li>same thing happens for instruction <code>tmp[i * nj + j] += alpha * A[i * nk + k] * B[k * nj + j];</code>.</li>
  </ul>
  <p>When I add braces around instructions of each loop plus DoChildren, all instructions are detected by the pattern <code>| Instr(i) -&gt; Format.printf &quot;Find instruction %a. @.\n&quot; Printer.pp_instr i; Cil.SkipChildren</code>.</p>
  <p>Do you have any idea how I can solve this problem?
  Thanks</p>