-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathException.ml
490 lines (469 loc) · 17.8 KB
/
Exception.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
let posToString = Common.posToString
module LocSet = Common.LocSet
module Values = struct
let valueBindingsTable =
(Hashtbl.create 15 : (string, (Name.t, Exceptions.t) Hashtbl.t) Hashtbl.t)
let currentFileTable = ref (Hashtbl.create 1)
let add ~name exceptions =
let path = (name |> Name.create) :: (ModulePath.getCurrent ()).path in
Hashtbl.replace !currentFileTable (path |> Common.Path.toName) exceptions
let getFromModule ~moduleName ~modulePath (path_ : Common.Path.t) =
let name = path_ @ modulePath |> Common.Path.toName in
match
Hashtbl.find_opt valueBindingsTable (String.capitalize_ascii moduleName)
with
| Some tbl -> Hashtbl.find_opt tbl name
| None -> (
match
Hashtbl.find_opt valueBindingsTable
(String.uncapitalize_ascii moduleName)
with
| Some tbl -> Hashtbl.find_opt tbl name
| None -> None)
let rec findLocal ~moduleName ~modulePath path =
match path |> getFromModule ~moduleName ~modulePath with
| Some exceptions -> Some exceptions
| None -> (
match modulePath with
| [] -> None
| _ :: restModulePath ->
path |> findLocal ~moduleName ~modulePath:restModulePath)
let findPath ~moduleName ~modulePath path =
let findExternal ~externalModuleName ~pathRev =
pathRev |> List.rev
|> getFromModule
~moduleName:(externalModuleName |> Name.toString)
~modulePath:[]
in
match path |> findLocal ~moduleName ~modulePath with
| None -> (
(* Search in another file *)
match path |> List.rev with
| externalModuleName :: pathRev -> (
match (findExternal ~externalModuleName ~pathRev, pathRev) with
| (Some _ as found), _ -> found
| None, externalModuleName2 :: pathRev2
when !Common.Cli.cmtCommand && pathRev2 <> [] ->
(* Simplistic namespace resolution for dune namespace: skip the root of the path *)
findExternal ~externalModuleName:externalModuleName2 ~pathRev:pathRev2
| None, _ -> None)
| [] -> None)
| Some exceptions -> Some exceptions
let newCmt () =
currentFileTable := Hashtbl.create 15;
Hashtbl.replace valueBindingsTable !Common.currentModule !currentFileTable
end
module Event = struct
type kind =
| Catches of t list (* with | E => ... *)
| Call of {callee: Common.Path.t; modulePath: Common.Path.t} (* foo() *)
| DoesNotRaise of
t list (* DoesNotRaise(events) where events come from an expression *)
| Raises (** raise E *)
and t = {exceptions: Exceptions.t; kind: kind; loc: Location.t}
let rec print ppf event =
match event with
| {kind = Call {callee; modulePath}; exceptions; loc} ->
Format.fprintf ppf "%s Call(%s, modulePath:%s) %a@."
(loc.loc_start |> posToString)
(callee |> Common.Path.toString)
(modulePath |> Common.Path.toString)
(Exceptions.pp ~exnTable:None)
exceptions
| {kind = DoesNotRaise nestedEvents; loc} ->
Format.fprintf ppf "%s DoesNotRaise(%a)@."
(loc.loc_start |> posToString)
(fun ppf () ->
nestedEvents |> List.iter (fun e -> Format.fprintf ppf "%a " print e))
()
| {kind = Raises; exceptions; loc} ->
Format.fprintf ppf "%s raises %a@."
(loc.loc_start |> posToString)
(Exceptions.pp ~exnTable:None)
exceptions
| {kind = Catches nestedEvents; exceptions; loc} ->
Format.fprintf ppf "%s Catches exceptions:%a nestedEvents:%a@."
(loc.loc_start |> posToString)
(Exceptions.pp ~exnTable:None)
exceptions
(fun ppf () ->
nestedEvents |> List.iter (fun e -> Format.fprintf ppf "%a " print e))
()
let combine ~moduleName events =
if !Common.Cli.debug then (
Log_.item "@.";
Log_.item "Events combine: #events %d@." (events |> List.length));
let exnTable = Hashtbl.create 1 in
let extendExnTable exn loc =
match Hashtbl.find_opt exnTable exn with
| Some locSet -> Hashtbl.replace exnTable exn (LocSet.add loc locSet)
| None -> Hashtbl.replace exnTable exn (LocSet.add loc LocSet.empty)
in
let shrinkExnTable exn loc =
match Hashtbl.find_opt exnTable exn with
| Some locSet -> Hashtbl.replace exnTable exn (LocSet.remove loc locSet)
| None -> ()
in
let rec loop exnSet events =
match events with
| ({kind = Raises; exceptions; loc} as ev) :: rest ->
if !Common.Cli.debug then Log_.item "%a@." print ev;
exceptions |> Exceptions.iter (fun exn -> extendExnTable exn loc);
loop (Exceptions.union exnSet exceptions) rest
| ({kind = Call {callee; modulePath}; loc} as ev) :: rest ->
if !Common.Cli.debug then Log_.item "%a@." print ev;
let exceptions =
match callee |> Values.findPath ~moduleName ~modulePath with
| Some exceptions -> exceptions
| _ -> (
match ExnLib.find callee with
| Some exceptions -> exceptions
| None -> Exceptions.empty)
in
exceptions |> Exceptions.iter (fun exn -> extendExnTable exn loc);
loop (Exceptions.union exnSet exceptions) rest
| ({kind = DoesNotRaise nestedEvents; loc} as ev) :: rest ->
if !Common.Cli.debug then Log_.item "%a@." print ev;
let nestedExceptions = loop Exceptions.empty nestedEvents in
(if Exceptions.isEmpty nestedExceptions (* catch-all *) then
let name =
match nestedEvents with
| {kind = Call {callee}} :: _ -> callee |> Common.Path.toName
| _ -> "expression" |> Name.create
in
Log_.warning ~loc
(Common.ExceptionAnalysis
{
message =
Format.asprintf
"@{<info>%s@} does not raise and is annotated with \
redundant @doesNotRaise"
(name |> Name.toString);
}));
loop exnSet rest
| ({kind = Catches nestedEvents; exceptions} as ev) :: rest ->
if !Common.Cli.debug then Log_.item "%a@." print ev;
if Exceptions.isEmpty exceptions then loop exnSet rest
else
let nestedExceptions = loop Exceptions.empty nestedEvents in
let newRaises = Exceptions.diff nestedExceptions exceptions in
exceptions
|> Exceptions.iter (fun exn ->
nestedEvents
|> List.iter (fun event -> shrinkExnTable exn event.loc));
loop (Exceptions.union exnSet newRaises) rest
| [] -> exnSet
in
let exnSet = loop Exceptions.empty events in
(exnSet, exnTable)
end
module Checks = struct
type check = {
events: Event.t list;
loc: Location.t;
locFull: Location.t;
moduleName: string;
exnName: string;
exceptions: Exceptions.t;
}
type t = check list
let checks = (ref [] : t ref)
let add ~events ~exceptions ~loc ?(locFull = loc) ~moduleName exnName =
checks := {events; exceptions; loc; locFull; moduleName; exnName} :: !checks
let doCheck {events; exceptions; loc; locFull; moduleName; exnName} =
let raiseSet, exnTable = events |> Event.combine ~moduleName in
let missingAnnotations = Exceptions.diff raiseSet exceptions in
let redundantAnnotations = Exceptions.diff exceptions raiseSet in
(if not (Exceptions.isEmpty missingAnnotations) then
let description =
Common.ExceptionAnalysisMissing
{exnName; exnTable; raiseSet; missingAnnotations; locFull}
in
Log_.warning ~loc description);
if not (Exceptions.isEmpty redundantAnnotations) then
Log_.warning ~loc
(Common.ExceptionAnalysis
{
message =
(let raisesDescription ppf () =
if raiseSet |> Exceptions.isEmpty then
Format.fprintf ppf "raises nothing"
else
Format.fprintf ppf "might raise %a"
(Exceptions.pp ~exnTable:(Some exnTable))
raiseSet
in
Format.asprintf
"@{<info>%s@} %a and is annotated with redundant @raises(%a)"
exnName raisesDescription ()
(Exceptions.pp ~exnTable:None)
redundantAnnotations);
})
let doChecks () = !checks |> List.rev |> List.iter doCheck
end
let traverseAst () =
ModulePath.init ();
let super = Tast_mapper.default in
let currentId = ref "" in
let currentEvents = ref [] in
let exceptionsOfPatterns patterns =
patterns
|> List.fold_left
(fun acc desc ->
match desc with
| Typedtree.Tpat_construct ({txt}, _, _) ->
Exceptions.add (Exn.fromLid txt) acc
| _ -> acc)
Exceptions.empty
in
let iterExpr self e = self.Tast_mapper.expr self e |> ignore in
let iterExprOpt self eo =
match eo with
| None -> ()
| Some e -> e |> iterExpr self
in
let iterPat self p = self.Tast_mapper.pat self p |> ignore in
let iterCases self cases =
cases
|> List.iter (fun case ->
case.Typedtree.c_lhs |> iterPat self;
case.c_guard |> iterExprOpt self;
case.c_rhs |> iterExpr self)
in
let isRaise s = s = "Pervasives.raise" || s = "Pervasives.raise_notrace" in
let raiseArgs args =
match args with
| [(_, Some {Typedtree.exp_desc = Texp_construct ({txt}, _, _)})] ->
[Exn.fromLid txt] |> Exceptions.fromList
| [(_, Some {Typedtree.exp_desc = Texp_ident _})] ->
[Exn.fromString "genericException"] |> Exceptions.fromList
| _ -> [Exn.fromString "TODO_from_raise1"] |> Exceptions.fromList
in
let doesNotRaise attributes =
attributes
|> Annotation.getAttributePayload (fun s ->
s = "doesNotRaise" || s = "doesnotraise" || s = "DoesNoRaise"
|| s = "doesNotraise" || s = "doNotRaise" || s = "donotraise"
|| s = "DoNoRaise" || s = "doNotraise")
<> None
in
let expr (self : Tast_mapper.mapper) (expr : Typedtree.expression) =
let loc = expr.exp_loc in
let isDoesNoRaise = expr.exp_attributes |> doesNotRaise in
let oldEvents = !currentEvents in
if isDoesNoRaise then currentEvents := [];
(match expr.exp_desc with
| Texp_ident (callee_, _, _) ->
let callee =
callee_ |> Common.Path.fromPathT |> ModulePath.resolveAlias
in
let calleeName = callee |> Common.Path.toName in
if calleeName |> Name.toString |> isRaise then
Log_.warning ~loc
(Common.ExceptionAnalysis
{
message =
Format.asprintf
"@{<info>%s@} can be analyzed only if called directly"
(calleeName |> Name.toString);
});
currentEvents :=
{
Event.exceptions = Exceptions.empty;
loc;
kind = Call {callee; modulePath = (ModulePath.getCurrent ()).path};
}
:: !currentEvents
| Texp_apply
( {exp_desc = Texp_ident (atat, _, _)},
[(_lbl1, Some {exp_desc = Texp_ident (callee, _, _)}); arg] )
when (* raise @@ Exn(...) *)
atat |> Path.name = "Pervasives.@@" && callee |> Path.name |> isRaise
->
let exceptions = [arg] |> raiseArgs in
currentEvents := {Event.exceptions; loc; kind = Raises} :: !currentEvents;
arg |> snd |> iterExprOpt self
| Texp_apply
( {exp_desc = Texp_ident (atat, _, _)},
[arg; (_lbl1, Some {exp_desc = Texp_ident (callee, _, _)})] )
when (* Exn(...) |> raise *)
atat |> Path.name = "Pervasives.|>" && callee |> Path.name |> isRaise
->
let exceptions = [arg] |> raiseArgs in
currentEvents := {Event.exceptions; loc; kind = Raises} :: !currentEvents;
arg |> snd |> iterExprOpt self
| Texp_apply (({exp_desc = Texp_ident (callee, _, _)} as e), args) ->
let calleeName = Path.name callee in
if calleeName |> isRaise then
let exceptions = args |> raiseArgs in
currentEvents :=
{Event.exceptions; loc; kind = Raises} :: !currentEvents
else e |> iterExpr self;
args |> List.iter (fun (_, eOpt) -> eOpt |> iterExprOpt self)
| Texp_match (e, casesOk, casesExn, partial) ->
let cases = casesOk @ casesExn in
let exceptionPatterns =
casesExn
|> List.map (fun (case : Typedtree.case) -> case.c_lhs.pat_desc)
in
let exceptions = exceptionPatterns |> exceptionsOfPatterns in
if exceptionPatterns <> [] then (
let oldEvents = !currentEvents in
currentEvents := [];
e |> iterExpr self;
currentEvents :=
{Event.exceptions; loc; kind = Catches !currentEvents} :: oldEvents)
else e |> iterExpr self;
cases |> iterCases self;
if partial = Partial then
currentEvents :=
{
Event.exceptions = [Exn.matchFailure] |> Exceptions.fromList;
loc;
kind = Raises;
}
:: !currentEvents
| Texp_try (e, cases) ->
let exceptions =
cases
|> List.map (fun case -> case.Typedtree.c_lhs.pat_desc)
|> exceptionsOfPatterns
in
let oldEvents = !currentEvents in
currentEvents := [];
e |> iterExpr self;
currentEvents :=
{Event.exceptions; loc; kind = Catches !currentEvents} :: oldEvents;
cases |> iterCases self
| _ -> super.expr self expr |> ignore);
(if isDoesNoRaise then
let nestedEvents = !currentEvents in
currentEvents :=
{
Event.exceptions = Exceptions.empty;
loc;
kind = DoesNotRaise nestedEvents;
}
:: oldEvents);
expr
in
let getExceptionsFromAnnotations attributes =
let raisesAnnotationPayload =
attributes
|> Annotation.getAttributePayload (fun s -> s = "raises" || s = "raise")
in
let rec getExceptions payload =
match payload with
| Annotation.StringPayload s -> [Exn.fromString s] |> Exceptions.fromList
| Annotation.ConstructPayload s when s <> "::" ->
[Exn.fromString s] |> Exceptions.fromList
| Annotation.IdentPayload s ->
[Exn.fromString (s |> Longident.flatten |> String.concat ".")]
|> Exceptions.fromList
| Annotation.TuplePayload tuple ->
tuple
|> List.map (fun payload ->
payload |> getExceptions |> Exceptions.toList)
|> List.concat |> Exceptions.fromList
| _ -> Exceptions.empty
in
match raisesAnnotationPayload with
| None -> Exceptions.empty
| Some payload -> payload |> getExceptions
in
let toplevelEval (self : Tast_mapper.mapper) (expr : Typedtree.expression)
attributes =
let oldId = !currentId in
let oldEvents = !currentEvents in
let name = "Toplevel expression" in
currentId := name;
currentEvents := [];
let moduleName = !Common.currentModule in
self.expr self expr |> ignore;
Checks.add ~events:!currentEvents
~exceptions:(getExceptionsFromAnnotations attributes)
~loc:expr.exp_loc ~moduleName name;
currentId := oldId;
currentEvents := oldEvents
in
let structure_item (self : Tast_mapper.mapper)
(structureItem : Typedtree.structure_item) =
let oldModulePath = ModulePath.getCurrent () in
(match structureItem.str_desc with
| Tstr_eval (expr, attributes) -> toplevelEval self expr attributes
| Tstr_module {mb_id; mb_loc} ->
ModulePath.setCurrent
{
oldModulePath with
loc = mb_loc;
path = (mb_id |> Ident.name |> Name.create) :: oldModulePath.path;
}
| _ -> ());
let result = super.structure_item self structureItem in
ModulePath.setCurrent oldModulePath;
(match structureItem.str_desc with
| Tstr_module {mb_id; mb_expr = {mod_desc = Tmod_ident (path_, _lid)}} ->
ModulePath.addAlias
~name:(mb_id |> Ident.name |> Name.create)
~path:(path_ |> Common.Path.fromPathT)
| _ -> ());
result
in
let value_binding (self : Tast_mapper.mapper) (vb : Typedtree.value_binding) =
let oldId = !currentId in
let oldEvents = !currentEvents in
let isFunction =
match vb.vb_expr.exp_desc with
| Texp_function _ -> true
| _ -> false
in
let isToplevel = !currentId = "" in
let processBinding name =
currentId := name;
currentEvents := [];
let exceptionsFromAnnotations =
getExceptionsFromAnnotations vb.vb_attributes
in
exceptionsFromAnnotations |> Values.add ~name;
let res = super.value_binding self vb in
let moduleName = !Common.currentModule in
let path = [name |> Name.create] in
let exceptions =
match
path
|> Values.findPath ~moduleName
~modulePath:(ModulePath.getCurrent ()).path
with
| Some exceptions -> exceptions
| _ -> Exceptions.empty
in
Checks.add ~events:!currentEvents ~exceptions ~loc:vb.vb_pat.pat_loc
~locFull:vb.vb_loc ~moduleName name;
currentId := oldId;
currentEvents := oldEvents;
res
in
match vb.vb_pat.pat_desc with
| Tpat_any when isToplevel && not vb.vb_loc.loc_ghost -> processBinding "_"
| Tpat_construct ({txt}, _, _)
when isToplevel && (not vb.vb_loc.loc_ghost)
&& txt = Longident.Lident "()" ->
processBinding "()"
| Tpat_var (id, {loc = {loc_ghost}})
when (isFunction || isToplevel) && (not loc_ghost)
&& not vb.vb_loc.loc_ghost ->
processBinding (id |> Ident.name)
| _ -> super.value_binding self vb
in
let open Tast_mapper in
{super with expr; value_binding; structure_item}
let processStructure (structure : Typedtree.structure) =
let traverseAst = traverseAst () in
structure |> traverseAst.structure traverseAst |> ignore
let processCmt (cmt_infos : Cmt_format.cmt_infos) =
match cmt_infos.cmt_annots with
| Interface _ -> ()
| Implementation structure ->
Values.newCmt ();
structure |> processStructure
| _ -> ()