Skip to content

Commit b89a85e

Browse files
authored
Improvements in attributes exclusion (#42)
1 parent 98dfe40 commit b89a85e

File tree

7 files changed

+56
-27
lines changed

7 files changed

+56
-27
lines changed

source/MetadataProcessor.Core/DumpGenerator/DumpTemplates.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ internal partial class DumpTemplates
1212
{{#AssemblyReferences}}
1313
AssemblyRefProps [{{ReferenceId}}]: Flags: {{Flags}} '{{Name}}'
1414
{{/AssemblyReferences}}
15+
1516
{{#TypeReferences}}
1617
TypeRefProps [{{ReferenceId}}]: Scope: {{Scope}} '{{Name}}'
1718
{{#MemberReferences}}
1819
MemberRefProps [{{ReferenceId}}]: '{{Name}}' [{{Signature}}]
1920
{{/MemberReferences}}
2021
{{/TypeReferences}}
22+
2123
{{#TypeDefinitions}}
2224
TypeDefProps [{{ReferenceId}}]: Flags: {{Flags}} Extends: {{ExtendsType}} Enclosed: {{EnclosedType}} '{{Name}}'
2325
{{#FieldDefinitions}}
@@ -43,6 +45,7 @@ internal partial class DumpTemplates
4345
{{/InterfaceDefinitions}}
4446
4547
{{/TypeDefinitions}}
48+
4649
{{#UserStrings}}
4750
UserString [{{ReferenceId}}]: '{{Content}}'
4851
{{/UserStrings}}

source/MetadataProcessor.Core/Extensions/TypeDefinitionExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ public static bool IncludeInStub(this TypeDefinition value)
2828

2929
return false;
3030
}
31+
32+
public static bool IsClassToExclude(this TypeDefinition value)
33+
{
34+
return nanoTablesContext.ClassNamesToExclude.Contains(value.FullName) ||
35+
nanoTablesContext.ClassNamesToExclude.Contains(value.DeclaringType?.FullName);
36+
}
3137
}
3238
}

source/MetadataProcessor.Core/Tables/nanoTablesContext.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public sealed class nanoTablesContext
1919
// Assembly-level attributes
2020
"System.Runtime.InteropServices.ComVisibleAttribute",
2121
"System.Runtime.InteropServices.GuidAttribute",
22+
"System.Reflection.AssemblyTitleAttribute",
23+
"System.Reflection.AssemblyDescriptionAttribute",
24+
"System.Reflection.AssemblyConfigurationAttribute",
25+
"System.Reflection.AssemblyCompanyAttribute",
26+
"System.Reflection.AssemblyProductAttribute",
27+
"System.Reflection.AssemblyCopyrightAttribute",
28+
"System.Reflection.AssemblyTrademarkAttribute",
29+
"System.Reflection.AssemblyFileVersionAttribute",
2230

2331
// Compiler-specific attributes
2432
//"System.ParamArrayAttribute",
@@ -56,7 +64,6 @@ public sealed class nanoTablesContext
5664
// Not supported attributes
5765
"System.MTAThreadAttribute",
5866
"System.STAThreadAttribute",
59-
//"System.Reflection.DefaultMemberAttribute",
6067
};
6168

6269
public nanoTablesContext(
@@ -101,7 +108,17 @@ public nanoTablesContext(
101108
AssemblyReferenceTable = new nanoAssemblyReferenceTable(
102109
mainModule.AssemblyReferences, this);
103110

104-
var typeReferences = mainModule.GetTypeReferences();
111+
var typeReferences = mainModule.GetTypeReferences().ToList();
112+
113+
// remove types in ignore list
114+
foreach (var a in _ignoringAttributes)
115+
{
116+
var typeToExclude = typeReferences.FirstOrDefault(t => t.FullName == a);
117+
if (typeToExclude != null)
118+
{
119+
typeReferences.Remove(typeToExclude);
120+
}
121+
}
105122

106123
TypeReferencesTable = new nanoTypeReferenceTable(
107124
typeReferences, this);
@@ -123,6 +140,16 @@ public nanoTablesContext(
123140

124141
var types = GetOrderedTypes(mainModule, explicitTypesOrder);
125142

143+
// remove types in ignore list
144+
foreach (var a in _ignoringAttributes)
145+
{
146+
var typeToExclude = types.FirstOrDefault(t => t.FullName == a);
147+
if(typeToExclude != null)
148+
{
149+
types.Remove(typeToExclude);
150+
}
151+
}
152+
126153
TypeDefinitionTable = new nanoTypeDefinitionTable(types, this);
127154

128155
var fields = types
@@ -241,7 +268,7 @@ public ushort GetMethodReferenceId(
241268

242269
public nanoResourceFileTable ResourceFileTable { get; private set; }
243270

244-
public List<string> ClassNamesToExclude { get; private set; }
271+
public static List<string> ClassNamesToExclude { get; private set; }
245272

246273
private IEnumerable<Tuple<CustomAttribute, ushort>> GetAttributes(
247274
IEnumerable<ICustomAttributeProvider> types,

source/MetadataProcessor.Core/Tables/nanoTypeDefinitionTable.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,6 @@ internal static nanoTypeDefinitionFlags GetFlags(
381381
return flags;
382382
}
383383

384-
internal bool IsClassToExclude(TypeDefinition td)
385-
{
386-
return _context.ClassNamesToExclude.Contains(td.FullName);
387-
}
388-
389384
internal void ResetByteCodeOffsets()
390385
{
391386
_byteCodeOffsets = new Dictionary<uint, List<Tuple<uint, uint>>>();

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66

77
using Mono.Cecil;
8+
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
89
using System;
910
using System.Collections.Generic;
1011
using System.IO;
@@ -101,7 +102,7 @@ public void Minimize()
101102

102103
foreach (var t in _tablesContext.TypeDefinitionTable.TypeDefinitions)
103104
{
104-
if (!_tablesContext.TypeDefinitionTable.IsClassToExclude(t))
105+
if (!t.IsClassToExclude())
105106
{
106107
setNew.Add(t.MetadataToken);
107108
}
@@ -339,7 +340,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
339340
// include attributes
340341
foreach(var c in td.CustomAttributes)
341342
{
342-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
343+
if (!nanoTablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
343344
{
344345
set.Add(c.AttributeType.MetadataToken);
345346
}
@@ -350,7 +351,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
350351

351352
foreach (var f in tdFields)
352353
{
353-
if (!_tablesContext.ClassNamesToExclude.Contains(f.DeclaringType.FullName))
354+
if (!nanoTablesContext.ClassNamesToExclude.Contains(f.DeclaringType.FullName))
354355
{
355356
set.Add(f.MetadataToken);
356357
}
@@ -359,7 +360,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
359360
// methods
360361
foreach (var m in td.Methods)
361362
{
362-
if (!_tablesContext.ClassNamesToExclude.Contains(m.DeclaringType.FullName))
363+
if (!nanoTablesContext.ClassNamesToExclude.Contains(m.DeclaringType.FullName))
363364
{
364365
set.Add(m.MetadataToken);
365366
}
@@ -368,7 +369,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
368369
// interfaces
369370
foreach (var i in td.Interfaces)
370371
{
371-
if (!_tablesContext.ClassNamesToExclude.Contains(i.InterfaceType.FullName))
372+
if (!nanoTablesContext.ClassNamesToExclude.Contains(i.InterfaceType.FullName))
372373
{
373374
set.Add(i.MetadataToken);
374375
}
@@ -387,7 +388,7 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
387388
// attributes
388389
foreach (var c in fd.CustomAttributes)
389390
{
390-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
391+
if (!nanoTablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
391392
{
392393
set.Add(c.Constructor.MetadataToken);
393394
}
@@ -464,7 +465,7 @@ i.Operand is TypeDefinition ||
464465
// attributes
465466
foreach (var c in md.CustomAttributes)
466467
{
467-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
468+
if (!nanoTablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
468469
{
469470
set.Add(c.Constructor.MetadataToken);
470471
}

source/MetadataProcessor.Core/nanoDumperGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,12 @@ private void DumpTypeReferences(DumpAllTable dumpTable)
235235
{
236236
ushort refId;
237237

238+
var metadataScopeType = _tablesContext.AssemblyReferenceTable.Items.FirstOrDefault(a => a == t.Scope);
239+
238240
var typeRef = new TypeRef()
239241
{
240242
Name = t.Name,
241-
Scope = t.Scope.MetadataScopeType.ToString("x8")
243+
Scope = _tablesContext.AssemblyReferenceTable.GetReferenceId(metadataScopeType).ToString("x8")
242244
};
243245

244246
if (_tablesContext.TypeReferencesTable.TryGetTypeReferenceId(t, out refId))

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ private void GenerateStubs()
5858
{
5959
foreach (var c in _tablesContext.TypeDefinitionTable.TypeDefinitions)
6060
{
61-
if (c.IncludeInStub() && !IsClassToExclude(c))
61+
if (c.IncludeInStub() &&
62+
!c.IsClassToExclude())
6263
{
6364
var className = NativeMethodsCrc.GetClassName(c);
6465

@@ -121,7 +122,7 @@ private void GenerateAssemblyLookup()
121122
if (c.IncludeInStub())
122123
{
123124
// don't include if it's on the exclude list
124-
if (!IsClassToExclude(c))
125+
if (!c.IsClassToExclude())
125126
{
126127
var className = NativeMethodsCrc.GetClassName(c);
127128

@@ -145,7 +146,7 @@ private void GenerateAssemblyLookup()
145146
// need to add a NULL entry for it
146147
// unless it's on the exclude list
147148

148-
if (!IsClassToExclude(c))
149+
if (!c.IsClassToExclude())
149150
{
150151
assemblyLookup.LookupTable.Add(new Method()
151152
{
@@ -163,7 +164,7 @@ private void GenerateAssemblyLookup()
163164
// need to add a NULL entry for each method
164165
// unless it's on the exclude list
165166

166-
if (!IsClassToExclude(c))
167+
if (!c.IsClassToExclude())
167168
{
168169
foreach (var m in nanoTablesContext.GetOrderedMethods(c.Methods))
169170
{
@@ -201,7 +202,7 @@ private void GenerateAssemblyHeader()
201202
foreach (var c in _tablesContext.TypeDefinitionTable.Items)
202203
{
203204
if (c.IncludeInStub() &&
204-
!IsClassToExclude(c))
205+
!c.IsClassToExclude())
205206
{
206207
var classData = new Class()
207208
{
@@ -297,12 +298,6 @@ private void GenerateAssemblyHeader()
297298
}
298299
}
299300

300-
private bool IsClassToExclude(TypeDefinition td)
301-
{
302-
return (_tablesContext.ClassNamesToExclude.Contains(td.FullName) ||
303-
_tablesContext.ClassNamesToExclude.Contains(td.DeclaringType?.FullName));
304-
}
305-
306301
private int GetInstanceFieldsOffset(TypeDefinition c)
307302
{
308303
// check if this type has a base type different from System.Object

0 commit comments

Comments
 (0)