Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can frama-c be used for header file analysis?

Tags:

ocaml

frama-c

I was looking at frama-c as a way to process C header files in OCaml (e.g. for generating language bindings). 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? It seems like a fairly trivial thing for it to do, compared to some of the other plugins.

like image 582
Martin DeMello Avatar asked Feb 06 '13 06:02

Martin DeMello


2 Answers

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. 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 :

$ 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.

like image 79
Anne Avatar answered Sep 29 '22 11:09

Anne


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.

t.h:

int mybinding (int x, int y);

This gives you a view of the normalized 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.

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 */
like image 34
Pascal Cuoq Avatar answered Sep 29 '22 13:09

Pascal Cuoq