Skip to content

Commit 2c36291

Browse files
Merge pull request #1688 from goblint/issue_1536
Audit usages of `fold_right`
2 parents 85aaec9 + 7ab076e commit 2c36291

File tree

9 files changed

+33
-40
lines changed

9 files changed

+33
-40
lines changed

src/analyses/apron/relationAnalysis.apron.ml

+13-24
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,15 @@ struct
465465

466466
(* Give the set of reachables from argument. *)
467467
let reachables (ask: Queries.ask) es =
468-
let reachable e st =
469-
match st with
470-
| None -> None
471-
| Some st ->
468+
let reachable acc e =
469+
Option.bind acc (fun st ->
472470
let ad = ask.f (Queries.ReachableFrom e) in
473471
if Queries.AD.is_top ad then
474472
None
475473
else
476-
Some (Queries.AD.join ad st)
474+
Some (Queries.AD.join ad st))
477475
in
478-
List.fold_right reachable es (Some (Queries.AD.empty ()))
476+
List.fold_left reachable (Some (Queries.AD.empty ())) es
479477

480478

481479
let forget_reachable man st es =
@@ -484,9 +482,8 @@ struct
484482
match reachables ask es with
485483
| None ->
486484
(* top reachable, so try to invalidate everything *)
487-
RD.vars st.rel
488-
|> List.filter_map RV.to_cil_varinfo
489-
|> List.map Cil.var
485+
let to_cil_lval x = Option.map Cil.var @@ RV.to_cil_varinfo x in
486+
RD.vars st.rel |> List.filter_map to_cil_lval
490487
| Some ad ->
491488
let to_cil addr rs =
492489
match addr with
@@ -521,14 +518,10 @@ struct
521518
match desc.special args, f.vname with
522519
| Assert { exp; refine; _ }, _ -> assert_fn man exp refine
523520
| ThreadJoin { thread = id; ret_var = retvar }, _ ->
524-
(
525-
(* Forget value that thread return is assigned to *)
526-
let st' = forget_reachable man st [retvar] in
527-
let st' = Priv.thread_join ask man.global id st' in
528-
match r with
529-
| Some lv -> invalidate_one ask man st' lv
530-
| None -> st'
531-
)
521+
(* Forget value that thread return is assigned to *)
522+
let st' = forget_reachable man st [retvar] in
523+
let st' = Priv.thread_join ask man.global id st' in
524+
Option.map_default (invalidate_one ask man st') st' r
532525
| ThreadExit _, _ ->
533526
begin match ThreadId.get_current ask with
534527
| `Lifted tid ->
@@ -543,11 +536,10 @@ struct
543536
let id = List.hd args in
544537
Priv.thread_join ~force:true ask man.global id st
545538
| Rand, _ ->
546-
(match r with
547-
| Some lv ->
539+
Option.map_default (fun lv ->
548540
let st = invalidate_one ask man st lv in
549541
assert_fn {man with local = st} (BinOp (Ge, Lval lv, zero, intType)) true
550-
| None -> st)
542+
) st r
551543
| _, _ ->
552544
let lvallist e =
553545
match ask.f (Queries.MayPointTo e) with
@@ -575,10 +567,7 @@ struct
575567
let shallow_lvals = List.concat_map lvallist shallow_addrs in
576568
let st' = List.fold_left (invalidate_one ask man) st' shallow_lvals in
577569
(* invalidate lval if present *)
578-
match r with
579-
| Some lv -> invalidate_one ask man st' lv
580-
| None -> st'
581-
570+
Option.map_default (invalidate_one ask man st') st' r
582571

583572
let query_invariant man context =
584573
let keep_local = GobConfig.get_bool "ana.relation.invariant.local" in

src/analyses/base.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ struct
518518
(* From a list of values, presumably arguments to a function, simply extract
519519
* the pointer arguments. *)
520520
let get_ptrs (vals: value list): address list =
521-
let f (x:value) acc = match x with
521+
let f acc (x:value) = match x with
522522
| Address adrs when AD.is_top adrs ->
523523
M.info ~category:Unsound "Unknown address given as function argument"; acc
524524
| Address adrs when AD.to_var_may adrs = [] -> acc
@@ -528,7 +528,7 @@ struct
528528
| Top -> M.info ~category:Unsound "Unknown value type given as function argument"; acc
529529
| _ -> acc
530530
in
531-
List.fold_right f vals []
531+
List.fold_left f [] vals
532532

533533
let rec reachable_from_value ask (value: value) (t: typ) (description: string) =
534534
let empty = AD.empty () in
@@ -572,7 +572,7 @@ struct
572572
if M.tracing then M.traceli "reachability" "Checking reachable arguments from [%a]!" (d_list ", " AD.pretty) args;
573573
let empty = AD.empty () in
574574
(* We begin looking at the parameters: *)
575-
let argset = List.fold_right (AD.join) args empty in
575+
let argset = List.fold_left (AD.join) empty args in
576576
let workset = ref argset in
577577
(* And we keep a set of already visited variables *)
578578
let visited = ref empty in

src/analyses/malloc_null.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct
9494
(* Remove null values from state that are unreachable from exp.*)
9595
let remove_unreachable (ask: Queries.ask) (args: exp list) (st: D.t) : D.t =
9696
let reachable =
97-
let do_exp e a =
97+
let do_exp a e =
9898
match ask.f (Queries.ReachableFrom e) with
9999
| ad when not (Queries.AD.is_top ad) ->
100100
ad
@@ -103,9 +103,9 @@ struct
103103
| _ -> false)
104104
|> Queries.AD.join a
105105
(* Ignore soundness warnings, as invalidation proper will raise them. *)
106-
| _ -> AD.empty ()
106+
| _ -> a
107107
in
108-
List.fold_right do_exp args (AD.empty ())
108+
List.fold_left do_exp (AD.empty ()) args
109109
in
110110
let vars =
111111
reachable
@@ -164,7 +164,7 @@ struct
164164
let return_addr () = !return_addr_
165165

166166
let return man (exp:exp option) (f:fundec) : D.t =
167-
let remove_var x v = List.fold_right D.remove (to_addrs v) x in
167+
let remove_var x v = List.fold_left (Fun.flip D.remove) x (to_addrs v) in
168168
let nst = List.fold_left remove_var man.local (f.slocals @ f.sformals) in
169169
match exp with
170170
| Some ret ->

src/analyses/region.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct
5555
| Queries.Regions e ->
5656
let regpart = man.global () in
5757
if is_bullet e regpart man.local then Queries.Result.bot q (* TODO: remove bot *) else
58-
let ls = List.fold_right Queries.LS.add (regions e regpart man.local) (Queries.LS.empty ()) in
58+
let ls = List.fold_left (Fun.flip Queries.LS.add) (Queries.LS.empty ()) (regions e regpart man.local) in
5959
ls
6060
| _ -> Queries.Result.top q
6161

src/analyses/symbLocks.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ struct
4343
let assign man lval rval = invalidate_lval (Analyses.ask_of_man man) lval man.local
4444

4545
let return man exp fundec =
46-
List.fold_right D.remove_var (fundec.sformals@fundec.slocals) man.local
46+
let rm list acc = List.fold_left (Fun.flip D.remove_var) acc list in
47+
rm fundec.slocals (rm fundec.sformals man.local)
4748

4849
let enter man lval f args = [(man.local,man.local)]
4950
let combine_env man lval fexp f args fc au f_ask = au

src/analyses/uninit.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ struct
202202
| _ -> false)
203203
|> Queries.AD.join a
204204
(* Ignore soundness warnings, as invalidation proper will raise them. *)
205-
| _ -> AD.empty ()
205+
| _ -> a
206206
in
207207
List.fold_right do_exp args (AD.empty ())
208208
in

src/analyses/varEq.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ struct
386386
*)
387387
(* Give the set of reachables from argument. *)
388388
let reachables ~deep (ask: Queries.ask) es =
389-
let reachable e st =
389+
let reachable acc e =
390390
let q = if deep then Queries.ReachableFrom e else Queries.MayPointTo e in
391391
let ad = ask.f q in
392-
Queries.AD.join ad st
392+
Queries.AD.join ad acc
393393
in
394-
List.fold_right reachable es (Queries.AD.empty ())
394+
List.fold_left reachable (Queries.AD.empty ()) es
395395

396396

397397
(* Probably ok as is. *)
@@ -402,8 +402,8 @@ struct
402402

403403
(* Just remove things that go out of scope. *)
404404
let return man exp fundec =
405-
let rm v = remove (Analyses.ask_of_man man) (Var v,NoOffset) in
406-
List.fold_right rm (fundec.sformals@fundec.slocals) man.local
405+
let rm acc v = remove (Analyses.ask_of_man man) (Var v, NoOffset) acc in
406+
List.fold_left rm man.local (fundec.sformals@fundec.slocals)
407407

408408
(* removes all equalities with lval and then tries to make a new one: lval=rval *)
409409
let assign man (lval:lval) (rval:exp) : D.t =

src/cdomain/value/cdomains/int/defExcDomain.ml

+3
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ struct
321321
let of_excl_list t l =
322322
let r = size t in (* elements in l are excluded from the full range of t! *)
323323
`Excluded (List.fold_right S.add l (S.empty ()), r)
324+
(* TODO: Change after #1686 has landed *)
325+
(* `Excluded (S.of_list l, r) *)
326+
324327
let is_excl_list l = match l with `Excluded _ -> true | _ -> false
325328
let to_excl_list (x:t) = match x with
326329
| `Definite _ -> None

src/util/library/libraryFunctions.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ let reset_lazy () =
12881288
ResettableLazy.reset activated_library_descs
12891289

12901290
let lib_funs = ref (Set.String.of_list ["__raw_read_unlock"; "__raw_write_unlock"; "spin_trylock"])
1291-
let add_lib_funs funs = lib_funs := List.fold_right Set.String.add funs !lib_funs
1291+
let add_lib_funs funs = lib_funs := List.fold_left (Fun.flip Set.String.add) !lib_funs funs
12921292
let use_special fn_name = Set.String.mem fn_name !lib_funs
12931293

12941294
let kernel_safe_uncalled = Set.String.of_list ["__inittest"; "init_module"; "__exittest"; "cleanup_module"]

0 commit comments

Comments
 (0)