Browse Source

Basic output of custom attributes attached to types and methods.

pull/52/head
Artur Zgodziski 14 years ago
parent
commit
d13d7bd48f
  1. 21
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 15
      ICSharpCode.Decompiler/Tests/CustomAttributes.cs
  3. 9
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  4. 92
      ICSharpCode.Decompiler/Tests/TestRunner.cs
  5. 9
      NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

21
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -181,6 +181,7 @@ namespace Decompiler @@ -181,6 +181,7 @@ namespace Decompiler
AddTypeMembers(astType, typeDef);
}
ConvertCustomAtributes(astType, typeDef);
return astType;
}
@ -461,6 +462,7 @@ namespace Decompiler @@ -461,6 +462,7 @@ namespace Decompiler
astMethod.Modifiers = ConvertModifiers(methodDef);
astMethod.Body = AstMethodBodyBuilder.CreateMethodBody(methodDef, context);
}
ConvertCustomAtributes(astMethod, methodDef);
return astMethod;
}
@ -565,5 +567,24 @@ namespace Decompiler @@ -565,5 +567,24 @@ namespace Decompiler
yield return astParam;
}
}
static void ConvertCustomAtributes(AttributedNode attributedNode, ICustomAttributeProvider customAttributeProvider)
{
if (customAttributeProvider.HasCustomAttributes)
{
var section = new AttributeSection();
//section.AttributeTarget = target;
foreach (var customAttribute in customAttributeProvider.CustomAttributes)
{
ICSharpCode.NRefactory.CSharp.Attribute attribute = new ICSharpCode.NRefactory.CSharp.Attribute();
//customAttribute.
attribute.Type = ConvertType(customAttribute.AttributeType);
section.Attributes.Add(attribute);
}
attributedNode.Attributes.Add(section);
}
}
}
}

15
ICSharpCode.Decompiler/Tests/CustomAttributes.cs

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
// 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 CustomAtributes
{
[Flags]
public enum EnumWithFlag
{
Item1,
Item2
}
}

9
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -38,6 +38,14 @@ @@ -38,6 +38,14 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="Gallio, Version=0.0.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\libs\Gallio.dll</HintPath>
</Reference>
<Reference Include="MbUnit, Version=0.0.0.0, Culture=neutral, PublicKeyToken=eb9cfa67ee6ab36e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\libs\MbUnit.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -49,6 +57,7 @@ @@ -49,6 +57,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DelegateConstruction.cs" />
<Compile Include="CustomAttributes.cs" />
<Compile Include="Loops.cs" />
<Compile Include="TestRunner.cs" />
</ItemGroup>

92
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -9,6 +9,7 @@ using System.Text; @@ -9,6 +9,7 @@ using System.Text;
using Decompiler;
using Microsoft.CSharp;
using Mono.Cecil;
using MbUnit.Framework;
namespace ICSharpCode.Decompiler.Tests
{
@ -16,14 +17,31 @@ namespace ICSharpCode.Decompiler.Tests @@ -16,14 +17,31 @@ namespace ICSharpCode.Decompiler.Tests
{
public static void Main()
{
Test(@"..\..\Tests\DelegateConstruction.cs");
TestFile(@"..\..\Tests\DelegateConstruction.cs");
Console.ReadKey();
}
[Test]
//[Row(@"..\..\Tests\DelegateConstruction.cs")]
//[Row(@"..\..\Tests\Loops.cs")]
[Row(@"..\..\Tests\CustomAttributes.cs")]
public void RoundtripFile(string fileName)
{
string code = File.ReadAllText(fileName);
AssemblyDefinition assembly = Compile(code);
AstBuilder decompiler = new AstBuilder(new DecompilerContext());
decompiler.AddAssembly(assembly);
StringWriter output = new StringWriter();
decompiler.GenerateCode(new PlainTextOutput(output));
var decompiledCode = output.ToString();
var onlyCode = "using System;" + Environment.NewLine + StripCodeFileHeader(code);
Assert.AreEqual(onlyCode, decompiledCode);
}
static void Test(string fileName)
static void TestFile(string fileName)
{
string code = File.ReadAllText(fileName);
AssemblyDefinition assembly = Compile(code);
@ -32,11 +50,45 @@ namespace ICSharpCode.Decompiler.Tests @@ -32,11 +50,45 @@ namespace ICSharpCode.Decompiler.Tests
StringWriter output = new StringWriter();
decompiler.GenerateCode(new PlainTextOutput(output));
StringWriter diff = new StringWriter();
if (!Compare(code, output.ToString(), diff)) {
if (!Compare(code, output.ToString(), diff))
{
throw new Exception("Test failure." + Environment.NewLine + diff.ToString());
}
}
static string StripCodeFileHeader(string code)
{
var reader = new StringReader(code);
var buffer = new StringWriter();
string line;
var skipBlankLine = false;
while ((line = reader.ReadLine()) != null)
{
if (line.Trim().StartsWith("//"))
{
skipBlankLine = true;
continue;
}
else if (line.StartsWith("using "))
{
skipBlankLine = true;
continue;
}
else if (skipBlankLine && String.IsNullOrWhiteSpace(line))
{
continue;
}
skipBlankLine = false;
buffer.WriteLine(line);
}
return buffer.ToString();
}
static bool Compare(string input1, string input2, StringWriter diff)
{
bool ok = true;
@ -44,33 +96,40 @@ namespace ICSharpCode.Decompiler.Tests @@ -44,33 +96,40 @@ namespace ICSharpCode.Decompiler.Tests
StringReader r1 = new StringReader(input1);
StringReader r2 = new StringReader(input2);
string line1, line2;
while ((line1 = r1.ReadLine()) != null) {
while ((line1 = r1.ReadLine()) != null)
{
string trimmed = line1.Trim();
if (trimmed.Length == 0 || trimmed.StartsWith("//", StringComparison.Ordinal) || line1.StartsWith("using ", StringComparison.Ordinal)) {
if (trimmed.Length == 0 || trimmed.StartsWith("//", StringComparison.Ordinal) || line1.StartsWith("using ", StringComparison.Ordinal))
{
diff.WriteLine(" " + line1);
continue;
}
line2 = r2.ReadLine();
while (line2 != null && (line2.StartsWith("using ", StringComparison.Ordinal) || line2.Trim().Length == 0))
line2 = r2.ReadLine();
if (line2 == null) {
if (line2 == null)
{
ok = false;
diff.WriteLine("-" + line1);
continue;
}
if (line1 != line2) {
if (line1 != line2)
{
ok = false;
if (numberOfContinuousMistakes++ > 5)
return false;
diff.WriteLine("-" + line1);
diff.WriteLine("+" + line2);
} else {
}
else
{
if (numberOfContinuousMistakes > 0)
numberOfContinuousMistakes--;
diff.WriteLine(" " + line1);
}
}
while ((line2 = r2.ReadLine()) != null) {
while ((line2 = r2.ReadLine()) != null)
{
ok = false;
diff.WriteLine("+" + line2);
}
@ -79,20 +138,25 @@ namespace ICSharpCode.Decompiler.Tests @@ -79,20 +138,25 @@ namespace ICSharpCode.Decompiler.Tests
static AssemblyDefinition Compile(string code)
{
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> {{ "CompilerVersion", "v4.0" }});
CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
CompilerParameters options = new CompilerParameters();
options.ReferencedAssemblies.Add("System.Core.dll");
CompilerResults results = provider.CompileAssemblyFromSource(options, code);
try {
if (results.Errors.Count > 0) {
try
{
if (results.Errors.Count > 0)
{
StringBuilder b = new StringBuilder("Compiler error:");
foreach (var error in results.Errors) {
foreach (var error in results.Errors)
{
b.AppendLine(error.ToString());
}
throw new Exception(b.ToString());
}
return AssemblyDefinition.ReadAssembly(results.PathToAssembly);
} finally {
}
finally
{
File.Delete(results.PathToAssembly);
results.TempFiles.Delete();
}

9
NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -1083,6 +1083,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1083,6 +1083,7 @@ namespace ICSharpCode.NRefactory.CSharp
StartNode(attribute);
attribute.Type.AcceptVisitor(this, data);
Space(policy.BeforeMethodCallParentheses);
if (attribute.Arguments.Count != 0)
WriteCommaSeparatedListInParenthesis(attribute.Arguments, policy.WithinMethodCallParentheses);
return EndNode(attribute);
}
@ -1879,8 +1880,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1879,8 +1880,14 @@ namespace ICSharpCode.NRefactory.CSharp
public object VisitSimpleType(SimpleType simpleType, object data)
{
bool inAttribute = currentContainerNode is Attribute;
StartNode(simpleType);
WriteIdentifier(simpleType.Identifier);
var identifier = simpleType.Identifier;
if (inAttribute && identifier.EndsWith("Attribute"))
identifier = identifier.Substring(0, identifier.Length - 9);
WriteIdentifier(identifier);
WriteTypeArguments(simpleType.TypeArguments);
return EndNode(simpleType);
}

Loading…
Cancel
Save