-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathCheckForObsoletePreserveAttributeStep.cs
More file actions
42 lines (36 loc) · 1.33 KB
/
CheckForObsoletePreserveAttributeStep.cs
File metadata and controls
42 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#nullable enable
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Utilities;
using Mono.Cecil;
using Xamarin.Android.Tasks;
namespace MonoDroid.Tuner;
/// <summary>
/// Post-trimming step that warns when an assembly references the obsolete
/// Android.Runtime.PreserveAttribute. Runs as part of AssemblyModifierPipeline
/// so the assemblies are already loaded by Mono.Cecil and the check is free.
/// </summary>
class CheckForObsoletePreserveAttributeStep : IAssemblyModifierPipelineStep
{
readonly TaskLoggingHelper log;
public CheckForObsoletePreserveAttributeStep (TaskLoggingHelper log)
{
this.log = log;
}
public void ProcessAssembly (AssemblyDefinition assembly, StepContext context)
{
if (HasObsoletePreserveAttribute (assembly)) {
log.LogCodedWarning ("IL6001", $"Assembly '{assembly.Name.Name}' contains reference to obsolete attribute 'Android.Runtime.PreserveAttribute'. Members with this attribute may be trimmed. Please use System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute instead");
}
}
static bool HasObsoletePreserveAttribute (AssemblyDefinition assembly)
{
foreach (var module in assembly.Modules) {
foreach (var typeRef in module.GetTypeReferences ()) {
if (typeRef.Namespace == "Android.Runtime" && typeRef.Name == "PreserveAttribute") {
return true;
}
}
}
return false;
}
}