[cleanup] generalize PatternMatch.iter_overrides to override_exists

Summary:
The thread-safety and annotation reachibility analyses were defining ugly custom versions of override_exists with refs.
Let's give them a canonical, ref-free version instead.

Reviewed By: jberdine

Differential Revision: D4475777

fbshipit-source-id: 0bb45fc
master
Sam Blackshear 8 years ago committed by Facebook Github Bot
parent 50c73e517b
commit d0f4e8771b

@ -596,18 +596,13 @@ let process_results_table file_env tab =
(* TODO (t15588153): clean this up *)
let is_thread_safe_method pdesc tenv =
let overrides_thread_safe_method pname tenv =
let found = ref false in
PatternMatch.proc_iter_overridden_methods
PatternMatch.override_exists
(fun pn ->
found :=
!found ||
Annotations.pname_has_return_annot
pn
~attrs_of_pname:Specs.proc_resolve_attributes
Annotations.ia_is_thread_safe_method)
tenv
pname;
!found in
Annotations.pname_has_return_annot
pn
~attrs_of_pname:Specs.proc_resolve_attributes
Annotations.ia_is_thread_safe_method)
tenv pname in
Annotations.pdesc_return_annot_ends_with pdesc Annotations.thread_safe_method ||
overrides_thread_safe_method (Procdesc.get_proc_name pdesc) tenv in
let should_report ((_, tenv, _, pdesc) as proc_env) =

@ -131,14 +131,8 @@ let check_attributes check tenv pname =
Annotations.pname_has_return_annot pname ~attrs_of_pname:Specs.proc_resolve_attributes check
let method_overrides is_annotated tenv pname =
let overrides () =
let found = ref false in
PatternMatch.proc_iter_overridden_methods
(fun pn -> found := is_annotated tenv pn)
tenv pname;
!found in
is_annotated tenv pname ||
overrides ()
PatternMatch.override_exists (fun pn -> is_annotated tenv pn) tenv pname
let method_has_annot annot tenv pname =
let has_annot ia = Annotations.ia_ends_with ia annot.Annot.class_name in
@ -366,8 +360,7 @@ module Interprocedural = struct
Reporting.log_error proc_name ~loc exn in
if expensive then
PatternMatch.proc_iter_overridden_methods
check_expensive_subtyping_rules tenv proc_name;
PatternMatch.override_iter check_expensive_subtyping_rules tenv proc_name;
let report_src_snk_paths call_map (src_annot_list, (snk_annot: Annot.t)) =
let extract_calls_with_annot annot call_map =

@ -327,33 +327,28 @@ let proc_calls resolve_attributes pdesc filter : (Procname.t * ProcAttributes.t)
IList.iter do_node nodes;
IList.rev !res
(** Iterate over all the methods overridden by the procedure.
Only Java supported at the moment. *)
let proc_iter_overridden_methods f tenv proc_name =
let do_super_type tenv super_class_name =
let override_exists f tenv proc_name =
let super_type_exists tenv super_class_name =
let super_proc_name =
Procname.replace_class proc_name (Typename.name super_class_name) in
match Tenv.lookup tenv super_class_name with
| Some ({ methods }) ->
let is_override pname =
Procname.equal pname super_proc_name &&
not (Procname.is_constructor pname) in
IList.iter
(fun pname ->
if is_override pname
then f pname)
methods
| _ -> () in
Procname.equal pname super_proc_name && not (Procname.is_constructor pname) in
IList.exists (fun pname -> is_override pname && f pname) methods
| _ ->
false in
match proc_name with
| Procname.Java proc_name_java ->
let type_name = Typename.Java.from_string (Procname.java_get_class_name proc_name_java) in
IList.iter
(do_super_type tenv)
IList.exists
(super_type_exists tenv)
(type_get_direct_supertypes tenv (Typ.Tstruct type_name))
| _ ->
() (* Only java supported at the moment *)
false (* Only java supported at the moment *)
let override_iter f tenv proc_name =
ignore(override_exists (fun pname -> f pname; false) tenv proc_name)
(** return the set of instance fields that are assigned to a null literal in [procdesc] *)
let get_fields_nullified procdesc =

@ -82,9 +82,14 @@ val proc_calls :
(Procname.t -> ProcAttributes.t -> bool) ->
(Procname.t * ProcAttributes.t) list
(** Iterate over all the methods overridden by the procedure.
Only Java supported at the moment. *)
val proc_iter_overridden_methods : (Procname.t -> unit) -> Tenv.t -> Procname.t -> unit
(** Return true if applying the given predicate to an override of [procname] returns [true]. Note
that this does apply the predicate to [procname] itself. For the moment, this only works for
Java *)
val override_exists : (Procname.t -> bool) -> Tenv.t -> Procname.t -> bool
(** Apply the given predicate to each override of [procname]. Notet hat this does apply the
predicate to [procname] itself. For the moment, this only works for Java *)
val override_iter : (Procname.t -> unit) -> Tenv.t -> Procname.t -> unit
val type_get_annotation : Tenv.t -> Typ.t -> Annot.Item.t option

@ -554,4 +554,4 @@ let check_overridden_annotations
| None ->
() in
PatternMatch.proc_iter_overridden_methods check tenv proc_name
PatternMatch.override_iter check tenv proc_name

Loading…
Cancel
Save