Skip to content

Commit 54c2bb9

Browse files
committed
Extract Cilfacade.exp_is_boolean
1 parent de2d51e commit 54c2bb9

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/analyses/baseInvariant.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,9 @@ struct
870870
in
871871
if eval_bool exp st = Some (not tv) then contra st (* we already know that the branch is dead *)
872872
else
873-
(* C11 6.5.13, 6.5.14, 6.5.3.3: LAnd, LOr and LNot also return 0 or 1 *)
874-
let is_cmp = function
875-
| UnOp (LNot, _, _)
876-
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _, _) -> true
877-
| _ -> false
878-
in
879873
match Cilfacade.get_ikind_exp exp with
880874
| ik ->
881-
let itv = if not tv || is_cmp exp then (* false is 0, but true can be anything that is not 0, except for comparisons which yield 1 *)
875+
let itv = if not tv || Cilfacade.exp_is_boolean exp then (* false is 0, but true can be anything that is not 0, except for comparisons which yield 1 *)
882876
ID.of_bool ik tv (* this will give 1 for true which is only ok for comparisons *)
883877
else
884878
ID.of_excl_list ik [Z.zero] (* Lvals, Casts, arithmetic operations etc. should work with true = non_zero *)

src/cdomains/apron/sharedFunctions.apron.ml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -544,18 +544,12 @@ struct
544544
module Tracked = Tracked
545545
module Convert = Convert (V) (Bounds) (Arg) (Tracked)
546546

547-
(* TODO: deduplicate with BaseInvariant is_cmp *)
548-
let exp_is_constraint = function
549-
| UnOp (LNot, _, _)
550-
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _, _) -> true
551-
| _ -> false
552-
553547
(* TODO: move logic-handling assert_constraint from Apron back to here, after fixing affeq bot-bot join *)
554548

555549
(** Assert any expression. *)
556550
let assert_inv ask d e negate no_ov =
557551
let e' =
558-
if exp_is_constraint e then
552+
if Cilfacade.exp_is_boolean e then
559553
e
560554
else
561555
(* convert non-constraint expression, such that we assert(e != 0) *)
@@ -588,8 +582,8 @@ struct
588582
| exception Invalid_argument _ ->
589583
ID.top () (* real top, not a top of any ikind because we don't even know the ikind *)
590584
| ik ->
591-
if M.tracing then M.trace "relation" "eval_int: exp_is_constraint %a = %B" d_plainexp e (exp_is_constraint e);
592-
if exp_is_constraint e then
585+
if M.tracing then M.trace "relation" "eval_int: exp_is_constraint %a = %B" d_plainexp e (Cilfacade.exp_is_boolean e);
586+
if Cilfacade.exp_is_boolean e then
593587
match check_assert ask d e no_ov with
594588
| `True -> ID.of_bool ik true
595589
| `False -> ID.of_bool ik false

src/common/util/cilfacade.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ let makeBinOp binop e1 e2 =
441441
let (_, e) = Cabs2cil.doBinOp binop e1 t1 e2 t2 in
442442
e
443443

444+
(** Is effectively boolean-returning operation?
445+
These operations have type [int], but only have values 0 or 1. *)
446+
let exp_is_boolean = function
447+
(* C11 6.5.13, 6.5.14, 6.5.3.3: LAnd, LOr and LNot also return 0 or 1 *)
448+
| UnOp (LNot, _, _)
449+
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _, _) -> true
450+
| _ -> false
451+
444452
let anoncomp_name_regexp = Str.regexp {|^__anon\(struct\|union\)\(_\(.+\)\)?_\([0-9]+\)$|}
445453

446454
let split_anoncomp_name name =

0 commit comments

Comments
 (0)