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

TestChecker.cs « ILLink.RoslynAnalyzer.Tests « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7502710991b6a0c153fba541de8d0be917f853c1 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Xunit;

namespace ILLink.RoslynAnalyzer.Tests
{
	internal sealed class TestChecker : CSharpSyntaxWalker
	{
		private readonly CSharpSyntaxTree _tree;
		private readonly SemanticModel _semanticModel;
		private readonly IReadOnlyList<Diagnostic> _diagnostics;
		private readonly List<Diagnostic> _unmatched;
		private readonly List<(AttributeSyntax Attribute, string Message)> _missing;
		private readonly List<AttributeSyntax> _expectedNoWarnings;

		public TestChecker (
			CSharpSyntaxTree tree,
			SemanticModel semanticModel,
			ImmutableArray<Diagnostic> diagnostics)
		{
			_tree = tree;
			_semanticModel = semanticModel;
			_diagnostics = diagnostics
				// Filter down to diagnostics which originate from this tree or have no location
				.Where (d => d.Location.SourceTree == tree || d.Location.SourceTree == null).ToList ();

			// Filled in later
			_unmatched = new List<Diagnostic> ();
			_missing = new List<(AttributeSyntax Attribute, string Message)> ();
			_expectedNoWarnings = new List<AttributeSyntax> ();
		}

		public void Check (bool allowMissingWarnings)
		{
			_unmatched.Clear ();
			_unmatched.AddRange (_diagnostics);
			_missing.Clear ();
			_expectedNoWarnings.Clear ();

			Visit (_tree.GetRoot ());

			string message = "";
			if (!allowMissingWarnings && _missing.Any ()) {
				var missingLines = string.Join (
					Environment.NewLine,
					_missing.Select (md => $"({md.Attribute.Parent?.Parent?.GetLocation ().GetLineSpan ()}) {md.Message}"));
				message += $@"Expected warnings were not generated:{Environment.NewLine}{missingLines}{Environment.NewLine}";
			}
			var unexpected = _unmatched.Where (diag =>
				diag.Location.SourceTree == null ||
				_expectedNoWarnings.Any (attr => attr.Parent?.Parent?.Span.Contains (diag.Location.SourceSpan) == true));
			if (unexpected.Any ()) {
				message += $"Unexpected warnings were generated:{Environment.NewLine}{string.Join (Environment.NewLine, unexpected)}";
			}

			if (message.Length > 0) {
				Assert.True (false, message);
			}
		}

		public override void VisitClassDeclaration (ClassDeclarationSyntax node)
		{
			base.VisitClassDeclaration (node);
			CheckMember (node);
		}

		public override void VisitConstructorDeclaration (ConstructorDeclarationSyntax node)
		{
			base.VisitConstructorDeclaration (node);
			CheckMember (node);
		}

		public override void VisitInterfaceDeclaration (InterfaceDeclarationSyntax node)
		{
			base.VisitInterfaceDeclaration (node);
			CheckMember (node);
		}

		public override void VisitMethodDeclaration (MethodDeclarationSyntax node)
		{
			base.VisitMethodDeclaration (node);
			CheckMember (node);
		}

		public override void VisitPropertyDeclaration (PropertyDeclarationSyntax node)
		{
			base.VisitPropertyDeclaration (node);
			CheckMember (node);
		}

		public override void VisitFieldDeclaration (FieldDeclarationSyntax node)
		{
			base.VisitFieldDeclaration (node);
			CheckMember (node);
		}

		private void CheckMember (MemberDeclarationSyntax node)
		{
			ValidateDiagnostics (node, node.AttributeLists);
		}

		public override void VisitLocalFunctionStatement (LocalFunctionStatementSyntax node)
		{
			base.VisitLocalFunctionStatement (node);
			ValidateDiagnostics (node, node.AttributeLists);
		}

		public override void VisitSimpleLambdaExpression (SimpleLambdaExpressionSyntax node)
		{
			base.VisitSimpleLambdaExpression (node);
			ValidateDiagnostics (node, node.AttributeLists);
		}

		public override void VisitParenthesizedLambdaExpression (ParenthesizedLambdaExpressionSyntax node)
		{
			base.VisitParenthesizedLambdaExpression (node);
			ValidateDiagnostics (node, node.AttributeLists);
		}

		public override void VisitAccessorDeclaration (AccessorDeclarationSyntax node)
		{
			base.VisitAccessorDeclaration (node);
			ValidateDiagnostics (node, node.AttributeLists);
		}

		private void ValidateDiagnostics (CSharpSyntaxNode memberSyntax, SyntaxList<AttributeListSyntax> attrLists)
		{
			var memberDiagnostics = _unmatched.Where (d => {
				// Filter down to diagnostics which originate from this member
				if (memberSyntax is ClassDeclarationSyntax classSyntax) {
					if (_semanticModel.GetDeclaredSymbol (classSyntax) is not ITypeSymbol typeSymbol)
						throw new NotImplementedException ("Unable to get type symbol for class declaration syntax.");

					if (typeSymbol.Locations.Length != 1)
						throw new NotImplementedException ("Type defined in multiple source locations.");

					// For classes, only consider diagnostics which originate from the type (not its members).
					// Approximate this by getting the location from the start of the type's syntax (which includes
					// attributes declared on the type) to the opening brace.
					var classSpan = TextSpan.FromBounds (
						classSyntax.GetLocation ().SourceSpan.Start,
						classSyntax.OpenBraceToken.GetLocation ().SourceSpan.Start
					);

					return d.Location.SourceSpan.IntersectsWith (classSpan);
				}

				return d.Location.SourceSpan.IntersectsWith (memberSyntax.Span);
			}).ToList ();

			foreach (var attrList in attrLists) {
				foreach (var attribute in attrList.Attributes) {
					switch (attribute.Name.ToString ()) {
					case "LogDoesNotContain":
						ValidateLogDoesNotContainAttribute (attribute, memberDiagnostics);
						break;
					case "ExpectedNoWarnings":
						_expectedNoWarnings.Add (attribute);
						break;
					}

					if (!IsExpectedDiagnostic (attribute))
						continue;

					if (!TryValidateExpectedDiagnostic (attribute, memberDiagnostics, out int? matchIndexResult, out string? missingDiagnosticMessage)) {
						_missing.Add ((attribute, missingDiagnosticMessage));
						continue;
					}

					int matchIndex = matchIndexResult.GetValueOrDefault ();
					var diagnostic = memberDiagnostics[matchIndex];
					memberDiagnostics.RemoveAt (matchIndex);
					Assert.True (_unmatched.Remove (diagnostic));
				}
			}
		}

		static bool IsExpectedDiagnostic (AttributeSyntax attribute)
		{
			switch (attribute.Name.ToString ()) {
			case "ExpectedWarning":
			case "LogContains":
				var args = LinkerTestBase.GetAttributeArguments (attribute);
				if (args.TryGetValue ("ProducedBy", out var producedBy)) {
					// Skip if this warning is not expected to be produced by any of the analyzers that we are currently testing.
					return GetProducedBy (producedBy).HasFlag (ProducedBy.Analyzer);
				}

				return true;
			default:
				return false;
			}

			static ProducedBy GetProducedBy (ExpressionSyntax expression)
			{
				var producedBy = (ProducedBy) 0x0;
				switch (expression) {
				case BinaryExpressionSyntax binaryExpressionSyntax:
					if (!Enum.TryParse<ProducedBy> ((binaryExpressionSyntax.Left as MemberAccessExpressionSyntax)!.Name.Identifier.ValueText, out var besProducedBy))
						throw new ArgumentException ("Expression must be a ProducedBy value", nameof (expression));
					producedBy |= besProducedBy;
					producedBy |= GetProducedBy (binaryExpressionSyntax.Right);
					break;

				case MemberAccessExpressionSyntax memberAccessExpressionSyntax:
					if (!Enum.TryParse<ProducedBy> (memberAccessExpressionSyntax.Name.Identifier.ValueText, out var maeProducedBy))
						throw new ArgumentException ("Expression must be a ProducedBy value", nameof (expression));
					producedBy |= maeProducedBy;
					break;

				default:
					break;
				}

				return producedBy;
			}
		}

		bool TryValidateExpectedDiagnostic (AttributeSyntax attribute, List<Diagnostic> diagnostics, [NotNullWhen (true)] out int? matchIndex, [NotNullWhen (false)] out string? missingDiagnosticMessage)
		{
			switch (attribute.Name.ToString ()) {
			case "ExpectedWarning":
				return TryValidateExpectedWarningAttribute (attribute!, diagnostics, out matchIndex, out missingDiagnosticMessage);
			case "LogContains":
				return TryValidateLogContainsAttribute (attribute!, diagnostics, out matchIndex, out missingDiagnosticMessage);
			default:
				throw new InvalidOperationException ($"Unsupported attribute type {attribute.Name}");
			}
		}

		private bool TryValidateExpectedWarningAttribute (AttributeSyntax attribute, List<Diagnostic> diagnostics, out int? matchIndex, out string? missingDiagnosticMessage)
		{
			missingDiagnosticMessage = null;
			matchIndex = null;
			var args = LinkerTestBase.GetAttributeArguments (attribute);
			string expectedWarningCode = LinkerTestBase.GetStringFromExpression (args["#0"], _semanticModel);

			if (!expectedWarningCode.StartsWith ("IL"))
				throw new InvalidOperationException ($"Expected warning code should start with \"IL\" prefix.");

			List<string> expectedMessages = args
				.Where (arg => arg.Key.StartsWith ("#") && arg.Key != "#0")
				.Select (arg => LinkerTestBase.GetStringFromExpression (arg.Value, _semanticModel))
				.ToList ();

			for (int i = 0; i < diagnostics.Count; i++) {
				if (Matches (diagnostics[i])) {
					matchIndex = i;
					return true;
				}
			}

			missingDiagnosticMessage = $"Warning '{expectedWarningCode}'. Expected to find warning containing:{string.Join (" ", expectedMessages.Select (m => "'" + m + "'"))}" +
					$", but no such message was found.{Environment.NewLine}";
			return false;

			bool Matches (Diagnostic diagnostic)
			{
				if (!attribute.Parent?.Parent?.Span.Contains (diagnostic.Location.SourceSpan) == true)
					return false;

				if (diagnostic.Id != expectedWarningCode)
					return false;

				foreach (var expectedMessage in expectedMessages)
					if (!diagnostic.GetMessage ().Contains (expectedMessage))
						return false;

				return true;
			}
		}

		private bool TryValidateLogContainsAttribute (AttributeSyntax attribute, List<Diagnostic> diagnostics, out int? matchIndex, out string? missingDiagnosticMessage)
		{
			if (!LogContains (attribute, diagnostics, out matchIndex, out string text)) {
				missingDiagnosticMessage = $"Could not find text:\n{text}\nIn diagnostics:\n{string.Join (Environment.NewLine, _diagnostics)}";
				return false;
			} else {
				missingDiagnosticMessage = null;
				return true;
			}
		}

		private void ValidateLogDoesNotContainAttribute (AttributeSyntax attribute, IReadOnlyList<Diagnostic> diagnosticMessages)
		{
			var args = LinkerTestBase.GetAttributeArguments (attribute);
			var arg = args["#0"];
			Assert.False (args.ContainsKey ("#1"));
			_ = LinkerTestBase.GetStringFromExpression (arg, _semanticModel);
			if (LogContains (attribute, diagnosticMessages, out var matchIndex, out var findText)) {
				Assert.True (false, $"LogDoesNotContain failure: Text\n\"{findText}\"\nfound in diagnostic:\n {diagnosticMessages[(int) matchIndex]}");
			}
		}

		private bool LogContains (AttributeSyntax attribute, IReadOnlyList<Diagnostic> diagnostics, [NotNullWhen (true)] out int? matchIndex, out string findText)
		{

			var args = LinkerTestBase.GetAttributeArguments (attribute);
			findText = LinkerTestBase.GetStringFromExpression (args["#0"], _semanticModel);

			// If the text starts with `warning IL...` then it probably follows the pattern
			//	'warning <diagId>: <location>:'
			// We don't want to repeat the location in the error message for the analyzer, so
			// it's better to just trim here. We've already filtered by diagnostic location so
			// the text location shouldn't matter
			if (findText.StartsWith ("warning IL")) {
				var firstColon = findText.IndexOf (": ");
				if (firstColon > 0) {
					var secondColon = findText.IndexOf (": ", firstColon + 1);
					if (secondColon > 0) {
						findText = findText.Substring (secondColon + 2);
					}
				}
			}

			bool isRegex = args.TryGetValue ("regexMatch", out var regexMatchExpr)
					&& regexMatchExpr.GetLastToken ().Value is bool regexMatch
					&& regexMatch;
			if (isRegex) {
				var regex = new Regex (findText);
				for (int i = 0; i < diagnostics.Count; i++) {
					if (regex.IsMatch (diagnostics[i].GetMessage ())) {
						matchIndex = i;
						return true;
					}
				}
			} else {
				for (int i = 0; i < diagnostics.Count; i++) {
					if (diagnostics[i].GetMessage ().Contains (findText)) {
						matchIndex = i;
						return true;
					}
				}
			}
			matchIndex = null;
			return false;
		}
	}
}