Skip to content

Commit 605caa1

Browse files
committed
Action for adding @dead annotation.
1 parent 4d74ba7 commit 605caa1

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

src/DeadCommon.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ let addValueDeclaration ?(isToplevel = true) ~(loc : CL.Location.t) ~moduleLoc
486486

487487
let emitWarning ?(onDeadDecl = fun () -> ()) ~decl ~message name =
488488
let loc = decl |> declGetLoc in
489-
Log_.warning ~loc ~notFinished:true ~name (fun ppf () ->
489+
Log_.warning ~loc ~notClosed:true ~name (fun ppf () ->
490490
Format.fprintf ppf "@{<info>%s@} %s"
491491
(decl.path |> Path.withoutHead)
492492
message);
@@ -604,13 +604,15 @@ module WriteDeadAnnotations = struct
604604
let posAnnotation = decl |> getPosAnnotation in
605605
let offset = decl.posAdjustment |> offsetOfPosAdjustment in
606606
EmitJson.emitAnnotate
607-
~line:(posAnnotation.pos_lnum - 1)
608-
~character:(posAnnotation.pos_cnum - posAnnotation.pos_bol + offset)
607+
~pos:
608+
( posAnnotation.pos_lnum - 1,
609+
posAnnotation.pos_cnum - posAnnotation.pos_bol + offset )
609610
~text:
610611
(if decl.posAdjustment = FirstVariant then
611612
(* avoid syntax error *)
612613
"| @dead "
613614
else "@dead ")
615+
~action:"Suppress dead code warning"
614616
else
615617
Format.fprintf ppf " <-- line %d@. %s@." decl.pos.pos_lnum
616618
(line |> lineToString)

src/EmitJson.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@ let items = ref 0
22

33
let start () = Format.fprintf Format.std_formatter "["
44

5-
let finish () = Format.fprintf Format.std_formatter "\n]\n"
5+
let finish () = Format.fprintf Format.std_formatter "\n]@."
66

7-
let emitClose () =
8-
Format.fprintf Format.std_formatter (if !items = 0 then "\n}" else "\n}")
7+
let emitClose () = Format.fprintf Format.std_formatter "\n}"
98

109
let emitItem ~name ~kind ~file ~range ~message =
1110
let open Format in
1211
items := !items + 1;
1312
let ppf = std_formatter in
1413
let startLine, startCharacter, endLine, endCharacter = range in
1514
fprintf ppf "%s{\n" (if !items = 1 then "\n" else ",\n");
16-
fprintf ppf " \"name\": \"%s\",@." name;
17-
fprintf ppf " \"kind\": \"%s\",@." kind;
18-
fprintf ppf " \"file\": \"%s\",@." file;
19-
fprintf ppf " \"range\": [%d,%d,%d,%d],@." startLine startCharacter endLine
15+
fprintf ppf " \"name\": \"%s\",\n" name;
16+
fprintf ppf " \"kind\": \"%s\",\n" kind;
17+
fprintf ppf " \"file\": \"%s\",\n" file;
18+
fprintf ppf " \"range\": [%d,%d,%d,%d],\n" startLine startCharacter endLine
2019
endCharacter;
2120
fprintf ppf " \"message\": \"%s\"" message
2221

23-
let emitAnnotate ~line ~character ~text =
22+
let locToPos (loc : CL.Location.t) =
23+
(loc.loc_start.pos_lnum - 1, loc.loc_start.pos_cnum - loc.loc_start.pos_bol)
24+
25+
let emitAnnotate ~pos ~text ~action =
26+
let line, character = pos in
2427
Format.fprintf Format.std_formatter
25-
",@. \"annotate\": { \"line\": %d, \"character\": %d, \"text\": \"%s\"}@."
26-
line character text
28+
",\n\
29+
\ \"annotate\": { \"line\": %d, \"character\": %d, \"text\": \"%s\", \
30+
\"action\": \"%s\"}"
31+
line character text action

src/Exception.ml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ module Checks = struct
169169
type check = {
170170
events : Event.t list;
171171
loc : CL.Location.t;
172+
locFull : CL.Location.t;
172173
moduleName : string;
173174
name : string;
174175
exceptions : Exceptions.t;
@@ -178,22 +179,30 @@ module Checks = struct
178179

179180
let checks = (ref [] : t ref)
180181

181-
let add ~events ~exceptions ~loc ~moduleName ~name =
182-
checks := {events; exceptions; loc; moduleName; name} :: !checks
182+
let add ~events ~exceptions ~loc ?(locFull = loc) ~moduleName name =
183+
checks := {events; exceptions; loc; locFull; moduleName; name} :: !checks
183184

184-
let doCheck {events; exceptions; loc; moduleName; name} =
185+
let doCheck {events; exceptions; loc; locFull; moduleName; name} =
185186
let raiseSet, exnTable = events |> Event.combine ~moduleName in
186187
let missingAnnotations = Exceptions.diff raiseSet exceptions in
187188
let redundantAnnotations = Exceptions.diff exceptions raiseSet in
188-
if not (Exceptions.isEmpty missingAnnotations) then
189-
Log_.warning ~loc ~name:"Exception Analysis" (fun ppf () ->
189+
if not (Exceptions.isEmpty missingAnnotations) then (
190+
let raisesTxt =
191+
Format.asprintf "%a" (Exceptions.pp ~exnTable:(Some exnTable)) raiseSet
192+
in
193+
let missingTxt =
194+
Format.asprintf "%a" (Exceptions.pp ~exnTable:None) missingAnnotations
195+
in
196+
Log_.warning ~loc ~name:"Exception Analysis" ~notClosed:true
197+
(fun ppf () ->
190198
Format.fprintf ppf
191-
"@{<info>%s@} might raise %a and is not annotated with @raises(%a)"
192-
name
193-
(Exceptions.pp ~exnTable:(Some exnTable))
194-
raiseSet
195-
(Exceptions.pp ~exnTable:None)
196-
missingAnnotations);
199+
"@{<info>%s@} might raise %s and is not annotated with @raises(%s)"
200+
name raisesTxt missingTxt);
201+
if !Common.Cli.json then (
202+
EmitJson.emitAnnotate ~action:"Add @raises annotation"
203+
~pos:(EmitJson.locToPos locFull)
204+
~text:(Format.asprintf "@raises(%s)\\n" missingTxt);
205+
EmitJson.emitClose ()));
197206
if not (Exceptions.isEmpty redundantAnnotations) then
198207
Log_.warning ~loc ~name:"Exception Analysis" (fun ppf () ->
199208
let raisesDescription ppf () =
@@ -389,7 +398,7 @@ let traverseAst () =
389398
self.expr self expr |> ignore;
390399
Checks.add ~events:!currentEvents
391400
~exceptions:(getExceptionsFromAnnotations attributes)
392-
~loc:expr.exp_loc ~moduleName ~name;
401+
~loc:expr.exp_loc ~moduleName name;
393402
currentId := oldId;
394403
currentEvents := oldEvents
395404
in
@@ -445,7 +454,7 @@ let traverseAst () =
445454
| _ -> Exceptions.empty
446455
in
447456
Checks.add ~events:!currentEvents ~exceptions ~loc:vb.vb_pat.pat_loc
448-
~moduleName ~name;
457+
~locFull:vb.vb_loc ~moduleName name;
449458
currentId := oldId;
450459
currentEvents := oldEvents;
451460
res

src/Log_.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type kind = Warning | Error
142142

143143
let first = ref true
144144

145-
let logKind ~count ~kind ~(loc : CL.Location.t) ~name ~notFinished body =
145+
let logKind ~count ~kind ~(loc : CL.Location.t) ~name ~notClosed body =
146146
if Suppress.filter loc.loc_start then (
147147
let open Format in
148148
first := false;
@@ -159,16 +159,16 @@ let logKind ~count ~kind ~(loc : CL.Location.t) ~name ~notFinished body =
159159
~file
160160
~range:(startLine, startCharacter, endLine, endCharacter)
161161
~message;
162-
if notFinished = false then EmitJson.emitClose ())
162+
if notClosed = false then EmitJson.emitClose ())
163163
else
164164
let color =
165165
match kind with Warning -> Color.info | Error -> Color.error
166166
in
167167
fprintf std_formatter "@[<v 2>@,%a@,%a@,%a@]@." color name Loc.print loc
168168
body ())
169169

170-
let warning ?(count = true) ?(notFinished = false) ~loc ~name body =
171-
body |> logKind ~kind:Warning ~count ~loc ~name ~notFinished
170+
let warning ?(count = true) ?(notClosed = false) ~loc ~name body =
171+
body |> logKind ~kind:Warning ~count ~loc ~name ~notClosed
172172

173173
let error ~loc ~name body =
174-
body |> logKind ~kind:Error ~count:true ~loc ~name ~notFinished:false
174+
body |> logKind ~kind:Error ~count:true ~loc ~name ~notClosed:false

0 commit comments

Comments
 (0)