Browse Source

Add FindReferencesConsistencyCheck.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
194b33438b
  1. 2
      ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  2. 5
      ICSharpCode.NRefactory.ConsistencyCheck/CSharpProject.cs
  3. 160
      ICSharpCode.NRefactory.ConsistencyCheck/FindReferencesConsistencyCheck.cs
  4. 1
      ICSharpCode.NRefactory.ConsistencyCheck/ICSharpCode.NRefactory.ConsistencyCheck.csproj
  5. 29
      ICSharpCode.NRefactory.ConsistencyCheck/Program.cs

2
ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs

@ -292,7 +292,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -292,7 +292,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
where typeDef.IsDerivedFrom(referencedTypeDefinition)
from part in typeDef.Parts
select part.ParsedFile
).OfType<CSharpParsedFile>();
).OfType<CSharpParsedFile>().Distinct();
}
#endregion

5
ICSharpCode.NRefactory.ConsistencyCheck/CSharpProject.cs

@ -134,6 +134,11 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck @@ -134,6 +134,11 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
{
return string.Format("[CSharpProject AssemblyName={0}]", AssemblyName);
}
public CSharpFile GetFile(string fileName)
{
return Files.Single(f => f.FileName == fileName);
}
}
public class ProjectReference : IAssemblyReference

160
ICSharpCode.NRefactory.ConsistencyCheck/FindReferencesConsistencyCheck.cs

@ -0,0 +1,160 @@ @@ -0,0 +1,160 @@
// Copyright (c) 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");
HashSet<AstNode> list;
if (!referenceDict.TryGetValue(entity, out list)) {
list = new HashSet<AstNode>();
referenceDict.Add(entity, list);
}
list.Add(node);
}
);
var resolver = new CSharpAstResolver(file.Project.Compilation, file.CompilationUnit, file.ParsedFile);
resolver.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();
}
Dictionary<EntityType, TimeSpan> timings = new Dictionary<EntityType, TimeSpan>();
Dictionary<EntityType, int> entityCount = new Dictionary<EntityType, int>();
void TestFindReferences(IEntity entity)
{
FindReferences fr = new FindReferences();
fr.FindTypeReferencesEvenIfAliased = true;
HashSet<AstNode> foundReferences = new HashSet<AstNode>();
Stopwatch w = new Stopwatch();
var searchScopes = fr.GetSearchScopes(entity);
foreach (var project in solution.Projects) {
w.Restart();
var interestingFiles = new HashSet<CSharpFile>();
foreach (var searchScope in searchScopes) {
foreach (var parsedFile in fr.GetInterestingFiles(searchScope, project.Compilation)) {
var file = project.GetFile(parsedFile.FileName);
Debug.Assert(file.ParsedFile == parsedFile);
// Skip file if it doesn't contain the search term
if (searchScope.SearchTerm != null && file.Content.IndexOf(searchScope.SearchTerm, 0, file.Content.TextLength, StringComparison.Ordinal) < 0)
continue;
interestingFiles.Add(file);
}
}
foreach (var file in interestingFiles) {
fr.FindReferencesInFile(searchScopes, file.ParsedFile, file.CompilationUnit, project.Compilation,
delegate(AstNode node, ResolveResult result) {
foundReferences.Add(node);
}, CancellationToken.None);
}
w.Stop();
if (timings.ContainsKey(entity.EntityType)) {
timings[entity.EntityType] += w.Elapsed;
} else {
timings[entity.EntityType] = 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.StartLocation + ")");
}
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.StartLocation + ")");
}
}
if (entityCount.ContainsKey(entity.EntityType)) {
entityCount[entity.EntityType]++;
} else {
entityCount[entity.EntityType] = 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));
}
}
}
}

1
ICSharpCode.NRefactory.ConsistencyCheck/ICSharpCode.NRefactory.ConsistencyCheck.csproj

@ -48,6 +48,7 @@ @@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CSharpProject.cs" />
<Compile Include="FindReferencesConsistencyCheck.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomizedOrderResolverTest.cs" />

29
ICSharpCode.NRefactory.ConsistencyCheck/Program.cs

@ -57,8 +57,9 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck @@ -57,8 +57,9 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
solution.Projects.Count);
//RunTestOnAllFiles("Roundtripping test", RoundtripTest.RunTest);
RunTestOnAllFiles("Resolver test", ResolverTest.RunTest);
RunTestOnAllFiles("Resolver test (randomized order)", RandomizedOrderResolverTest.RunTest);
//RunTestOnAllFiles("Resolver test", ResolverTest.RunTest);
//RunTestOnAllFiles("Resolver test (randomized order)", RandomizedOrderResolverTest.RunTest);
new FindReferencesConsistencyCheck(solution).Run();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
@ -79,20 +80,20 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck @@ -79,20 +80,20 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
{
return assemblyDict.GetOrAdd(assemblyFileName, file => new CecilLoader().LoadAssemblyFile(file));
}
}
sealed class Timer : IDisposable
{
Stopwatch w = Stopwatch.StartNew();
sealed class Timer : IDisposable
public Timer(string title)
{
Stopwatch w = Stopwatch.StartNew();
public Timer(string title)
{
Console.Write(title);
}
public void Dispose()
{
Console.WriteLine(w.Elapsed);
}
Console.Write(title);
}
public void Dispose()
{
Console.WriteLine(w.Elapsed);
}
}
}
Loading…
Cancel
Save