Browse Source

Add analyzer for fields.

pull/70/head
Daniel Grunwald 15 years ago
parent
commit
6b7e88e122
  1. 3
      ILSpy/ILSpy.csproj
  2. 109
      ILSpy/TreeNodes/Analyzer/AnalyzedFieldAccessNode.cs
  3. 45
      ILSpy/TreeNodes/Analyzer/AnalyzedFieldNode.cs
  4. 2
      ILSpy/TreeNodes/Analyzer/AnalyzedMethodTreeNode.cs
  5. 85
      ILSpy/TreeNodes/Analyzer/AnalyzedMethodUsesNode.cs
  6. 28
      ILSpy/TreeNodes/FieldTreeNode.cs

3
ILSpy/ILSpy.csproj

@ -133,8 +133,11 @@ @@ -133,8 +133,11 @@
<Compile Include="TextView\ReferenceElementGenerator.cs" />
<Compile Include="TextView\AvalonEditTextOutput.cs" />
<Compile Include="TextView\UIElementGenerator.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedFieldAccessNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedFieldNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedMethodTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedMethodUsedByTreeNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzedMethodUsesNode.cs" />
<Compile Include="TreeNodes\Analyzer\AnalyzerTreeNode.cs" />
<Compile Include="TreeNodes\AssemblyListTreeNode.cs" />
<Compile Include="TreeNodes\AssemblyReferenceTreeNode.cs" />

109
ILSpy/TreeNodes/Analyzer/AnalyzedFieldAccessNode.cs

@ -0,0 +1,109 @@ @@ -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;
}
}
}
}

45
ILSpy/TreeNodes/Analyzer/AnalyzedFieldNode.cs

@ -0,0 +1,45 @@ @@ -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));
}
}
}

2
ILSpy/TreeNodes/Analyzer/AnalyzedMethodTreeNode.cs

@ -49,6 +49,8 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer @@ -49,6 +49,8 @@ namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
protected override void LoadChildren()
{
if (analyzedMethod.HasBody)
this.Children.Add(new AnalyzedMethodUsesNode(analyzedMethod));
this.Children.Add(new AnalyzedMethodUsedByTreeNode(analyzedMethod));
}
}

85
ILSpy/TreeNodes/Analyzer/AnalyzedMethodUsesNode.cs

@ -0,0 +1,85 @@ @@ -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;
}
}
}
}
}

28
ILSpy/TreeNodes/FieldTreeNode.cs

@ -17,7 +17,9 @@ @@ -17,7 +17,9 @@
// DEALINGS IN THE SOFTWARE.
using System;
using System.Windows.Controls;
using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.TreeNodes.Analyzer;
using Mono.Cecil;
namespace ICSharpCode.ILSpy.TreeNodes
@ -45,12 +47,15 @@ namespace ICSharpCode.ILSpy.TreeNodes @@ -45,12 +47,15 @@ namespace ICSharpCode.ILSpy.TreeNodes
}
public override object Icon {
get {
if (field.IsLiteral)
return Images.Literal;
else
return Images.Field;
}
get { return GetIcon(field); }
}
public static object GetIcon(FieldDefinition field)
{
if (field.IsLiteral)
return Images.Literal;
else
return Images.Field;
}
public override FilterResult Filter(FilterSettings settings)
@ -65,5 +70,16 @@ namespace ICSharpCode.ILSpy.TreeNodes @@ -65,5 +70,16 @@ namespace ICSharpCode.ILSpy.TreeNodes
{
language.DecompileField(field, output, options);
}
public override System.Windows.Controls.ContextMenu GetContextMenu()
{
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem() { Header = "Analyze", Icon = new Image() { Source = Images.Search } };
item.Click += delegate { MainWindow.Instance.AddToAnalyzer(new AnalyzedFieldNode(field)); };
menu.Items.Add(item);
return menu;
}
}
}

Loading…
Cancel
Save