Skip to content

Commit 0d9dfc4

Browse files
panglesdjonludlam
authored andcommitted
Occurrence: change datastructure to capture more information
Signed-off-by: Paul-Elliot <[email protected]>
1 parent 22e00bf commit 0d9dfc4

File tree

4 files changed

+114
-15
lines changed

4 files changed

+114
-15
lines changed

src/odoc/occurrences.ml

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,109 @@ let fold_dirs ~dirs ~f ~init =
2222

2323
module H = Hashtbl.Make (Odoc_model.Paths.Identifier)
2424

25+
module Occtbl : sig
26+
type item = { direct : int; indirect : int; sub : item H.t }
27+
type t = item H.t
28+
type key = Odoc_model.Paths.Identifier.t
29+
val v : unit -> t
30+
31+
val add : t -> key -> unit
32+
33+
val iter : (key -> item -> unit) -> t -> unit
34+
35+
val get : t -> key -> item option
36+
end = struct
37+
type item = { direct : int; indirect : int; sub : item H.t }
38+
type t = item H.t
39+
type key = Odoc_model.Paths.Identifier.t
40+
41+
let v_item () = { direct = 0; indirect = 0; sub = H.create 0 }
42+
43+
let v () = H.create 0
44+
45+
let add tbl id =
46+
let rec add ?(kind = `Indirect) id =
47+
let incr htbl id =
48+
let { direct; indirect; sub } =
49+
match H.find_opt htbl id with Some n -> n | None -> v_item ()
50+
in
51+
let direct, indirect =
52+
match kind with
53+
| `Direct -> (direct + 1, indirect)
54+
| `Indirect -> (direct, indirect + 1)
55+
in
56+
H.replace htbl id { direct; indirect; sub };
57+
sub
58+
in
59+
let do_ parent =
60+
let htbl = add (parent :> key) in
61+
incr htbl id
62+
in
63+
match id.iv with
64+
| `InstanceVariable (parent, _) -> do_ parent
65+
| `Parameter (parent, _) -> do_ parent
66+
| `Module (parent, _) -> do_ parent
67+
| `ModuleType (parent, _) -> do_ parent
68+
| `Method (parent, _) -> do_ parent
69+
| `Field (parent, _) -> do_ parent
70+
| `Extension (parent, _) -> do_ parent
71+
| `Type (parent, _) -> do_ parent
72+
| `CoreType _ -> incr tbl id
73+
| `Constructor (parent, _) -> do_ parent
74+
| `Exception (parent, _) -> do_ parent
75+
| `ExtensionDecl (parent, _, _) -> do_ parent
76+
| `Class (parent, _) -> do_ parent
77+
| `Value (parent, _) -> do_ parent
78+
| `ClassType (parent, _) -> do_ parent
79+
| `Root _ -> incr tbl id
80+
| `SourcePage _ | `Page _ | `LeafPage _ | `SourceLocation _
81+
| `CoreException _ | `Label _ | `SourceLocationMod _ | `Result _
82+
| `AssetFile _ | `SourceDir _ | `SourceLocationInternal _ ->
83+
assert false
84+
in
85+
let _htbl = add ~kind:`Direct id in
86+
()
87+
88+
let rec get t id =
89+
let ( >>= ) = Option.bind in
90+
let do_ parent =
91+
get t (parent :> key) >>= fun { sub; _ } -> H.find_opt sub id
92+
in
93+
match id.iv with
94+
| `InstanceVariable (parent, _) -> do_ parent
95+
| `Parameter (parent, _) -> do_ parent
96+
| `Module (parent, _) -> do_ parent
97+
| `ModuleType (parent, _) -> do_ parent
98+
| `Method (parent, _) -> do_ parent
99+
| `Field (parent, _) -> do_ parent
100+
| `Extension (parent, _) -> do_ parent
101+
| `ExtensionDecl (parent, _, _) -> do_ parent
102+
| `Type (parent, _) -> do_ parent
103+
| `Constructor (parent, _) -> do_ parent
104+
| `Exception (parent, _) -> do_ parent
105+
| `Class (parent, _) -> do_ parent
106+
| `Value (parent, _) -> do_ parent
107+
| `ClassType (parent, _) -> do_ parent
108+
| `Root _ -> H.find_opt t id
109+
| `SourcePage _ | `Page _ | `LeafPage _ | `CoreType _ | `SourceLocation _
110+
| `CoreException _ | `Label _ | `SourceLocationMod _ | `Result _
111+
| `AssetFile _ | `SourceDir _ | `SourceLocationInternal _ ->
112+
assert false
113+
114+
let rec iter f tbl =
115+
H.iter
116+
(fun id v ->
117+
iter f v.sub;
118+
f id v)
119+
tbl
120+
end
121+
25122
let count ~dst ~warnings_options:_ directories =
26123
let htbl = H.create 100 in
27124
let f () (unit : Odoc_model.Lang.Compilation_unit.t) =
28125
let incr tbl p p' =
29126
let id = Odoc_model.Paths.Path.Resolved.(identifier (p :> t)) in
30-
let old_value = match H.find_opt tbl id with Some n -> n | None -> 0 in
31-
if not Odoc_model.Paths.Path.(is_hidden p') then
32-
H.replace tbl id (old_value + 1)
127+
if not Odoc_model.Paths.Path.(is_hidden p') then Occtbl.add tbl id
33128
in
34129
let () =
35130
List.iter

test/occurrences/double_wrapped.t/run.t

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ Uses of values Y.x and Z.y (in b.ml) are not counted since they come from a "loc
5050
Uses of values Main__.C.y and Main__.A.x are not rewritten since we use references instead of paths.
5151

5252
$ occurrences_print occurrences.txt | sort
53-
Main.A was used 4 times
54-
Main.A.(||>) was used 1 times
55-
Main.A.M was used 2 times
56-
Main.A.t was used 1 times
57-
Main.A.x was used 2 times
58-
Main.B was used 1 times
59-
string was used 1 times
53+
Main was used directly 0 times and indirectly 11 times
54+
Main.A was used directly 4 times and indirectly 6 times
55+
Main.A.(||>) was used directly 1 times and indirectly 0 times
56+
Main.A.M was used directly 2 times and indirectly 0 times
57+
Main.A.t was used directly 1 times and indirectly 0 times
58+
Main.A.x was used directly 2 times and indirectly 0 times
59+
Main.B was used directly 1 times and indirectly 0 times
60+
string was used directly 1 times and indirectly 0 times

test/odoc_print/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
(executable
1212
(name occurrences_print)
1313
(modules occurrences_print)
14-
(libraries odoc_model_desc compatcmdliner))
14+
(libraries odoc_model_desc compatcmdliner odoc_odoc))

test/odoc_print/occurrences_print.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ module H = Hashtbl.Make (Odoc_model.Paths.Identifier)
22

33
let run inp =
44
let ic = open_in_bin inp in
5-
let htbl = Marshal.from_channel ic in
6-
H.iter
7-
(fun id occ ->
5+
let htbl : Odoc_odoc.Occurrences.Occtbl.item Odoc_odoc.Occurrences.H.t =
6+
Marshal.from_channel ic
7+
in
8+
Odoc_odoc.Occurrences.Occtbl.iter
9+
(fun id { Odoc_odoc.Occurrences.Occtbl.direct; indirect; _ } ->
810
let id = String.concat "." (Odoc_model.Paths.Identifier.fullname id) in
9-
Format.printf "%s was used %d times\n" id occ)
11+
Format.printf "%s was used directly %d times and indirectly %d times\n" id
12+
direct indirect)
1013
htbl
1114

1215
open Compatcmdliner

0 commit comments

Comments
 (0)