[cleanup] remove some needless language checks in checkers callbacks

Summary:
The only language types we have are Java/Clang/Python. The unit of analysis is a source file, and you can't write a source file that mixes two or more of these languages (to the best of my knowledge).
This diff simplifies using the assumption that all procedures in a file are written in the same language.

Reviewed By: jeremydubreil

Differential Revision: D5886942

fbshipit-source-id: 8555a16
master
Sam Blackshear 7 years ago committed by Facebook Github Bot
parent 18a9e52752
commit 39a71d607c

@ -30,19 +30,17 @@ let procedure_callbacks = ref []
let cluster_callbacks = ref [] let cluster_callbacks = ref []
let register_procedure_callback language_opt (callback: proc_callback_t) = let register_procedure_callback language (callback: proc_callback_t) =
procedure_callbacks := (language_opt, callback) :: !procedure_callbacks procedure_callbacks := (language, callback) :: !procedure_callbacks
let register_cluster_callback language_opt (callback: cluster_callback_t) = let register_cluster_callback language (callback: cluster_callback_t) =
cluster_callbacks := (language_opt, callback) :: !cluster_callbacks cluster_callbacks := (language, callback) :: !cluster_callbacks
(** Collect what we need to know about a procedure for the analysis. *) (** Collect what we need to know about a procedure for the analysis. *)
let get_procedure_definition exe_env proc_name = let get_procedure_definition exe_env proc_name =
let tenv = Exe_env.get_tenv exe_env proc_name in let tenv = Exe_env.get_tenv exe_env proc_name in
Option.map Option.map
~f:(fun proc_desc -> ~f:(fun proc_desc -> (tenv, proc_name, proc_desc))
let language = (Procdesc.get_attributes proc_desc).ProcAttributes.language in
(tenv, proc_name, proc_desc, language))
(Exe_env.get_proc_desc exe_env proc_name) (Exe_env.get_proc_desc exe_env proc_name)
let get_language proc_name = if Typ.Procname.is_java proc_name then Config.Java else Config.Clang let get_language proc_name = if Typ.Procname.is_java proc_name then Config.Java else Config.Clang
@ -62,17 +60,10 @@ let iterate_procedure_callbacks exe_env summary caller_pname =
match get_procedure_definition exe_env caller_pname with match get_procedure_definition exe_env caller_pname with
| None | None
-> summary -> summary
| Some (tenv, _, proc_desc, _) | Some (tenv, _, proc_desc)
-> List.fold ~init:summary -> List.fold ~init:summary
~f:(fun summary (language_opt, proc_callback) -> ~f:(fun summary (language, proc_callback) ->
let language_matches = if Config.equal_language language procedure_language then
match language_opt with
| Some language
-> Config.equal_language language procedure_language
| None
-> true
in
if language_matches then
proc_callback {get_proc_desc; get_procs_in_file; tenv; summary; proc_desc} proc_callback {get_proc_desc; get_procs_in_file; tenv; summary; proc_desc}
else summary) else summary)
!procedure_callbacks !procedure_callbacks
@ -80,23 +71,17 @@ let iterate_procedure_callbacks exe_env summary caller_pname =
(** Invoke all registered cluster callbacks on a cluster of procedures. *) (** Invoke all registered cluster callbacks on a cluster of procedures. *)
let iterate_cluster_callbacks all_procs exe_env = let iterate_cluster_callbacks all_procs exe_env =
let get_procdesc = Exe_env.get_proc_desc exe_env in let get_procdesc = Exe_env.get_proc_desc exe_env in
let procedure_definitions = List.filter_map ~f:(get_procedure_definition exe_env) all_procs in let environment = List.filter_map ~f:(get_procedure_definition exe_env) all_procs in
let environment = let language_matches language =
List.map match environment with
~f:(fun (tenv, proc_name, proc_desc, _) -> (tenv, proc_name, proc_desc)) | (_, pname, _) :: _
procedure_definitions -> Config.equal_language language (get_language pname)
in | _
(* Procedures matching the given language or all if no language is specified. *) -> true
let relevant_procedures language_opt =
Option.value_map
~f:(fun l -> List.filter ~f:(fun p -> Config.equal_language l (get_language p)) all_procs)
~default:all_procs language_opt
in in
List.iter List.iter
~f:(fun (language_opt, cluster_callback) -> ~f:(fun (language, cluster_callback) ->
let proc_names = relevant_procedures language_opt in if language_matches language then cluster_callback exe_env all_procs get_procdesc environment)
if List.length proc_names > 0 then
cluster_callback exe_env all_procs get_procdesc environment)
!cluster_callbacks !cluster_callbacks
(** Invoke all procedure and cluster callbacks on a given environment. *) (** Invoke all procedure and cluster callbacks on a given environment. *)

@ -30,10 +30,10 @@ type cluster_callback_t =
Exe_env.t -> Typ.Procname.t list -> (Typ.Procname.t -> Procdesc.t option) Exe_env.t -> Typ.Procname.t list -> (Typ.Procname.t -> Procdesc.t option)
-> (Tenv.t * Typ.Procname.t * Procdesc.t) list -> unit -> (Tenv.t * Typ.Procname.t * Procdesc.t) list -> unit
val register_procedure_callback : Config.language option -> proc_callback_t -> unit val register_procedure_callback : Config.language -> proc_callback_t -> unit
(** register a procedure callback *) (** register a procedure callback *)
val register_cluster_callback : Config.language option -> cluster_callback_t -> unit val register_cluster_callback : Config.language -> cluster_callback_t -> unit
(** register a cluster callback *) (** register a cluster callback *)
val iterate_callbacks : Cg.t -> Exe_env.t -> unit val iterate_callbacks : Cg.t -> Exe_env.t -> unit

@ -66,9 +66,9 @@ let register () =
let register_callback (callback, language) = let register_callback (callback, language) =
match callback with match callback with
| Procedure procedure_cb | Procedure procedure_cb
-> Callbacks.register_procedure_callback (Some language) procedure_cb -> Callbacks.register_procedure_callback language procedure_cb
| Cluster cluster_cb | Cluster cluster_cb
-> Callbacks.register_cluster_callback (Some language) cluster_cb -> Callbacks.register_cluster_callback language cluster_cb
in in
if active then List.iter ~f:register_callback callbacks if active then List.iter ~f:register_callback callbacks
in in

Loading…
Cancel
Save