Skip to content

Commit 0f1d8a1

Browse files
committed
Move WitnessUtil.InvariantExp into Invariant module
1 parent a8d21d0 commit 0f1d8a1

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

src/cdomain/value/domains/invariant.ml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,47 @@ struct
2424
let narrow = meet
2525
end
2626

27+
module ES = SetDomain.Make (ExpLat)
28+
29+
(* Turns an expression into alist of conjuncts, pulling out common conjuncts from top-level disjunctions *)
30+
let rec pullOutCommonConjuncts e =
31+
let rec to_conjunct_set = function
32+
| Cil.BinOp(LAnd,e1,e2,_) -> ES.join (to_conjunct_set e1) (to_conjunct_set e2)
33+
| e -> ES.singleton e
34+
in
35+
let combine_conjuncts es = ES.fold (fun e acc -> match acc with | None -> Some e | Some acce -> Some (BinOp(LAnd,acce,e,Cil.intType))) es None in
36+
match e with
37+
| Cil.BinOp(LOr, e1, e2,t) ->
38+
let e1s = pullOutCommonConjuncts e1 in
39+
let e2s = pullOutCommonConjuncts e2 in
40+
let common = ES.inter e1s e2s in
41+
let e1s' = ES.diff e1s e2s in
42+
let e2s' = ES.diff e2s e1s in
43+
(match combine_conjuncts e1s', combine_conjuncts e2s' with
44+
| Some e1e, Some e2e -> ES.add (BinOp(LOr,e1e,e2e,Cil.intType)) common
45+
| _ -> common (* if one of the disjuncts is empty, it is equivalent to true here *)
46+
)
47+
| e -> to_conjunct_set e
48+
49+
let process_exp inv =
50+
let exp_deep_unroll_types =
51+
if GobConfig.get_bool "witness.invariant.typedefs" then
52+
Fun.id
53+
else
54+
InvariantCil.exp_deep_unroll_types
55+
in
56+
let inv' =
57+
inv
58+
|> exp_deep_unroll_types
59+
|> InvariantCil.exp_replace_original_name
60+
in
61+
if GobConfig.get_bool "witness.invariant.split-conjunction" then
62+
ES.elements (pullOutCommonConjuncts inv')
63+
|> List.filter (Fun.negate InvariantCil.exp_contains_anon_type)
64+
else
65+
[inv']
66+
67+
2768
(** Lift {!ExpLat} such that join/meet folds don't introduce excessive [|| 0|] or [&& 1] expressions. *)
2869

2970
module N =

src/cdomain/value/domains/invariant.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
include Lattice.S with type t = [ `Bot | `Lifted of GoblintCil.exp | `Top ]
44

5+
val process_exp: GoblintCil.exp -> GoblintCil.exp list
6+
57
val none: t
68
val of_exp: GoblintCil.exp -> t
79

src/transform/evalAssert.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct
6060
let context = {Invariant.default_context with lvals} in
6161
match (ask ~node loc).f (Queries.Invariant context) with
6262
| `Lifted e ->
63-
let es = WitnessUtil.InvariantExp.process_exp e in
63+
let es = Invariant.process_exp e in
6464
let asserts = List.map (fun e -> cInstr ("%v:assert (%e:exp);") loc [("assert", Fv assert_function); ("exp", Fe e)]) es in
6565
if surroundByAtomic then
6666
let abegin = (cInstr ("%v:__VERIFIER_atomic_begin();") loc [("__VERIFIER_atomic_begin", Fv atomicBegin)]) in

src/witness/witnessUtil.ml

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -139,48 +139,6 @@ struct
139139
end
140140
[@@deprecated]
141141

142-
module InvariantExp =
143-
struct
144-
module ES = SetDomain.Make (CilType.Exp)
145-
146-
(* Turns an expression into alist of conjuncts, pulling out common conjuncts from top-level disjunctions *)
147-
let rec pullOutCommonConjuncts e =
148-
let rec to_conjunct_set = function
149-
| Cil.BinOp(LAnd,e1,e2,_) -> ES.join (to_conjunct_set e1) (to_conjunct_set e2)
150-
| e -> ES.singleton e
151-
in
152-
let combine_conjuncts es = ES.fold (fun e acc -> match acc with | None -> Some e | Some acce -> Some (BinOp(LAnd,acce,e,Cil.intType))) es None in
153-
match e with
154-
| Cil.BinOp(LOr, e1, e2,t) ->
155-
let e1s = pullOutCommonConjuncts e1 in
156-
let e2s = pullOutCommonConjuncts e2 in
157-
let common = ES.inter e1s e2s in
158-
let e1s' = ES.diff e1s e2s in
159-
let e2s' = ES.diff e2s e1s in
160-
(match combine_conjuncts e1s', combine_conjuncts e2s' with
161-
| Some e1e, Some e2e -> ES.add (BinOp(LOr,e1e,e2e,Cil.intType)) common
162-
| _ -> common (* if one of the disjuncts is empty, it is equivalent to true here *)
163-
)
164-
| e -> to_conjunct_set e
165-
166-
let process_exp inv =
167-
let exp_deep_unroll_types =
168-
if GobConfig.get_bool "witness.invariant.typedefs" then
169-
Fun.id
170-
else
171-
InvariantCil.exp_deep_unroll_types
172-
in
173-
let inv' =
174-
inv
175-
|> exp_deep_unroll_types
176-
|> InvariantCil.exp_replace_original_name
177-
in
178-
if GobConfig.get_bool "witness.invariant.split-conjunction" then
179-
ES.elements (pullOutCommonConjuncts inv')
180-
|> List.filter (Fun.negate InvariantCil.exp_contains_anon_type)
181-
else
182-
[inv']
183-
end
184142

185143
module InvariantParser =
186144
struct

src/witness/yamlWitness.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ struct
232232
(* Currently same invariants (from InvariantGlobal query) for all nodes (from InvariantGlobalNodes query).
233233
The alternative would be querying InvariantGlobal per local unknown when looping over them to generate location invariants.
234234
See: https://github.com/goblint/analyzer/pull/1394#discussion_r1698149514. *)
235-
let invs = WitnessUtil.InvariantExp.process_exp inv in
235+
let invs = Invariant.process_exp inv in
236236
Queries.NS.fold (fun n acc ->
237237
let fundec = Node.find_fundec n in
238238
match WitnessInvariant.location_location n with (* Not just using Node.location because it returns expression location which may be invalid for location invariant (e.g. inside conditional). *)
@@ -293,7 +293,7 @@ struct
293293
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
294294
let location_function = fundec.svar.vname in
295295
let location = Entry.location ~location:loc ~location_function in
296-
let invs = WitnessUtil.InvariantExp.process_exp inv in
296+
let invs = Invariant.process_exp inv in
297297
List.fold_left (fun acc inv ->
298298
let invariant = CilType.Exp.show inv in
299299
let invariant = Entry.location_invariant ~location ~invariant in
@@ -323,7 +323,7 @@ struct
323323
let fundec = Node.find_fundec (List.hd ns) in (* TODO: fix location hack *)
324324
let location_function = fundec.svar.vname in
325325
let location = Entry.location ~location:loc ~location_function in
326-
let invs = WitnessUtil.InvariantExp.process_exp inv in
326+
let invs = Invariant.process_exp inv in
327327
List.fold_left (fun acc inv ->
328328
let invariant = CilType.Exp.show inv in
329329
let invariant = Entry.loop_invariant ~location ~invariant in
@@ -349,7 +349,7 @@ struct
349349
| `Left g -> (* global unknown from analysis Spec *)
350350
begin match R.ask_global (InvariantGlobal (Obj.repr g)), GobConfig.get_string "witness.invariant.flow_insensitive-as" with
351351
| `Lifted inv, "invariant_set-flow_insensitive_invariant" ->
352-
let invs = WitnessUtil.InvariantExp.process_exp inv in
352+
let invs = Invariant.process_exp inv in
353353
List.fold_left (fun acc inv ->
354354
let invariant = CilType.Exp.show inv in
355355
let invariant = Entry.flow_insensitive_invariant ~invariant in

0 commit comments

Comments
 (0)