Skip to content

Commit

Permalink
[hack][builder pattern] Migrate builder pattern config to yojson ppx …
Browse files Browse the repository at this point in the history
…deriving

Summary: Migrate builder pattern config to yojson ppx deriving since I'm planning to add more config options so we won't need to parse them manually

Reviewed By: ngorogiannis

Differential Revision: D65540798

fbshipit-source-id: dfe835183c818ff5a8ecc543f8a2f076827a2ac8
  • Loading branch information
geralt-encore authored and facebook-github-bot committed Nov 6, 2024
1 parent f4a2303 commit 25b6d87
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
17 changes: 12 additions & 5 deletions infer/src/base/Config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ type pulse_taint_config =
; policies: Pulse_config_t.taint_policies
; data_flow_kinds: string list }

type pulse_hack_builder_pattern = {class_name: string [@yojson.key "class"]; finalizers: string list}
[@@deriving of_yojson]

type pulse_hack_builder_patterns = pulse_hack_builder_pattern list [@@deriving of_yojson]

(* List of ([build system], [executable name]). Several executables may map to the same build
system. In that case, the first one in the list will be used for printing, eg, in which mode
infer is running. *)
Expand Down Expand Up @@ -4211,12 +4216,14 @@ and global_tenv = !global_tenv
and hackc_binary = !hackc_binary

and hack_builder_patterns =
let open Yojson.Safe.Util in
let json = !hack_builder_patterns in
let class_of j = j |> member "class" |> to_string in
let finalizers j = j |> member "finalizers" |> to_list |> List.map ~f:to_string in
let pattern j = (class_of j, finalizers j) in
json |> to_list |> List.map ~f:pattern
match json with
| `List [] ->
[]
| json -> (
try pulse_hack_builder_patterns_of_yojson json
with _ ->
L.die UserError "Failed parsing hack-builder-patterns from %a!@\n" Yojson.Safe.pp json )


and hack_builtin_models = !hack_builtin_models
Expand Down
6 changes: 5 additions & 1 deletion infer/src/base/Config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ val global_tenv : bool

val hackc_binary : string option

val hack_builder_patterns : (string * string list) list
type pulse_hack_builder_pattern = {class_name: string; finalizers: string list}

type pulse_hack_builder_patterns = pulse_hack_builder_pattern list

val hack_builder_patterns : pulse_hack_builder_patterns

val hack_builtin_models : string

Expand Down
1 change: 1 addition & 0 deletions infer/src/base/dune
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
ppx_sexp_conv
ppx_show
ppx_variants_conv
ppx_yojson_conv
inferppx))
(preprocessor_deps
(glob_files ../../documentation/checkers/*.md)
Expand Down
8 changes: 4 additions & 4 deletions infer/src/pulse/Pulse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let is_hack_builder_procname tenv pname =
else
match Procname.get_class_type_name pname with
| Some (HackClass _ as tn) ->
List.exists Config.hack_builder_patterns ~f:(fun (class_name, _) ->
List.exists Config.hack_builder_patterns ~f:(fun Config.{class_name} ->
PatternMatch.is_subtype tenv tn (HackClass (HackClassName.make class_name)) )
| _ ->
false
Expand All @@ -67,11 +67,11 @@ let is_hack_builder_consumer tenv pname =
match Procname.get_class_type_name pname with
| Some (HackClass _ as tn) ->
let res =
List.exists Config.hack_builder_patterns ~f:(fun (class_name, consumer_names) ->
List.exists Config.hack_builder_patterns ~f:(fun Config.{class_name; finalizers} ->
PatternMatch.is_subtype tenv tn (HackClass (HackClassName.make class_name))
&& List.mem consumer_names (Procname.get_method pname) ~equal:String.equal )
&& List.mem finalizers (Procname.get_method pname) ~equal:String.equal )
in
L.d_printfln "doing builder consumer check, result is %b" res ;
L.d_printfln "doing builder finalizer check, result is %b" res ;
res
| _ ->
false
Expand Down
4 changes: 2 additions & 2 deletions infer/src/pulse/PulseModelsDSL.ml
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ module Syntax = struct
let is_hack_builder_in_config tenv hacktypname =
L.d_printfln "typname = %a" HackClassName.pp hacktypname ;
(* TODO: deal with namespaces properly! *)
List.exists Config.hack_builder_patterns ~f:(fun (builder_class_name, _) ->
let builder_class_name = Typ.HackClass (HackClassName.make builder_class_name) in
List.exists Config.hack_builder_patterns ~f:(fun Config.{class_name} ->
let builder_class_name = Typ.HackClass (HackClassName.make class_name) in
PatternMatch.is_subtype tenv (HackClass hacktypname) builder_class_name )


Expand Down

0 comments on commit 25b6d87

Please sign in to comment.