From 51f4dc7a9a89f1314e287477637ba0ab3d3dacdb Mon Sep 17 00:00:00 2001 From: Luis Cruz Date: Mon, 25 Apr 2016 15:51:27 -0700 Subject: [PATCH] added Tenv.get_overriden_method Summary: As suggested in the discussion https://github.com/facebook/infer/issues/326 this pull request implements ```ocaml get_overriden_method : Tenv.t -> Procname.java -> Procname.t ``` to get the method of a superclass that is being overridden by a specific java pname. I thought of unit test this, but unfortunately I wasn't able to figure out how to create the proper context with OUnit2. Perhaps the easiest way to test this will be integration tests. Feel free to reject the pull request if unit tests are mandatory (or for any other reason, of course). Closes https://github.com/facebook/infer/pull/341 Reviewed By: jeremydubreil Differential Revision: D3221254 Pulled By: sblackshear fb-gh-sync-id: 9c26258 fbshipit-source-id: 9c26258 --- infer/src/IR/tenv.ml | 31 +++++++++++++++++++++++++++++++ infer/src/IR/tenv.mli | 3 +++ 2 files changed, 34 insertions(+) diff --git a/infer/src/IR/tenv.ml b/infer/src/IR/tenv.ml index be0d61f18..fc321d3cf 100644 --- a/infer/src/IR/tenv.ml +++ b/infer/src/IR/tenv.ml @@ -92,6 +92,37 @@ let proc_extract_declaring_class_typ tenv pname_java = let proc_extract_return_typ tenv pname_java = lookup_java_typ_from_string tenv (Procname.java_get_return_type pname_java) +(** Get method that is being overriden by java_pname (if any) **) +let get_overriden_method tenv pname_java = + let struct_typ_get_def_method_by_name struct_typ method_name = + IList.find + (fun def_method -> method_name = Procname.get_method def_method) + struct_typ.Sil.def_methods in + let rec get_overriden_method_in_superclasses pname_java superclasses= + match superclasses with + | superclass :: superclasses_tail -> + begin + match lookup tenv superclass with + | Some struct_typ -> + begin + try + Some (struct_typ_get_def_method_by_name + struct_typ + (Procname.java_get_method pname_java)) + with Not_found -> + get_overriden_method_in_superclasses + pname_java + (superclasses_tail @ struct_typ.Sil.superclasses) + end + | None -> get_overriden_method_in_superclasses pname_java superclasses_tail + end + | [] -> None in + match proc_extract_declaring_class_typ tenv pname_java with + | Some proc_struct_typ -> + get_overriden_method_in_superclasses pname_java proc_struct_typ.superclasses + | _ -> None + + (** expand a type if it is a typename by looking it up in the type environment *) let expand_type tenv typ = match typ with diff --git a/infer/src/IR/tenv.mli b/infer/src/IR/tenv.mli index c58cccd64..6bb1306be 100644 --- a/infer/src/IR/tenv.mli +++ b/infer/src/IR/tenv.mli @@ -55,3 +55,6 @@ val pp : Format.formatter -> t -> unit (** Save a type environment into a file *) val store_to_file : DB.filename -> t -> unit + +(** Get method that is being overriden by java_pname (if any) **) +val get_overriden_method : t -> Procname.java -> Procname.t option