[nullsafe][refactor] Migrate TypeOrigin.t to Procname.Java.t

Summary:
This is part of work aimed to reduce usage of language-agnostics modules
in Java-specific parts of nullsafe.

Reviewed By: artempyanykh

Differential Revision: D23052339

fbshipit-source-id: 665126957
master
Mitya Lyubarskiy 5 years ago committed by Facebook GitHub Bot
parent 4bc4376c85
commit 94f7d6aeeb

@ -69,7 +69,7 @@ let is_object_nullability_self_explanatory ~object_expression (object_origin : T
Latter case is self-explanatory: it is easy to the user to jump to definition Latter case is self-explanatory: it is easy to the user to jump to definition
and check out the method annotation. and check out the method annotation.
*) *)
let method_name = Procname.to_simplified_string pname in let method_name = Procname.Java.to_simplified_string pname in
String.is_suffix object_expression ~suffix:method_name String.is_suffix object_expression ~suffix:method_name
| ThirdParty ModelledInternally | ThirdParty (InThirdPartyRepo _) -> | ThirdParty ModelledInternally | ThirdParty (InThirdPartyRepo _) ->
(* This is non-trivial and should always be explained to the user *) (* This is non-trivial and should always be explained to the user *)
@ -96,14 +96,6 @@ type message_info =
; recommendation: string ; recommendation: string
; issue_type: IssueType.t } ; issue_type: IssueType.t }
let get_method_class_name procname =
match procname with
| Procname.Java java_pname ->
Some (Procname.Java.get_simple_class_name java_pname)
| _ ->
None
let get_field_class_name field_name = let get_field_class_name field_name =
let class_with_field = Fieldname.to_simplified_string field_name in let class_with_field = Fieldname.to_simplified_string field_name in
String.rsplit2 class_with_field ~on:'.' String.rsplit2 class_with_field ~on:'.'
@ -163,7 +155,7 @@ let get_info object_origin nullsafe_mode untrusted_kind =
| TypeOrigin.MethodCall {pname; call_loc} -> | TypeOrigin.MethodCall {pname; call_loc} ->
let offending_object = let offending_object =
F.asprintf "%a" MarkupFormatter.pp_monospaced F.asprintf "%a" MarkupFormatter.pp_monospaced
(Procname.to_simplified_string ~withclass:true pname) (Procname.Java.to_simplified_string ~withclass:true pname)
in in
let object_loc = call_loc in let object_loc = call_loc in
let what_is_used = "Result of this call" in let what_is_used = "Result of this call" in
@ -171,7 +163,7 @@ let get_info object_origin nullsafe_mode untrusted_kind =
match untrusted_kind with match untrusted_kind with
| UserFriendlyNullable.ThirdPartyNonnull -> | UserFriendlyNullable.ThirdPartyNonnull ->
let suggested_third_party_sig_file = let suggested_third_party_sig_file =
ThirdPartyAnnotationInfo.lookup_related_sig_file_for_proc ThirdPartyAnnotationInfo.lookup_related_sig_file_for_java_proc
(ThirdPartyAnnotationGlobalRepo.get_repo ()) (ThirdPartyAnnotationGlobalRepo.get_repo ())
pname pname
in in
@ -191,9 +183,7 @@ let get_info object_origin nullsafe_mode untrusted_kind =
mk_coming_from_unchecked_or_locally_checked_case_only nullsafe_mode untrusted_kind mk_coming_from_unchecked_or_locally_checked_case_only nullsafe_mode untrusted_kind
in in
let recommendation = let recommendation =
let what_to_strictify = let what_to_strictify = Procname.Java.get_simple_class_name pname in
Option.value (get_method_class_name pname) ~default:offending_object
in
mk_strictification_advice_unchecked_or_locally_checked_case_only nullsafe_mode mk_strictification_advice_unchecked_or_locally_checked_case_only nullsafe_mode
untrusted_kind ~what_to_strictify untrusted_kind ~what_to_strictify
in in
@ -269,11 +259,11 @@ let mk_nullsafe_issue_for_untrusted_values ~nullsafe_mode ~untrusted_kind ~bad_u
let find_alternative_nonnull_method_description nullable_origin = let find_alternative_nonnull_method_description nullable_origin =
let open IOption.Let_syntax in let open IOption.Let_syntax in
match nullable_origin with match nullable_origin with
| TypeOrigin.MethodCall {pname= Procname.Java java_pname as pname} -> | TypeOrigin.MethodCall {pname} ->
let* ModelTables.{package_name; class_name; method_name} = let* ModelTables.{package_name; class_name; method_name} =
Models.find_nonnullable_alternative pname Models.find_nonnullable_alternative pname
in in
let+ original_package_name = Procname.Java.get_package java_pname in let+ original_package_name = Procname.Java.get_package pname in
if String.equal original_package_name package_name then if String.equal original_package_name package_name then
(* The same package that is from origin - omit name for simplicity *) (* The same package that is from origin - omit name for simplicity *)
class_name ^ "." ^ method_name ^ "()" class_name ^ "." ^ method_name ^ "()"

@ -149,9 +149,10 @@ let is_mapPut proc_name = table_has_procedure mapPut_table proc_name
(** Check if a (nullable) method has a non-nullable alternative: A method that does the same as (** Check if a (nullable) method has a non-nullable alternative: A method that does the same as
[proc_name] but asserts the result is not null before returning to the caller. *) [proc_name] but asserts the result is not null before returning to the caller. *)
let find_nonnullable_alternative proc_name = let find_nonnullable_alternative java_proc_name =
(* NOTE: For now we fetch this info from internal models. (* NOTE: For now we fetch this info from internal models.
It is a good idea to support this feature in a user-facing third party repository. *) It is a good idea to support this feature in a user-facing third party repository. *)
let proc_name = Procname.Java java_proc_name in
let proc_id = Procname.to_unique_id proc_name in let proc_id = Procname.to_unique_id proc_name in
Hashtbl.find_opt nonnull_alternatives_table proc_id Hashtbl.find_opt nonnull_alternatives_table proc_id

@ -997,8 +997,7 @@ let calc_typestate_after_call
({IntraproceduralAnalysis.proc_desc= curr_pdesc; tenv; _} as analysis_data) ({IntraproceduralAnalysis.proc_desc= curr_pdesc; tenv; _} as analysis_data)
find_canonical_duplicate calls_this checks idenv instr_ref signature_params cflags call_params find_canonical_duplicate calls_this checks idenv instr_ref signature_params cflags call_params
~is_anonymous_inner_class_constructor ~callee_annotated_signature ~callee_attributes ~is_anonymous_inner_class_constructor ~callee_annotated_signature ~callee_attributes
~callee_pname ~callee_pname_java ~curr_annotated_signature ~nullsafe_mode ~typestate ~typestate1 ~callee_pname ~curr_annotated_signature ~nullsafe_mode ~typestate ~typestate1 loc node =
loc node =
let resolve_param i (formal_param, actual_param) = let resolve_param i (formal_param, actual_param) =
let (orig_e2, e2), t2 = actual_param in let (orig_e2, e2), t2 = actual_param in
let _, inferred_nullability_actual = let _, inferred_nullability_actual =
@ -1058,19 +1057,18 @@ let calc_typestate_after_call
if not is_anonymous_inner_class_constructor then ( if not is_anonymous_inner_class_constructor then (
if cflags.CallFlags.cf_virtual && checks.eradicate then if cflags.CallFlags.cf_virtual && checks.eradicate then
EradicateChecks.check_call_receiver analysis_data ~nullsafe_mode find_canonical_duplicate EradicateChecks.check_call_receiver analysis_data ~nullsafe_mode find_canonical_duplicate
node typestate1 call_params callee_pname_java instr_ref loc node typestate1 call_params callee_pname instr_ref loc
(typecheck_expr analysis_data ~nullsafe_mode find_canonical_duplicate calls_this checks) ; (typecheck_expr analysis_data ~nullsafe_mode find_canonical_duplicate calls_this checks) ;
if checks.eradicate then if checks.eradicate then
EradicateChecks.check_call_parameters ~callee_pname:callee_pname_java analysis_data EradicateChecks.check_call_parameters ~callee_pname analysis_data ~nullsafe_mode
~nullsafe_mode ~callee_annotated_signature find_canonical_duplicate node resolved_params ~callee_annotated_signature find_canonical_duplicate node resolved_params loc instr_ref ;
loc instr_ref ; if Models.is_check_not_null (Procname.Java callee_pname) then
if Models.is_check_not_null callee_pname then match Models.get_check_not_null_parameter (Procname.Java callee_pname) with
match Models.get_check_not_null_parameter callee_pname with
| Some index -> | Some index ->
do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node
loc curr_annotated_signature checks call_params idenv index ~is_vararg:false loc curr_annotated_signature checks call_params idenv index ~is_vararg:false
typestate1 typestate1
| None when Procname.Java.is_vararg callee_pname_java -> | None when Procname.Java.is_vararg callee_pname ->
let last_parameter = List.length call_params in let last_parameter = List.length call_params in
do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node
loc curr_annotated_signature checks call_params idenv last_parameter ~is_vararg:true loc curr_annotated_signature checks call_params idenv last_parameter ~is_vararg:true
@ -1079,13 +1077,16 @@ let calc_typestate_after_call
(* assume the first parameter is checked for null *) (* assume the first parameter is checked for null *)
do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node do_preconditions_check_not_null analysis_data instr_ref find_canonical_duplicate node
loc curr_annotated_signature checks call_params idenv 1 ~is_vararg:false typestate1 loc curr_annotated_signature checks call_params idenv 1 ~is_vararg:false typestate1
else if Models.is_check_state callee_pname || Models.is_check_argument callee_pname then else if
Models.is_check_state (Procname.Java callee_pname)
|| Models.is_check_argument (Procname.Java callee_pname)
then
let curr_pname = Procdesc.get_proc_name curr_pdesc in let curr_pname = Procdesc.get_proc_name curr_pdesc in
do_preconditions_check_state instr_ref idenv tenv curr_pname curr_annotated_signature do_preconditions_check_state instr_ref idenv tenv curr_pname curr_annotated_signature
call_params loc node typestate1 call_params loc node typestate1
else if Models.is_mapPut callee_pname then else if Models.is_mapPut (Procname.Java callee_pname) then
do_map_put analysis_data call_params callee_pname loc node calls_this checks instr_ref do_map_put analysis_data call_params (Procname.Java callee_pname) loc node calls_this checks
~nullsafe_mode find_canonical_duplicate typestate1 instr_ref ~nullsafe_mode find_canonical_duplicate typestate1
else typestate1 ) else typestate1 )
else typestate1 else typestate1
in in
@ -1175,7 +1176,7 @@ let typecheck_sil_call_function
let typestate_after_call, finally_resolved_ret = let typestate_after_call, finally_resolved_ret =
calc_typestate_after_call analysis_data find_canonical_duplicate calls_this checks idenv calc_typestate_after_call analysis_data find_canonical_duplicate calls_this checks idenv
instr_ref signature_params cflags call_params ~is_anonymous_inner_class_constructor instr_ref signature_params cflags call_params ~is_anonymous_inner_class_constructor
~callee_annotated_signature ~callee_attributes ~callee_pname ~callee_pname_java ~callee_annotated_signature ~callee_attributes ~callee_pname:callee_pname_java
~curr_annotated_signature ~nullsafe_mode ~typestate ~typestate1 loc node ~curr_annotated_signature ~nullsafe_mode ~typestate ~typestate1 loc node
in in
do_return finally_resolved_ret typestate_after_call ) do_return finally_resolved_ret typestate_after_call )

@ -219,7 +219,7 @@ let get_nonnull_explanation_for_condition_redudant (nonnull_origin : TypeOrigin.
match nonnull_origin with match nonnull_origin with
| MethodCall {pname} -> | MethodCall {pname} ->
Format.asprintf ": %a is not annotated as `@Nullable`" MF.pp_monospaced Format.asprintf ": %a is not annotated as `@Nullable`" MF.pp_monospaced
(Procname.to_simplified_string ~withclass:true pname) (Procname.Java.to_simplified_string ~withclass:true pname)
| NullConst _ -> | NullConst _ ->
Logging.die Logging.InternalError Logging.die Logging.InternalError
"Unexpected origin NullConst: this is for nullable types, should not lead to condition \ "Unexpected origin NullConst: this is for nullable types, should not lead to condition \

@ -23,7 +23,7 @@ type t =
| CurrMethodParameter of method_parameter_origin | CurrMethodParameter of method_parameter_origin
| This | This
| MethodCall of | MethodCall of
{ pname: Procname.t { pname: Procname.Java.t
; call_loc: Location.t ; call_loc: Location.t
; annotated_signature: AnnotatedSignature.t ; annotated_signature: AnnotatedSignature.t
; is_defined: bool } ; is_defined: bool }
@ -96,7 +96,7 @@ let rec to_string = function
| This -> | This ->
"this" "this"
| MethodCall {pname} -> | MethodCall {pname} ->
Printf.sprintf "Fun %s" (Procname.to_simplified_string pname) Printf.sprintf "Fun %s" (Procname.Java.to_simplified_string pname)
| CallToGetKnownToContainsKey -> | CallToGetKnownToContainsKey ->
"CallToGetKnownToContainsKey" "CallToGetKnownToContainsKey"
| New -> | New ->
@ -152,7 +152,7 @@ let get_method_ret_description pname call_loc
line_number line_number
in in
Format.sprintf "call to %s%s%s" Format.sprintf "call to %s%s%s"
(Procname.to_simplified_string ~withclass:should_show_class_name pname) (Procname.Java.to_simplified_string ~withclass:should_show_class_name pname)
(atline call_loc) model_info (atline call_loc) model_info

@ -22,7 +22,7 @@ type t =
(** Parameter of a method we are currently in, *) (** Parameter of a method we are currently in, *)
| This (* `this` object. Can not be null, according to Java rules. *) | This (* `this` object. Can not be null, according to Java rules. *)
| MethodCall of | MethodCall of
{ pname: Procname.t { pname: Procname.Java.t
; call_loc: Location.t ; call_loc: Location.t
; annotated_signature: AnnotatedSignature.t ; annotated_signature: AnnotatedSignature.t
; is_defined: bool } (** A result of a method call *) ; is_defined: bool } (** A result of a method call *)

Loading…
Cancel
Save