Skip to content

Commit ec22f3e

Browse files
panglesdjonludlam
authored andcommitted
Occurrences: detection of hidden modules should be more efficient
We consider as internal children of internal modules, and double underscored roots. Signed-off-by: Paul-Elliot <[email protected]>
1 parent c1d324e commit ec22f3e

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/model/paths.ml

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ module Ocaml_env = Env
1919

2020
open Names
2121

22+
let contains_double_underscore s =
23+
let len = String.length s in
24+
let rec aux i =
25+
if i > len - 2 then false
26+
else if s.[i] = '_' && s.[i + 1] = '_' then true
27+
else aux (i + 1)
28+
in
29+
aux 0
30+
2231
module Identifier = struct
2332
type 'a id = 'a Paths_types.id = { iv : 'a; ihash : int; ikey : string }
2433

@@ -66,7 +75,9 @@ module Identifier = struct
6675
let rec is_internal : t -> bool =
6776
fun x ->
6877
match x.iv with
69-
| `Root (_, name) -> ModuleName.is_internal name
78+
| `Root (_, name) ->
79+
ModuleName.is_internal name
80+
|| contains_double_underscore (ModuleName.to_string name)
7081
| `Page (_, _) -> false
7182
| `LeafPage (_, _) -> false
7283
| `Module (_, name) -> ModuleName.is_internal name
@@ -91,6 +102,36 @@ module Identifier = struct
91102
| `SourceLocationInternal _ | `AssetFile _ ->
92103
false
93104

105+
let rec is_internal_rec : t -> bool =
106+
fun x ->
107+
is_internal x
108+
||
109+
match x.iv with
110+
| `Root (_, name) -> ModuleName.is_internal name
111+
| `Page (_, _) -> false
112+
| `LeafPage (_, _) -> false
113+
| `Module (parent, _) -> is_internal_rec (parent :> t)
114+
| `Parameter (parent, _) -> is_internal_rec (parent :> t)
115+
| `Result x -> is_internal_rec (x :> t)
116+
| `ModuleType (parent, _) -> is_internal_rec (parent :> t)
117+
| `Type (parent, _) -> is_internal_rec (parent :> t)
118+
| `CoreType name -> TypeName.is_internal name
119+
| `Constructor (parent, _) -> is_internal (parent :> t)
120+
| `Field (parent, _) -> is_internal (parent :> t)
121+
| `Extension (parent, _) -> is_internal (parent :> t)
122+
| `ExtensionDecl (parent, _, _) -> is_internal (parent :> t)
123+
| `Exception (parent, _) -> is_internal (parent :> t)
124+
| `CoreException _ -> false
125+
| `Value (parent, _) -> is_internal_rec (parent :> t)
126+
| `Class (parent, _) -> is_internal_rec (parent :> t)
127+
| `ClassType (parent, _) -> is_internal_rec (parent :> t)
128+
| `Method (parent, _) -> is_internal (parent :> t)
129+
| `InstanceVariable (parent, _) -> is_internal (parent :> t)
130+
| `Label (parent, _) -> is_internal (parent :> t)
131+
| `SourceDir _ | `SourceLocationMod _ | `SourceLocation _ | `SourcePage _
132+
| `SourceLocationInternal _ | `AssetFile _ ->
133+
false
134+
94135
let name : [< t_pv ] id -> string = fun n -> name_aux (n :> t)
95136

96137
let rec full_name_aux : t -> string list =
@@ -671,7 +712,7 @@ module Path = struct
671712
| `Identifier { iv = `Module (_, m); _ }
672713
when Names.ModuleName.is_internal m ->
673714
true
674-
| `Identifier _ -> false
715+
| `Identifier i -> Identifier.is_internal_rec i
675716
| `Canonical (_, `Resolved _) -> false
676717
| `Canonical (x, _) ->
677718
(not weak_canonical_test) && inner (x : module_ :> any)
@@ -708,15 +749,6 @@ module Path = struct
708749
in
709750
inner x
710751

711-
and contains_double_underscore s =
712-
let len = String.length s in
713-
let rec aux i =
714-
if i > len - 2 then false
715-
else if s.[i] = '_' && s.[i + 1] = '_' then true
716-
else aux (i + 1)
717-
in
718-
aux 0
719-
720752
and is_path_hidden : Paths_types.Path.any -> bool =
721753
let open Paths_types.Path in
722754
function

test/occurrences/double_wrapped.t/run.t

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ and prints the number of occurrences in a readable format.
6363
Uses of A are: 2 times in b.ml, 1 time in c.ml, 1 time in main.ml
6464
Uses of B are: 1 time in main.ml
6565
Uses of C are not counted, since the canonical destination (Main.C, generated by dune) does not exist.
66+
Uses of B.Z are not counted since they go to a hidden module.
6667
Uses of values Y.x and Z.y (in b.ml) are not counted since they come from a "local" module.
6768

6869
$ occurrences_print occurrences.txt | sort
@@ -73,9 +74,6 @@ Uses of values Y.x and Z.y (in b.ml) are not counted since they come from a "loc
7374
Main.A.t was used directly 1 times and indirectly 0 times
7475
Main.A.x was used directly 4 times and indirectly 0 times
7576
Main.B was used directly 1 times and indirectly 0 times
76-
Main__B was used directly 0 times and indirectly 1 times
77-
Main__B.Z was used directly 0 times and indirectly 1 times
78-
Main__B.Z.y was used directly 1 times and indirectly 0 times
7977
string was used directly 1 times and indirectly 0 times
8078

8179
$ occurrences_print occurrences1.txt | sort
@@ -95,9 +93,6 @@ Uses of values Y.x and Z.y (in b.ml) are not counted since they come from a "loc
9593
Main.A.M was used directly 2 times and indirectly 0 times
9694
Main.A.t was used directly 1 times and indirectly 0 times
9795
Main.A.x was used directly 2 times and indirectly 0 times
98-
Main__B was used directly 0 times and indirectly 1 times
99-
Main__B.Z was used directly 0 times and indirectly 1 times
100-
Main__B.Z.y was used directly 1 times and indirectly 0 times
10196

10297
$ occurrences_print occurrences5.txt | sort
10398
Main was used directly 0 times and indirectly 3 times
@@ -121,7 +116,4 @@ Now we can merge both files
121116
Main.A.t was used directly 1 times and indirectly 0 times
122117
Main.A.x was used directly 4 times and indirectly 0 times
123118
Main.B was used directly 1 times and indirectly 0 times
124-
Main__B was used directly 0 times and indirectly 1 times
125-
Main__B.Z was used directly 0 times and indirectly 1 times
126-
Main__B.Z.y was used directly 1 times and indirectly 0 times
127119
string was used directly 1 times and indirectly 0 times

0 commit comments

Comments
 (0)