简体   繁体   English

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

[英]Can frama-c be used for header file analysis?

I was looking at frama-c as a way to process C header files in OCaml (eg for generating language bindings). 我在将frama-c看作是在OCaml中处理C头文件的一种方法(例如,用于生成语言绑定)。 It's attractive because it seems like a very well-documented and maintained project. 它很有吸引力,因为它看起来像是一个记录良好且维护良好的项目。 However, after a lot of googling and searching through the documentation, I can't find anything suitable for the purpose. 然而,经过大量的谷歌搜索和搜索文档后,我找不到任何适合此目的的东西。 Am I just missing the right way to do this, or is it outside the scope of frama-c? 我只是错过了正确的方法,或者它是否超出了frama-c的范围? It seems like a fairly trivial thing for it to do, compared to some of the other plugins. 与其他一些插件相比,它似乎是一件相当简单的事情。

As Pascal said, I don't think that it is possible from the command line, but because you will have to write some code anyway, you can set the flag Rmtmps.keepUnused . 正如Pascal所说,我不认为可以从命令行进行,但是因为你必须编写一些代码,你可以设置标志Rmtmps.keepUnused This is a script that you can use to see the declarations : 这是一个可用于查看声明的脚本:

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

To run it, you have to use the -load-script option like this : 要运行它,您必须使用如下的-load-script选项:

$ frama-c -load-script script.ml

Developing a plug-in will be more appropriate for more complex processing (see the Developer Manual for that), but a script make it easy to test. 开发插件更适合于更复杂的处理(参见“ 开发人员手册 ”),但脚本可以轻松进行测试。

In the current state, I would say that it is unfortunately impossible to use Frama-C to parse declarations of functions that are neither defined or used. 在当前状态中,我会说遗憾的是不可能使用Frama-C来解析既未定义也未使用的函数的声明。

th: 日:

int mybinding (int x, int y);

This gives you a view of the normalized AST. 这为您提供了标准化AST的视图。 Normalized means that everything that could be simplified was: 标准化意味着可以简化的一切都是:

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

And unfortunately, since mybinding was neither used nor defined, it was erased. 不幸的是,由于mybinding既未使用也未定义,因此被删除了。

There is an option to keep declarations with specifications, but what you want is an option to keep all declarations. 有一个选项可以保留声明的规范,但你想要的是保留所有声明的选项。 I have never noticed such an option: 我从来没有注意到这样的选择:

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

And the option to keep functions with specifications does not do what you want: 保持功能与规格的选项不符合您的要求:

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

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

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