Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace some folds with exists or for_all #1614

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .semgrep/fold.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
- id: fold-exists
patterns:
- pattern-either:
- pattern: $D.fold ... false
- pattern: $D.fold_left ... false
- pattern: $D.fold_right ... false
- pattern: fold ... false
- pattern: fold_left ... false
- pattern: fold_right ... false
message: consider replacing fold with exists
languages: [ocaml]
severity: WARNING

- id: fold-for_all
patterns:
- pattern-either:
- pattern: $D.fold ... true
- pattern: $D.fold_left ... true
- pattern: $D.fold_right ... true
- pattern: fold ... true
- pattern: fold_left ... true
- pattern: fold_right ... true
message: consider replacing fold with for_all
languages: [ocaml]
severity: WARNING
12 changes: 6 additions & 6 deletions src/analyses/basePriv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1040,11 +1040,11 @@ struct
let s = MustLockset.remove m (current_lockset ask) in
let t = current_thread ask in
let side_cpa = CPA.filter (fun x _ ->
GWeak.fold (fun s' tm acc ->
GWeak.exists (fun s' tm ->
(* TODO: swap 2^M and T partitioning for lookup by t here first? *)
let v = ThreadMap.find t tm in
(MustLockset.mem m s' && not (VD.is_bot v)) || acc
) (G.weak (getg (V.global x))) false
(MustLockset.mem m s' && not (VD.is_bot v))
) (G.weak (getg (V.global x)))
) st.cpa
in
sideg (V.mutex m) (G.create_sync (GSync.singleton s side_cpa));
Expand Down Expand Up @@ -1098,9 +1098,9 @@ struct
let unlock ask getg sideg (st: BaseComponents (D).t) m =
let s = MustLockset.remove m (current_lockset ask) in
let side_cpa = CPA.filter (fun x _ ->
GWeak.fold (fun s' v acc ->
(MustLockset.mem m s' && not (VD.is_bot v)) || acc
) (G.weak (getg (V.global x))) false
GWeak.exists (fun s' v ->
(MustLockset.mem m s' && not (VD.is_bot v))
) (G.weak (getg (V.global x)))
) st.cpa
in
sideg (V.mutex m) (G.create_sync (GSync.singleton s side_cpa));
Expand Down
6 changes: 3 additions & 3 deletions src/domain/partitionDomain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct
let meet _ _ = failwith "PartitonDomain.Set.meet: unsound"

let collapse (s1:t) (s2:t): bool =
let f vf2 res =
res || exists (fun vf1 -> S.collapse vf1 vf2) s1
let f vf2 =
exists (fun vf1 -> S.collapse vf1 vf2) s1
in
fold f s2 false
exists f s2

let add e s = join s (singleton e)

Expand Down
2 changes: 1 addition & 1 deletion src/incremental/compareCFG.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
false
end in
let cond n2 = Node.equal n2 (FunctionEntry f2) || check_all_nodes_in_same (List.map snd (CfgNew.prev n2)) n2 in
let forall = NH.fold (fun n2 n1 acc -> acc && cond n2) same.node2to1 true in
let forall = NH.fold (fun n2 n1 acc -> acc && cond n2) same.node2to1 true in (* nosemgrep: fold-for_all *) (* cond does side effects *)
Dismissed Show dismissed Hide dismissed
if not forall then repeat () in
repeat ();
NH.to_seq same.node1to2, NH.to_seq_keys diffNodes1
Expand Down
2 changes: 1 addition & 1 deletion src/solver/td3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ module Base =
destabilize_vs y || b || was_stable && List.mem_cmp S.Var.compare y vs
else
true
) w false
) w false (* nosemgrep: fold-exists *) (* does side effects *)
and solve ?reuse_eq x phase =
if tracing then trace "sol2" "solve %a, phase: %s, called: %b, stable: %b, wpoint: %b" S.Var.pretty_trace x (show_phase phase) (HM.mem called x) (HM.mem stable x) (HM.mem wpoint x);
init x;
Expand Down
8 changes: 4 additions & 4 deletions src/witness/witness.ml
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ struct
| UnreachCall _ ->
(* error function name is globally known through Svcomp.task *)
let is_unreach_call =
LHT.fold (fun (n, c) v acc ->
LHT.for_all (fun (n, c) v ->
match n with
(* FunctionEntry isn't used for extern __VERIFIER_error... *)
| FunctionEntry f when Svcomp.is_error_function f.svar ->
let is_dead = Spec.D.is_bot v in
acc && is_dead
| _ -> acc
) lh true
is_dead
| _ -> true
) lh
in

if is_unreach_call then (
Expand Down
Loading