~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.ConsistencyCheck/FindReferencesConsistencyCheck.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Diagnostics;
 
22
using System.Linq;
 
23
using System.Threading;
 
24
using ICSharpCode.NRefactory.CSharp;
 
25
using ICSharpCode.NRefactory.CSharp.Resolver;
 
26
using ICSharpCode.NRefactory.Semantics;
 
27
using ICSharpCode.NRefactory.TypeSystem;
 
28
 
 
29
namespace ICSharpCode.NRefactory.ConsistencyCheck
 
30
{
 
31
        public class FindReferencesConsistencyCheck
 
32
        {
 
33
                readonly Solution solution;
 
34
                Dictionary<IEntity, HashSet<AstNode>> referenceDict = new Dictionary<IEntity, HashSet<AstNode>>();
 
35
                
 
36
                public FindReferencesConsistencyCheck(Solution solution)
 
37
                {
 
38
                        this.solution = solution;
 
39
                }
 
40
                
 
41
                public void Run()
 
42
                {
 
43
                        using (new Timer("Finding referenced entities... ")) {
 
44
                                foreach (var file in solution.AllFiles) {
 
45
                                        var navigator = new FindReferencedEntities(
 
46
                                                delegate (AstNode node, IEntity entity) {
 
47
                                                        if (node == null)
 
48
                                                                throw new ArgumentNullException("node");
 
49
                                                        if (entity == null)
 
50
                                                                throw new ArgumentNullException("entity");
 
51
                                                        
 
52
                                                        if (!IgnoreEntity(entity)) {
 
53
                                                                HashSet<AstNode> list;
 
54
                                                                if (!referenceDict.TryGetValue(entity, out list)) {
 
55
                                                                        list = new HashSet<AstNode>();
 
56
                                                                        referenceDict.Add(entity, list);
 
57
                                                                }
 
58
                                                                list.Add(node);
 
59
                                                        }
 
60
                                                }
 
61
                                        );
 
62
                                        file.CreateResolver().ApplyNavigator(navigator);
 
63
                                }
 
64
                        }
 
65
                        Console.WriteLine("For each entity, find all references...");
 
66
                        Stopwatch w = Stopwatch.StartNew();
 
67
                        foreach (var project in solution.Projects) {
 
68
                                foreach (var type in project.Compilation.MainAssembly.GetAllTypeDefinitions()) {
 
69
                                        TestFindReferences(type);
 
70
                                        foreach (IMember m in type.Members) {
 
71
                                                TestFindReferences(m);
 
72
                                        }
 
73
                                        Console.Write('.');
 
74
                                }
 
75
                        }
 
76
                        w.Stop();
 
77
                        Console.WriteLine("FindReferencesConsistencyCheck is done ({0}).", w.Elapsed);
 
78
                        PrintTimingsPerEntityType();
 
79
                }
 
80
                
 
81
                bool IgnoreEntity(IEntity entity)
 
82
                {
 
83
                        return false;
 
84
                        //return entity.FullName != "ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition.parts";
 
85
                }
 
86
                
 
87
                Dictionary<EntityType, TimeSpan> timings = new Dictionary<EntityType, TimeSpan>();
 
88
                Dictionary<EntityType, int> entityCount = new Dictionary<EntityType, int>();
 
89
                
 
90
                void TestFindReferences(IEntity entity)
 
91
                {
 
92
                        if (IgnoreEntity(entity))
 
93
                                return;
 
94
                        FindReferences fr = new FindReferences();
 
95
                        fr.FindTypeReferencesEvenIfAliased = true;
 
96
                        
 
97
                        Stopwatch w = new Stopwatch();
 
98
                        var searchScopes = fr.GetSearchScopes(entity);
 
99
                        foreach (var project in solution.Projects) {
 
100
                                w.Restart();
 
101
                                HashSet<AstNode> foundReferences = new HashSet<AstNode>();
 
102
                                var interestingFiles = new HashSet<CSharpFile>();
 
103
                                foreach (var searchScope in searchScopes) {
 
104
                                        foreach (var unresolvedFile in fr.GetInterestingFiles(searchScope, project.Compilation)) {
 
105
                                                var file = project.Files.Single(f => f.FileName == unresolvedFile.FileName);
 
106
                                                Debug.Assert(file.UnresolvedTypeSystemForFile == unresolvedFile);
 
107
                                                
 
108
                                                // Skip file if it doesn't contain the search term
 
109
                                                if (searchScope.SearchTerm != null && file.OriginalText.IndexOf(searchScope.SearchTerm, StringComparison.Ordinal) < 0)
 
110
                                                        continue;
 
111
                                                
 
112
                                                interestingFiles.Add(file);
 
113
                                        }
 
114
                                }
 
115
                                foreach (var file in interestingFiles) {
 
116
                                        fr.FindReferencesInFile(searchScopes, file.UnresolvedTypeSystemForFile, file.SyntaxTree, project.Compilation,
 
117
                                                                delegate(AstNode node, ResolveResult result) {
 
118
                                                                        foundReferences.Add(node);
 
119
                                                                }, CancellationToken.None);
 
120
                                }
 
121
                                w.Stop();
 
122
                                if (timings.ContainsKey(entity.EntityType)) {
 
123
                                        timings[entity.EntityType] += w.Elapsed;
 
124
                                } else {
 
125
                                        timings[entity.EntityType] = w.Elapsed;
 
126
                                }
 
127
                                
 
128
                                
 
129
                                IEntity importedEntity = project.Compilation.Import(entity);
 
130
                                
 
131
                                HashSet<AstNode> expectedReferences;
 
132
                                if (importedEntity == null || !referenceDict.TryGetValue(importedEntity, out expectedReferences)) {
 
133
                                        if (foundReferences.Any()) {
 
134
                                                // There aren't any expected references stored, but we found some references anyways:
 
135
                                                Console.WriteLine();
 
136
                                                Console.WriteLine("Entity not in reference dictionary: " + entity);
 
137
                                        }
 
138
                                        return;
 
139
                                }
 
140
                                if (foundReferences.Except(expectedReferences).Any()) {
 
141
                                        Console.WriteLine();
 
142
                                        Console.WriteLine("Reference mismatch for " + entity + ":");
 
143
                                        var n = foundReferences.Except(expectedReferences).First();
 
144
                                        Console.WriteLine("Found unexpected reference " + n + " (" + n.GetRegion() + ")");
 
145
                                }
 
146
                                if (expectedReferences.Except(foundReferences).Any()) {
 
147
                                        Console.WriteLine();
 
148
                                        Console.WriteLine("Reference mismatch for " + entity + ":");
 
149
                                        var n = expectedReferences.Except(foundReferences).First();
 
150
                                        Console.WriteLine("Did not find expected reference " + n + " (" + n.GetRegion() + ")");
 
151
                                }
 
152
                        }
 
153
                        
 
154
                        if (entityCount.ContainsKey(entity.EntityType)) {
 
155
                                entityCount[entity.EntityType]++;
 
156
                        } else {
 
157
                                entityCount[entity.EntityType] = 1;
 
158
                        }
 
159
                }
 
160
                
 
161
                void PrintTimingsPerEntityType()
 
162
                {
 
163
                        foreach (var pair in entityCount) {
 
164
                                Console.WriteLine("{0} - avg. {1} per entity", pair.Key, TimeSpan.FromSeconds(timings[pair.Key].TotalSeconds / pair.Value));
 
165
                        }
 
166
                }
 
167
        }
 
168
}