Skip to content

Commit

Permalink
Run OCamlformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair committed May 5, 2023
1 parent fcdcdc5 commit c435e8f
Show file tree
Hide file tree
Showing 135 changed files with 39,171 additions and 39,118 deletions.
9 changes: 9 additions & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
profile = default
version = 0.25.1
margin = 120
exp-grouping = preserve
parens-ite = true
space-around-lists = false
indicate-multiline-delimiters = closing-on-separate-line
module-item-spacing = preserve
doc-comments = before
2 changes: 2 additions & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/**

2 changes: 1 addition & 1 deletion sailcov/dune
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(executable
(name main))
(name main))
146 changes: 83 additions & 63 deletions src/bin/callgraph_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let node_string n = node_id n |> string_of_id |> String.escaped
let edge_color _from_node _to_node = "black"

let node_color cuts =
let module NodeSet = Set.Make(Node) in
let module NodeSet = Set.Make (Node) in
function
| node when NodeSet.mem node cuts -> "red"
| Register _ -> "lightpink"
Expand All @@ -92,83 +92,103 @@ let node_color cuts =
| Outcome _ -> "purple"

let dot_of_ast out_chan ast =
let module G = Graph.Make(Node) in
let module NodeSet = Set.Make(Node) in
let module G = Graph.Make (Node) in
let module NodeSet = Set.Make (Node) in
let g = graph_of_ast ast in
G.make_dot (node_color NodeSet.empty) edge_color node_string out_chan g

let node_of_id env =
let lets = Type_check.Env.get_toplevel_lets env in
let specs = Type_check.Env.get_defined_val_specs env in
fun id ->
if IdSet.mem id lets then Letbind id
else if IdSet.mem id specs then Function id
else if Type_check.Env.bound_typ_id env id then Type id
else (prerr_endline ("Warning: unknown identifier " ^ string_of_id id); Function id)
if IdSet.mem id lets then Letbind id
else if IdSet.mem id specs then Function id
else if Type_check.Env.bound_typ_id env id then Type id
else (
prerr_endline ("Warning: unknown identifier " ^ string_of_id id);
Function id
)

let () =
let slice_roots = ref IdSet.empty in
let slice_keep_std = ref false in
let slice_cuts = ref IdSet.empty in

ArgString ("identifiers", fun arg -> ActionUnit (fun _ ->
let args = Str.split (Str.regexp " +") arg in
let ids = List.map mk_id args |> IdSet.of_list in
Specialize.add_initial_calls ids;
slice_roots := IdSet.union ids !slice_roots
)) |> register_command ~name:"slice_roots" ~help:"Set the roots for :slice";
ArgString
( "identifiers",
fun arg ->
ActionUnit
(fun _ ->
let args = Str.split (Str.regexp " +") arg in
let ids = List.map mk_id args |> IdSet.of_list in
Specialize.add_initial_calls ids;
slice_roots := IdSet.union ids !slice_roots
)
)
|> register_command ~name:"slice_roots" ~help:"Set the roots for :slice";

ActionUnit (fun _ ->
slice_keep_std := true
) |> register_command ~name:"slice_keep_std" ~help:"Keep standard library contents during :slice";
ActionUnit (fun _ -> slice_keep_std := true)
|> register_command ~name:"slice_keep_std" ~help:"Keep standard library contents during :slice";

ArgString ("identifiers", fun arg -> ActionUnit (fun _ ->
let args = Str.split (Str.regexp " +") arg in
let ids = List.map mk_id args |> IdSet.of_list in
slice_cuts := IdSet.union ids !slice_cuts
)) |> register_command ~name:"slice_cuts" ~help:"Set the cuts for :slice";
ArgString
( "identifiers",
fun arg ->
ActionUnit
(fun _ ->
let args = Str.split (Str.regexp " +") arg in
let ids = List.map mk_id args |> IdSet.of_list in
slice_cuts := IdSet.union ids !slice_cuts
)
)
|> register_command ~name:"slice_cuts" ~help:"Set the cuts for :slice";

Action (fun istate ->
let module NodeSet = Set.Make(Node) in
let module G = Graph.Make(Node) in
let g = graph_of_ast istate.ast in
let roots = !slice_roots |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let cuts = !slice_cuts |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let g = G.prune roots cuts g in
{ istate with ast = filter_ast_extra cuts g istate.ast !slice_keep_std }
) |> register_command
~name:"slice"
~help:"Slice AST to the definitions which the functions given \
by :slice_roots depend on, up to the functions given \
by :slice_cuts";
Action
(fun istate ->
let module NodeSet = Set.Make (Node) in
let module G = Graph.Make (Node) in
let g = graph_of_ast istate.ast in
let roots = !slice_roots |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let cuts = !slice_cuts |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let g = G.prune roots cuts g in
{ istate with ast = filter_ast_extra cuts g istate.ast !slice_keep_std }
)
|> register_command ~name:"slice"
~help:
"Slice AST to the definitions which the functions given by :slice_roots depend on, up to the functions given \
by :slice_cuts";

Action (fun istate ->
let module NodeSet = Set.Make(Node) in
let module NodeMap = Map.Make(Node) in
let module G = Graph.Make(Node) in
let g = graph_of_ast istate.ast in
let roots = !slice_roots |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let keep = function
| (Function id,_) when IdSet.mem id (!slice_roots) -> None
| (Function id,_) -> Some (Function id)
| _ -> None
in
let cuts = NodeMap.bindings g |> List.filter_map keep |> NodeSet.of_list in
let g = G.prune roots cuts g in
{ istate with ast = filter_ast_extra cuts g istate.ast !slice_keep_std }
) |> register_command
~name:"thin_slice"
~help:(sprintf ":thin_slice - Slice AST to the function definitions given with %s" (command "slice_roots"));
Action
(fun istate ->
let module NodeSet = Set.Make (Node) in
let module NodeMap = Map.Make (Node) in
let module G = Graph.Make (Node) in
let g = graph_of_ast istate.ast in
let roots = !slice_roots |> IdSet.elements |> List.map (node_of_id istate.env) |> NodeSet.of_list in
let keep = function
| Function id, _ when IdSet.mem id !slice_roots -> None
| Function id, _ -> Some (Function id)
| _ -> None
in
let cuts = NodeMap.bindings g |> List.filter_map keep |> NodeSet.of_list in
let g = G.prune roots cuts g in
{ istate with ast = filter_ast_extra cuts g istate.ast !slice_keep_std }
)
|> register_command ~name:"thin_slice"
~help:(sprintf ":thin_slice - Slice AST to the function definitions given with %s" (command "slice_roots"));

ArgString ("format", fun arg -> ActionUnit (fun istate ->
let format = if arg = "" then "svg" else arg in
let dotfile, out_chan = Filename.open_temp_file "sail_graph_" ".gz" in
let image = Filename.temp_file "sail_graph_" ("." ^ format) in
dot_of_ast out_chan istate.ast;
close_out out_chan;
let _ = Unix.system (Printf.sprintf "dot -T%s %s -o %s" format dotfile image) in
let _ = Unix.system (Printf.sprintf "xdg-open %s" image) in
()
)) |> register_command
~name:"graph"
~help:"Draw a callgraph using dot in :0 (e.g. svg), and open with xdg-open"
ArgString
( "format",
fun arg ->
ActionUnit
(fun istate ->
let format = if arg = "" then "svg" else arg in
let dotfile, out_chan = Filename.open_temp_file "sail_graph_" ".gz" in
let image = Filename.temp_file "sail_graph_" ("." ^ format) in
dot_of_ast out_chan istate.ast;
close_out out_chan;
let _ = Unix.system (Printf.sprintf "dot -T%s %s -o %s" format dotfile image) in
let _ = Unix.system (Printf.sprintf "xdg-open %s" image) in
()
)
)
|> register_command ~name:"graph" ~help:"Draw a callgraph using dot in :0 (e.g. svg), and open with xdg-open"
Loading

0 comments on commit c435e8f

Please sign in to comment.