简体   繁体   English

使用Frama-C脚本打印ACSL注释

[英]Print ACSL Annotations with Frama-C script

I am learning how to develop a Frama-C plugin. 我正在学习如何开发Frama-C插件。 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. 在阅读Frama-C Developer手册并执行CFG插件示例后,我尝试执行一个基本脚本,在C文件中打印所有注释。 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. 即使输入文件具有ACSL注释并且从不打印注释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:

 $ 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

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 . requiresensures是功能合同的一部分,并附加到功能g ,而不是任何g陈述。

An annotation that would be attached to a statement would for instance be /*@ assert x == 150; */ 附加到语句的注释例如是/*@ assert x == 150; */ /*@ assert x == 150; */ after the line x=x+50; /*@ assert x == 150; */x=x+50; .

If I modify condi.c to insert this assertion, then with the same commandline, I get: 如果我修改condi.c来插入这个断言,那么使用相同的命令行,我得到:

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. 您的脚本似乎正在按预期工作,其值为“expected”,对应于打印附加到语句的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM