Skip to content

Commit 819ddb2

Browse files
nojafzth
authored andcommitted
Inital dump of cmt (#7411)
* Inital dump of cmt * Dump locItems * Improve locTypeToString * Filter by cursor
1 parent b1d8a44 commit 819ddb2

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

analysis/bin/main.ml

+4
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ let main () =
223223
Cfg.useRevampedCompletion := true;
224224
Commands.test ~path ~debug
225225
| args when List.mem "-h" args || List.mem "--help" args -> prerr_endline help
226+
| [_; "cmt"; path] -> CmtViewer.dump path
227+
| [_; "cmt"; line; col; path] ->
228+
let cursor = Some (int_of_string line, int_of_string col) in
229+
CmtViewer.dump ~cursor path
226230
| _ ->
227231
prerr_endline help;
228232
exit 1

analysis/src/CmtViewer.ml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
let loc_to_string (loc : Warnings.loc) : string =
2+
Format.sprintf "(%03d,%03d--%03d,%03d)" loc.loc_start.pos_lnum
3+
(loc.loc_start.pos_cnum - loc.loc_start.pos_bol)
4+
loc.loc_end.pos_lnum
5+
(loc.loc_end.pos_cnum - loc.loc_end.pos_bol)
6+
7+
let filter_by_cursor cursor (loc : Warnings.loc) : bool =
8+
match cursor with
9+
| None -> true
10+
| Some (line, col) ->
11+
let start = loc.loc_start and end_ = loc.loc_end in
12+
let line_in = start.pos_lnum <= line && line <= end_.pos_lnum in
13+
let col_in =
14+
if start.pos_lnum = end_.pos_lnum then
15+
start.pos_cnum - start.pos_bol <= col
16+
&& col <= end_.pos_cnum - end_.pos_bol
17+
else if line = start.pos_lnum then col >= start.pos_cnum - start.pos_bol
18+
else if line = end_.pos_lnum then col <= end_.pos_cnum - end_.pos_bol
19+
else true
20+
in
21+
line_in && col_in
22+
23+
let dump ?(cursor = None) path =
24+
match Cmt.loadFullCmtFromPath ~path with
25+
| None -> failwith (Format.sprintf "Could not load cmt for %s" path)
26+
| Some full ->
27+
let open SharedTypes in
28+
let open SharedTypes.Stamps in
29+
let filter = filter_by_cursor cursor in
30+
cursor
31+
|> Option.iter (fun (line, col) ->
32+
Printf.printf "Filtering by cursor %d,%d\n" line col);
33+
let stamps =
34+
full.file.stamps |> getEntries
35+
|> List.filter (fun (_, stamp) -> filter (locOfKind stamp))
36+
in
37+
38+
let total_stamps = List.length stamps in
39+
Printf.printf "Found %d stamps:\n%s" total_stamps
40+
(if total_stamps > 0 then "\n" else "");
41+
42+
stamps
43+
|> List.sort (fun (_, a) (_, b) ->
44+
let aLoc = locOfKind a in
45+
let bLoc = locOfKind b in
46+
match compare aLoc.loc_start.pos_lnum bLoc.loc_start.pos_lnum with
47+
| 0 -> compare aLoc.loc_start.pos_cnum bLoc.loc_start.pos_cnum
48+
| c -> c)
49+
|> List.iter (fun (stamp, kind) ->
50+
match kind with
51+
| KType t ->
52+
Printf.printf "%d ktype %s\n" stamp
53+
(loc_to_string t.extentLoc)
54+
| KValue t ->
55+
Printf.printf "%d kvalue %s\n" stamp
56+
(loc_to_string t.extentLoc)
57+
| KModule t ->
58+
Printf.printf "%d kmodule %s\n" stamp
59+
(loc_to_string t.extentLoc)
60+
| KConstructor t ->
61+
Printf.printf "%d kconstructor %s\n" stamp
62+
(loc_to_string t.extentLoc));
63+
64+
(* Dump all locItems (typed nodes) *)
65+
let locItems =
66+
match full.extra with
67+
| {locItems} ->
68+
locItems |> List.filter (fun locItem -> filter locItem.loc)
69+
in
70+
71+
Printf.printf "\nFound %d locItems (typed nodes):\n\n"
72+
(List.length locItems);
73+
74+
locItems
75+
|> List.sort (fun a b ->
76+
let aLoc = a.loc.Location.loc_start in
77+
let bLoc = b.loc.Location.loc_start in
78+
match compare aLoc.pos_lnum bLoc.pos_lnum with
79+
| 0 -> compare aLoc.pos_cnum bLoc.pos_cnum
80+
| c -> c)
81+
|> List.iter (fun {loc; locType} ->
82+
let locStr = loc_to_string loc in
83+
let kindStr = SharedTypes.locTypeToString locType in
84+
Printf.printf "%s %s\n" locStr kindStr)

analysis/src/SharedTypes.ml

+31-5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ module Declared = struct
155155
end
156156

157157
module Stamps : sig
158+
type kind =
159+
| KType of Type.t Declared.t
160+
| KValue of Types.type_expr Declared.t
161+
| KModule of Module.t Declared.t
162+
| KConstructor of Constructor.t Declared.t
163+
164+
val locOfKind : kind -> Warnings.loc
165+
158166
type t
159167

160168
val addConstructor : t -> int -> Constructor.t Declared.t -> unit
@@ -169,6 +177,7 @@ module Stamps : sig
169177
val iterModules : (int -> Module.t Declared.t -> unit) -> t -> unit
170178
val iterTypes : (int -> Type.t Declared.t -> unit) -> t -> unit
171179
val iterValues : (int -> Types.type_expr Declared.t -> unit) -> t -> unit
180+
val getEntries : t -> (int * kind) list
172181
end = struct
173182
type 't stampMap = (int, 't Declared.t) Hashtbl.t
174183

@@ -178,6 +187,12 @@ end = struct
178187
| KModule of Module.t Declared.t
179188
| KConstructor of Constructor.t Declared.t
180189

190+
let locOfKind = function
191+
| KType declared -> declared.extentLoc
192+
| KValue declared -> declared.extentLoc
193+
| KModule declared -> declared.extentLoc
194+
| KConstructor declared -> declared.extentLoc
195+
181196
type t = (int, kind) Hashtbl.t
182197

183198
let init () = Hashtbl.create 10
@@ -239,6 +254,8 @@ end = struct
239254
| KConstructor d -> f stamp d
240255
| _ -> ())
241256
stamps
257+
258+
let getEntries t = t |> Hashtbl.to_seq |> List.of_seq
242259
end
243260

244261
module File = struct
@@ -533,16 +550,25 @@ let locKindToString = function
533550
| NotFound -> "NotFound"
534551
| Definition (_, tip) -> "(Definition " ^ Tip.toString tip ^ ")"
535552

553+
let constantToString = function
554+
| Asttypes.Const_int _ -> "Const_int"
555+
| Asttypes.Const_char _ -> "Const_char"
556+
| Asttypes.Const_string _ -> "Const_string"
557+
| Asttypes.Const_float _ -> "Const_float"
558+
| Asttypes.Const_int32 _ -> "Const_int32"
559+
| Asttypes.Const_int64 _ -> "Const_int64"
560+
| Asttypes.Const_bigint _ -> "Const_bigint"
561+
536562
let locTypeToString = function
537563
| Typed (name, e, locKind) ->
538-
"Typed " ^ name ^ " " ^ Shared.typeToString e ^ " "
539-
^ locKindToString locKind
540-
| Constant _ -> "Constant"
564+
Format.sprintf "Typed(%s) %s: %s" (locKindToString locKind) name
565+
(Shared.typeToString e)
566+
| Constant c -> "Constant " ^ constantToString c
541567
| OtherExpression e -> "OtherExpression " ^ Shared.typeToString e
542568
| OtherPattern e -> "OtherPattern " ^ Shared.typeToString e
543569
| LModule locKind -> "LModule " ^ locKindToString locKind
544-
| TopLevelModule _ -> "TopLevelModule"
545-
| TypeDefinition _ -> "TypeDefinition"
570+
| TopLevelModule name -> "TopLevelModule " ^ name
571+
| TypeDefinition (name, _, _) -> "TypeDefinition " ^ name
546572

547573
let locItemToString {loc = {Location.loc_start; loc_end}; locType} =
548574
let pos1 = Utils.cmtPosToPosition loc_start in

0 commit comments

Comments
 (0)