Merge pull request #2227 from esdrubal/tzparse
[mono.git] / mcs / tools / linker-analyzer / ConsoleDependencyGraph.cs
1 //
2 // ConsoleDependencyGraph.cs: text output related code for dependency graph
3 //
4 // Author:
5 //   Radek Doulik (rodo@xamarin.com)
6 //
7 // Copyright 2015 Xamarin Inc (http://www.xamarin.com).
8 //
9 using System;
10 using System.Collections.Generic;
11 using System.Text.RegularExpressions;
12 using LinkerAnalyzer.Core;
13
14 namespace LinkerAnalyzer
15 {
16         public class ConsoleDependencyGraph : DependencyGraph
17         {
18                 public bool Tree = false;
19
20                 public void ShowDependencies (string raw, List<VertexData> verticesList, string searchString)
21                 {
22                         VertexData vertex = Vertex (raw);
23                         if (vertex == null) {
24                                 Regex regex = new Regex (searchString);
25                                 int count = 0;
26
27                                 foreach (var v in verticesList) {
28                                         if (regex.Match (v.value) != Match.Empty) {
29                                                 ShowDependencies (v);
30                                                 count++;
31                                         }
32                                 }
33
34                                 if (count == 0)
35                                         Console.WriteLine ("\nUnable to find vertex: {0}", raw);
36                                 else
37                                         Console.WriteLine ("\nFound {0} matches", count);
38                         } else
39                                 ShowDependencies (vertex);
40                 }
41
42                 public void ShowDependencies (VertexData vertex)
43                 {
44                         Header ("{0} dependencies", vertex.value);
45                         if (vertex.parentIndexes == null) {
46                                 Console.WriteLine ("Root dependency");
47                         } else {
48                                 int i = 0;
49                                 foreach (int index in vertex.parentIndexes) {
50                                         Console.WriteLine ("Dependency #{0}", ++i);
51                                         Console.WriteLine ("\t{0}", vertex.value);
52                                         var childVertex = Vertex (index);
53                                         Console.WriteLine ("\t| {0}{1}", childVertex.value, childVertex.DepsCount);
54                                         while (childVertex.parentIndexes != null) {
55                                                 childVertex = Vertex (childVertex.parentIndexes [0]);
56                                                 Console.WriteLine ("\t| {0}{1}", childVertex.value, childVertex.DepsCount);
57                                         }
58                                         if (Tree)
59                                                 break;
60                                 }
61                         }
62                 }
63
64                 public void ShowAllDependencies ()
65                 {
66                         Header ("All dependencies");
67                         Console.WriteLine ("Types count: {0}", vertices.Count);
68                         foreach (var vertex in vertices)
69                                 ShowDependencies (vertex);
70                 }
71
72                 public void ShowTypesDependencies ()
73                 {
74                         Header ("All types dependencies");
75                         Console.WriteLine ("Deps count: {0}", Types.Count);
76                         foreach (var type in Types)
77                                 ShowDependencies (type);
78                 }
79
80                 string Tabs (string key)
81                 {
82                         int count = Math.Max (1, 2 - key.Length / 8);
83
84                         if (count == 1)
85                                 return "\t";
86                         else
87                                 return "\t\t";
88                 }
89
90                 public void ShowStat (bool verbose = false)
91                 {
92                         Header ("Statistics");
93                         if (verbose) {
94                                 foreach (var key in counts.Keys)
95                                         Console.WriteLine ("Vertex type:\t{0}{1}count:{2}", key, Tabs (key), counts [key]);
96                         } else {
97                                 Console.WriteLine ("Assemblies:\t{0}", counts ["Assembly"]);
98                                 Console.WriteLine ("Modules:\t{0}", counts ["Module"]);
99                                 Console.WriteLine ("Types:\t\t{0}", counts ["TypeDef"]);
100                                 Console.WriteLine ("Fields:\t\t{0}", counts ["Field"]);
101                                 Console.WriteLine ("Methods:\t{0}", counts ["Method"]);
102                         }
103
104                         Console.WriteLine ();
105                         Console.WriteLine ("Total vertices: {0}", vertices.Count);
106                 }
107
108                 public void ShowRoots ()
109                 {
110                         Header ("Root vertices");
111
112                         int count = 0;
113                         foreach (var vertex in vertices) {
114                                 if (vertex.parentIndexes == null) {
115                                         Console.WriteLine ("{0}", vertex.value);
116                                         count++;
117                                 }
118                         }
119
120                         Console.WriteLine ();
121                         Console.WriteLine ("Total root vertices: {0}", count);
122                 }
123
124                 public void ShowRawDependencies (string raw)
125                 {
126                         Header ("Raw dependencies: '{0}'", raw);
127                         ShowDependencies (raw, vertices, raw);
128                 }
129
130                 public void ShowTypeDependencies (string raw)
131                 {
132                         Header ("Type dependencies: '{0}'", raw);
133                         ShowDependencies ("TypeDef:" + raw, Types, raw);
134                 }
135
136                 void Header (string header, params object[] values)
137                 {
138                         string formatted = string.Format (header, values);
139                         Console.WriteLine ();
140                         Console.Write ("--- {0} ", formatted);
141                         for (int i=0; i< Math.Max (3, 64 - formatted.Length); i++)
142                                 Console.Write ('-');
143                         Console.WriteLine ();
144                 }
145         }
146 }