Skip to content

Commit

Permalink
Migrate callGraph.ml to using PyrePysaLogic
Browse files Browse the repository at this point in the history
Summary:
I made another helper here to prevent TypeInfo.Unit from being exposed.

I went ahead and added ResolvedReference and Module.Export though - the
dependence of the callgraph on `resolve_exports` seems okay to me.

Reviewed By: grievejia

Differential Revision: D64509542

fbshipit-source-id: 0c0c1ffa26471cabf758ef29fa9f008b2d33bb40
  • Loading branch information
stroxler authored and facebook-github-bot committed Oct 17, 2024
1 parent 85f6e3a commit 8fdbc5d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
14 changes: 11 additions & 3 deletions source/analysis/pyrePysaLogic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Fixpoint = Fixpoint
module DecoratorPreprocessing = DecoratorPreprocessing
module ClassSummary = ClassSummary
module AnnotatedAttribute = AnnotatedAttribute
module ResolvedReference = ResolvedReference
module ModuleExport = Module.Export
module SharedMemoryKeys = SharedMemoryKeys

exception UntrackedClass = ClassHierarchy.Untracked
Expand All @@ -38,10 +40,16 @@ let qualifier_and_bodies_of_function_definition ({ FunctionDefinition.qualifier;

let artifact_path_of_module_path = ArtifactPaths.artifact_path_of_module_path

let type_of_attribute instantiated_attribute =
AnnotatedAttribute.annotation instantiated_attribute |> TypeInfo.Unit.annotation


let name_of_method method_as_instantiated_attribute =
AnnotatedAttribute.annotation method_as_instantiated_attribute
|> TypeInfo.Unit.annotation
|> Type.callable_name
type_of_attribute method_as_instantiated_attribute |> Type.callable_name


let undecorated_signature_of_global { AttributeResolution.Global.undecorated_signature; _ } =
undecorated_signature


module Testing = struct
Expand Down
60 changes: 31 additions & 29 deletions source/interprocedural/callGraph.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ open Statement
open Expression
open Pyre
module PyrePysaEnvironment = Analysis.PyrePysaEnvironment
module Cfg = Analysis.Cfg
module ResolvedReference = Analysis.ResolvedReference
module ClassSummary = Analysis.ClassSummary
module AnnotatedAttribute = Analysis.AnnotatedAttribute
module TypeInfo = Analysis.TypeInfo
module PyrePysaLogic = Analysis.PyrePysaLogic

module JsonHelper = struct
let add_optional name value to_json bindings =
Expand Down Expand Up @@ -124,7 +120,7 @@ module ReturnType = struct
in
{ is_boolean; is_integer; is_float; is_enumeration }
with
| Analysis.ClassHierarchy.Untracked untracked_type ->
| PyrePysaLogic.UntrackedClass untracked_type ->
Log.warning
"Found untracked type `%s` when checking the return type `%a` of a call. The return type \
will NOT be considered a scalar, which could lead to missing breadcrumbs."
Expand Down Expand Up @@ -1360,7 +1356,7 @@ let compute_indirect_targets ~pyre_in_context ~override_graph ~receiver_type imp
~left:candidate_type
~right:receiver_type
with
| Analysis.ClassHierarchy.Untracked untracked_type ->
| PyrePysaLogic.UntrackedClass untracked_type ->
Log.warning
"Found untracked type `%s` when comparing `%a` and `%a`. The class `%a` will be \
considered a subclass of `%a`, which could lead to false positives."
Expand Down Expand Up @@ -1683,8 +1679,7 @@ let resolve_callee_from_defining_expression
(* If implementing_class is unknown, this must be a function rather than a method. We can use
global resolution on the callee. *)
PyrePysaEnvironment.ReadOnly.global pyre_api (Ast.Expression.name_to_reference_exn name)
>>= fun { Analysis.AttributeResolution.Global.undecorated_signature; _ } ->
undecorated_signature
>>= PyrePysaLogic.undecorated_signature_of_global
>>| fun undecorated_signature ->
resolve_callees_from_type
~debug
Expand Down Expand Up @@ -1764,7 +1759,7 @@ let resolve_stringify_call ~pyre_in_context expression =
"__repr__"
| _ -> "__str__"
with
| Analysis.ClassHierarchy.Untracked _ -> "__str__"
| PyrePysaLogic.UntrackedClass _ -> "__str__"


(* Rewrite certain calls for the interprocedural analysis (e.g, pysa).
Expand Down Expand Up @@ -2007,9 +2002,11 @@ let resolve_callee_ignoring_decorators
let name = Ast.Expression.name_to_reference_exn name in
match PyrePysaEnvironment.ReadOnly.resolve_exports pyre_api name with
| Some
(ResolvedReference.ModuleAttribute
(PyrePysaLogic.ResolvedReference.ModuleAttribute
{
export = ResolvedReference.Exported (Analysis.Module.Export.Name.Define _);
export =
PyrePysaLogic.ResolvedReference.Exported
(PyrePysaLogic.ModuleExport.Name.Define _);
remaining = [];
_;
}) ->
Expand All @@ -2022,18 +2019,19 @@ let resolve_callee_ignoring_decorators
|> Target.from_regular);
]
| Some
(ResolvedReference.ModuleAttribute
(PyrePysaLogic.ResolvedReference.ModuleAttribute
{
from;
name;
export = ResolvedReference.Exported Analysis.Module.Export.Name.Class;
export =
PyrePysaLogic.ResolvedReference.Exported PyrePysaLogic.ModuleExport.Name.Class;
remaining = [attribute];
_;
}) -> (
let class_name = Reference.create ~prefix:from name |> Reference.show in
PyrePysaEnvironment.ReadOnly.get_class_summary pyre_api class_name
>>| Node.value
>>| ClassSummary.attributes
>>| PyrePysaLogic.ClassSummary.attributes
>>= Identifier.SerializableMap.find_opt attribute
>>| Node.value
|> function
Expand All @@ -2054,7 +2052,7 @@ let resolve_callee_ignoring_decorators
let () =
log
"Bypassing decorators - Non-method attribute `%s` for callee `%s`"
(ClassSummary.Attribute.show_attribute attribute)
(PyrePysaLogic.ClassSummary.Attribute.show_attribute attribute)
(Expression.show callee)
in
[]
Expand Down Expand Up @@ -2093,11 +2091,15 @@ let resolve_callee_ignoring_decorators
match
PyrePysaEnvironment.ReadOnly.get_class_summary pyre_api element
>>| Node.value
>>| ClassSummary.attributes
>>| PyrePysaLogic.ClassSummary.attributes
>>= Identifier.SerializableMap.find_opt attribute
>>| Node.value
with
| Some { ClassSummary.Attribute.kind = Method { static; signatures; _ }; _ } ->
| Some
{
PyrePysaLogic.ClassSummary.Attribute.kind = Method { static; signatures; _ };
_;
} ->
Some (element, contain_class_method signatures, static)
| _ -> None
in
Expand Down Expand Up @@ -2199,11 +2201,9 @@ let resolve_attribute_access_properties
ReturnType.none
else
let pyre_api = PyrePysaEnvironment.InContext.pyre_api pyre_in_context in
AnnotatedAttribute.annotation property
|> TypeInfo.Unit.annotation
|> ReturnType.from_annotation ~pyre_api
PyrePysaLogic.type_of_attribute property |> ReturnType.from_annotation ~pyre_api
in
let parent = AnnotatedAttribute.parent property |> Reference.create in
let parent = PyrePysaLogic.AnnotatedAttribute.parent property |> Reference.create in
let property_targets =
let kind = if setter then Target.PropertySetter else Target.Normal in
if Type.is_builtins_type base_type_info then
Expand All @@ -2228,7 +2228,8 @@ let resolve_attribute_access_properties
let properties, non_properties =
List.partition_map
~f:(function
| Some property when AnnotatedAttribute.property property -> Either.First property
| Some property when PyrePysaLogic.AnnotatedAttribute.property property ->
Either.First property
| attribute -> Either.Second attribute)
attributes
in
Expand All @@ -2254,7 +2255,7 @@ let as_identifier_reference ~define ~pyre_in_context expression =
(PyrePysaEnvironment.InContext.pyre_api pyre_in_context)
reference
>>= function
| ResolvedReference.ModuleAttribute { from; name; remaining = []; _ } ->
| PyrePysaLogic.ResolvedReference.ModuleAttribute { from; name; remaining = []; _ } ->
Some (IdentifierCallees.Global (Reference.combine from (Reference.create name)))
| _ -> None)
| _ -> None
Expand Down Expand Up @@ -2298,8 +2299,9 @@ let resolve_attribute_access_global_targets
~type_for_lookup:annotation
in
match attribute with
| Some attribute when AnnotatedAttribute.defined attribute ->
Type.Primitive (AnnotatedAttribute.parent attribute) |> Type.class_name
| Some attribute when PyrePysaLogic.AnnotatedAttribute.defined attribute ->
Type.Primitive (PyrePysaLogic.AnnotatedAttribute.parent attribute)
|> Type.class_name
| _ -> Type.class_name annotation
in
let attribute = Format.sprintf "__class__.%s" attribute in
Expand Down Expand Up @@ -2926,7 +2928,7 @@ struct

module CalleeVisitor = Visit.MakeNodeVisitor (NodeVisitor)

include Analysis.Fixpoint.Make (struct
include PyrePysaLogic.Fixpoint.Make (struct
type t = unit [@@deriving show]

let bottom = ()
Expand Down Expand Up @@ -2985,7 +2987,7 @@ let call_graph_of_define
~qualifier
~define
=
let name = Analysis.FunctionDefinition.qualified_name_of_define ~module_name:qualifier define in
let name = PyrePysaLogic.qualified_name_of_define ~module_name:qualifier define in
let timer = Timer.start () in
let callees_at_location = Location.Table.create () in
let module DefineFixpoint = DefineCallGraphFixpoint (struct
Expand Down Expand Up @@ -3027,7 +3029,7 @@ let call_graph_of_define
value))
in

DefineFixpoint.forward ~cfg:(Cfg.create define) ~initial:() |> ignore;
DefineFixpoint.forward ~cfg:(PyrePysaLogic.Cfg.create define) ~initial:() |> ignore;
let call_graph =
Hashtbl.to_alist callees_at_location
|> List.map ~f:(fun (location, unprocessed_callees) ->
Expand Down

0 comments on commit 8fdbc5d

Please sign in to comment.