Browse Source

Merge branch 'master' of git://github.com/icsharpcode/ILSpy

pull/205/head
Pent Ploompuu 14 years ago
parent
commit
ac8a4314c8
  1. 29
      ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs
  2. 4
      ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs
  3. 20
      ICSharpCode.Decompiler/Tests/ExceptionHandling.cs
  4. 4
      ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj
  5. 22
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BAML format.txt
  6. 117
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlRecordType.cs
  7. 93
      ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs
  8. 1
      ILSpy/ILSpy.csproj
  9. 153
      ILSpy/TreeNodes/Analyzer/AnalyzedEventFiredByTreeNode.cs
  10. 6
      ILSpy/TreeNodes/Analyzer/AnalyzedEventTreeNode.cs
  11. 16
      ILSpy/TreeNodes/Analyzer/AnalyzedVirtualMethodUsedByTreeNode.cs
  12. 4
      ILSpy/TreeNodes/Analyzer/Helpers.cs

29
ICSharpCode.Decompiler/ILAst/ILAstOptimizer.cs

@ -586,16 +586,37 @@ namespace ICSharpCode.Decompiler.ILAst
// This ensures that a single IL variable is a single C# variable (gets assigned only one name) // This ensures that a single IL variable is a single C# variable (gets assigned only one name)
// The DeclareVariables transformation might then split up the C# variable again if it is used indendently in two separate scopes. // The DeclareVariables transformation might then split up the C# variable again if it is used indendently in two separate scopes.
Dictionary<VariableDefinition, ILVariable> dict = new Dictionary<VariableDefinition, ILVariable>(); Dictionary<VariableDefinition, ILVariable> dict = new Dictionary<VariableDefinition, ILVariable>();
foreach (ILExpression expr in method.GetSelfAndChildrenRecursive<ILExpression>()) { ReplaceVariables(
ILVariable v = expr.Operand as ILVariable; method,
if (v != null && v.OriginalVariable != null) { delegate(ILVariable v) {
if (v.OriginalVariable == null)
return v;
ILVariable combinedVariable; ILVariable combinedVariable;
if (!dict.TryGetValue(v.OriginalVariable, out combinedVariable)) { if (!dict.TryGetValue(v.OriginalVariable, out combinedVariable)) {
dict.Add(v.OriginalVariable, v); dict.Add(v.OriginalVariable, v);
combinedVariable = v; combinedVariable = v;
} }
expr.Operand = combinedVariable; return combinedVariable;
});
}
public static void ReplaceVariables(ILNode node, Func<ILVariable, ILVariable> variableMapping)
{
ILExpression expr = node as ILExpression;
if (expr != null) {
ILVariable v = expr.Operand as ILVariable;
if (v != null)
expr.Operand = variableMapping(v);
foreach (ILExpression child in expr.Arguments)
ReplaceVariables(child, variableMapping);
} else {
var catchBlock = node as ILTryCatchBlock.CatchBlock;
if (catchBlock != null && catchBlock.ExceptionVariable != null) {
catchBlock.ExceptionVariable = variableMapping(catchBlock.ExceptionVariable);
} }
foreach (ILNode child in node.GetChildren())
ReplaceVariables(child, variableMapping);
} }
} }

4
ICSharpCode.Decompiler/ILAst/PeepholeTransform.cs

@ -523,8 +523,8 @@ namespace ICSharpCode.Decompiler.ILAst
if (recombineVariable) { if (recombineVariable) {
// Split local variable, unsplit these two instances // Split local variable, unsplit these two instances
foreach (var ilExpression in method.GetSelfAndChildrenRecursive<ILExpression>(expression => expression.Operand == nextExpr.Operand)) // replace nextExpr.Operand with exprInit.Operand
ilExpression.Operand = exprInit.Operand; ReplaceVariables(method, oldVar => oldVar == nextExpr.Operand ? (ILVariable)exprInit.Operand : oldVar);
} }
switch (loadInstruction) { switch (loadInstruction) {

20
ICSharpCode.Decompiler/Tests/ExceptionHandling.cs

@ -105,4 +105,24 @@ public class ExceptionHandling
cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource = new CancellationTokenSource();
} }
} }
public void TwoCatchBlocksWithSameVariable()
{
try
{
Console.WriteLine("Try1");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
Console.WriteLine("Try2");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
} }

4
ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj

@ -14,6 +14,7 @@
<NoStdLib>False</NoStdLib> <NoStdLib>False</NoStdLib>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' "> <PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
@ -130,5 +131,8 @@
<Private>False</Private> <Private>False</Private>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="Ricciolo.StylesExplorer.MarkupReflection\BAML format.txt" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project> </Project>

22
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BAML format.txt

@ -0,0 +1,22 @@
BAML format description
A BAML stream consists of a header and sequential records that contain the data.
The Header
The header is 28 bytes long. The first four bytes are a little endian integer containing the length of the preamble in bytes. The preamble is the UTF-16 string "MSBAML", followed by three integers with the value 0x60000.
Record format:
In general a record consists of a type byte and its content. Some records also have a field, following the type byte, containing the remaining length of the record, encoded as 7-bit encoded integer.
Record Types:
DocumentStart (= 0x01):
The document start (after the type byte) is 6 Bytes long. Usually 00 FF FF FF FF 00. These bytes can be safely ignored.
AssemblyInfo (= 0xC1):
The length field is followed by a 2 byte long ID. The ID is followed by a string containing the assembly name.

117
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlRecordType.cs

@ -9,64 +9,63 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
internal enum BamlRecordType : byte internal enum BamlRecordType : byte
{ {
AssemblyInfo = 0x1c, Unknown,
AttributeInfo = 0x1f, DocumentStart,
ClrEvent = 0x13, DocumentEnd,
Comment = 0x17, ElementStart,
ConnectionId = 0x2d, ElementEnd,
ConstructorParametersEnd = 0x2b, Property,
ConstructorParametersStart = 0x2a, PropertyCustom,
ConstructorParameterType = 0x2c, PropertyComplexStart,
ContentProperty = 0x2e, PropertyComplexEnd,
DefAttribute = 0x19, PropertyArrayStart,
DefAttributeKeyString = 0x26, PropertyArrayEnd,
DefAttributeKeyType = 0x27, PropertyListStart,
DeferableContentStart = 0x25, PropertyListEnd,
DefTag = 0x18, PropertyDictionaryStart,
DocumentEnd = 2, PropertyDictionaryEnd,
DocumentStart = 1, LiteralContent,
ElementEnd = 4, Text,
ElementStart = 3, TextWithConverter,
EndAttributes = 0x1a, RoutedEvent,
KeyElementEnd = 0x29, ClrEvent,
KeyElementStart = 40, XmlnsProperty,
LastRecordType = 0x39, XmlAttribute,
LineNumberAndPosition = 0x35, ProcessingInstruction,
LinePosition = 0x36, Comment,
LiteralContent = 15, DefTag,
NamedElementStart = 0x2f, DefAttribute,
OptimizedStaticResource = 0x37, EndAttributes,
PIMapping = 0x1b, PIMapping,
PresentationOptionsAttribute = 0x34, AssemblyInfo,
ProcessingInstruction = 0x16, TypeInfo,
Property = 5, TypeSerializerInfo,
PropertyArrayEnd = 10, AttributeInfo,
PropertyArrayStart = 9, StringInfo,
PropertyComplexEnd = 8, PropertyStringReference,
PropertyComplexStart = 7, PropertyTypeReference,
PropertyCustom = 6, PropertyWithExtension,
PropertyDictionaryEnd = 14, PropertyWithConverter,
PropertyDictionaryStart = 13, DeferableContentStart,
PropertyListEnd = 12, DefAttributeKeyString,
PropertyListStart = 11, DefAttributeKeyType,
PropertyStringReference = 0x21, KeyElementStart,
PropertyTypeReference = 0x22, KeyElementEnd,
PropertyWithConverter = 0x24, ConstructorParametersStart,
PropertyWithExtension = 0x23, ConstructorParametersEnd,
PropertyWithStaticResourceId = 0x38, ConstructorParameterType,
RoutedEvent = 0x12, ConnectionId,
StaticResourceEnd = 0x31, ContentProperty,
StaticResourceId = 50, NamedElementStart,
StaticResourceStart = 0x30, StaticResourceStart,
StringInfo = 0x20, StaticResourceEnd,
Text = 0x10, StaticResourceId,
TextWithConverter = 0x11, TextWithId,
TextWithId = 0x33, PresentationOptionsAttribute,
TypeInfo = 0x1d, LineNumberAndPosition,
TypeSerializerInfo = 30, LinePosition,
Unknown = 0, OptimizedStaticResource,
XmlAttribute = 0x15, PropertyWithStaticResourceId,
XmlnsProperty = 20 LastRecordType
} }
} }

93
ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs

@ -21,10 +21,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
#region Variables #region Variables
private BamlBinaryReader reader; private BamlBinaryReader reader;
private Hashtable assemblyTable = new Hashtable(); private Dictionary<short, string> assemblyTable = new Dictionary<short, string>();
private Hashtable stringTable = new Hashtable(); private Dictionary<short, string> stringTable = new Dictionary<short, string>();
private Hashtable typeTable = new Hashtable(); private Dictionary<short, TypeDeclaration> typeTable = new Dictionary<short, TypeDeclaration>();
private Hashtable propertyTable = new Hashtable(); private Dictionary<short, PropertyDeclaration> propertyTable = new Dictionary<short, PropertyDeclaration>();
private readonly ITypeResolver _resolver; private readonly ITypeResolver _resolver;
@ -247,9 +247,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
do do
{ {
currentType = (BamlRecordType)reader.ReadByte(); ReadRecordType();
//Debug.WriteLine(currentType); if (currentType == BamlRecordType.DocumentEnd)
if (currentType == BamlRecordType.DocumentEnd) break; break;
long position = reader.BaseStream.Position; long position = reader.BaseStream.Position;
@ -257,10 +257,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
ProcessNext(); ProcessNext();
if (bytesToSkip > 0) if (bytesToSkip > 0)
{ // jump to the end of the record
reader.BaseStream.Position = position + bytesToSkip; reader.BaseStream.Position = position + bytesToSkip;
} }
}
//while (currentType != BamlRecordType.DocumentEnd); //while (currentType != BamlRecordType.DocumentEnd);
while (nodes.Count == 0 || (currentType != BamlRecordType.ElementEnd) || complexPropertyOpened > 0); while (nodes.Count == 0 || (currentType != BamlRecordType.ElementEnd) || complexPropertyOpened > 0);
@ -277,6 +276,23 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
} }
} }
void ReadRecordType()
{
byte type = reader.ReadByte();
if (type < 0)
currentType = BamlRecordType.DocumentEnd;
else
currentType = (BamlRecordType)type;
if (currentType.ToString().EndsWith("End"))
Debug.Unindent();
Debug.WriteLine(currentType);
if (currentType.ToString().StartsWith("Start"))
Debug.Indent();
}
private bool SetNextNode() private bool SetNextNode()
{ {
while (nodes.Count > 0) while (nodes.Count > 0)
@ -305,12 +321,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
switch (currentType) switch (currentType)
{ {
case BamlRecordType.DocumentStart: case BamlRecordType.DocumentStart:
{ reader.ReadBytes(6);
reader.ReadBoolean();
reader.ReadInt32();
reader.ReadBoolean();
break; break;
}
case BamlRecordType.DocumentEnd: case BamlRecordType.DocumentEnd:
break; break;
case BamlRecordType.ElementStart: case BamlRecordType.ElementStart:
@ -407,6 +419,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
case BamlRecordType.TextWithConverter: case BamlRecordType.TextWithConverter:
this.ReadTextWithConverter(); this.ReadTextWithConverter();
break; break;
case BamlRecordType.TextWithId:
this.ReadTextWithId();
break;
case BamlRecordType.PropertyWithStaticResourceId: case BamlRecordType.PropertyWithStaticResourceId:
this.ReadPropertyWithStaticResourceIdentifier(); this.ReadPropertyWithStaticResourceIdentifier();
break; break;
@ -435,10 +450,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
this.ReadPresentationOptionsAttribute(); this.ReadPresentationOptionsAttribute();
break; break;
default: default:
throw new NotImplementedException("UnsupportedNode: " + currentType);
break; break;
} }
} }
void ReadTextWithId()
{
short textId = reader.ReadInt16();
string text = stringTable[textId];
nodes.Enqueue(new XmlBamlText(text));
}
private void ComputeBytesToSkip() private void ComputeBytesToSkip()
{ {
bytesToSkip = 0; bytesToSkip = 0;
@ -450,6 +473,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
case BamlRecordType.Property: case BamlRecordType.Property:
case BamlRecordType.PropertyCustom: case BamlRecordType.PropertyCustom:
case BamlRecordType.Text: case BamlRecordType.Text:
case BamlRecordType.TextWithId:
case BamlRecordType.TextWithConverter: case BamlRecordType.TextWithConverter:
case BamlRecordType.XmlnsProperty: case BamlRecordType.XmlnsProperty:
case BamlRecordType.DefAttribute: case BamlRecordType.DefAttribute:
@ -586,7 +610,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
PropertyDeclaration declaration; PropertyDeclaration declaration;
if (identifier >= 0) if (identifier >= 0)
{ {
declaration = (PropertyDeclaration)this.propertyTable[identifier]; declaration = this.propertyTable[identifier];
} }
else else
{ {
@ -603,7 +627,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
if (identifier >= 0) if (identifier >= 0)
{ {
PropertyDeclaration declaration = (PropertyDeclaration)this.propertyTable[identifier]; PropertyDeclaration declaration = this.propertyTable[identifier];
return declaration; return declaration;
} }
else else
@ -752,35 +776,23 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
throw new ArgumentException(); throw new ArgumentException();
Int32Collection ints = new Int32Collection(capacity); Int32Collection ints = new Int32Collection(capacity);
switch (type) switch (type) {
{
case IntegerCollectionType.Byte: case IntegerCollectionType.Byte:
for (int i = 0; i < capacity; i++) for (int i = 0; i < capacity; i++)
{
ints.Add(reader.ReadByte()); ints.Add(reader.ReadByte());
}
return ints; return ints;
case IntegerCollectionType.UShort: case IntegerCollectionType.UShort:
for (int j = 0; j < capacity; j++) for (int j = 0; j < capacity; j++)
{
ints.Add(reader.ReadUInt16()); ints.Add(reader.ReadUInt16());
}
return ints; return ints;
case IntegerCollectionType.Integer: case IntegerCollectionType.Integer:
for (int k = 0; k < capacity; k++) for (int k = 0; k < capacity; k++)
{ ints.Add(reader.ReadInt32());
int num7 = reader.ReadInt32();
ints.Add(num7);
}
return ints; return ints;
case IntegerCollectionType.Consecutive: case IntegerCollectionType.Consecutive:
for (int m = reader.ReadInt32(); m < capacity; m++) int start = reader.ReadInt32();
{ for (int m = start; m < capacity + start; m++)
ints.Add(m); ints.Add(m);
}
return ints; return ints;
} }
throw new ArgumentException(); throw new ArgumentException();
@ -906,7 +918,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
pd = new PropertyDeclaration("Name", XamlTypeDeclaration); pd = new PropertyDeclaration("Name", XamlTypeDeclaration);
break; break;
default: default:
string recordName = (string)this.stringTable[identifier]; string recordName = this.stringTable[identifier];
if (recordName != "Key") throw new NotSupportedException(recordName); if (recordName != "Key") throw new NotSupportedException(recordName);
pd = new PropertyDeclaration(recordName, XamlTypeDeclaration); pd = new PropertyDeclaration(recordName, XamlTypeDeclaration);
@ -926,7 +938,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
int position = reader.ReadInt32(); int position = reader.ReadInt32();
//bool shared = reader.ReadBoolean(); //bool shared = reader.ReadBoolean();
//bool sharedSet = reader.ReadBoolean(); //bool sharedSet = reader.ReadBoolean();
string text = (string)this.stringTable[num]; string text = this.stringTable[num];
if (text == null) if (text == null)
throw new NotSupportedException(); throw new NotSupportedException();
@ -963,7 +975,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
string[] textArray = new string[(uint)reader.ReadInt16()]; string[] textArray = new string[(uint)reader.ReadInt16()];
for (int i = 0; i < textArray.Length; i++) for (int i = 0; i < textArray.Length; i++)
{ {
textArray[i] = (string)this.assemblyTable[reader.ReadInt16()]; textArray[i] = this.assemblyTable[reader.ReadInt16()];
} }
XmlNamespaceCollection namespaces = elements.Peek().Namespaces; XmlNamespaceCollection namespaces = elements.Peek().Namespaces;
@ -1045,6 +1057,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
private void ReadPropertyComplexEnd() private void ReadPropertyComplexEnd()
{ {
if (!(elements.Peek() is XmlBamlPropertyElement))
throw new InvalidCastException();
XmlBamlPropertyElement propertyElement = (XmlBamlPropertyElement) elements.Peek(); XmlBamlPropertyElement propertyElement = (XmlBamlPropertyElement) elements.Peek();
CloseElement(); CloseElement();
@ -1157,7 +1172,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
private void ReadElementStart() private void ReadElementStart()
{ {
short identifier = reader.ReadInt16(); short identifier = reader.ReadInt16();
reader.ReadByte(); sbyte flags = reader.ReadSByte();
if (flags < 0 || flags > 3)
throw new NotImplementedException();
TypeDeclaration declaration = GetTypeDeclaration(identifier); TypeDeclaration declaration = GetTypeDeclaration(identifier);
XmlBamlElement element; XmlBamlElement element;
@ -1546,7 +1563,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{ {
TypeDeclaration declaration; TypeDeclaration declaration;
if (identifier >= 0) if (identifier >= 0)
declaration = (TypeDeclaration)this.typeTable[identifier]; declaration = this.typeTable[identifier];
else else
declaration = KnownInfo.KnownTypeTable[-identifier]; declaration = KnownInfo.KnownTypeTable[-identifier];
@ -1558,7 +1575,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
internal string GetAssembly(short identifier) internal string GetAssembly(short identifier)
{ {
return this.assemblyTable[identifier].ToString(); return this.assemblyTable[identifier];
} }
private XmlBamlNode CurrentNode private XmlBamlNode CurrentNode

1
ILSpy/ILSpy.csproj

@ -148,6 +148,7 @@
<Compile Include="TreeNodes\Analyzer\AnalyzedInterfacePropertyImplementedByTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedInterfacePropertyImplementedByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedInterfaceMethodImplementedByTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedInterfaceMethodImplementedByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedInterfaceEventImplementedByTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedInterfaceEventImplementedByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedEventFiredByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedPropertyAccessorTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedPropertyAccessorTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedTypeExposedByTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedTypeExposedByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedTypeExtensionMethodsTreeNode.cs" /> <Compile Include="TreeNodes\Analyzer\AnalyzedTypeExtensionMethodsTreeNode.cs" />

153
ILSpy/TreeNodes/Analyzer/AnalyzedEventFiredByTreeNode.cs

@ -0,0 +1,153 @@
// Copyright (c) 2011 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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ICSharpCode.TreeView;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
{
internal sealed class AnalyzedEventFiredByTreeNode : AnalyzerTreeNode
{
private readonly EventDefinition analyzedEvent;
private readonly FieldDefinition eventBackingField;
private readonly MethodDefinition eventFiringMethod;
private readonly ThreadingSupport threading;
private ConcurrentDictionary<MethodDefinition, int> foundMethods;
public AnalyzedEventFiredByTreeNode(EventDefinition analyzedEvent)
{
if (analyzedEvent == null)
throw new ArgumentNullException("analyzedEvent");
this.analyzedEvent = analyzedEvent;
this.threading = new ThreadingSupport();
this.LazyLoading = true;
this.eventBackingField = GetBackingField(analyzedEvent);
this.eventFiringMethod = analyzedEvent.EventType.Resolve().Methods.First(md => md.Name == "Invoke");
}
public override object Text
{
get { return "Raised By"; }
}
public override object Icon
{
get { return Images.Search; }
}
protected override void LoadChildren()
{
threading.LoadChildren(this, FetchChildren);
}
protected override void OnCollapsing()
{
if (threading.IsRunning) {
this.LazyLoading = true;
threading.Cancel();
this.Children.Clear();
}
}
private IEnumerable<SharpTreeNode> FetchChildren(CancellationToken ct)
{
foundMethods = new ConcurrentDictionary<MethodDefinition, int>();
foreach (var child in FindReferencesInType(analyzedEvent.DeclaringType)) {
yield return child;
}
foundMethods = null;
}
private IEnumerable<SharpTreeNode> FindReferencesInType(TypeDefinition type)
{
// HACK: in lieu of proper flow analysis, I'm going to use a simple heuristic
// If the method accesses the event's backing field, and calls invoke on a delegate
// with the same signature, then it is (most likely) raise the given event.
foreach (MethodDefinition method in type.Methods) {
bool readBackingField = false;
bool found = false;
if (!method.HasBody)
continue;
foreach (Instruction instr in method.Body.Instructions) {
Code code = instr.OpCode.Code;
if (code == Code.Ldfld || code == Code.Ldflda) {
FieldReference fr = instr.Operand as FieldReference;
if (fr != null && fr.Name == eventBackingField.Name && fr == eventBackingField) {
readBackingField = true;
}
}
if (readBackingField && (code == Code.Callvirt || code == Code.Call)) {
MethodReference mr = instr.Operand as MethodReference;
if (mr != null && mr.Name == eventFiringMethod.Name && mr.Resolve() == eventFiringMethod) {
found = true;
break;
}
}
}
method.Body = null;
if (found) {
MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
if (codeLocation != null && !HasAlreadyBeenFound(codeLocation)) {
yield return new AnalyzedMethodTreeNode(codeLocation);
}
}
}
}
private bool HasAlreadyBeenFound(MethodDefinition method)
{
return !foundMethods.TryAdd(method, 0);
}
// HACK: we should probably examine add/remove methods to determine this
private static FieldDefinition GetBackingField(EventDefinition ev)
{
var fieldName = ev.Name;
var vbStyleFieldName = fieldName + "Event";
var fieldType = ev.EventType;
foreach (var fd in ev.DeclaringType.Fields) {
if (fd.Name == fieldName || fd.Name == vbStyleFieldName)
if (fd.FieldType.FullName == fieldType.FullName)
return fd;
}
return null;
}
public static bool CanShow(EventDefinition ev)
{
return GetBackingField(ev) != null;
}
}
}

6
ILSpy/TreeNodes/Analyzer/AnalyzedEventTreeNode.cs

@ -59,13 +59,19 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
{ {
if (analyzedEvent.AddMethod != null) if (analyzedEvent.AddMethod != null)
this.Children.Add(new AnalyzedEventAccessorTreeNode(analyzedEvent.AddMethod, "add")); this.Children.Add(new AnalyzedEventAccessorTreeNode(analyzedEvent.AddMethod, "add"));
if (analyzedEvent.RemoveMethod != null) if (analyzedEvent.RemoveMethod != null)
this.Children.Add(new AnalyzedEventAccessorTreeNode(analyzedEvent.RemoveMethod, "remove")); this.Children.Add(new AnalyzedEventAccessorTreeNode(analyzedEvent.RemoveMethod, "remove"));
foreach (var accessor in analyzedEvent.OtherMethods) foreach (var accessor in analyzedEvent.OtherMethods)
this.Children.Add(new AnalyzedEventAccessorTreeNode(accessor, null)); this.Children.Add(new AnalyzedEventAccessorTreeNode(accessor, null));
if (AnalyzedEventFiredByTreeNode.CanShow(analyzedEvent))
this.Children.Add(new AnalyzedEventFiredByTreeNode(analyzedEvent));
if (AnalyzedEventOverridesTreeNode.CanShow(analyzedEvent)) if (AnalyzedEventOverridesTreeNode.CanShow(analyzedEvent))
this.Children.Add(new AnalyzedEventOverridesTreeNode(analyzedEvent)); this.Children.Add(new AnalyzedEventOverridesTreeNode(analyzedEvent));
if (AnalyzedInterfaceEventImplementedByTreeNode.CanShow(analyzedEvent)) if (AnalyzedInterfaceEventImplementedByTreeNode.CanShow(analyzedEvent))
this.Children.Add(new AnalyzedInterfaceEventImplementedByTreeNode(analyzedEvent)); this.Children.Add(new AnalyzedInterfaceEventImplementedByTreeNode(analyzedEvent));
} }

16
ILSpy/TreeNodes/Analyzer/AnalyzedVirtualMethodUsedByTreeNode.cs

@ -89,7 +89,8 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
var BaseMethods = TypesHierarchyHelpers.FindBaseMethods(analyzedMethod).ToArray(); var BaseMethods = TypesHierarchyHelpers.FindBaseMethods(analyzedMethod).ToArray();
if (BaseMethods.Length > 0) { if (BaseMethods.Length > 0) {
baseMethod = BaseMethods[BaseMethods.Length - 1]; baseMethod = BaseMethods[BaseMethods.Length - 1];
} } else
baseMethod = analyzedMethod;
possibleTypes = new List<TypeReference>(); possibleTypes = new List<TypeReference>();
@ -119,18 +120,27 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
MethodReference mr = instr.Operand as MethodReference; MethodReference mr = instr.Operand as MethodReference;
if (mr != null && mr.Name == name) { if (mr != null && mr.Name == name) {
// explicit call to the requested method // explicit call to the requested method
if (Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) && mr.Resolve() == analyzedMethod) { if (instr.OpCode.Code == Code.Call
&& Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType)
&& mr.Resolve() == analyzedMethod) {
found = true; found = true;
prefix = "(as base) "; prefix = "(as base) ";
break; break;
} }
// virtual call to base method // virtual call to base method
if (instr.OpCode.Code == Code.Callvirt && Helpers.IsReferencedBy(baseMethod.DeclaringType, mr.DeclaringType) && mr.Resolve() == baseMethod) { if (instr.OpCode.Code == Code.Callvirt) {
MethodDefinition md = mr.Resolve();
if (md == null) {
// cannot resolve the operand, so ignore this method
break;
}
if (md == baseMethod) {
found = true; found = true;
break; break;
} }
} }
} }
}
method.Body = null; method.Body = null;

4
ILSpy/TreeNodes/Analyzer/Helpers.cs

@ -67,11 +67,11 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
return FindMethodUsageInType(method.DeclaringType, method) ?? method; return FindMethodUsageInType(method.DeclaringType, method) ?? method;
} }
var typeUsage = GetOriginalCodeLocation(method.DeclaringType, method); var typeUsage = GetOriginalCodeLocation(method.DeclaringType);
return typeUsage ?? method; return typeUsage ?? method;
} }
public static MethodDefinition GetOriginalCodeLocation(TypeDefinition type, MethodDefinition method) public static MethodDefinition GetOriginalCodeLocation(TypeDefinition type)
{ {
if (type != null && type.DeclaringType != null && type.IsCompilerGenerated()) { if (type != null && type.DeclaringType != null && type.IsCompilerGenerated()) {
MethodDefinition constructor = GetTypeConstructor(type); MethodDefinition constructor = GetTypeConstructor(type);

Loading…
Cancel
Save