[infer][java] make the code more consistent to detect that constructor is a creating a resource

Summary: Other parts of the code where using the checks in the AndroidFramework module. It is better to have those things in one place.

Reviewed By: sblackshear

Differential Revision: D5654247

fbshipit-source-id: 2a783e7
master
Jeremy Dubreil 7 years ago committed by Facebook Github Bot
parent 7fc5cb7930
commit 9d67b6c07d

@ -71,6 +71,8 @@ let android_lifecycles =
let is_subtype_package_class tenv tname package classname = let is_subtype_package_class tenv tname package classname =
PatternMatch.is_subtype tenv tname (Typ.Name.Java.from_package_class package classname) PatternMatch.is_subtype tenv tname (Typ.Name.Java.from_package_class package classname)
let is_autocloseable tenv tname = is_subtype_package_class tenv tname "java.lang" "AutoCloseable"
let is_context tenv tname = is_subtype_package_class tenv tname "android.content" "Context" let is_context tenv tname = is_subtype_package_class tenv tname "android.content" "Context"
let is_application tenv tname = is_subtype_package_class tenv tname "android.app" "Application" let is_application tenv tname = is_subtype_package_class tenv tname "android.app" "Application"

@ -14,6 +14,8 @@ open! IStd
val get_lifecycles : (string * string * string list) list val get_lifecycles : (string * string * string list) list
(** return the complete list of (package, lifecycle_classname, lifecycle_methods) trios *) (** return the complete list of (package, lifecycle_classname, lifecycle_methods) trios *)
val is_autocloseable : Tenv.t -> Typ.Name.t -> bool
val is_context : Tenv.t -> Typ.Name.t -> bool val is_context : Tenv.t -> Typ.Name.t -> bool
(** return true if [typename] <: android.content.Context *) (** return true if [typename] <: android.content.Context *)

@ -634,9 +634,10 @@ let method_invocation (context: JContext.t) loc pc var_opt cn ms sil_obj_opt exp
| _ | _
when Config.models_mode || JClasspath.is_model callee_procname when Config.models_mode || JClasspath.is_model callee_procname
-> call_instrs -> call_instrs
| (_, {Typ.desc= Typ.Tptr ({desc= Tstruct typename}, _)} as exp) :: _
(* add a file attribute when calling the constructor of a subtype of Closeable *) (* add a file attribute when calling the constructor of a subtype of Closeable *)
| (_, typ as exp) :: _ when Typ.Procname.is_constructor callee_procname
when Typ.Procname.is_constructor callee_procname && JTransType.is_closeable program tenv typ && AndroidFramework.is_autocloseable tenv typename
-> let set_file_attr = -> let set_file_attr =
let set_builtin = Exp.Const (Const.Cfun BuiltinDecl.__set_file_attribute) in let set_builtin = Exp.Const (Const.Cfun BuiltinDecl.__set_file_attribute) in
Sil.Call (None, set_builtin, [exp], loc, CallFlags.default) Sil.Call (None, set_builtin, [exp], loc, CallFlags.default)

@ -374,14 +374,6 @@ let get_class_type program tenv cn =
let is_autogenerated_assert_field field_name = let is_autogenerated_assert_field field_name =
String.equal (Typ.Fieldname.java_get_field field_name) "$assertionsDisabled" String.equal (Typ.Fieldname.java_get_field field_name) "$assertionsDisabled"
let is_closeable program tenv typ =
let closeable_cn = JBasics.make_cn "java.io.Closeable" in
let closeable_typ = get_class_type program tenv closeable_cn in
let autocloseable_cn = JBasics.make_cn "java.lang.AutoCloseable" in
let autocloseable_typ = get_class_type program tenv autocloseable_cn in
let implements t = Prover.Subtyping_check.check_subtype tenv typ t in
implements closeable_typ || implements autocloseable_typ
(** translate an object type *) (** translate an object type *)
let rec object_type program tenv ot = let rec object_type program tenv ot =
match ot with match ot with

@ -43,9 +43,6 @@ val get_class_type : JClasspath.program -> Tenv.t -> JBasics.class_name -> Typ.t
val is_autogenerated_assert_field : Typ.Fieldname.t -> bool val is_autogenerated_assert_field : Typ.Fieldname.t -> bool
(** return true if [field_name] is the autogenerated C.$assertionsDisabled field for class C *) (** return true if [field_name] is the autogenerated C.$assertionsDisabled field for class C *)
val is_closeable : JClasspath.program -> Tenv.t -> Typ.t -> bool
(** [is_closeable program tenv typ] check if typ is an implemtation of the Closeable interface *)
val object_type : JClasspath.program -> Tenv.t -> JBasics.object_type -> Typ.t val object_type : JClasspath.program -> Tenv.t -> JBasics.object_type -> Typ.t
(** transforms a Java object type to a Typ.t *) (** transforms a Java object type to a Typ.t *)

Loading…
Cancel
Save