Annot.Method: record rather than pair

Reviewed By: ngorogiannis

Differential Revision: D10238615

fbshipit-source-id: 7f7785275
master
Mehdi Bouaziz 6 years ago committed by Facebook Github Bot
parent 0b4beda1cd
commit 15839539a7

@ -49,7 +49,7 @@ module Item = struct
(** Empty item annotation. *) (** Empty item annotation. *)
let empty = [] let empty = []
(** Check if the item annodation is empty. *) (** Check if the item annotation is empty. *)
let is_empty ia = List.is_empty ia let is_empty ia = List.is_empty ia
end end
@ -66,17 +66,16 @@ module Class = struct
end end
module Method = struct module Method = struct
(** Annotation for a method: return value and list of parameters. *) type t = {return: Item.t; params: Item.t list}
type t = Item.t * Item.t list [@@deriving compare]
let equal = [%compare.equal: t]
(** Pretty print a method annotation. *) (** Pretty print a method annotation. *)
let pp s fmt (ia, ial) = F.fprintf fmt "%a %s(%a)" Item.pp ia s (Pp.seq Item.pp) ial let pp s fmt {return; params} =
F.fprintf fmt "%a %s(%a)" Item.pp return s (Pp.seq Item.pp) params
(** Empty method annotation. *) (** Empty method annotation. *)
let empty = ([], []) let empty = {return= []; params= []}
(** Check if the method annodation is empty. *) (** Check if the method annotation is empty. *)
let is_empty (ia, ial) = List.for_all ~f:Item.is_empty (ia :: ial) let is_empty {return; params} = Item.is_empty return && List.for_all ~f:Item.is_empty params
end end

@ -47,15 +47,13 @@ end
module Method : sig module Method : sig
(** Annotation for a method: return value and list of parameters. *) (** Annotation for a method: return value and list of parameters. *)
type t = Item.t * Item.t list [@@deriving compare] type t = {return: Item.t; params: Item.t list}
val equal : t -> t -> bool
val empty : t val empty : t
(** Empty method annotation. *) (** Empty method annotation. *)
val is_empty : t -> bool val is_empty : t -> bool
(** Check if the method annodation is empty. *) (** Check if the method annotation is empty. *)
val pp : string -> F.formatter -> t -> unit val pp : string -> F.formatter -> t -> unit
(** Pretty print a method annotation. *) (** Pretty print a method annotation. *)

@ -63,9 +63,6 @@ type t =
; proc_name: Typ.Procname.t (** name of the procedure *) ; proc_name: Typ.Procname.t (** name of the procedure *)
; ret_type: Typ.t (** return type *) ; ret_type: Typ.t (** return type *)
; has_added_return_param: bool (** whether or not a return param was added *) } ; has_added_return_param: bool (** whether or not a return param was added *) }
[@@deriving compare]
let equal = [%compare.equal: t]
let default translation_unit proc_name = let default translation_unit proc_name =
{ access= PredSymb.Default { access= PredSymb.Default
@ -175,7 +172,7 @@ let pp f
locals ; locals ;
pp_bool_default ~default:default.has_added_return_param "has_added_return_param" pp_bool_default ~default:default.has_added_return_param "has_added_return_param"
has_added_return_param f () ; has_added_return_param f () ;
if not (Annot.Method.equal default.method_annotation method_annotation) then if not (Annot.Method.is_empty method_annotation) then
F.fprintf f "; method_annotation= %a@," (Annot.Method.pp "") method_annotation ; F.fprintf f "; method_annotation= %a@," (Annot.Method.pp "") method_annotation ;
if not ([%compare.equal: objc_accessor_type option] default.objc_accessor objc_accessor) then if not ([%compare.equal: objc_accessor_type option] default.objc_accessor objc_accessor) then
F.fprintf f "; objc_accessor= %a@," (Pp.option pp_objc_accessor_type) objc_accessor ; F.fprintf f "; objc_accessor= %a@," (Pp.option pp_objc_accessor_type) objc_accessor ;

@ -47,9 +47,6 @@ type t =
; ret_type: Typ.t (** return type *) ; ret_type: Typ.t (** return type *)
; has_added_return_param: bool (** whether or not a return param was added *) } ; has_added_return_param: bool (** whether or not a return param was added *) }
val equal : t -> t -> bool [@@warning "-32"]
(** can be useful for debugging *)
val default : SourceFile.t -> Typ.Procname.t -> t val default : SourceFile.t -> Typ.Procname.t -> t
(** Create a proc_attributes with default values. *) (** Create a proc_attributes with default values. *)

@ -299,7 +299,7 @@ let with_block_args callee_pdesc pname_with_block_args block_args =
let new_formals_blocks_captured_vars, extended_formals_annots = let new_formals_blocks_captured_vars, extended_formals_annots =
let new_formals_blocks_captured_vars_with_annots = let new_formals_blocks_captured_vars_with_annots =
let formals_annots = let formals_annots =
List.zip_exn callee_attributes.formals (snd callee_attributes.method_annotation) List.zip_exn callee_attributes.formals callee_attributes.method_annotation.params
in in
List.fold formals_annots ~init:[] ~f:(fun acc ((param_name, typ), annot) -> List.fold formals_annots ~init:[] ~f:(fun acc ((param_name, typ), annot) ->
try try
@ -327,7 +327,8 @@ let with_block_args callee_pdesc pname_with_block_args block_args =
proc_name= pname_with_block_args proc_name= pname_with_block_args
; is_defined= true ; is_defined= true
; formals= new_formals_blocks_captured_vars ; formals= new_formals_blocks_captured_vars
; method_annotation= (fst callee_attributes.method_annotation, extended_formals_annots) ; method_annotation=
{return= callee_attributes.method_annotation.return; params= extended_formals_annots}
; translation_unit } ; translation_unit }
in in
let resolved_pdesc = Procdesc.from_proc_attributes resolved_attributes in let resolved_pdesc = Procdesc.from_proc_attributes resolved_attributes in

@ -39,7 +39,7 @@ let log_issue_from_summary severity summary ~node ~session ~loc ~ltr ?extras exn
let should_suppress_lint = let should_suppress_lint =
Language.curr_language_is Java Language.curr_language_is Java
&& Annotations.ia_is_suppress_lint && Annotations.ia_is_suppress_lint
(fst (Summary.get_attributes summary).ProcAttributes.method_annotation) (Summary.get_attributes summary).ProcAttributes.method_annotation.return
in in
if should_suppress_lint || is_java_generated_method then () (* Skip the reporting *) if should_suppress_lint || is_java_generated_method then () (* Skip the reporting *)
else else

@ -1056,8 +1056,7 @@ let execute_load ?(report_deref_errors = true) pname pdesc tenv id rhs_exp typ l
let load_ret_annots pname = let load_ret_annots pname =
match Attributes.load pname with match Attributes.load pname with
| Some attrs -> | Some attrs ->
let ret_annots, _ = attrs.ProcAttributes.method_annotation in attrs.ProcAttributes.method_annotation.return
ret_annots
| None -> | None ->
Annot.Item.empty Annot.Item.empty
@ -1346,7 +1345,7 @@ let rec sym_exec exe_env tenv current_pdesc instr_ (prop_ : Prop.normal Prop.t)
(call_args prop_ callee_pname norm_args ret_id_typ loc) (call_args prop_ callee_pname norm_args ret_id_typ loc)
| Some reason -> | Some reason ->
let proc_attrs = Summary.get_attributes resolved_summary in let proc_attrs = Summary.get_attributes resolved_summary in
let ret_annots, _ = proc_attrs.ProcAttributes.method_annotation in let ret_annots = proc_attrs.ProcAttributes.method_annotation.return in
exec_skip_call ~reason resolved_pname ret_annots proc_attrs.ProcAttributes.ret_type exec_skip_call ~reason resolved_pname ret_annots proc_attrs.ProcAttributes.ret_type
) ) ) )
| Java callee_pname_java -> | Java callee_pname_java ->
@ -1372,7 +1371,7 @@ let rec sym_exec exe_env tenv current_pdesc instr_ (prop_ : Prop.normal Prop.t)
proc_call exe_env callee_summary handled_args proc_call exe_env callee_summary handled_args
| Some reason -> | Some reason ->
let proc_attrs = Summary.get_attributes callee_summary in let proc_attrs = Summary.get_attributes callee_summary in
let ret_annots, _ = proc_attrs.ProcAttributes.method_annotation in let ret_annots = proc_attrs.ProcAttributes.method_annotation.return in
exec_skip_call ~reason ret_annots proc_attrs.ProcAttributes.ret_type ) exec_skip_call ~reason ret_annots proc_attrs.ProcAttributes.ret_type )
in in
List.fold ~f:(fun acc pname -> exec_one_pname pname @ acc) ~init:[] resolved_pnames List.fold ~f:(fun acc pname -> exec_one_pname pname @ acc) ~init:[] resolved_pnames
@ -1427,10 +1426,7 @@ let rec sym_exec exe_env tenv current_pdesc instr_ (prop_ : Prop.normal Prop.t)
let ret_annots = let ret_annots =
match resolved_summary_opt with match resolved_summary_opt with
| Some summ -> | Some summ ->
let ret_annots, _ = (Summary.get_attributes summ).ProcAttributes.method_annotation.return
(Summary.get_attributes summ).ProcAttributes.method_annotation
in
ret_annots
| None -> | None ->
load_ret_annots resolved_pname load_ret_annots resolved_pname
in in

@ -1430,7 +1430,7 @@ let exe_call_postprocess tenv ret_id trace_call callee_pname callee_attrs loc re
~f:(fun (p, path) -> (quantify_path_idents_remove_constant_strings tenv p, path)) ~f:(fun (p, path) -> (quantify_path_idents_remove_constant_strings tenv p, path))
res_with_path_idents res_with_path_idents
in in
let ret_annot, _ = callee_attrs.ProcAttributes.method_annotation in let ret_annot = callee_attrs.ProcAttributes.method_annotation.return in
let returns_nullable ret_annot = Annotations.ia_is_nullable ret_annot in let returns_nullable ret_annot = Annotations.ia_is_nullable ret_annot in
let should_add_ret_attr _ = let should_add_ret_attr _ =
let is_likely_getter = function let is_likely_getter = function

@ -115,9 +115,10 @@ let ia_has_annotation_with (ia : Annot.Item.t) (predicate : Annot.t -> bool) : b
List.exists ~f:(fun (a, _) -> predicate a) ia List.exists ~f:(fun (a, _) -> predicate a) ia
let ma_has_annotation_with ((ia, ial) : Annot.Method.t) (predicate : Annot.t -> bool) : bool = let ma_has_annotation_with ({return; params} : Annot.Method.t) (predicate : Annot.t -> bool) : bool
=
let has_annot a = ia_has_annotation_with a predicate in let has_annot a = ia_has_annotation_with a predicate in
has_annot ia || List.exists ~f:has_annot ial has_annot return || List.exists ~f:has_annot params
(** [annot_ends_with annot ann_name] returns true if the class name of [annot], without the package, (** [annot_ends_with annot ann_name] returns true if the class name of [annot], without the package,
@ -137,7 +138,7 @@ let ia_ends_with ia ann_name = List.exists ~f:(fun (a, _) -> annot_ends_with a a
let ia_contains ia ann_name = List.exists ~f:(class_name_matches ann_name) ia let ia_contains ia ann_name = List.exists ~f:(class_name_matches ann_name) ia
let pdesc_get_return_annot pdesc = let pdesc_get_return_annot pdesc =
fst (Procdesc.get_attributes pdesc).ProcAttributes.method_annotation (Procdesc.get_attributes pdesc).ProcAttributes.method_annotation.return
let pdesc_has_return_annot pdesc predicate = predicate (pdesc_get_return_annot pdesc) let pdesc_has_return_annot pdesc predicate = predicate (pdesc_get_return_annot pdesc)
@ -151,7 +152,7 @@ let pdesc_return_annot_ends_with pdesc annot =
let pname_has_return_annot pname ~attrs_of_pname predicate = let pname_has_return_annot pname ~attrs_of_pname predicate =
match attrs_of_pname pname with match attrs_of_pname pname with
| Some attributes -> | Some attributes ->
predicate (fst attributes.ProcAttributes.method_annotation) predicate attributes.ProcAttributes.method_annotation.return
| None -> | None ->
false false

@ -223,12 +223,12 @@ let create_local_procdesc ?(set_objc_accessor_attr = false) trans_unit_ctx cfg t
in in
let create_new_procdesc () = let create_new_procdesc () =
let all_params = Option.to_list ms.CMethodSignature.class_param @ ms.CMethodSignature.params in let all_params = Option.to_list ms.CMethodSignature.class_param @ ms.CMethodSignature.params in
let params_annots =
List.map ~f:(fun ({annot} : CMethodSignature.param_type) -> annot) all_params
in
let return_annot = snd ms.CMethodSignature.ret_type in
let has_added_return_param = ms.CMethodSignature.has_added_return_param in let has_added_return_param = ms.CMethodSignature.has_added_return_param in
let method_annotation = (return_annot, params_annots) in let method_annotation =
let return = snd ms.CMethodSignature.ret_type in
let params = List.map ~f:(fun ({annot} : CMethodSignature.param_type) -> annot) all_params in
Annot.Method.{return; params}
in
let formals = let formals =
List.map ~f:(fun ({name; typ} : CMethodSignature.param_type) -> (name, typ)) all_params List.map ~f:(fun ({name; typ} : CMethodSignature.param_type) -> (name, typ)) all_params
in in

@ -25,7 +25,7 @@ let get proc_attributes : t =
let method_annotation = proc_attributes.ProcAttributes.method_annotation in let method_annotation = proc_attributes.ProcAttributes.method_annotation in
let formals = proc_attributes.ProcAttributes.formals in let formals = proc_attributes.ProcAttributes.formals in
let ret_type = proc_attributes.ProcAttributes.ret_type in let ret_type = proc_attributes.ProcAttributes.ret_type in
let ia, ial0 = method_annotation in let Annot.Method.({return; params}) = method_annotation in
let natl = let natl =
let rec extract ial parl = let rec extract ial parl =
match (ial, parl) with match (ial, parl) with
@ -38,9 +38,9 @@ let get proc_attributes : t =
| _ :: _, [] -> | _ :: _, [] ->
assert false assert false
in in
List.rev (extract (List.rev ial0) (List.rev formals)) List.rev (extract (List.rev params) (List.rev formals))
in in
let annotated_signature = {ret= (ia, ret_type); params= natl} in let annotated_signature = {ret= (return, ret_type); params= natl} in
annotated_signature annotated_signature

@ -44,6 +44,6 @@ let translate_item avlist : Annot.Item.t =
let translate_method ann : Annot.Method.t = let translate_method ann : Annot.Method.t =
let global_ann = ann.Javalib.ma_global in let global_ann = ann.Javalib.ma_global in
let param_ann = ann.Javalib.ma_parameters in let param_ann = ann.Javalib.ma_parameters in
let ret_item = translate_item global_ann in let return = translate_item global_ann in
let param_items = List.map ~f:translate_item param_ann in let params = List.map ~f:translate_item param_ann in
(ret_item, param_items) {return; params}

Loading…
Cancel
Save