Domanda

I am learning how to develop a Frama-C plugin. After reading the Frama-C Developer manual and doing the CFG plugin example, I tried to do a basic script that prints all annotations in a C file. I came up with this:

open Cil_types

class print_annot out = object
    inherit Visitor.frama_c_inplace

method vstmt_aux s =
let annots = Annotations.code_annot s in
let anleng = List.length annots in
if anleng <= 0 then Format.fprintf out "Empty List\n"
  else
  Format.fprintf out "Number of Annotations: %d\n" anleng;
  List.iter (fun annot -> Format.fprintf out " -> s%d\n" annot.annot_id) annots;
  Cil.DoChildren
end

let run () =
let chan = open_out "annots.out" in
let fmt = Format.formatter_of_out_channel chan in
Visitor.visitFramacFileSameGlobals (new print_annot fmt) (Ast.get());
close_out chan

let () = Db.Main.extend run

It always says the list is empty even when the input file has ACSL annotations and never prints the annotations id. What am I doing wrong?

Edit:

An example with the following code:

 /*@ requires y >= 0;
 @ ensures \result >= 0;
 */

 int g(int y){
int x=0;

if(y>0){
    x=100;
    x=x+50;
    x=x-100;
}else{
    x = x - 150;
    x=x-100;
    x=x+100;
}
return x;
    }

    void main(){
int a = g(0);

 }

And invoking frama-c with:

 $ frama-c -load-script annot_script.ml condi.c

Gives the following output:

 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
 Empty List
È stato utile?

Soluzione

In your example, none of the annotations are attached to a statement. The requires and the ensures are part of a function contract and are attached to the function g, not to any statement of g.

An annotation that would be attached to a statement would for instance be /*@ assert x == 150; */ after the line x=x+50;.

If I modify condi.c to insert this assertion, then with the same commandline, I get:

Empty List
Empty List
Empty List
Empty List
Number of Annotations: 1
 -> s1
Empty List
Empty List
Empty List
Empty List
Empty List
Empty List
Empty List

It seems that your script is working as expected, for a value of “expected” that corresponds to printing annotations attached to statements.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top