Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DependencyGraph.cs « LinkerAnalyzerCore « analyzer « src - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2507cae700ac7cd6afb5d00fef0a2b67f8aeda8b (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

//
// DependencyGraph.cs: linker dependencies graph
//
// Author:
//   Radek Doulik (rodo@xamarin.com)
//
// Copyright 2015 Xamarin Inc (http://www.xamarin.com).
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace LinkerAnalyzer.Core
{
	public class VertexData
	{
		public string value;
		public List<int> parentIndexes;
		public int index;

		public string DepsCount {
			get {
				if (parentIndexes == null || parentIndexes.Count < 1)
					return "";
				return string.Format (" [{0} deps]", parentIndexes.Count);
			}
		}
	}

	public class DependencyGraph
	{
		protected List<VertexData> vertices = new List<VertexData> ();
		public List<VertexData> Types = new List<VertexData> ();
		readonly Dictionary<string, int> indexes = new Dictionary<string, int> ();
		protected Dictionary<string, int> counts = new Dictionary<string, int> ();
		internal SpaceAnalyzer SpaceAnalyzer { get; set; }

		public void Load (string filename)
		{
			Console.WriteLine ("Loading dependency tree from: {0}", filename);

			try {
				using (var fileStream = File.OpenRead (filename)) {
					Load (fileStream);
				}
			} catch (Exception) {
				Console.WriteLine ("Unable to open and read the dependencies.");
				Environment.Exit (1);
			}
		}

		void Load (FileStream fileStream)
		{
			using (XmlReader reader = XmlReader.Create (fileStream)) {
				while (reader.Read ()) {
					switch (reader.NodeType) {
					case XmlNodeType.Element:
						// Console.WriteLine (reader.Name);
						if (reader.Name == "edge" && reader.IsStartElement ()) {
							string b = reader.GetAttribute ("b");
							string e = reader.GetAttribute ("e");
							//Console.WriteLine ("edge value " + b + "  -->  " + e);

							if (e != b) {
								VertexData begin = Vertex (b, true);
								VertexData end = Vertex (e, true);

								if (end.parentIndexes == null)
									end.parentIndexes = new List<int> ();
								if (!end.parentIndexes.Contains (begin.index)) {
									end.parentIndexes.Add (begin.index);
									//Console.WriteLine (" end parent index: {0}", end.parentIndexes);
								}
							}
						}
						break;
					default:
						//Console.WriteLine ("node: " + reader.NodeType);
						break;
					}
				}
			}
		}

		public VertexData Vertex (string vertexName, bool create = false)
		{
			VertexData vertex;

			try {
				vertex = vertices[indexes[vertexName]];
			} catch (KeyNotFoundException) {
				if (create) {
					int index = vertices.Count;
					vertex = new VertexData () { value = vertexName, index = index };
					vertices.Add (vertex);
					indexes.Add (vertexName, index);
					string prefix = vertexName.Substring (0, vertexName.IndexOf (':'));
					if (counts.TryGetValue (prefix, out var count))
						counts[prefix] = count + 1;
					else
						counts[prefix] = 1;
					//Console.WriteLine ("prefix " + prefix + " count " + counts[prefix]);
					if (prefix == "TypeDef") {
						Types.Add (vertex);
					}
				} else
					return null;
			}

			return vertex;
		}

		public VertexData Vertex (int index)
		{
			return vertices[index];
		}

		IEnumerable<Tuple<VertexData, int>> AddDependencies (VertexData vertex, HashSet<int> reachedVertices, int depth)
		{
			reachedVertices.Add (vertex.index);
			yield return new Tuple<VertexData, int> (vertex, depth);

			if (vertex.parentIndexes == null)
				yield break;

			foreach (var pi in vertex.parentIndexes) {
				var parent = Vertex (pi);
				if (reachedVertices.Contains (parent.index))
					continue;

				foreach (var d in AddDependencies (parent, reachedVertices, depth + 1))
					yield return d;
			}
		}

		public List<Tuple<VertexData, int>> GetAllDependencies (VertexData vertex)
		{
			return new List<Tuple<VertexData, int>> (AddDependencies (vertex, new HashSet<int> (), 0));
		}
	}
}