Skip to content

Commit 960b9d8

Browse files
authored
Merge pull request #1573 from goblint/issue-1572
Improve use of `Lattice.BotValue` and `Lattice.TopValue`
2 parents b68df11 + 372a838 commit 960b9d8

File tree

6 files changed

+57
-35
lines changed

6 files changed

+57
-35
lines changed

src/cdomain/value/cdomains/stringDomain.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let to_string_length x =
7171

7272
let to_exp = function
7373
| Some x -> GoblintCil.mkString x
74-
| None -> raise (Lattice.Unsupported "Cannot express unknown string pointer as expression.")
74+
| None -> failwith "Cannot express unknown string pointer as expression."
7575

7676
let semantic_equal x y =
7777
match x, y with

src/cdomains/stackDomain.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ struct
1313
let n = 3
1414
let rec times x = function 0 -> [] | n -> x::times x (n-1)
1515
let rec map2 f xs ys =
16-
match xs, ys with x::xs, y::ys -> (try f x y :: map2 f xs ys with Lattice.Unsupported _ -> []) | _ -> []
16+
match xs, ys with x::xs, y::ys -> (try f x y :: map2 f xs ys with Lattice.BotValue | Lattice.TopValue -> []) | _ -> []
1717

1818
let rec fold_left2 f a b xs ys =
1919
match xs, ys with
2020
| [], _ | _, [] -> a
2121
| x::xs, y::ys ->
2222
try fold_left2 f (f a x y) b xs ys with
23-
| Lattice.Unsupported _ -> b
23+
| Lattice.BotValue | Lattice.TopValue -> b
2424

2525
let rec take n xs =
2626
match n, xs with

src/domain/lattice.ml

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ exception BotValue
4545
(** Exception raised by a bottomless lattice in place of a bottom value.
4646
Surrounding lattice functors may handle this on their own. *)
4747

48-
exception Unsupported of string
49-
let unsupported x = raise (Unsupported x)
50-
5148
exception Invalid_widen of Pretty.doc
5249

5350
let () = Printexc.register_printer (function
@@ -93,10 +90,10 @@ struct
9390
include Base
9491
let leq = equal
9592
let join x y =
96-
if equal x y then x else raise (Unsupported "fake join")
93+
if equal x y then x else raise TopValue
9794
let widen = join
9895
let meet x y =
99-
if equal x y then x else raise (Unsupported "fake meet")
96+
if equal x y then x else raise BotValue
10097
let narrow = meet
10198
include NoBotTop
10299

@@ -259,24 +256,36 @@ struct
259256
| (_, `Top) -> `Top
260257
| (`Bot, x) -> x
261258
| (x, `Bot) -> x
262-
| (`Lifted x, `Lifted y) -> `Lifted (Base.join x y)
259+
| (`Lifted x, `Lifted y) ->
260+
try `Lifted (Base.join x y)
261+
with TopValue -> `Top
263262

264263
let meet x y =
265264
match (x,y) with
266265
| (`Bot, _) -> `Bot
267266
| (_, `Bot) -> `Bot
268267
| (`Top, x) -> x
269268
| (x, `Top) -> x
270-
| (`Lifted x, `Lifted y) -> `Lifted (Base.meet x y)
269+
| (`Lifted x, `Lifted y) ->
270+
try `Lifted (Base.meet x y)
271+
with BotValue -> `Bot
271272

272273
let widen x y =
273274
match (x,y) with
274-
| (`Lifted x, `Lifted y) -> `Lifted (Base.widen x y)
275+
| (`Lifted x, `Lifted y) ->
276+
begin
277+
try `Lifted (Base.widen x y)
278+
with TopValue -> `Top
279+
end
275280
| _ -> y
276281

277282
let narrow x y =
278283
match (x,y) with
279-
| (`Lifted x, `Lifted y) -> `Lifted (Base.narrow x y)
284+
| (`Lifted x, `Lifted y) ->
285+
begin
286+
try `Lifted (Base.narrow x y)
287+
with BotValue -> `Bot
288+
end
280289
| (_, `Bot) -> `Bot
281290
| (`Top, y) -> y
282291
| _ -> x
@@ -315,7 +324,7 @@ struct
315324
| (x, `Bot) -> x
316325
| (`Lifted x, `Lifted y) ->
317326
try `Lifted (Base.join x y)
318-
with Uncomparable -> `Top
327+
with Uncomparable | TopValue -> `Top
319328

320329
let meet x y =
321330
match (x,y) with
@@ -325,20 +334,24 @@ struct
325334
| (x, `Top) -> x
326335
| (`Lifted x, `Lifted y) ->
327336
try `Lifted (Base.meet x y)
328-
with Uncomparable -> `Bot
337+
with Uncomparable | BotValue -> `Bot
329338

330339
let widen x y =
331340
match (x,y) with
332341
| (`Lifted x, `Lifted y) ->
333-
(try `Lifted (Base.widen x y)
334-
with Uncomparable -> `Top)
342+
begin
343+
try `Lifted (Base.widen x y)
344+
with Uncomparable | TopValue -> `Top
345+
end
335346
| _ -> y
336347

337348
let narrow x y =
338349
match (x,y) with
339350
| (`Lifted x, `Lifted y) ->
340-
(try `Lifted (Base.narrow x y)
341-
with Uncomparable -> `Bot)
351+
begin
352+
try `Lifted (Base.narrow x y)
353+
with Uncomparable | BotValue -> `Bot
354+
end
342355
| (_, `Bot) -> `Bot
343356
| (`Top, y) -> y
344357
| _ -> x
@@ -378,11 +391,11 @@ struct
378391
| (x, `Bot) -> x
379392
| (`Lifted1 x, `Lifted1 y) -> begin
380393
try `Lifted1 (Base1.join x y)
381-
with Unsupported _ -> `Top
394+
with TopValue -> `Top
382395
end
383396
| (`Lifted2 x, `Lifted2 y) -> begin
384397
try `Lifted2 (Base2.join x y)
385-
with Unsupported _ -> `Top
398+
with TopValue -> `Top
386399
end
387400
| _ -> `Top
388401

@@ -394,11 +407,11 @@ struct
394407
| (x, `Top) -> x
395408
| (`Lifted1 x, `Lifted1 y) -> begin
396409
try `Lifted1 (Base1.meet x y)
397-
with Unsupported _ -> `Bot
410+
with BotValue -> `Bot
398411
end
399412
| (`Lifted2 x, `Lifted2 y) -> begin
400413
try `Lifted2 (Base2.meet x y)
401-
with Unsupported _ -> `Bot
414+
with BotValue -> `Bot
402415
end
403416
| _ -> `Bot
404417

@@ -489,7 +502,9 @@ struct
489502
match (x,y) with
490503
| (`Bot, _) -> `Bot
491504
| (_, `Bot) -> `Bot
492-
| (`Lifted x, `Lifted y) -> `Lifted (Base.meet x y)
505+
| (`Lifted x, `Lifted y) ->
506+
try `Lifted (Base.meet x y)
507+
with BotValue -> `Bot
493508

494509
let widen x y =
495510
match (x,y) with
@@ -498,7 +513,11 @@ struct
498513

499514
let narrow x y =
500515
match (x,y) with
501-
| (`Lifted x, `Lifted y) -> `Lifted (Base.narrow x y)
516+
| (`Lifted x, `Lifted y) ->
517+
begin
518+
try `Lifted (Base.narrow x y)
519+
with BotValue -> `Bot
520+
end
502521
| (_, `Bot) -> `Bot
503522
| _ -> x
504523
end
@@ -525,7 +544,9 @@ struct
525544
match (x,y) with
526545
| (`Top, x) -> `Top
527546
| (x, `Top) -> `Top
528-
| (`Lifted x, `Lifted y) -> `Lifted (Base.join x y)
547+
| (`Lifted x, `Lifted y) ->
548+
try `Lifted (Base.join x y)
549+
with TopValue -> `Top
529550

530551
let meet x y =
531552
match (x,y) with
@@ -535,7 +556,11 @@ struct
535556

536557
let widen x y =
537558
match (x,y) with
538-
| (`Lifted x, `Lifted y) -> `Lifted (Base.widen x y)
559+
| (`Lifted x, `Lifted y) ->
560+
begin
561+
try `Lifted (Base.widen x y)
562+
with TopValue -> `Top
563+
end
539564
| _ -> y
540565

541566
let narrow x y =
@@ -553,10 +578,7 @@ end
553578
module Liszt (Base: S) =
554579
struct
555580
include Printable.Liszt (Base)
556-
let bot () = raise (Unsupported "bot?")
557-
let is_top _ = false
558-
let top () = raise (Unsupported "top?")
559-
let is_bot _ = false
581+
include NoBotTop
560582

561583
let leq =
562584
let f acc x y = Base.leq x y && acc in

src/domain/mapDomain.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ struct
398398
let leq = leq_with_fct Range.leq
399399

400400
let find x m = try find x m with | Not_found -> Range.bot ()
401-
let top () = Lattice.unsupported "partial map top"
401+
let top () = raise Lattice.TopValue
402402
let bot () = empty ()
403403
let is_top _ = false
404404
let is_bot = is_empty
@@ -448,7 +448,7 @@ struct
448448

449449
let find x m = try find x m with | Not_found -> Range.top ()
450450
let top () = empty ()
451-
let bot () = Lattice.unsupported "partial map bot"
451+
let bot () = raise Lattice.BotValue
452452
let is_top = is_empty
453453
let is_bot _ = false
454454

src/domains/domainProperties.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct
126126
with
127127
| Failure _ (* raised by IntDomain *)
128128
| SetDomain.Unsupported _ (* raised by SetDomain *)
129-
| Lattice.Unsupported _ (* raised by MapDomain *) ->
129+
| Lattice.TopValue (* raised by MapDomain *) ->
130130
false
131131

132132
let top_leq = make ~name:"top leq" (arb) (fun a ->

tests/unit/domains/mapDomainTest.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct
1818
let get_empty () =
1919
try
2020
(is_empty_top := true; M.top ())
21-
with Lattice.Unsupported _ | Lattice.BotValue | Lattice.TopValue ->
21+
with Lattice.TopValue ->
2222
(is_empty_top := false; M.bot ())
2323

2424
let is_empty x =
@@ -44,7 +44,7 @@ struct
4444
map := M.remove "key1" !map;
4545
begin
4646
try ignore (M.find "key1" !map); assert_failure "problem removeing key1"
47-
with Lattice.Unsupported _ | Lattice.BotValue | Lattice.TopValue -> ()
47+
with Lattice.BotValue | Lattice.TopValue -> ()
4848
end ;
4949

5050
end

0 commit comments

Comments
 (0)