Skip to content

Commit 3d062bb

Browse files
committed
Improve processing of custom attributes
- Add new list of namespaces to exclude from metadata. - TypeReferenceExtensions.IsToInclude extension now uses that list along with the classes to exclude. - Add new class to test app with DoesNotReturn attribute. - Improve GetTestNFAppAssemblyDefinitionWithLoadHints. - Add new test for System.Diagnostics.CodeAnalysis attributes.
1 parent 9641570 commit 3d062bb

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

MetadataProcessor.Shared/Extensions/TypeReferenceExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@
44
//
55

66
using Mono.Cecil;
7+
using System.Collections.Generic;
8+
using System.Linq;
79
using System.Text;
810

911
namespace nanoFramework.Tools.MetadataProcessor.Core.Extensions
1012
{
1113
internal static class TypeReferenceExtensions
1214
{
15+
internal static readonly List<string> NameSpacesToExclude = new List<string>
16+
{
17+
"System.Diagnostics.CodeAnalysis"
18+
};
19+
1320
public static bool IsToInclude(this TypeReference value)
1421
{
15-
return !nanoTablesContext.IgnoringAttributes.Contains(value.FullName);
22+
// check list of known attributes to ignore
23+
bool ignoreFromList = nanoTablesContext.IgnoringAttributes.Contains(value.FullName);
24+
25+
// is this attribute in the list of namespaces to exclude
26+
bool ignoreFromCodeAnalysis = NameSpacesToExclude.Any(ns => value.FullName.StartsWith(ns));
27+
28+
return !(ignoreFromList || ignoreFromCodeAnalysis);
1629
}
1730

1831
public static string TypeSignatureAsString(this TypeReference type)

MetadataProcessor.Tests/Core/Tables/nanoAttributesTableTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
using Microsoft.VisualStudio.TestTools.UnitTesting;
77
using Mono.Cecil;
8+
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
89
using System;
910
using System.Collections.Generic;
11+
using System.IO;
1012
using System.Linq;
13+
using CustomAttribute = Mono.Cecil.CustomAttribute;
1114

1215
namespace nanoFramework.Tools.MetadataProcessor.Tests.Core.Tables
1316
{
@@ -167,7 +170,28 @@ public void RemoveUnusedItems_MethodAttributesTest()
167170
bytesWritten,
168171
String.Join(", ", bytesWritten.Select(i => i.ToString("X"))));
169172
}
170-
}
171173

174+
[TestMethod]
175+
public void TestCodeAnalysisAttributes()
176+
{
177+
var assemblyDefinition = TestObjectHelper.GetTestNFAppAssemblyDefinitionWithLoadHints();
178+
var assemblyBuilder = new nanoAssemblyBuilder(assemblyDefinition, new List<string>(), false);
179+
180+
using (var stream = File.Open(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite))
181+
using (var writer = new BinaryWriter(stream))
182+
{
183+
// test
184+
assemblyBuilder.Write(nanoBinaryWriter.CreateLittleEndianBinaryWriter(writer));
185+
}
186+
187+
// minimize the assembly, following the first pass
188+
assemblyBuilder.Minimize();
172189

190+
// Assert that TypeReferencesTable doesn't contain any of the items in NameSpacesToExclude
191+
foreach (var item in assemblyBuilder.TablesContext.TypeReferencesTable.Items)
192+
{
193+
Assert.IsFalse(TypeReferenceExtensions.NameSpacesToExclude.Any(ns => item.FullName.StartsWith(ns)), $"TypeRef table includes {item.FullName} when it shouldn't.");
194+
}
195+
}
196+
}
173197
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace TestNFApp
5+
{
6+
public class ClassWithNullAttribs
7+
{
8+
[DoesNotReturn]
9+
public int MethodDecoratedWithDoesNotReturn()
10+
{
11+
throw new Exception();
12+
}
13+
14+
public string MethoWIthParamNotNullAttrib([NotNull] string value)
15+
{
16+
return value;
17+
}
18+
}
19+
}

MetadataProcessor.Tests/TestNFApp/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public static void Main()
6161
break;
6262
}
6363

64+
// null attributes tests
65+
_ = new ClassWithNullAttribs();
66+
6467
Debug.WriteLine("Exiting TestNFApp");
6568
}
6669

MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2020
<ItemGroup>
2121
<Compile Include="AuthorAttribute.cs" />
22+
<Compile Include="ClassWithNullAttribs.cs" />
2223
<Compile Include="DataRowAttribute.cs" />
2324
<Compile Include="DummyCustomAttribute1.cs" />
2425
<Compile Include="DummyCustomAttribute2.cs" />

MetadataProcessor.Tests/TestObjectHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ public static AssemblyDefinition GetTestNFAppAssemblyDefinition()
192192

193193
return ret;
194194
}
195+
196+
public static AssemblyDefinition GetTestNFAppAssemblyDefinitionWithLoadHints()
197+
{
198+
IDictionary<string, string> loadHints = new Dictionary<string, string>(StringComparer.Ordinal)
199+
{
200+
["mscorlib"] = GetmscorlibAssemblyDefinition().MainModule.FileName,
201+
["TestNFClassLibrary"] = GetTestNFClassLibraryDefinition().MainModule.FileName
202+
};
203+
204+
return AssemblyDefinition.ReadAssembly(
205+
TestNFAppFullPath,
206+
new ReaderParameters { AssemblyResolver = new LoadHintsAssemblyResolver(loadHints) });
207+
}
208+
209+
195210
public static AssemblyDefinition GetTestNFClassLibraryDefinition()
196211
{
197212
AssemblyDefinition ret = null;

0 commit comments

Comments
 (0)