Skip to content

Commit 59aaf6b

Browse files
arthaudmeta-codesync[bot]
authored andcommitted
Parse define targets from pyrefly call graphs
Summary: As titled. Reviewed By: tianhan0 Differential Revision: D85775948 fbshipit-source-id: cc486836f4824a03a426c17b2867e899fe0a9893
1 parent c716612 commit 59aaf6b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

source/interprocedural/pyreflyApi.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,11 +1456,25 @@ module ModuleCallGraphs = struct
14561456
>>| fun if_called -> { if_called }
14571457
end
14581458

1459+
module JsonDefineCallees = struct
1460+
type t = { define_targets: JsonCallTarget.t list }
1461+
1462+
let from_json json =
1463+
let open Core.Result.Monad_infix in
1464+
let parse_call_target_list targets =
1465+
targets |> List.map ~f:JsonCallTarget.from_json |> Result.all
1466+
in
1467+
JsonUtil.get_list_member json "define_targets"
1468+
>>= parse_call_target_list
1469+
>>| fun define_targets -> { define_targets }
1470+
end
1471+
14591472
module JsonExpressionCallees = struct
14601473
type t =
14611474
| Call of JsonCallCallees.t
14621475
| Identifier of JsonIdentifierCallees.t
14631476
| AttributeAccess of JsonAttributeAccessCallees.t
1477+
| Define of JsonDefineCallees.t
14641478

14651479
let from_json json =
14661480
let open Core.Result.Monad_infix in
@@ -1473,6 +1487,8 @@ module ModuleCallGraphs = struct
14731487
| `Assoc [("AttributeAccess", attribute_access_callees)] ->
14741488
JsonAttributeAccessCallees.from_json attribute_access_callees
14751489
>>| fun attribute_access_callees -> AttributeAccess attribute_access_callees
1490+
| `Assoc [("Define", define_callees)] ->
1491+
JsonDefineCallees.from_json define_callees >>| fun define_callees -> Define define_callees
14761492
| _ ->
14771493
Error (FormatError.UnexpectedJsonType { json; message = "expected expression callees" })
14781494
end
@@ -4099,13 +4115,21 @@ module ReadOnly = struct
40994115
if_called;
41004116
}
41014117
in
4118+
let instantiate_define_callees { JsonDefineCallees.define_targets } =
4119+
{
4120+
CallGraph.DefineCallees.define_targets = List.map ~f:instantiate_call_target define_targets;
4121+
decorated_targets = [];
4122+
}
4123+
in
41024124
let instantiate_expression_callees = function
41034125
| JsonExpressionCallees.Call callees ->
41044126
ExpressionCallees.Call (instantiate_call_callees callees)
41054127
| JsonExpressionCallees.Identifier callees ->
41064128
ExpressionCallees.Identifier (instantiate_identifier_callees callees)
41074129
| JsonExpressionCallees.AttributeAccess callees ->
41084130
ExpressionCallees.AttributeAccess (instantiate_attribute_access_callees callees)
4131+
| JsonExpressionCallees.Define callees ->
4132+
ExpressionCallees.Define (instantiate_define_callees callees)
41094133
in
41104134
let instantiate_call_edge ~key:location ~data:callees call_graph =
41114135
let callees = instantiate_expression_callees callees in

source/interprocedural/test/callGraphTest.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,7 @@ let test_call_graph_of_define =
20322032
();
20332033
labeled_test_case __FUNCTION__ __LINE__
20342034
@@ assert_call_graph_of_define
2035+
~skip_for_pyrefly:false
20352036
~_migrated_to_pyrefly:false
20362037
~source:
20372038
{|
@@ -2075,6 +2076,29 @@ let test_call_graph_of_define =
20752076
]
20762077
()) );
20772078
]
2079+
~pyrefly_expected:
2080+
[
2081+
( "5:2-7:12",
2082+
ExpressionCallees.from_define
2083+
(DefineCallees.create
2084+
~define_targets:
2085+
[
2086+
CallTarget.create_regular
2087+
~return_type:None
2088+
(Target.Regular.Function { name = "test.foo.inner"; kind = Decorated });
2089+
]
2090+
()) );
2091+
( "9:2-9:9",
2092+
ExpressionCallees.from_call
2093+
(CallCallees.create
2094+
~call_targets:
2095+
[
2096+
CallTarget.create_regular
2097+
~return_type:(Some ReturnType.integer)
2098+
(Target.Regular.Function { name = "test.foo.inner"; kind = Decorated });
2099+
]
2100+
()) );
2101+
]
20782102
();
20792103
labeled_test_case __FUNCTION__ __LINE__
20802104
@@ assert_call_graph_of_define
@@ -2598,6 +2622,7 @@ let test_call_graph_of_define =
25982622
();
25992623
labeled_test_case __FUNCTION__ __LINE__
26002624
@@ assert_call_graph_of_define
2625+
~skip_for_pyrefly:false
26012626
~_migrated_to_pyrefly:true
26022627
~source:
26032628
{|
@@ -2632,6 +2657,7 @@ let test_call_graph_of_define =
26322657
();
26332658
labeled_test_case __FUNCTION__ __LINE__
26342659
@@ assert_call_graph_of_define
2660+
~skip_for_pyrefly:false
26352661
~_migrated_to_pyrefly:true
26362662
~source:
26372663
{|

0 commit comments

Comments
 (0)