mirror of https://github.com/icsharpcode/ILSpy.git
10 changed files with 300 additions and 10 deletions
@ -0,0 +1,30 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
|
||||||
|
public static class Generics |
||||||
|
{ |
||||||
|
class MyArray<T> |
||||||
|
{ |
||||||
|
private T[] arr; |
||||||
|
|
||||||
|
public MyArray(int capacity) |
||||||
|
{ |
||||||
|
this.arr = new T[capacity]; |
||||||
|
} |
||||||
|
|
||||||
|
public void Size(int capacity) |
||||||
|
{ |
||||||
|
Array.Resize(ref this.arr, capacity); |
||||||
|
} |
||||||
|
|
||||||
|
public void Grow(int capacity) |
||||||
|
{ |
||||||
|
if (capacity >= this.arr.Length) |
||||||
|
{ |
||||||
|
this.Size(capacity); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,109 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
|
||||||
|
using ICSharpCode.NRefactory.Utils; |
||||||
|
using ICSharpCode.TreeView; |
||||||
|
using Mono.Cecil; |
||||||
|
using Mono.Cecil.Cil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
class AnalyzedFieldAccessNode : AnalyzerTreeNode |
||||||
|
{ |
||||||
|
readonly bool showWrites; // true: show writes; false: show read access
|
||||||
|
readonly FieldDefinition analyzedField; |
||||||
|
readonly ThreadingSupport threading; |
||||||
|
|
||||||
|
public AnalyzedFieldAccessNode(FieldDefinition analyzedField, bool showWrites) |
||||||
|
{ |
||||||
|
if (analyzedField == null) |
||||||
|
throw new ArgumentNullException("analyzedField"); |
||||||
|
|
||||||
|
this.analyzedField = analyzedField; |
||||||
|
this.showWrites = showWrites; |
||||||
|
this.threading = new ThreadingSupport(); |
||||||
|
this.LazyLoading = true; |
||||||
|
} |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { return showWrites ? "Assigned By" : "Read 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(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FetchChildren(CancellationToken ct) |
||||||
|
{ |
||||||
|
return FindReferences(MainWindow.Instance.AssemblyList.GetAssemblies(), ct); |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FindReferences(LoadedAssembly[] assemblies, CancellationToken ct) |
||||||
|
{ |
||||||
|
// use parallelism only on the assembly level (avoid locks within Cecil)
|
||||||
|
return assemblies.AsParallel().WithCancellation(ct).SelectMany((LoadedAssembly asm) => FindReferences(asm, ct)); |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<SharpTreeNode> FindReferences(LoadedAssembly asm, CancellationToken ct) |
||||||
|
{ |
||||||
|
string name = analyzedField.Name; |
||||||
|
string declTypeName = analyzedField.DeclaringType.FullName; |
||||||
|
foreach (TypeDefinition type in TreeTraversal.PreOrder(asm.AssemblyDefinition.MainModule.Types, t => t.NestedTypes)) { |
||||||
|
ct.ThrowIfCancellationRequested(); |
||||||
|
foreach (MethodDefinition method in type.Methods) { |
||||||
|
ct.ThrowIfCancellationRequested(); |
||||||
|
bool found = false; |
||||||
|
if (!method.HasBody) |
||||||
|
continue; |
||||||
|
foreach (Instruction instr in method.Body.Instructions) { |
||||||
|
if (CanBeReference(instr.OpCode.Code)) { |
||||||
|
FieldReference fr = instr.Operand as FieldReference; |
||||||
|
if (fr != null && fr.Name == name && fr.DeclaringType.FullName == declTypeName && fr.Resolve() == analyzedField) { |
||||||
|
found = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (found) |
||||||
|
yield return new AnalyzedMethodTreeNode(method); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool CanBeReference(Code code) |
||||||
|
{ |
||||||
|
switch (code) { |
||||||
|
case Code.Ldfld: |
||||||
|
case Code.Ldsfld: |
||||||
|
return !showWrites; |
||||||
|
case Code.Stfld: |
||||||
|
case Code.Stsfld: |
||||||
|
return showWrites; |
||||||
|
case Code.Ldflda: |
||||||
|
case Code.Ldsflda: |
||||||
|
return true; // always show address-loading
|
||||||
|
default: |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using Mono.Cecil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
class AnalyzedFieldNode : AnalyzerTreeNode |
||||||
|
{ |
||||||
|
FieldDefinition analyzedField; |
||||||
|
|
||||||
|
public AnalyzedFieldNode(FieldDefinition analyzedField) |
||||||
|
{ |
||||||
|
if (analyzedField == null) |
||||||
|
throw new ArgumentNullException("analyzedField"); |
||||||
|
this.analyzedField = analyzedField; |
||||||
|
this.LazyLoading = true; |
||||||
|
} |
||||||
|
|
||||||
|
public override object Icon { |
||||||
|
get { return FieldTreeNode.GetIcon(analyzedField); } |
||||||
|
} |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { |
||||||
|
return Language.TypeToString(analyzedField.DeclaringType, true) + |
||||||
|
"." + analyzedField.Name + " : " + this.Language.TypeToString(analyzedField.FieldType, false, analyzedField); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void ActivateItem(System.Windows.RoutedEventArgs e) |
||||||
|
{ |
||||||
|
e.Handled = true; |
||||||
|
MainWindow.Instance.JumpToReference(analyzedField); |
||||||
|
} |
||||||
|
|
||||||
|
protected override void LoadChildren() |
||||||
|
{ |
||||||
|
this.Children.Add(new AnalyzedFieldAccessNode(analyzedField, false)); |
||||||
|
if (!analyzedField.IsLiteral) |
||||||
|
this.Children.Add(new AnalyzedFieldAccessNode(analyzedField, true)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,85 @@ |
|||||||
|
// 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.Generic; |
||||||
|
using System.Linq; |
||||||
|
using Mono.Cecil; |
||||||
|
using Mono.Cecil.Cil; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.TreeNodes.Analyzer |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Shows the methods that are used by this method.
|
||||||
|
/// </summary>
|
||||||
|
sealed class AnalyzedMethodUsesNode : AnalyzerTreeNode |
||||||
|
{ |
||||||
|
MethodDefinition analyzedMethod; |
||||||
|
|
||||||
|
public AnalyzedMethodUsesNode(MethodDefinition analyzedMethod) |
||||||
|
{ |
||||||
|
if (analyzedMethod == null) |
||||||
|
throw new ArgumentNullException("analyzedMethod"); |
||||||
|
|
||||||
|
this.analyzedMethod = analyzedMethod; |
||||||
|
this.LazyLoading = true; |
||||||
|
} |
||||||
|
|
||||||
|
public override object Text { |
||||||
|
get { return "Uses"; } |
||||||
|
} |
||||||
|
|
||||||
|
public override object Icon { |
||||||
|
get { return Images.Search; } |
||||||
|
} |
||||||
|
|
||||||
|
protected override void LoadChildren() |
||||||
|
{ |
||||||
|
foreach (var f in GetUsedFields().Distinct()) { |
||||||
|
this.Children.Add(new AnalyzedFieldNode(f)); |
||||||
|
} |
||||||
|
foreach (var m in GetUsedMethods().Distinct()) { |
||||||
|
this.Children.Add(new AnalyzedMethodTreeNode(m)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<MethodDefinition> GetUsedMethods() |
||||||
|
{ |
||||||
|
foreach (Instruction instr in analyzedMethod.Body.Instructions) { |
||||||
|
MethodReference mr = instr.Operand as MethodReference; |
||||||
|
if (mr != null) { |
||||||
|
MethodDefinition def = mr.Resolve(); |
||||||
|
if (def != null) |
||||||
|
yield return def; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
IEnumerable<FieldDefinition> GetUsedFields() |
||||||
|
{ |
||||||
|
foreach (Instruction instr in analyzedMethod.Body.Instructions) { |
||||||
|
FieldReference fr = instr.Operand as FieldReference; |
||||||
|
if (fr != null) { |
||||||
|
FieldDefinition def = fr.Resolve(); |
||||||
|
if (def != null) |
||||||
|
yield return def; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue