Browse Source

fixing some exceptions while loading and analyzing assemblies

pull/18/head
Siegfried Pammer 14 years ago
parent
commit
2b56c5d934
  1. 28
      src/AddIns/Analysis/CodeQuality/Engine/AssemblyAnalyzer.cs
  2. 41
      src/AddIns/Analysis/CodeQuality/Engine/ILAnalyzer.cs

28
src/AddIns/Analysis/CodeQuality/Engine/AssemblyAnalyzer.cs

@ -8,8 +8,10 @@ using System.Diagnostics; @@ -8,8 +8,10 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using ICSharpCode.CodeQuality.Engine.Dom;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Gui;
using Mono.Cecil;
@ -46,7 +48,7 @@ namespace ICSharpCode.CodeQuality.Engine @@ -46,7 +48,7 @@ namespace ICSharpCode.CodeQuality.Engine
public ReadOnlyCollection<AssemblyNode> Analyze()
{
var loadedAssemblies = LoadAssemblies().ToArray();
IUnresolvedAssembly[] loadedAssemblies = LoadAssemblies().ToArray();
compilation = new SimpleCompilation(loadedAssemblies.First(), loadedAssemblies.Skip(1));
assemblyMappings = new Dictionary<IAssembly, AssemblyNode>();
@ -227,9 +229,14 @@ namespace ICSharpCode.CodeQuality.Engine @@ -227,9 +229,14 @@ namespace ICSharpCode.CodeQuality.Engine
void AddRelationshipsForAttributes(IList<IAttribute> attributes, NodeBase node)
{
foreach (var attr in attributes) {
if (attr.Constructor != null)
node.AddRelationship(methodMappings[attr.Constructor]);
try {
foreach (var attr in attributes) {
if (attr.Constructor != null)
node.AddRelationship(methodMappings[attr.Constructor]);
}
} catch (NotSupportedException nse) {
// HACK : workaround for bug in NR5's attribute blob parser.
LoggingService.DebugFormatted("CQA: Skipping attributes of: {0}\r\nException:\r\n{1}", node.Name, nse);
}
}
@ -267,7 +274,7 @@ namespace ICSharpCode.CodeQuality.Engine @@ -267,7 +274,7 @@ namespace ICSharpCode.CodeQuality.Engine
foreach (var file in fileNames.Distinct(StringComparer.OrdinalIgnoreCase))
assemblies.Add(resolver.LoadAssemblyFile(file));
foreach (var asm in assemblies.ToArray())
assemblies.AddRange(asm.Modules.SelectMany(m => m.AssemblyReferences).Select(r => resolver.Resolve(r)));
assemblies.AddRange(asm.Modules.SelectMany(m => m.AssemblyReferences).Select(r => resolver.TryResolve(r)).Where(r => r != null));
return assemblies.Distinct().Select(asm => loader.LoadAssembly(asm));
}
@ -319,6 +326,17 @@ namespace ICSharpCode.CodeQuality.Engine @@ -319,6 +326,17 @@ namespace ICSharpCode.CodeQuality.Engine
RegisterAssembly(assembly);
return assembly;
}
public AssemblyDefinition TryResolve(AssemblyNameReference reference)
{
try {
return Resolve(reference);
} catch (AssemblyResolutionException are) {
LoggingService.DebugFormatted("CQA: Skipping assembly reference: {0}\r\nException:\r\n{1}", reference, are);
TaskService.Add(new Task(null, are.Message, 0, 0, TaskType.Warning));
return null;
}
}
}
}
}

41
src/AddIns/Analysis/CodeQuality/Engine/ILAnalyzer.cs

@ -5,6 +5,7 @@ using System; @@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.CodeQuality.Engine.Dom;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.TypeSystem;
using Mono.Cecil;
using Mono.Cecil.Cil;
@ -42,26 +43,30 @@ namespace ICSharpCode.CodeQuality.Engine @@ -42,26 +43,30 @@ namespace ICSharpCode.CodeQuality.Engine
var operand = ReadOperand(instruction);
if (operand is MethodReference) {
var md = ((MethodReference)operand).Resolve();
if (md != null && assemblies.Contains(md.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(md)) {
if (md.IsGetter || md.IsSetter) {
var propertyNode = mappings.propertyMappings[(IProperty)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(propertyNode);
} else if (md.IsAddOn || md.IsRemoveOn) {
var eventNode = mappings.eventMappings[(IEvent)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(eventNode);
} else {
var methodNode = mappings.methodMappings[(IMethod)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(methodNode);
try {
if (operand is MethodReference) {
var md = ((MethodReference)operand).Resolve();
if (md != null && assemblies.Contains(md.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(md)) {
if (md.IsGetter || md.IsSetter) {
var propertyNode = mappings.propertyMappings[(IProperty)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(propertyNode);
} else if (md.IsAddOn || md.IsRemoveOn) {
var eventNode = mappings.eventMappings[(IEvent)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(eventNode);
} else {
var methodNode = mappings.methodMappings[(IMethod)mappings.cecilMappings[md]];
analyzedNode.AddRelationship(methodNode);
}
}
} else if (operand is FieldReference) {
var fd = ((FieldReference)operand).Resolve();
if (fd != null && assemblies.Contains(fd.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(fd)) {
var fieldNode = mappings.fieldMappings[(IField)mappings.cecilMappings[fd]];
analyzedNode.AddRelationship(fieldNode);
}
}
} else if (operand is FieldReference) {
var fd = ((FieldReference)operand).Resolve();
if (fd != null && assemblies.Contains(fd.DeclaringType.Module.Assembly) && mappings.cecilMappings.ContainsKey(fd)) {
var fieldNode = mappings.fieldMappings[(IField)mappings.cecilMappings[fd]];
analyzedNode.AddRelationship(fieldNode);
}
} catch (AssemblyResolutionException are) {
LoggingService.InfoFormatted("CQA: Skipping operand reference: {0}\r\nException:\r\n{1}", operand, are);
}
}
}

Loading…
Cancel
Save