You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
490 lines
20 KiB
490 lines
20 KiB
10 years ago
|
(*
|
||
7 years ago
|
* Copyright (c) 2014-present, Facebook, Inc.
|
||
10 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
10 years ago
|
*)
|
||
10 years ago
|
|
||
8 years ago
|
open! IStd
|
||
10 years ago
|
|
||
|
(** Module for the checks called by Eradicate. *)
|
||
|
|
||
|
(* do not report RETURN_NOT_NULLABLE if the return is annotated @Nonnull *)
|
||
|
let return_nonnull_silent = true
|
||
|
|
||
8 years ago
|
let get_field_annotation tenv fn typ =
|
||
8 years ago
|
let lookup = Tenv.lookup tenv in
|
||
8 years ago
|
match Typ.Struct.get_field_type_and_annotation ~lookup fn typ with
|
||
7 years ago
|
| None ->
|
||
|
None
|
||
|
| Some (t, ia) ->
|
||
|
let ia' =
|
||
10 years ago
|
(* TODO (t4968422) eliminate not !Config.eradicate check by marking fields as nullified *)
|
||
|
(* outside of Eradicate in some other way *)
|
||
6 years ago
|
if not Config.eradicate then AnnotatedSignature.mk_ia AnnotatedSignature.Nullable ia
|
||
8 years ago
|
else ia
|
||
|
in
|
||
10 years ago
|
Some (t, ia')
|
||
|
|
||
7 years ago
|
|
||
6 years ago
|
let report_error tenv = TypeErr.report_error tenv (EradicateCheckers.report_error tenv)
|
||
10 years ago
|
|
||
8 years ago
|
let explain_expr tenv node e =
|
||
|
match Errdesc.exp_rv_dexp tenv node e with
|
||
7 years ago
|
| Some de ->
|
||
|
Some (DecompiledExp.to_string de)
|
||
|
| None ->
|
||
|
None
|
||
|
|
||
10 years ago
|
|
||
|
(** Classify a procedure. *)
|
||
9 years ago
|
let classify_procedure proc_attributes =
|
||
|
let pn = proc_attributes.ProcAttributes.proc_name in
|
||
8 years ago
|
let unique_id = Typ.Procname.to_unique_id pn in
|
||
10 years ago
|
let classification =
|
||
|
if Models.is_modelled_nullable pn then "M" (* modelled *)
|
||
7 years ago
|
else if Summary.proc_is_library proc_attributes then "L" (* library *)
|
||
9 years ago
|
else if not proc_attributes.ProcAttributes.is_defined then "S" (* skip *)
|
||
8 years ago
|
else if String.is_prefix ~prefix:"com.facebook" unique_id then "F" (* FB *)
|
||
8 years ago
|
else "?"
|
||
|
in
|
||
10 years ago
|
classification
|
||
|
|
||
7 years ago
|
|
||
6 years ago
|
let is_virtual = function (p, _, _) :: _ when Mangled.is_this p -> true | _ -> false
|
||
10 years ago
|
|
||
|
(** Check an access (read or write) to a field. *)
|
||
6 years ago
|
let check_field_access tenv find_canonical_duplicate curr_pname node instr_ref exp fname ta loc :
|
||
|
unit =
|
||
8 years ago
|
if TypeAnnotation.get_value AnnotatedSignature.Nullable ta then
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin ta in
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
8 years ago
|
(TypeErr.Null_field_access (explain_expr tenv node exp, fname, origin_descr, false))
|
||
8 years ago
|
(Some instr_ref) loc curr_pname
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Check an access to an array *)
|
||
8 years ago
|
let check_array_access tenv find_canonical_duplicate curr_pname node instr_ref array_exp fname ta
|
||
|
loc indexed =
|
||
8 years ago
|
if TypeAnnotation.get_value AnnotatedSignature.Nullable ta then
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin ta in
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
8 years ago
|
(TypeErr.Null_field_access (explain_expr tenv node array_exp, fname, origin_descr, indexed))
|
||
8 years ago
|
(Some instr_ref) loc curr_pname
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Where the condition is coming from *)
|
||
|
type from_call =
|
||
8 years ago
|
| From_condition (** Direct condition *)
|
||
|
| From_instanceof (** x instanceof C *)
|
||
|
| From_is_false_on_null (** returns false on null *)
|
||
|
| From_is_true_on_null (** returns true on null *)
|
||
|
| From_optional_isPresent (** x.isPresent *)
|
||
|
| From_containsKey (** x.containsKey *)
|
||
7 years ago
|
[@@deriving compare]
|
||
8 years ago
|
|
||
6 years ago
|
let equal_from_call = [%compare.equal: from_call]
|
||
10 years ago
|
|
||
|
(** Check the normalized "is zero" or "is not zero" condition of a prune instruction. *)
|
||
8 years ago
|
let check_condition tenv case_zero find_canonical_duplicate curr_pdesc node e typ ta true_branch
|
||
|
from_call idenv linereader loc instr_ref : unit =
|
||
|
let is_fun_nonnull ta =
|
||
|
match TypeAnnotation.get_origin ta with
|
||
7 years ago
|
| TypeOrigin.Proc proc_origin ->
|
||
|
let ia, _ = proc_origin.TypeOrigin.annotated_signature.AnnotatedSignature.ret in
|
||
10 years ago
|
Annotations.ia_is_nonnull ia
|
||
7 years ago
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
8 years ago
|
let contains_instanceof_throwable pdesc node =
|
||
10 years ago
|
(* Check if the current procedure has a catch Throwable. *)
|
||
|
(* That always happens in the bytecode generated by try-with-resources. *)
|
||
8 years ago
|
let loc = Procdesc.Node.get_loc node in
|
||
10 years ago
|
let throwable_found = ref false in
|
||
8 years ago
|
let typ_is_throwable {Typ.desc} =
|
||
|
match desc with
|
||
7 years ago
|
| Typ.Tstruct (Typ.JavaClass _ as name) ->
|
||
|
String.equal (Typ.Name.name name) "java.lang.Throwable"
|
||
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
10 years ago
|
let do_instr = function
|
||
7 years ago
|
| Sil.Call (_, Exp.Const (Const.Cfun pn), [_; (Exp.Sizeof {typ}, _)], _, _)
|
||
7 years ago
|
when Typ.Procname.equal pn BuiltinDecl.__instanceof && typ_is_throwable typ ->
|
||
|
throwable_found := true
|
||
|
| _ ->
|
||
|
()
|
||
8 years ago
|
in
|
||
10 years ago
|
let do_node n =
|
||
8 years ago
|
if Location.equal loc (Procdesc.Node.get_loc n) then
|
||
7 years ago
|
Instrs.iter ~f:do_instr (Procdesc.Node.get_instrs n)
|
||
8 years ago
|
in
|
||
|
Procdesc.iter_nodes do_node pdesc ; !throwable_found
|
||
|
in
|
||
10 years ago
|
let from_try_with_resources () : bool =
|
||
|
(* heuristic to check if the condition is the translation of try-with-resources *)
|
||
|
match Printer.LineReader.from_loc linereader loc with
|
||
7 years ago
|
| Some line ->
|
||
6 years ago
|
(not (String.is_substring ~substring:"==" line || String.is_substring ~substring:"!=" line))
|
||
|
&& String.is_substring ~substring:"}" line
|
||
|
&& contains_instanceof_throwable curr_pdesc node
|
||
7 years ago
|
| None ->
|
||
|
false
|
||
8 years ago
|
in
|
||
10 years ago
|
let is_temp = Idenv.exp_is_temp idenv e in
|
||
|
let nonnull = is_fun_nonnull ta in
|
||
|
let should_report =
|
||
6 years ago
|
(not (TypeAnnotation.get_value AnnotatedSignature.Nullable ta))
|
||
|
&& (Config.eradicate_condition_redundant || nonnull)
|
||
|
&& true_branch && ((not is_temp) || nonnull) && PatternMatch.type_is_class typ
|
||
|
&& (not (from_try_with_resources ()))
|
||
|
&& equal_from_call from_call From_condition
|
||
|
&& not (TypeAnnotation.origin_is_fun_library ta)
|
||
8 years ago
|
in
|
||
10 years ago
|
let is_always_true = not case_zero in
|
||
|
let nonnull = is_fun_nonnull ta in
|
||
|
if should_report then
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
8 years ago
|
(TypeErr.Condition_redundant (is_always_true, explain_expr tenv node e, nonnull))
|
||
8 years ago
|
(Some instr_ref) loc curr_pdesc
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Check an "is zero" condition. *)
|
||
8 years ago
|
let check_zero tenv find_canonical_duplicate = check_condition tenv true find_canonical_duplicate
|
||
10 years ago
|
|
||
|
(** Check an "is not zero" condition. *)
|
||
8 years ago
|
let check_nonzero tenv find_canonical_duplicate =
|
||
|
check_condition tenv false find_canonical_duplicate
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Check an assignment to a field. *)
|
||
8 years ago
|
let check_field_assignment tenv find_canonical_duplicate curr_pdesc node instr_ref typestate
|
||
|
exp_lhs exp_rhs typ loc fname t_ia_opt typecheck_expr : unit =
|
||
8 years ago
|
let curr_pname = Procdesc.get_proc_name curr_pdesc in
|
||
8 years ago
|
let t_lhs, ta_lhs, _ =
|
||
8 years ago
|
typecheck_expr node instr_ref curr_pdesc typestate exp_lhs
|
||
6 years ago
|
(typ, TypeAnnotation.const AnnotatedSignature.Nullable false TypeOrigin.ONone, [loc])
|
||
|
loc
|
||
8 years ago
|
in
|
||
|
let _, ta_rhs, _ =
|
||
8 years ago
|
typecheck_expr node instr_ref curr_pdesc typestate exp_rhs
|
||
6 years ago
|
(typ, TypeAnnotation.const AnnotatedSignature.Nullable false TypeOrigin.ONone, [loc])
|
||
|
loc
|
||
8 years ago
|
in
|
||
10 years ago
|
let should_report_nullable =
|
||
8 years ago
|
let field_is_field_injector_readwrite () =
|
||
|
match t_ia_opt with
|
||
7 years ago
|
| Some (_, ia) ->
|
||
|
Annotations.ia_is_field_injector_readwrite ia
|
||
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
6 years ago
|
(not (AndroidFramework.is_destroy_method curr_pname))
|
||
|
&& (not (TypeAnnotation.get_value AnnotatedSignature.Nullable ta_lhs))
|
||
8 years ago
|
&& TypeAnnotation.get_value AnnotatedSignature.Nullable ta_rhs
|
||
6 years ago
|
&& PatternMatch.type_is_class t_lhs
|
||
|
&& (not (Typ.Fieldname.Java.is_outer_instance fname))
|
||
8 years ago
|
&& not (field_is_field_injector_readwrite ())
|
||
|
in
|
||
10 years ago
|
let should_report_absent =
|
||
6 years ago
|
Config.eradicate_optional_present
|
||
|
&& TypeAnnotation.get_value AnnotatedSignature.Present ta_lhs
|
||
|
&& (not (TypeAnnotation.get_value AnnotatedSignature.Present ta_rhs))
|
||
7 years ago
|
&& not (Typ.Fieldname.Java.is_outer_instance fname)
|
||
8 years ago
|
in
|
||
10 years ago
|
let should_report_mutable =
|
||
8 years ago
|
let field_is_mutable () =
|
||
|
match t_ia_opt with Some (_, ia) -> Annotations.ia_is_mutable ia | _ -> false
|
||
|
in
|
||
6 years ago
|
Config.eradicate_field_not_mutable
|
||
|
&& (not (Typ.Procname.is_constructor curr_pname))
|
||
7 years ago
|
&& ( match curr_pname with
|
||
|
| Typ.Procname.Java java_pname ->
|
||
|
not (Typ.Procname.Java.is_class_initializer java_pname)
|
||
|
| _ ->
|
||
|
true )
|
||
|
&& not (field_is_mutable ())
|
||
8 years ago
|
in
|
||
6 years ago
|
( if should_report_nullable || should_report_absent then
|
||
7 years ago
|
let ann =
|
||
|
if should_report_nullable then AnnotatedSignature.Nullable else AnnotatedSignature.Present
|
||
|
in
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin ta_rhs in
|
||
7 years ago
|
report_error tenv find_canonical_duplicate
|
||
6 years ago
|
(TypeErr.Field_annotation_inconsistent (ann, fname, origin_descr))
|
||
|
(Some instr_ref) loc curr_pdesc ) ;
|
||
10 years ago
|
if should_report_mutable then
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin ta_rhs in
|
||
6 years ago
|
report_error tenv find_canonical_duplicate
|
||
|
(TypeErr.Field_not_mutable (fname, origin_descr))
|
||
8 years ago
|
(Some instr_ref) loc curr_pdesc
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Check that nonnullable fields are initialized in constructors. *)
|
||
8 years ago
|
let check_constructor_initialization tenv find_canonical_duplicate curr_pname curr_pdesc start_node
|
||
|
final_initializer_typestates final_constructor_typestates loc : unit =
|
||
|
State.set_node start_node ;
|
||
|
if Typ.Procname.is_constructor curr_pname then
|
||
8 years ago
|
match PatternMatch.get_this_type (Procdesc.get_attributes curr_pdesc) with
|
||
8 years ago
|
| Some {desc= Tptr (({desc= Tstruct name} as ts), _)} -> (
|
||
|
match Tenv.lookup tenv name with
|
||
7 years ago
|
| Some {fields} ->
|
||
|
let do_field (fn, ft, _) =
|
||
8 years ago
|
let annotated_with f =
|
||
|
match get_field_annotation tenv fn ts with None -> false | Some (_, ia) -> f ia
|
||
|
in
|
||
|
let nullable_annotated = annotated_with Annotations.ia_is_nullable in
|
||
|
let nonnull_annotated = annotated_with Annotations.ia_is_nonnull in
|
||
|
let injector_readonly_annotated =
|
||
|
annotated_with Annotations.ia_is_field_injector_readonly
|
||
|
in
|
||
|
let final_type_annotation_with unknown list f =
|
||
|
let filter_range_opt = function Some (_, ta, _) -> f ta | None -> unknown in
|
||
|
List.exists
|
||
|
~f:(function
|
||
6 years ago
|
| pname, typestate ->
|
||
|
let pvar =
|
||
|
Pvar.mk (Mangled.from_string (Typ.Fieldname.to_string fn)) pname
|
||
|
in
|
||
|
filter_range_opt (TypeState.lookup_pvar pvar typestate))
|
||
8 years ago
|
list
|
||
|
in
|
||
|
let may_be_assigned_in_final_typestate =
|
||
|
let origin_is_initialized = function
|
||
7 years ago
|
| TypeOrigin.Undef ->
|
||
|
false
|
||
|
| TypeOrigin.Field (TypeOrigin.Formal name, _, _) ->
|
||
6 years ago
|
let circular = Mangled.is_this name in
|
||
8 years ago
|
not circular
|
||
7 years ago
|
| _ ->
|
||
|
true
|
||
8 years ago
|
in
|
||
|
final_type_annotation_with false (Lazy.force final_initializer_typestates) (fun ta ->
|
||
|
origin_is_initialized (TypeAnnotation.get_origin ta) )
|
||
|
in
|
||
|
let may_be_nullable_in_final_typestate () =
|
||
|
final_type_annotation_with true (Lazy.force final_constructor_typestates) (fun ta ->
|
||
|
TypeAnnotation.get_value AnnotatedSignature.Nullable ta )
|
||
|
in
|
||
|
let should_check_field_initialization =
|
||
|
let in_current_class =
|
||
7 years ago
|
let fld_cname = Typ.Fieldname.Java.get_class fn in
|
||
8 years ago
|
String.equal (Typ.Name.name name) fld_cname
|
||
|
in
|
||
6 years ago
|
(not injector_readonly_annotated) && PatternMatch.type_is_class ft
|
||
|
&& in_current_class
|
||
7 years ago
|
&& not (Typ.Fieldname.Java.is_outer_instance fn)
|
||
8 years ago
|
in
|
||
|
if should_check_field_initialization then (
|
||
|
(* Check if field is missing annotation. *)
|
||
7 years ago
|
if
|
||
6 years ago
|
(not (nullable_annotated || nonnull_annotated))
|
||
7 years ago
|
&& not may_be_assigned_in_final_typestate
|
||
8 years ago
|
then
|
||
|
report_error tenv find_canonical_duplicate
|
||
6 years ago
|
(TypeErr.Field_not_initialized (fn, curr_pname))
|
||
|
None loc curr_pdesc ;
|
||
8 years ago
|
(* Check if field is over-annotated. *)
|
||
7 years ago
|
if
|
||
|
Config.eradicate_field_over_annotated && nullable_annotated
|
||
|
&& not (may_be_nullable_in_final_typestate ())
|
||
8 years ago
|
then
|
||
|
report_error tenv find_canonical_duplicate
|
||
6 years ago
|
(TypeErr.Field_over_annotated (fn, curr_pname))
|
||
|
None loc curr_pdesc )
|
||
8 years ago
|
in
|
||
|
List.iter ~f:do_field fields
|
||
7 years ago
|
| None ->
|
||
|
() )
|
||
|
| _ ->
|
||
|
()
|
||
|
|
||
10 years ago
|
|
||
|
(** Check the annotations when returning from a method. *)
|
||
8 years ago
|
let check_return_annotation tenv find_canonical_duplicate curr_pdesc ret_range
|
||
6 years ago
|
(annotated_signature : AnnotatedSignature.t) ret_implicitly_nullable loc : unit =
|
||
8 years ago
|
let ret_ia, _ = annotated_signature.ret in
|
||
8 years ago
|
let curr_pname = Procdesc.get_proc_name curr_pdesc in
|
||
8 years ago
|
let ret_annotated_nullable =
|
||
8 years ago
|
Annotations.ia_is_nullable ret_ia
|
||
|
|| List.exists
|
||
|
~f:(fun (_, ia, _) -> Annotations.ia_is_propagates_nullable ia)
|
||
|
annotated_signature.params
|
||
|
in
|
||
|
let ret_annotated_present = Annotations.ia_is_present ret_ia in
|
||
|
let ret_annotated_nonnull = Annotations.ia_is_nonnull ret_ia in
|
||
9 years ago
|
match ret_range with
|
||
8 years ago
|
(* Disables the warnings since it is not clear how to annotate the return value of lambdas *)
|
||
8 years ago
|
| Some _
|
||
7 years ago
|
when match curr_pname with
|
||
|
| Typ.Procname.Java java_pname ->
|
||
|
Typ.Procname.Java.is_lambda java_pname
|
||
|
| _ ->
|
||
|
false ->
|
||
7 years ago
|
()
|
||
|
| Some (_, final_ta, _) ->
|
||
|
let final_nullable = TypeAnnotation.get_value AnnotatedSignature.Nullable final_ta in
|
||
8 years ago
|
let final_present = TypeAnnotation.get_value AnnotatedSignature.Present final_ta in
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin final_ta in
|
||
9 years ago
|
let return_not_nullable =
|
||
6 years ago
|
final_nullable && (not ret_annotated_nullable) && (not ret_implicitly_nullable)
|
||
8 years ago
|
&& not (return_nonnull_silent && ret_annotated_nonnull)
|
||
|
in
|
||
9 years ago
|
let return_value_not_present =
|
||
6 years ago
|
Config.eradicate_optional_present && (not final_present) && ret_annotated_present
|
||
8 years ago
|
in
|
||
9 years ago
|
let return_over_annotated =
|
||
6 years ago
|
(not final_nullable) && ret_annotated_nullable && Config.eradicate_return_over_annotated
|
||
8 years ago
|
in
|
||
|
( if return_not_nullable || return_value_not_present then
|
||
6 years ago
|
let ann =
|
||
|
if return_not_nullable then AnnotatedSignature.Nullable else AnnotatedSignature.Present
|
||
|
in
|
||
|
report_error tenv find_canonical_duplicate
|
||
|
(TypeErr.Return_annotation_inconsistent (ann, curr_pname, origin_descr))
|
||
|
None loc curr_pdesc ) ;
|
||
9 years ago
|
if return_over_annotated then
|
||
8 years ago
|
report_error tenv find_canonical_duplicate (TypeErr.Return_over_annotated curr_pname) None
|
||
|
loc curr_pdesc
|
||
7 years ago
|
| None ->
|
||
|
()
|
||
|
|
||
10 years ago
|
|
||
|
(** Check the receiver of a virtual call. *)
|
||
8 years ago
|
let check_call_receiver tenv find_canonical_duplicate curr_pdesc node typestate call_params
|
||
6 years ago
|
callee_pname (instr_ref : TypeErr.InstrRef.t) loc typecheck_expr : unit =
|
||
10 years ago
|
match call_params with
|
||
7 years ago
|
| ((original_this_e, this_e), typ) :: _ ->
|
||
|
let _, this_ta, _ =
|
||
8 years ago
|
typecheck_expr tenv node instr_ref curr_pdesc typestate this_e
|
||
6 years ago
|
(typ, TypeAnnotation.const AnnotatedSignature.Nullable false TypeOrigin.ONone, [])
|
||
|
loc
|
||
8 years ago
|
in
|
||
8 years ago
|
let null_method_call = TypeAnnotation.get_value AnnotatedSignature.Nullable this_ta in
|
||
10 years ago
|
let optional_get_on_absent =
|
||
6 years ago
|
Config.eradicate_optional_present
|
||
|
&& Models.is_optional_get callee_pname
|
||
8 years ago
|
&& not (TypeAnnotation.get_value AnnotatedSignature.Present this_ta)
|
||
|
in
|
||
10 years ago
|
if null_method_call || optional_get_on_absent then
|
||
8 years ago
|
let ann =
|
||
|
if null_method_call then AnnotatedSignature.Nullable else AnnotatedSignature.Present
|
||
|
in
|
||
|
let descr = explain_expr tenv node original_this_e in
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin this_ta in
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
|
(TypeErr.Call_receiver_annotation_inconsistent (ann, descr, callee_pname, origin_descr))
|
||
|
(Some instr_ref) loc curr_pdesc
|
||
7 years ago
|
| [] ->
|
||
|
()
|
||
|
|
||
8 years ago
|
|
||
|
type resolved_param =
|
||
|
{ num: int
|
||
|
; formal: Mangled.t * TypeAnnotation.t * Typ.t
|
||
|
; actual: Exp.t * TypeAnnotation.t
|
||
|
; propagates_nullable: bool }
|
||
8 years ago
|
|
||
10 years ago
|
(** Check the parameters of a call. *)
|
||
7 years ago
|
let check_call_parameters tenv find_canonical_duplicate curr_pdesc node callee_attributes
|
||
|
resolved_params loc instr_ref : unit =
|
||
9 years ago
|
let callee_pname = callee_attributes.ProcAttributes.proc_name in
|
||
8 years ago
|
let check {num= param_num; formal= s1, ta1, t1; actual= orig_e2, ta2} =
|
||
8 years ago
|
let report ann =
|
||
|
let description =
|
||
|
match explain_expr tenv node orig_e2 with
|
||
7 years ago
|
| Some descr ->
|
||
|
descr
|
||
|
| None ->
|
||
|
"formal parameter " ^ Mangled.to_string s1
|
||
8 years ago
|
in
|
||
7 years ago
|
let origin_descr = TypeAnnotation.descr_origin ta2 in
|
||
8 years ago
|
let callee_loc = callee_attributes.ProcAttributes.loc in
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
|
(TypeErr.Parameter_annotation_inconsistent
|
||
6 years ago
|
(ann, description, param_num, callee_pname, callee_loc, origin_descr))
|
||
|
(Some instr_ref) loc curr_pdesc
|
||
8 years ago
|
in
|
||
8 years ago
|
let check_ann ann =
|
||
|
let b1 = TypeAnnotation.get_value ann ta1 in
|
||
|
let b2 = TypeAnnotation.get_value ann ta2 in
|
||
8 years ago
|
match (ann, b1, b2) with
|
||
7 years ago
|
| AnnotatedSignature.Nullable, false, true ->
|
||
6 years ago
|
report ann
|
||
7 years ago
|
| AnnotatedSignature.Present, true, false ->
|
||
|
report ann
|
||
|
| _ ->
|
||
|
()
|
||
8 years ago
|
in
|
||
|
if PatternMatch.type_is_class t1 then (
|
||
|
check_ann AnnotatedSignature.Nullable ;
|
||
|
if Config.eradicate_optional_present then check_ann AnnotatedSignature.Present )
|
||
|
in
|
||
10 years ago
|
let should_check_parameters =
|
||
7 years ago
|
match callee_pname with
|
||
|
| Typ.Procname.Java java_pname ->
|
||
6 years ago
|
(not (Typ.Procname.Java.is_external java_pname))
|
||
|
|| Models.is_modelled_nullable callee_pname
|
||
7 years ago
|
| _ ->
|
||
|
false
|
||
8 years ago
|
in
|
||
10 years ago
|
if should_check_parameters then
|
||
|
(* left to right to avoid guessing the different lengths *)
|
||
8 years ago
|
List.iter ~f:check resolved_params
|
||
10 years ago
|
|
||
7 years ago
|
|
||
10 years ago
|
(** Checks if the annotations are consistent with the inherited class or with the
|
||
10 years ago
|
implemented interfaces *)
|
||
8 years ago
|
let check_overridden_annotations find_canonical_duplicate tenv proc_name proc_desc
|
||
|
annotated_signature =
|
||
8 years ago
|
let start_node = Procdesc.get_start_node proc_desc in
|
||
|
let loc = Procdesc.Node.get_loc start_node in
|
||
10 years ago
|
let check_return overriden_proc_name overriden_signature =
|
||
|
let ret_is_nullable =
|
||
8 years ago
|
let ia, _ = annotated_signature.AnnotatedSignature.ret in
|
||
10 years ago
|
Annotations.ia_is_nullable ia
|
||
|
and ret_overridden_nullable =
|
||
8 years ago
|
let overriden_ia, _ = overriden_signature.AnnotatedSignature.ret in
|
||
8 years ago
|
Annotations.ia_is_nullable overriden_ia
|
||
|
in
|
||
10 years ago
|
if ret_is_nullable && not ret_overridden_nullable then
|
||
8 years ago
|
report_error tenv find_canonical_duplicate
|
||
6 years ago
|
(TypeErr.Inconsistent_subclass_return_annotation (proc_name, overriden_proc_name))
|
||
|
None loc proc_desc
|
||
10 years ago
|
and check_params overriden_proc_name overriden_signature =
|
||
|
let compare pos current_param overriden_param : int =
|
||
9 years ago
|
let current_name, current_ia, _ = current_param in
|
||
|
let _, overriden_ia, _ = overriden_param in
|
||
10 years ago
|
let () =
|
||
6 years ago
|
if (not (Annotations.ia_is_nullable current_ia)) && Annotations.ia_is_nullable overriden_ia
|
||
8 years ago
|
then
|
||
|
report_error tenv find_canonical_duplicate
|
||
10 years ago
|
(TypeErr.Inconsistent_subclass_parameter_annotation
|
||
6 years ago
|
(Mangled.to_string current_name, pos, proc_name, overriden_proc_name))
|
||
|
None loc proc_desc
|
||
8 years ago
|
in
|
||
|
pos + 1
|
||
|
in
|
||
10 years ago
|
(* TODO (#5280249): investigate why argument lists can be of different length *)
|
||
8 years ago
|
let current_params = annotated_signature.AnnotatedSignature.params
|
||
|
and overridden_params = overriden_signature.AnnotatedSignature.params in
|
||
10 years ago
|
let initial_pos = if is_virtual current_params then 0 else 1 in
|
||
8 years ago
|
if Int.equal (List.length current_params) (List.length overridden_params) then
|
||
8 years ago
|
ignore (List.fold2_exn ~f:compare ~init:initial_pos current_params overridden_params)
|
||
|
in
|
||
10 years ago
|
let check overriden_proc_name =
|
||
6 years ago
|
match PatternMatch.lookup_attributes tenv overriden_proc_name with
|
||
6 years ago
|
| Some attributes -> (
|
||
7 years ago
|
let overridden_signature = Models.get_modelled_annotated_signature attributes in
|
||
6 years ago
|
check_params overriden_proc_name overridden_signature ;
|
||
|
(* the analysis should not report return type inconsistencies with external code *)
|
||
|
match overriden_proc_name with
|
||
|
| Typ.Procname.Java java_pname when not (Typ.Procname.Java.is_external java_pname) ->
|
||
|
check_return overriden_proc_name overridden_signature
|
||
|
| _ ->
|
||
|
() )
|
||
7 years ago
|
| None ->
|
||
|
()
|
||
8 years ago
|
in
|
||
8 years ago
|
PatternMatch.override_iter check tenv proc_name
|