Skip to content

Commit 4df87a7

Browse files
committed
Extract SharedFunctions.int_of_coeff
1 parent 6b95d96 commit 4df87a7

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/cdomains/apron/gobApron.apron.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ module Coeff =
1919
struct
2020
include Coeff
2121

22+
let pp = print
23+
include Printable.SimpleFormat (
24+
struct
25+
type nonrec t = t
26+
let pp = pp
27+
end
28+
)
29+
2230
let s_of_z z = Coeff.s_of_mpqf (Mpqf.of_mpz (Z_mlgmpidl.mpz_of_z z))
2331
end
2432

src/cdomains/apron/sharedFunctions.apron.ml

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ let int_of_scalar ?(scalewith=Z.one) ?round (scalar: Scalar.t) =
4141
| _ ->
4242
failwith ("int_of_scalar: unsupported: " ^ Scalar.show scalar)
4343

44+
let int_of_coeff ?scalewith ?round (coeff: Coeff.union_5) =
45+
match coeff with
46+
| Scalar scalar -> int_of_scalar ?scalewith ?round scalar
47+
| Interval _ -> None
4448

4549
module type ConvertArg =
4650
sig
@@ -272,27 +276,24 @@ struct
272276

273277
(** Returned boolean indicates whether returned expression should be negated. *)
274278
let coeff_to_const ~scalewith (c:Coeff.union_5) =
275-
match c with
276-
| Scalar c ->
277-
(match int_of_scalar ?scalewith c with
278-
| Some i ->
279-
let ci,truncation = truncateCilint ILongLong i in
280-
if truncation = NoTruncation then
281-
if Z.compare i Z.zero >= 0 then
282-
false, Const (CInt(i,ILongLong,None))
283-
else
284-
(* attempt to negate if that does not cause an overflow *)
285-
let cneg, truncation = truncateCilint ILongLong (Z.neg i) in
286-
if truncation = NoTruncation then
287-
true, Const (CInt((Z.neg i),ILongLong,None))
288-
else
289-
false, Const (CInt(i,ILongLong,None))
290-
else
291-
raise Unsupported_Linexpr1
292-
| None ->
293-
M.warn ~category:Analyzer "Invariant Apron: coefficient is not int: %a" Scalar.pretty c;
294-
raise Unsupported_Linexpr1)
295-
| _ -> raise Unsupported_Linexpr1
279+
match int_of_coeff ?scalewith c with
280+
| Some i ->
281+
let ci,truncation = truncateCilint ILongLong i in
282+
if truncation = NoTruncation then
283+
if Z.compare i Z.zero >= 0 then
284+
false, Const (CInt(i,ILongLong,None))
285+
else
286+
(* attempt to negate if that does not cause an overflow *)
287+
let cneg, truncation = truncateCilint ILongLong (Z.neg i) in
288+
if truncation = NoTruncation then
289+
true, Const (CInt((Z.neg i),ILongLong,None))
290+
else
291+
false, Const (CInt(i,ILongLong,None))
292+
else
293+
raise Unsupported_Linexpr1
294+
| None ->
295+
M.warn ~category:Analyzer "Invariant Apron: coefficient is not int: %a" Coeff.pretty c;
296+
raise Unsupported_Linexpr1
296297

297298
(** Returned boolean indicates whether returned expression should be negated. *)
298299
let cil_exp_of_linexpr1_term ~scalewith (c: Coeff.t) v =

0 commit comments

Comments
 (0)