@@ -132,39 +132,54 @@ module Env = struct
132
132
env
133
133
end
134
134
135
- let postprocess_poses source_id poses uid_to_id uid_to_loc :
135
+ module LocHashtbl = Hashtbl. Make (struct
136
+ type t = Location .t
137
+ let equal l1 l2 = l1 = l2
138
+ let hash = Hashtbl. hash
139
+ end )
140
+
141
+ module IdentHashtbl = Hashtbl. Make (struct
142
+ type t = Ident .t
143
+ let equal l1 l2 = l1 = l2
144
+ let hash = Hashtbl. hash
145
+ end )
146
+
147
+ (* generate a [loc_to_id] and an [ident_to_id] map *)
148
+ let local_maps source_id poses =
149
+ let loc_tbl = LocHashtbl. create 10 and ident_to_id = IdentHashtbl. create 10 in
150
+ List. iter
151
+ (function
152
+ | Typedtree_traverse.Analysis. Definition id , loc ->
153
+ let name =
154
+ Odoc_model.Names.LocalName. make_std
155
+ (Printf. sprintf " local_%s_%d" (Ident. name id) (counter () ))
156
+ in
157
+ let identifier =
158
+ Odoc_model.Paths.Identifier.Mk. source_location_int (source_id, name)
159
+ in
160
+ IdentHashtbl. add ident_to_id id identifier;
161
+ LocHashtbl. add loc_tbl loc identifier
162
+ | _ -> () )
163
+ poses;
164
+ (loc_tbl, ident_to_id)
165
+
166
+ let postprocess_poses poses ident_to_id loc_to_id uid_to_id uid_to_loc :
136
167
Odoc_model.Lang.Source_info. infos =
137
- let local_def_anchors =
138
- List. filter_map
139
- (function
140
- | Typedtree_traverse.Analysis. Definition id , _ ->
141
- let name =
142
- Odoc_model.Names.LocalName. make_std
143
- (Printf. sprintf " local_%s_%d" (Ident. name id) (counter () ))
144
- in
145
- let identifier =
146
- Odoc_model.Paths.Identifier.Mk. source_location_int
147
- (source_id, name)
148
- in
149
- Some (id, identifier)
150
- | _ -> None )
151
- poses
152
- in
153
168
let poses =
154
169
List. filter_map
155
170
(function
156
171
| Typedtree_traverse.Analysis. Definition id , loc ->
157
172
Some
158
173
( Odoc_model.Lang.Source_info. Definition
159
- (List. assoc id local_def_anchors ),
160
- loc )
174
+ (IdentHashtbl. find ident_to_id id ),
175
+ pos_of_loc loc )
161
176
| Value (LocalValue uniq ), loc -> (
162
- match List. assoc_opt uniq local_def_anchors with
163
- | Some anchor -> Some (Value anchor, loc)
177
+ match IdentHashtbl. find_opt ident_to_id uniq with
178
+ | Some anchor -> Some (Value anchor, pos_of_loc loc)
164
179
| None -> None )
165
180
| Value (DefJmp x ), loc -> (
166
181
match Shape.Uid.Map. find_opt x uid_to_id with
167
- | Some id -> Some (Value id, loc)
182
+ | Some id -> Some (Value id, pos_of_loc loc)
168
183
| None -> None ))
169
184
poses
170
185
in
@@ -173,7 +188,7 @@ let postprocess_poses source_id poses uid_to_id uid_to_loc :
173
188
(fun uid id acc ->
174
189
let loc_opt = Shape.Uid.Tbl. find_opt uid_to_loc uid in
175
190
match loc_opt with
176
- | Some loc ->
191
+ | Some loc when not ( LocHashtbl. mem loc_to_id loc) ->
177
192
(Odoc_model.Lang.Source_info. Definition id, pos_of_loc loc) :: acc
178
193
| _ -> acc)
179
194
uid_to_id []
@@ -259,60 +274,58 @@ let anchor_of_identifier id =
259
274
260
275
(* * Returns an environment (containing a [loc] to [id] map) and an [uid] to [id]
261
276
maps. *)
262
- let id_maps_of_cmt (source_id : Odoc_model.Paths.Identifier.SourcePage.t )
263
- (id : Odoc_model.Paths.Identifier.RootModule.t )
264
- (structure : Typedtree.structure )
265
- (uid_to_loc : Warnings.loc Types.Uid.Tbl.t ) =
266
- let env = Env. of_structure id structure in
277
+ let id_map_of_cmt env (source_id : Odoc_model.Paths.Identifier.SourcePage.t )
278
+ (uid_to_loc : Warnings.loc Types.Uid.Tbl.t ) local_loc_to_id =
267
279
let uid_to_loc_map = Shape.Uid.Tbl. to_map uid_to_loc in
280
+ let mk_id name =
281
+ Some
282
+ (Odoc_model.Paths.Identifier.Mk. source_location (source_id, name)
283
+ :> Odoc_model.Paths.Identifier.SourceLocation. t)
284
+ in
268
285
let uid_to_id : Odoc_model.Paths.Identifier.SourceLocation.t Shape.Uid.Map.t =
269
286
Shape.Uid.Map. filter_map
270
287
(fun uid loc ->
271
288
if loc.Location. loc_ghost then None
272
289
else
273
290
let identifier = Ident_env. identifier_of_loc env loc in
274
- let anchor =
275
- match identifier with
276
- | Some x ->
277
- Some
278
- (Odoc_model.Names.DefName. make_std (anchor_of_identifier x))
279
- | None -> (
280
- match uid with
281
- | Compilation_unit _ -> None
282
- | Item _ ->
283
- let name =
284
- Odoc_model.Names.DefName. make_std
285
- (Printf. sprintf " def_%d" (counter () ))
286
- in
287
- Some name
288
- | _ -> None )
289
- in
290
- match anchor with
291
- | Some a ->
292
- Some
293
- (Odoc_model.Paths.Identifier.Mk. source_location (source_id, a)
294
- :> Odoc_model.Paths.Identifier.SourceLocation. t)
295
- | None -> None )
291
+ match identifier with
292
+ | Some x ->
293
+ mk_id (Odoc_model.Names.DefName. make_std (anchor_of_identifier x))
294
+ | None -> (
295
+ match uid with
296
+ | Compilation_unit _ -> None
297
+ | Item _ -> (
298
+ match LocHashtbl. find_opt local_loc_to_id loc with
299
+ | Some anchor -> Some anchor
300
+ | None ->
301
+ let name =
302
+ Odoc_model.Names.DefName. make_std
303
+ (Printf. sprintf " def_%d" (counter () ))
304
+ in
305
+ mk_id name)
306
+ | _ -> None ))
296
307
uid_to_loc_map
297
308
in
298
- ( uid_to_id, env)
309
+ uid_to_id
299
310
300
311
let read_cmt_infos source_id_opt id cmt_info =
301
312
match Odoc_model.Compat. shape_of_cmt_infos cmt_info with
302
313
| Some shape -> (
303
314
let uid_to_loc = cmt_info.cmt_uid_to_loc in
304
315
match (source_id_opt, cmt_info.cmt_annots) with
305
316
| Some source_id , Implementation impl ->
306
- let map, env = id_maps_of_cmt source_id id impl uid_to_loc in
317
+ let env = Env. of_structure id impl in
307
318
let occ_infos =
308
319
Typedtree_traverse. of_cmt env uid_to_loc impl |> List. rev
309
320
(* Information are accumulated in a list. We need to have the
310
321
first info first in the list, to assign anchors with increasing
311
322
numbers, so that adding some content at the end of a file does
312
323
not modify the anchors for existing anchors. *)
313
324
in
325
+ let loc_to_id, ident_to_id = local_maps source_id occ_infos in
326
+ let map = id_map_of_cmt env source_id uid_to_loc loc_to_id in
314
327
let source_infos =
315
- postprocess_poses source_id occ_infos map uid_to_loc
328
+ postprocess_poses occ_infos ident_to_id loc_to_id map uid_to_loc
316
329
in
317
330
( Some (shape, map),
318
331
Some
0 commit comments