Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
MoMA Updated to Cecil 0.9.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Egli authored and marek-safar committed Feb 16, 2017
1 parent cf38054 commit 84c6b2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
5 changes: 3 additions & 2 deletions MoMA.Analyzer/AssemblyAnalyzer.cs
Expand Up @@ -28,6 +28,7 @@
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;

Expand Down Expand Up @@ -257,7 +258,7 @@ private void AnalyzeAssembly (string assembly)
AssemblyDefinition ad = AssemblyDefinition.ReadAssembly (assembly);

assembly_version = ad.Name.Version;
AssemblyRuntime = ad.Runtime.ToString ();
AssemblyRuntime = ad.Modules.FirstOrDefault()?.Runtime.ToString () ?? "";
assembly_name = Path.GetFileName (assembly);

foreach (TypeDefinition type in ad.MainModule.Types) {
Expand Down Expand Up @@ -286,7 +287,7 @@ private void AnalyzeAssembly (string assembly)
}

// Check every constructor for calls that match our issues lists
foreach (MethodDefinition method in type.Constructors) {
foreach (MethodDefinition method in type.Methods.Where(m => m.IsConstructor)) {
if (method.Body != null) {
foreach (Instruction i in method.Body.Instructions) {
if (i.OpCode == OpCodes.Call || i.OpCode == OpCodes.Callvirt || i.OpCode == OpCodes.Calli || i.OpCode == OpCodes.Ldftn || i.OpCode == OpCodes.Ldvirtftn || i.OpCode == OpCodes.Newobj || i.OpCode == OpCodes.Initobj) {
Expand Down
2 changes: 1 addition & 1 deletion MoMA.Analyzer/Methods/Method.cs
Expand Up @@ -129,7 +129,7 @@ private void ParseMethod (MethodDefinition md)
final_parameters += (ConvertType (p.ParameterType.ToString ()) + ", ");

function_name = md.Name;
return_type = ConvertType (md.ReturnType.ReturnType.FullName);
return_type = ConvertType (md.ReturnType.FullName);

if (final_parameters.Length > 0)
final_parameters = final_parameters.Substring (0, final_parameters.Length - 2);
Expand Down
41 changes: 34 additions & 7 deletions MoMA.Analyzer/Methods/MethodExtractor.cs
Expand Up @@ -23,7 +23,10 @@
// Jonathan Pobst monkey@jpobst.com
//

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;

Expand Down Expand Up @@ -51,9 +54,9 @@ public static void ExtractFromAssembly (string assembly, SortedList<string, Meth
foreach (CustomAttribute ca in property.CustomAttributes) {
if (IsReportableMonoTODO (ca.Constructor.DeclaringType.ToString ())) {
if (property.GetMethod != null && IsMethodVisible (property.GetMethod))
monoTodoMethods[property.GetMethod.ToString ()] = new Method (property.GetMethod.ToString (), ca.ConstructorParameters.Count > 0 ? ca.ConstructorParameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
monoTodoMethods[property.GetMethod.ToString ()] = new Method (property.GetMethod.ToString (), ca.Constructor.Parameters.Count > 0 ? ca.Constructor.Parameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
if (property.SetMethod != null && IsMethodVisible (property.SetMethod))
monoTodoMethods[property.SetMethod.ToString ()] = new Method (property.SetMethod.ToString (), ca.ConstructorParameters.Count > 0 ? ca.ConstructorParameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
monoTodoMethods[property.SetMethod.ToString ()] = new Method (property.SetMethod.ToString (), ca.Constructor.Parameters.Count > 0 ? ca.Constructor.Parameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
}
}
}
Expand All @@ -72,15 +75,15 @@ public static void ExtractFromAssembly (string assembly, SortedList<string, Meth
if (monoTodoMethods != null)
foreach (CustomAttribute ca in method.CustomAttributes)
if (IsReportableMonoTODO (ca.Constructor.DeclaringType.ToString ()))
monoTodoMethods[method.ToString ()] = new Method (method.ToString (), ca.ConstructorParameters.Count > 0 ? ca.ConstructorParameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
monoTodoMethods[method.ToString ()] = new Method (method.ToString (), ca.Constructor.Parameters.Count > 0 ? ca.Constructor.Parameters[0].ToString ().Replace ('\n', ' ') : string.Empty);

// If adding methods that throw NotImplementedException, look for those
if (throwsNotImplementedMethods != null && ThrowsNotImplementedException (method))
throwsNotImplementedMethods[method.ToString ()] = new Method (method.ToString ());
}

//Gets all constructors of the current type
foreach (MethodDefinition method in type.Constructors) {
foreach (MethodDefinition method in type.Methods.Where(m => m.IsConstructor)) {
// We only want Public and Protected methods
if (!IsMethodVisible (method))
continue;
Expand All @@ -93,7 +96,7 @@ public static void ExtractFromAssembly (string assembly, SortedList<string, Meth
if (monoTodoMethods != null)
foreach (CustomAttribute ca in method.CustomAttributes)
if (IsReportableMonoTODO (ca.Constructor.DeclaringType.ToString ()))
monoTodoMethods[method.ToString ()] = new Method (method.ToString (), ca.ConstructorParameters.Count > 0 ? ca.ConstructorParameters[0].ToString ().Replace ('\n', ' ') : string.Empty);
monoTodoMethods[method.ToString ()] = new Method (method.ToString (), ca.Constructor.Parameters.Count > 0 ? ca.Constructor.Parameters[0].ToString ().Replace ('\n', ' ') : string.Empty);

// If adding methods that throw NotImplementedException, look for those
if (throwsNotImplementedMethods != null && ThrowsNotImplementedException (method))
Expand Down Expand Up @@ -132,10 +135,34 @@ private static bool IsTypeVisible (TypeDefinition type)

return true;
}


public class TypeDefinitionCache: KeyedCollection<string, TypeDefinition> {
protected override string GetKeyForItem(TypeDefinition item) => item.FullName;

public void Add(AssemblyDefinition a) {
var defs = a.Modules.SelectMany(m => m.Types);
foreach (var def in defs) Add(def);
}

public TypeDefinition Find(TypeReference reference) {
TypeDefinition def = null;
if (Dictionary != null) {
if (!Dictionary.TryGetValue(reference.FullName, out def)) Add(reference.Module.Assembly);
if (!Dictionary.TryGetValue(reference.FullName, out def)) throw new NotSupportedException();
} else {
if (!Contains(reference.FullName)) Add(reference.Module.Assembly);
if (!Contains(reference.FullName)) throw new NotSupportedException();
def = this[reference.FullName];
}
return def;
}
}

static TypeDefinitionCache Cache = new TypeDefinitionCache();
private static TypeDefinition TypeReferenceToDefinition (TypeReference type)
{
return type.Module.Types[type.FullName];
if (type.Module.Types.Count < 10) return type.Module.Types.FirstOrDefault(def => def.FullName == type.FullName);
else return Cache.Find(type);
}

// Is this attribute a MonoTODO that we want to report in MoMA?
Expand Down
8 changes: 4 additions & 4 deletions MoMA.Analyzer/MoMA.Analyzer.csproj
Expand Up @@ -37,19 +37,19 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion MoMA.Analyzer/packages.config
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Cecil" version="0.9.6.1" targetFramework="net40" />
<package id="Mono.Cecil" version="0.9.6.4" targetFramework="net40" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
</packages>

0 comments on commit 84c6b2f

Please sign in to comment.