forked from icsharpcode/NRefactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindReferencesConsistencyCheck.cs
168 lines (152 loc) · 6.22 KB
/
FindReferencesConsistencyCheck.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.ConsistencyCheck
{
public class FindReferencesConsistencyCheck
{
readonly Solution solution;
Dictionary<IEntity, HashSet<AstNode>> referenceDict = new Dictionary<IEntity, HashSet<AstNode>>();
public FindReferencesConsistencyCheck(Solution solution)
{
this.solution = solution;
}
public void Run()
{
using (new Timer("Finding referenced entities... ")) {
foreach (var file in solution.AllFiles) {
var navigator = new FindReferencedEntities(
delegate (AstNode node, IEntity entity) {
if (node == null)
throw new ArgumentNullException("node");
if (entity == null)
throw new ArgumentNullException("entity");
if (!IgnoreEntity(entity)) {
HashSet<AstNode> list;
if (!referenceDict.TryGetValue(entity, out list)) {
list = new HashSet<AstNode>();
referenceDict.Add(entity, list);
}
list.Add(node);
}
}
);
file.CreateResolver().ApplyNavigator(navigator);
}
}
Console.WriteLine("For each entity, find all references...");
Stopwatch w = Stopwatch.StartNew();
foreach (var project in solution.Projects) {
foreach (var type in project.Compilation.MainAssembly.GetAllTypeDefinitions()) {
TestFindReferences(type);
foreach (IMember m in type.Members) {
TestFindReferences(m);
}
Console.Write('.');
}
}
w.Stop();
Console.WriteLine("FindReferencesConsistencyCheck is done ({0}).", w.Elapsed);
PrintTimingsPerEntityType();
}
bool IgnoreEntity(IEntity entity)
{
return false;
//return entity.FullName != "ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition.parts";
}
Dictionary<SymbolKind, TimeSpan> timings = new Dictionary<SymbolKind, TimeSpan>();
Dictionary<SymbolKind, int> entityCount = new Dictionary<SymbolKind, int>();
void TestFindReferences(IEntity entity)
{
if (IgnoreEntity(entity))
return;
FindReferences fr = new FindReferences();
fr.FindTypeReferencesEvenIfAliased = true;
Stopwatch w = new Stopwatch();
var searchScopes = fr.GetSearchScopes(entity);
foreach (var project in solution.Projects) {
w.Restart();
HashSet<AstNode> foundReferences = new HashSet<AstNode>();
var interestingFiles = new HashSet<CSharpFile>();
foreach (var searchScope in searchScopes) {
foreach (var unresolvedFile in fr.GetInterestingFiles(searchScope, project.Compilation)) {
var file = project.Files.Single(f => f.FileName == unresolvedFile.FileName);
Debug.Assert(file.UnresolvedTypeSystemForFile == unresolvedFile);
// Skip file if it doesn't contain the search term
if (searchScope.SearchTerm != null && file.OriginalText.IndexOf(searchScope.SearchTerm, StringComparison.Ordinal) < 0)
continue;
interestingFiles.Add(file);
}
}
foreach (var file in interestingFiles) {
fr.FindReferencesInFile(searchScopes, file.UnresolvedTypeSystemForFile, file.SyntaxTree, project.Compilation,
delegate(AstNode node, ResolveResult result) {
foundReferences.Add(node);
}, CancellationToken.None);
}
w.Stop();
if (timings.ContainsKey(entity.SymbolKind)) {
timings[entity.SymbolKind] += w.Elapsed;
} else {
timings[entity.SymbolKind] = w.Elapsed;
}
IEntity importedEntity = project.Compilation.Import(entity);
HashSet<AstNode> expectedReferences;
if (importedEntity == null || !referenceDict.TryGetValue(importedEntity, out expectedReferences)) {
if (foundReferences.Any()) {
// There aren't any expected references stored, but we found some references anyways:
Console.WriteLine();
Console.WriteLine("Entity not in reference dictionary: " + entity);
}
return;
}
if (foundReferences.Except(expectedReferences).Any()) {
Console.WriteLine();
Console.WriteLine("Reference mismatch for " + entity + ":");
var n = foundReferences.Except(expectedReferences).First();
Console.WriteLine("Found unexpected reference " + n + " (" + n.GetRegion() + ")");
}
if (expectedReferences.Except(foundReferences).Any()) {
Console.WriteLine();
Console.WriteLine("Reference mismatch for " + entity + ":");
var n = expectedReferences.Except(foundReferences).First();
Console.WriteLine("Did not find expected reference " + n + " (" + n.GetRegion() + ")");
}
}
if (entityCount.ContainsKey(entity.SymbolKind)) {
entityCount[entity.SymbolKind]++;
} else {
entityCount[entity.SymbolKind] = 1;
}
}
void PrintTimingsPerEntityType()
{
foreach (var pair in entityCount) {
Console.WriteLine("{0} - avg. {1} per entity", pair.Key, TimeSpan.FromSeconds(timings[pair.Key].TotalSeconds / pair.Value));
}
}
}
}