-
Notifications
You must be signed in to change notification settings - Fork 134
/
CallGraph.cs
43 lines (37 loc) · 1.23 KB
/
CallGraph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using Mono.Cecil;
namespace Mono.Linker
{
class CallGraph
{
readonly Dictionary<MethodDefinition, HashSet<MethodDefinition>> callGraph;
public CallGraph () => callGraph = new Dictionary<MethodDefinition, HashSet<MethodDefinition>> ();
public void TrackCall (MethodDefinition fromMethod, MethodDefinition toMethod)
{
if (!callGraph.TryGetValue (fromMethod, out HashSet<MethodDefinition>? toMethods)) {
toMethods = new HashSet<MethodDefinition> ();
callGraph.Add (fromMethod, toMethods);
}
toMethods.Add (toMethod);
}
public IEnumerable<MethodDefinition> GetReachableMethods (MethodDefinition start)
{
Queue<MethodDefinition> queue = new ();
HashSet<MethodDefinition> visited = new ();
visited.Add (start);
queue.Enqueue (start);
while (queue.TryDequeue (out MethodDefinition? method)) {
if (!callGraph.TryGetValue (method, out HashSet<MethodDefinition>? callees))
continue;
foreach (var callee in callees) {
if (visited.Add (callee)) {
queue.Enqueue (callee);
yield return callee;
}
}
}
}
}
}