Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnconditionalSuppressMessageAttributeState.cs « Linker « linker « src - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 686b4ad466de0d14316fcea678ef7eb34d424f0d (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace Mono.Linker
{
	public class UnconditionalSuppressMessageAttributeState
	{
		internal const string ScopeProperty = "Scope";
		internal const string TargetProperty = "Target";
		internal const string MessageIdProperty = "MessageId";

		readonly LinkContext _context;
		readonly Dictionary<ICustomAttributeProvider, Dictionary<int, SuppressMessageInfo>> _suppressions;
		HashSet<AssemblyDefinition> InitializedAssemblies { get; }

		public UnconditionalSuppressMessageAttributeState (LinkContext context)
		{
			_context = context;
			_suppressions = new Dictionary<ICustomAttributeProvider, Dictionary<int, SuppressMessageInfo>> ();
			InitializedAssemblies = new HashSet<AssemblyDefinition> ();
		}

		public void AddSuppression (CustomAttribute ca, ICustomAttributeProvider provider)
		{
			SuppressMessageInfo info;
			if (!TryDecodeSuppressMessageAttributeData (ca, out info))
				return;

			AddSuppression (info, provider);
		}

		void AddSuppression (SuppressMessageInfo info, ICustomAttributeProvider provider)
		{
			if (!_suppressions.TryGetValue (provider, out var suppressions)) {
				suppressions = new Dictionary<int, SuppressMessageInfo> ();
				_suppressions.Add (provider, suppressions);
			}

			if (suppressions.ContainsKey (info.Id))
				_context.LogMessage ($"Element {provider} has more than one unconditional suppression. Note that only the last one is used.");

			suppressions[info.Id] = info;
		}

		public bool IsSuppressed (int id, MessageOrigin warningOrigin, out SuppressMessageInfo info)
		{
			// Check for suppressions on both the suppression context as well as the original member
			// (if they're different). This is to correctly handle compiler generated code
			// which needs to use suppressions from both the compiler generated scope
			// as well as the original user defined method.
			IMemberDefinition suppressionContextMember = warningOrigin.SuppressionContextMember;
			if (IsSuppressed (id, suppressionContextMember, out info))
				return true;

			IMemberDefinition originMember = warningOrigin.MemberDefinition;
			if (suppressionContextMember != originMember && IsSuppressed (id, originMember, out info))
				return true;

			return false;
		}

		bool IsSuppressed (int id, IMemberDefinition warningOriginMember, out SuppressMessageInfo info)
		{
			info = default;
			if (warningOriginMember == null)
				return false;

			ModuleDefinition module = GetModuleFromProvider (warningOriginMember);
			DecodeModuleLevelAndGlobalSuppressMessageAttributes (module);
			while (warningOriginMember != null) {
				if (IsSuppressedOnElement (id, warningOriginMember, out info))
					return true;

				warningOriginMember = warningOriginMember.DeclaringType;
			}

			// Check if there's an assembly or module level suppression.
			if (IsSuppressedOnElement (id, module, out info) ||
				IsSuppressedOnElement (id, module.Assembly, out info))
				return true;

			return false;
		}

		bool IsSuppressedOnElement (int id, ICustomAttributeProvider provider, out SuppressMessageInfo info)
		{
			info = default;
			if (provider == null)
				return false;

			return _suppressions.TryGetValue (provider, out var suppressions) &&
				suppressions.TryGetValue (id, out info);
		}

		static bool TryDecodeSuppressMessageAttributeData (CustomAttribute attribute, out SuppressMessageInfo info)
		{
			info = default;

			// We need at least the Category and Id to decode the warning to suppress.
			// The only UnconditionalSuppressMessageAttribute constructor requires those two parameters.
			if (attribute.ConstructorArguments.Count < 2) {
				return false;
			}

			// Ignore the category parameter because it does not identify the warning
			// and category information can be obtained from warnings themselves.
			// We only support warnings with code pattern IL####.
			if (!(attribute.ConstructorArguments[1].Value is string warningId) ||
				warningId.Length < 6 ||
				!warningId.StartsWith ("IL") ||
				!int.TryParse (warningId.Substring (2, 4), out info.Id)) {
				return false;
			}

			if (warningId.Length > 6 && warningId[6] != ':')
				return false;

			if (attribute.HasProperties) {
				foreach (var p in attribute.Properties) {
					switch (p.Name) {
					case ScopeProperty:
						info.Scope = (p.Argument.Value as string)?.ToLower ();
						break;
					case TargetProperty:
						info.Target = p.Argument.Value as string;
						break;
					case MessageIdProperty:
						info.MessageId = p.Argument.Value as string;
						break;
					}
				}
			}

			return true;
		}

		public static ModuleDefinition GetModuleFromProvider (ICustomAttributeProvider provider)
		{
			switch (provider.MetadataToken.TokenType) {
			case TokenType.Module:
				return provider as ModuleDefinition;
			case TokenType.Assembly:
				return (provider as AssemblyDefinition).MainModule;
			case TokenType.TypeDef:
				return (provider as TypeDefinition).Module;
			case TokenType.Method:
			case TokenType.Property:
			case TokenType.Field:
			case TokenType.Event:
				return (provider as IMemberDefinition).DeclaringType.Module;
			default:
				return null;
			}
		}

		void DecodeModuleLevelAndGlobalSuppressMessageAttributes (ModuleDefinition module)
		{
			AssemblyDefinition assembly = module.Assembly;
			if (InitializedAssemblies.Add (assembly)) {
				LookForModuleLevelAndGlobalSuppressions (module, assembly);
				foreach (var _module in assembly.Modules)
					LookForModuleLevelAndGlobalSuppressions (_module, _module);
			}
		}

		public void LookForModuleLevelAndGlobalSuppressions (ModuleDefinition module, ICustomAttributeProvider provider)
		{
			var attributes = _context.CustomAttributes.GetCustomAttributes (provider).
					Where (a => TypeRefHasUnconditionalSuppressions (a.AttributeType));
			foreach (var instance in attributes) {
				SuppressMessageInfo info;
				if (!TryDecodeSuppressMessageAttributeData (instance, out info))
					continue;

				if (info.Target == null && (info.Scope == "module" || info.Scope == null)) {
					AddSuppression (info, provider);
					continue;
				}

				if (info.Target == null || info.Scope == null)
					continue;

				switch (info.Scope) {
				case "type":
				case "member":
					foreach (var result in DocumentationSignatureParser.GetMembersForDocumentationSignature (info.Target, module))
						AddSuppression (info, result);

					break;
				default:
					_context.LogMessage ($"Scope `{info.Scope}` used in `UnconditionalSuppressMessage` is currently not supported.");
					break;
				}
			}
		}

		public static bool TypeRefHasUnconditionalSuppressions (TypeReference typeRef)
		{
			return typeRef.Name == "UnconditionalSuppressMessageAttribute" &&
				typeRef.Namespace == "System.Diagnostics.CodeAnalysis";
		}
	}
}