Frama-C:如何只获得行号
Frama-C: how to get only line number
我正在用 frama-c 开发一个插件,我想在源代码中获取行号。例如在这个小脚本中:
open Cil_types
open Cil_types
open Cil_datatype
let print_loc kf =
let locals = Kernel_function.get_locals kf in
List.iter (fun vi ->
let line = Cil_datatype.Location.pretty_line Format.sprintf "%a" Printer.pp_location vi.vdecl in
Format.printf "Line %a: %a %s@."
Printer.pp_location vi.vdecl Printer.pp_typ vi.vtype vi.vname
) locals
let () =
Db.Main.extend (fun () ->
Format.printf "Printing all local vars...@.";
let fun_name = "main" in
let kf = Globals.Functions.find_by_name fun_name in
print_loc kf;
Format.printf "done!@.")
Printer.pp_location
打印完整路径。我只想要行号。
我在 Cil_datatype.Location
模块中找到 pretty_line
并尝试使用它但我不能。我遇到了编译错误,而且我还没有找到在 Frama-C 源代码中使用此函数的示例。
我试过了,
let print_loc kf =
let locals = Kernel_function.get_locals kf in
List.iter (fun vi ->
let line = Cil_datatype.Location.pretty_line Format.sprintf "%a" Printer.pp_location vi.vdecl in
Format.printf "Line %d: %a %s@." line Printer.pp_typ vi.vtype vi.vname) locals
我收到了这条错误消息
Error: This function has type
Format.formatter -> Cil_datatype.Location.t -> unit
It is applied to too many arguments; maybe you forgot a `;'.
我尝试了几种组合,但无论如何,要么我收到之前的错误消息,要么我收到这条消息:
Error: This expression has type
Format.formatter -> Cil_types.location -> unit
but an expression was expected of type unit -> 'a -> string
Type Format.formatter is not compatible with type unit
否则我可以得到关于行号的信息吗?或者如何正确使用 Cil_datatype.Location.pretty_line
?
谢谢
类型为 Format.formatter -> 'a -> unit
的函数旨在与 %a
说明符结合使用,例如与格式化程序 ppf
和位置 loc
结合使用:
let () = Format.fprintf ppf "%a" Cil_datatype.Location.pretty_line loc
一个特殊的地方是,如果你想生成一个字符串,你应该使用asprintf
而不是sprintf
:
let s = Format.asprintf "%a" Cil_datatype.Location.pretty_line loc
octachtron 的回答很好地总结了如何将 pretty_line
与格式字符串一起使用。但是,如果您想将行号用作整数(不一定要用 %d
打印它),Cil_datatype.Location.t
是具体类型,更准确地说是 Cil_types.location
的别名,它是本身就是一对Lexing.location
。因此,
要检索位置开始的行,您只需执行
...
let line = (fst vi.vdecl).Lexing.pos_lnum in
...
我正在用 frama-c 开发一个插件,我想在源代码中获取行号。例如在这个小脚本中:
open Cil_types
open Cil_types
open Cil_datatype
let print_loc kf =
let locals = Kernel_function.get_locals kf in
List.iter (fun vi ->
let line = Cil_datatype.Location.pretty_line Format.sprintf "%a" Printer.pp_location vi.vdecl in
Format.printf "Line %a: %a %s@."
Printer.pp_location vi.vdecl Printer.pp_typ vi.vtype vi.vname
) locals
let () =
Db.Main.extend (fun () ->
Format.printf "Printing all local vars...@.";
let fun_name = "main" in
let kf = Globals.Functions.find_by_name fun_name in
print_loc kf;
Format.printf "done!@.")
Printer.pp_location
打印完整路径。我只想要行号。
我在 Cil_datatype.Location
模块中找到 pretty_line
并尝试使用它但我不能。我遇到了编译错误,而且我还没有找到在 Frama-C 源代码中使用此函数的示例。
我试过了,
let print_loc kf =
let locals = Kernel_function.get_locals kf in
List.iter (fun vi ->
let line = Cil_datatype.Location.pretty_line Format.sprintf "%a" Printer.pp_location vi.vdecl in
Format.printf "Line %d: %a %s@." line Printer.pp_typ vi.vtype vi.vname) locals
我收到了这条错误消息
Error: This function has type Format.formatter -> Cil_datatype.Location.t -> unit It is applied to too many arguments; maybe you forgot a `;'.
我尝试了几种组合,但无论如何,要么我收到之前的错误消息,要么我收到这条消息:
Error: This expression has type Format.formatter -> Cil_types.location -> unit but an expression was expected of type unit -> 'a -> string Type Format.formatter is not compatible with type unit
否则我可以得到关于行号的信息吗?或者如何正确使用 Cil_datatype.Location.pretty_line
?
谢谢
类型为 Format.formatter -> 'a -> unit
的函数旨在与 %a
说明符结合使用,例如与格式化程序 ppf
和位置 loc
结合使用:
let () = Format.fprintf ppf "%a" Cil_datatype.Location.pretty_line loc
一个特殊的地方是,如果你想生成一个字符串,你应该使用asprintf
而不是sprintf
:
let s = Format.asprintf "%a" Cil_datatype.Location.pretty_line loc
octachtron 的回答很好地总结了如何将 pretty_line
与格式字符串一起使用。但是,如果您想将行号用作整数(不一定要用 %d
打印它),Cil_datatype.Location.t
是具体类型,更准确地说是 Cil_types.location
的别名,它是本身就是一对Lexing.location
。因此,
要检索位置开始的行,您只需执行
...
let line = (fst vi.vdecl).Lexing.pos_lnum in
...