[racerd] merge Choice and Attribute modules

Summary:
Both modules define properties the analysis maps to addresses, there is no reason to have two modules for this.

Also remove an instance of `Caml.Not_found` usage.

Reviewed By: jberdine

Differential Revision: D20558683

fbshipit-source-id: eacafd780
master
Nikos Gorogiannis 5 years ago committed by Facebook GitHub Bot
parent c7d2f281f7
commit 7937615a20

@ -269,7 +269,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
{astate with threads= ThreadsDomain.AnyThreadButSelf} {astate with threads= ThreadsDomain.AnyThreadButSelf}
| MainThreadIfTrue -> | MainThreadIfTrue ->
let attribute_map = let attribute_map =
AttributeMapDomain.add_attribute ret_access_exp (Choice Choice.OnMainThread) AttributeMapDomain.add_attribute ret_access_exp Attribute.OnMainThread
astate.attribute_map astate.attribute_map
in in
{astate with attribute_map} {astate with attribute_map}
@ -301,7 +301,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
; threads= update_for_lock_use astate.threads } ; threads= update_for_lock_use astate.threads }
| LockedIfTrue _ | GuardLockedIfTrue _ -> | LockedIfTrue _ | GuardLockedIfTrue _ ->
let attribute_map = let attribute_map =
AttributeMapDomain.add_attribute ret_access_exp (Choice Choice.LockHeld) AttributeMapDomain.add_attribute ret_access_exp Attribute.LockHeld
astate.attribute_map astate.attribute_map
in in
{astate with attribute_map; threads= update_for_lock_use astate.threads} {astate with attribute_map; threads= update_for_lock_use astate.threads}
@ -397,18 +397,20 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let do_assume formals assume_exp loc proc_data (astate : Domain.t) = let do_assume formals assume_exp loc proc_data (astate : Domain.t) =
let open Domain in let open Domain in
let add_choice bool_value (acc : Domain.t) = function let apply_choice bool_value (acc : Domain.t) = function
| Choice.LockHeld -> | Attribute.LockHeld ->
let locks = let locks =
if bool_value then LocksDomain.acquire_lock acc.locks if bool_value then LocksDomain.acquire_lock acc.locks
else LocksDomain.release_lock acc.locks else LocksDomain.release_lock acc.locks
in in
{acc with locks} {acc with locks}
| Choice.OnMainThread -> | Attribute.OnMainThread ->
let threads = let threads =
if bool_value then ThreadsDomain.AnyThreadButSelf else ThreadsDomain.AnyThread if bool_value then ThreadsDomain.AnyThreadButSelf else ThreadsDomain.AnyThread
in in
{acc with threads} {acc with threads}
| Attribute.Functional ->
acc
in in
let accesses = let accesses =
add_access formals loc ~is_write_access:false astate.locks astate.threads astate.ownership add_access formals loc ~is_write_access:false astate.locks astate.threads astate.ownership
@ -422,7 +424,7 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
let choices = AttributeMapDomain.get_choices access_expr astate.attribute_map in let choices = AttributeMapDomain.get_choices access_expr astate.attribute_map in
(* prune (prune_exp) can only evaluate to true if the choice is [bool_value]. (* prune (prune_exp) can only evaluate to true if the choice is [bool_value].
add the constraint that the choice must be [bool_value] to the state *) add the constraint that the choice must be [bool_value] to the state *)
List.fold ~f:(add_choice bool_value) ~init choices ) List.fold ~f:(apply_choice bool_value) ~init choices )
| _ -> | _ ->
astate astate
in in

@ -438,26 +438,18 @@ module OwnershipDomain = struct
AccessSnapshot.OwnershipPrecondition.False AccessSnapshot.OwnershipPrecondition.False
end end
module Choice = struct module Attribute = struct
type t = OnMainThread | LockHeld [@@deriving compare] type t = Functional | OnMainThread | LockHeld [@@deriving compare]
let pp fmt = function let pp fmt = function
| Functional ->
F.pp_print_string fmt "Functional"
| OnMainThread -> | OnMainThread ->
F.pp_print_string fmt "OnMainThread" F.pp_print_string fmt "OnMainThread"
| LockHeld -> | LockHeld ->
F.pp_print_string fmt "LockHeld" F.pp_print_string fmt "LockHeld"
end end
module Attribute = struct
type t = Functional | Choice of Choice.t [@@deriving compare]
let pp fmt = function
| Functional ->
F.pp_print_string fmt "Functional"
| Choice choice ->
Choice.pp fmt choice
end
module AttributeSetDomain = AbstractDomain.InvertedSet (Attribute) module AttributeSetDomain = AbstractDomain.InvertedSet (Attribute)
module AttributeMapDomain = struct module AttributeMapDomain = struct
@ -468,16 +460,17 @@ module AttributeMapDomain = struct
let has_attribute access_expression attribute t = let has_attribute access_expression attribute t =
try find access_expression t |> AttributeSetDomain.mem attribute with Caml.Not_found -> false find_opt access_expression t |> Option.exists ~f:(AttributeSetDomain.mem attribute)
let get_choices access_expression t = let get_choices access_expression t =
try match find_opt access_expression t with
let attributes = find access_expression t in | None ->
AttributeSetDomain.fold []
(fun cc acc -> match cc with Attribute.Choice c -> c :: acc | _ -> acc) | Some attributes ->
attributes [] AttributeSetDomain.fold
with Caml.Not_found -> [] (fun cc acc -> match cc with OnMainThread | LockHeld -> cc :: acc | _ -> acc)
attributes []
let add_attribute access_expression attribute t = let add_attribute access_expression attribute t =
@ -492,8 +485,8 @@ module AttributeMapDomain = struct
let rec attributes_of_expr attribute_map (e : HilExp.t) = let rec attributes_of_expr attribute_map (e : HilExp.t) =
match e with match e with
| AccessExpression access_expr -> ( | AccessExpression access_expr ->
try find access_expr attribute_map with Caml.Not_found -> AttributeSetDomain.empty ) find_opt access_expr attribute_map |> Option.value ~default:AttributeSetDomain.empty
| Constant _ -> | Constant _ ->
AttributeSetDomain.singleton Attribute.Functional AttributeSetDomain.singleton Attribute.Functional
| Exception expr (* treat exceptions as transparent wrt attributes *) | Cast (_, expr) -> | Exception expr (* treat exceptions as transparent wrt attributes *) | Cast (_, expr) ->

@ -158,19 +158,11 @@ module OwnershipDomain : sig
val get_precondition : AccessExpression.t -> t -> AccessSnapshot.OwnershipPrecondition.t val get_precondition : AccessExpression.t -> t -> AccessSnapshot.OwnershipPrecondition.t
end end
(** attribute attached to a boolean variable specifying what it means when the boolean is true *)
module Choice : sig
type t =
| OnMainThread (** the current procedure is running on the main thread *)
| LockHeld (** a lock is currently held *)
include PrettyPrintable.PrintableOrderedType with type t := t
end
module Attribute : sig module Attribute : sig
type t = type t =
| Functional (** holds a value returned from a callee marked [@Functional] *) | Functional (** holds a value returned from a callee marked [@Functional] *)
| Choice of Choice.t (** holds a boolean choice variable *) | OnMainThread (** boolean is true if the current procedure is running on the main thread *)
| LockHeld (** boolean is true if a lock is currently held *)
include PrettyPrintable.PrintableOrderedType with type t := t include PrettyPrintable.PrintableOrderedType with type t := t
end end
@ -190,7 +182,7 @@ module AttributeMapDomain : sig
val has_attribute : AccessExpression.t -> Attribute.t -> t -> bool val has_attribute : AccessExpression.t -> Attribute.t -> t -> bool
val get_choices : AccessExpression.t -> t -> Choice.t list val get_choices : AccessExpression.t -> t -> Attribute.t list
(** get the choice attributes associated with the given access path *) (** get the choice attributes associated with the given access path *)
val add_attribute : AccessExpression.t -> Attribute.t -> t -> t val add_attribute : AccessExpression.t -> Attribute.t -> t -> t

Loading…
Cancel
Save