可以使用frama-c进行头文件分析吗?

时间:2013-02-06 06:54:27

标签: ocaml frama-c

我在将frama-c看作是在OCaml中处理C头文件的一种方法(例如,用于生成语言绑定)。它很有吸引力,因为它看起来像是一个记录良好且维护良好的项目。然而,经过大量的谷歌搜索和搜索文档后,我找不到任何适合此目的的东西。我只是错过了正确的方法,或者它是否超出了frama-c的范围?与其他一些插件相比,它似乎是一件相当简单的事情。

2 个答案:

答案 0 :(得分:5)

正如Pascal所说,我认为不可能从命令行开始,但是因为你必须编写一些代码,你可以设置标志Rmtmps.keepUnused。这是一个可用于查看声明的脚本:

let main () =
  Rmtmps.keepUnused := true;
  let file = File.from_filename "t.h" in
  let () = File.init_from_c_files [ file ] in
  let _ast = Ast.get () in
  let show_function f =
    let name = Kernel_function.get_name f in
    if not (Cil.Builtin_functions.mem name) then
      Format.printf "Function @[<2>%a:@ @[@[type: %a@]@ @[%s at %a@]@]@]@."
        Kernel_function.pretty f
        Cil_datatype.Typ.pretty (Kernel_function.get_type f)
        (if Kernel_function.is_definition f then "defined" else "declared")
        Cil.d_loc (Kernel_function.get_location f)
in Globals.Functions.iter show_function

let () = Db.Main.extend main

要运行它,您必须使用-load-script选项,如下所示:

$ frama-c -load-script script.ml

开发插件更适合于更复杂的处理(请参阅开发人员手册),但脚本可以轻松进行测试。

答案 1 :(得分:1)

在当前状态下,我会说遗憾的是不可能使用Frama-C来解析既未定义也未使用的函数的声明。

t.h:

int mybinding (int x, int y);

这为您提供了标准化AST的视图。标准化意味着可以简化的一切都是:

$ frama-c -print t.h
[kernel] preprocessing with "gcc -C -E -I.  t.h"
/* Generated by Frama-C */

不幸的是,由于mybinding既没有使用也没有定义,它被删除了。

可以选择使用规范保留声明,但您想要的是保留所有声明的选项。我从未注意到这样的选择:

$ frama-c -kernel-help
...
-keep-unused-specified-functions  keep specified-but-unused functions (set by
                    default, opposite option is
                    -remove-unused-specified-functions)

保持功能与规格的选项不符合您的要求:

$ frama-c -keep-unused-specified-functions -print t.h
[kernel] preprocessing with "gcc -C -E -I.  t.h"
/* Generated by Frama-C */
相关问题