-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathIssueLocationCollector.cs
358 lines (323 loc) · 15.8 KB
/
IssueLocationCollector.cs
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
352
353
354
355
356
357
358
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2022 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using FluentAssertions.Execution;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using SonarAnalyzer.Common;
using SonarAnalyzer.Helpers;
namespace SonarAnalyzer.UnitTest.TestFramework
{
/// <summary>
/// This class will look for specific patterns inside the unit test files being analyzed when testing a rule.
/// Here's a summary and examples of the different patterns that can be used to mark part of the code as noncompliant.
/// These patterns must appear after a single line comment (the supported comment tokens: "//" for C#, "'" for VB.NET and "<!--" for XML).
///
/// Simple 'Noncompliant' comment. Will mark the current line as expecting the primary location of an issue.
/// <code>
/// private void MyMethod() // Noncompliant
/// </code>
///
/// 'Secondary' location comment. Must be used together with a main primary location to mark the expected line of a
/// secondary location.
/// <code>
/// if (myCondition) // Noncompliant
/// {
/// var a = null; // Secondary
/// }
/// </code>
///
/// Using offsets. Using @[+-][0-9]+ after a 'Noncompliant' or 'Secondary' comment will mark the expected location to be
/// offset by the given number of lines.
/// <code>
/// private void MyMethod() // Noncompliant@+2 - issue is actually expected 2 lines after this comment
/// </code>
///
/// Checking the issue message. The message raised by the issue can be checked using the {{expected message}} pattern.
/// <code>
/// private void MyMethod() // Noncompliant {{Remove this unused private method}}
/// </code>
///
/// Checking the precise/exact location of an issue. Only one precise location or column location can be present
/// at one time. Precise location is used by adding '^^^^' comment under the location where the issue is expected.
/// The alternative column location pattern can be used by following the 'Noncompliant' or 'Secondary' comment
/// with '^X#Y' where 'X' is the expected start column and Y the length of the issue.
/// <code>
/// private void MyMethod() // Noncompliant
/// // ^^^^^^^
/// </code>
/// <code>
/// private void MyMethod() // Noncompliant ^4#7
/// </code>
///
/// Multiple issues per line. To declare that multiple issues are expected, each issue must be assigned an id. All
/// secondary locations associated with an issue must have the same id. Note that it is not possible to have multiple
/// precise/column locations on a single line.
/// <code>
/// var a = null; // Noncompliant [myId2]
/// if (myCondition) // Noncompliant [myId1, myId3]
/// {
/// a = null; // Secondary [myId1, myId2]
/// }
/// </code>
///
/// Note that most of the previous patterns can be used together when needed.
/// <code>
/// private void MyMethod() // Noncompliant@+1 ^4#7 [MyIssueId] {{Remove this unused private method}}
/// </code>
/// </summary>
public class IssueLocationCollector
{
private const string CommentPattern = @"(?<comment>//|'|<!--|/\*)";
private const string PrecisePositionPattern = @"\s*(?<position>\^+)(\s+(?<invalid>\^+))*";
private const string NoPrecisePositionPattern = @"(?<!\s*\^+\s)";
private const string IssueTypePattern = @"\s*(?<issueType>Noncompliant|Secondary)";
private const string ErrorTypePattern = @"\s*Error";
private const string OffsetPattern = @"(\s*@(?<offset>[+-]?\d+))?";
private const string ExactColumnPattern = @"(\s*\^(?<columnStart>\d+)#(?<length>\d+))?";
private const string IssueIdsPattern = @"(\s*\[(?<issueIds>.+)\])?";
private const string MessagePattern = @"(\s*\{\{(?<message>.+)\}\})?";
internal static readonly Regex RxIssue =
new Regex(CommentPattern + NoPrecisePositionPattern + IssueTypePattern + OffsetPattern + ExactColumnPattern + IssueIdsPattern + MessagePattern, RegexOptions.Compiled);
internal static readonly Regex RxPreciseLocation =
new Regex(@"^\s*" + CommentPattern + PrecisePositionPattern + IssueTypePattern + "?" + OffsetPattern + IssueIdsPattern + MessagePattern + @"\s*(-->|\*/)?$", RegexOptions.Compiled);
private static readonly Regex RxBuildError =
new Regex(CommentPattern + ErrorTypePattern + OffsetPattern + ExactColumnPattern + IssueIdsPattern, RegexOptions.Compiled);
private static readonly Regex RxInvalidType =
new Regex(CommentPattern + ".*" + IssueTypePattern, RegexOptions.Compiled);
private static readonly Regex RxInvalidxPreciseLocation =
new Regex(@"^\s*" + CommentPattern + ".*" + PrecisePositionPattern, RegexOptions.Compiled);
public static IList<IIssueLocation> GetExpectedIssueLocations(IEnumerable<TextLine> lines)
{
var preciseLocations = new List<IssueLocation>();
var locations = new List<IssueLocation>();
foreach (var line in lines)
{
var newPreciseLocations = GetPreciseIssueLocations(line);
if (newPreciseLocations.Any())
{
preciseLocations.AddRange(newPreciseLocations);
}
else if (GetIssueLocations(line) is IEnumerable<IssueLocation> newLocations && newLocations.Any())
{
locations.AddRange(newLocations);
}
else
{
EnsureNoInvalidFormat(line);
}
}
return EnsureNoDuplicatedPrimaryIds(MergeLocations(locations, preciseLocations));
}
internal static IEnumerable<IIssueLocation> GetExpectedBuildErrors(IEnumerable<TextLine> lines) =>
lines?.SelectMany(GetBuildErrorsLocations).OfType<IIssueLocation>() ?? Enumerable.Empty<IIssueLocation>();
internal static /*for testing*/ IList<IIssueLocation> MergeLocations(IEnumerable<IssueLocation> locations, IEnumerable<IssueLocation> preciseLocations)
{
var usedLocations = new List<IssueLocation>();
foreach (var location in locations)
{
var preciseLocationsOnSameLine = preciseLocations.Where(l => l.LineNumber == location.LineNumber);
if (preciseLocationsOnSameLine.Count() > 1)
{
ThrowUnexpectedPreciseLocationCount(preciseLocationsOnSameLine.Count(), preciseLocationsOnSameLine.First().LineNumber);
}
if (preciseLocationsOnSameLine.SingleOrDefault() is { } preciseLocation)
{
if (location.Start.HasValue)
{
throw new InvalidOperationException($"Unexpected redundant issue location on line {location.LineNumber}. Issue location can " +
$"be set either with 'precise issue location' or 'exact column location' pattern but not both.");
}
location.Start = preciseLocation.Start;
location.Length = preciseLocation.Length;
usedLocations.Add(preciseLocation);
}
}
return locations
.Union(preciseLocations.Except(usedLocations))
.Cast<IIssueLocation>()
.ToList();
}
internal static /*for testing*/ IEnumerable<IssueLocation> GetIssueLocations(TextLine line) =>
GetLocations(line, RxIssue);
internal static /*for testing*/ IEnumerable<IssueLocation> GetBuildErrorsLocations(TextLine line) =>
GetLocations(line, RxBuildError);
internal static /*for testing*/ IEnumerable<IssueLocation> GetPreciseIssueLocations(TextLine line)
{
var match = RxPreciseLocation.Match(line.ToString());
if (match.Success)
{
EnsureNoRemainingCurlyBrace(line, match);
return CreateIssueLocations(match, line.LineNumber);
}
return Enumerable.Empty<IssueLocation>();
}
private static IEnumerable<IssueLocation> GetLocations(TextLine line, Regex rx)
{
var match = rx.Match(line.ToString());
if (match.Success)
{
EnsureNoRemainingCurlyBrace(line, match);
return CreateIssueLocations(match, line.LineNumber + 1);
}
return Enumerable.Empty<IssueLocation>();
}
private static IEnumerable<IssueLocation> CreateIssueLocations(Match match, int lineNumber)
{
var line = lineNumber + GetOffset(match);
var isPrimary = GetIsPrimary(match);
var message = GetMessage(match);
var start = GetStart(match) ?? GetColumnStart(match);
var length = GetLength(match) ?? GetColumnLength(match);
var invalid = match.Groups["invalid"];
if (invalid.Success)
{
ThrowUnexpectedPreciseLocationCount(invalid.Captures.Count + 1, line);
}
return GetIssueIds(match).Select(
issueId => new IssueLocation
{
IsPrimary = isPrimary,
LineNumber = line,
Message = message,
IssueId = issueId,
Start = start,
Length = length,
});
}
private static int? GetStart(Match match)
{
var position = match.Groups["position"];
return position.Success ? (int?)position.Index : null;
}
private static int? GetLength(Match match)
{
var position = match.Groups["position"];
return position.Success ? (int?)position.Length : null;
}
private static int? GetColumnStart(Match match)
{
var columnStart = match.Groups["columnStart"];
return columnStart.Success ? (int?)int.Parse(columnStart.Value) - 1 : null;
}
private static int? GetColumnLength(Match match)
{
var length = match.Groups["length"];
return length.Success ? (int?)int.Parse(length.Value) : null;
}
private static bool GetIsPrimary(Match match)
{
var issueType = match.Groups["issueType"];
return !issueType.Success || issueType.Value == "Noncompliant";
}
private static string GetMessage(Match match)
{
var message = match.Groups["message"];
return message.Success ? message.Value : null;
}
private static int GetOffset(Match match)
{
var offset = match.Groups["offset"];
return offset.Success ? int.Parse(offset.Value) : 0;
}
private static IEnumerable<string> GetIssueIds(Match match)
{
var issueIds = match.Groups["issueIds"];
if (!issueIds.Success)
{
// We have a single issue without ID even if the group did not match
return new string[] { null };
}
return issueIds.Value
.Split(',')
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s))
.OrderBy(s => s);
}
private static void EnsureNoInvalidFormat(TextLine line)
{
var lineText = line.ToString();
if (RxInvalidType.IsMatch(lineText) || RxInvalidxPreciseLocation.IsMatch(lineText))
{
throw new InvalidOperationException($@"Line {line.LineNumber} looks like it contains comment for noncompliant code, but it is not recognized as one of the expected pattern.
Either remove the Noncompliant/Secondary word or precise pattern '^^' from the comment, or fix the pattern.");
}
}
private static IList<IIssueLocation> EnsureNoDuplicatedPrimaryIds(IList<IIssueLocation> mergedLocations)
{
var duplicateLocationsIds = mergedLocations
.Where(x => x.IsPrimary && x.IssueId != null)
.GroupBy(x => x.IssueId)
.FirstOrDefault(group => group.Count() > 1);
if (duplicateLocationsIds != null)
{
var duplicatedIdLines = duplicateLocationsIds.Select(issueLocation => issueLocation.LineNumber).JoinStr(", ");
throw new InvalidOperationException($"Primary location with id [{duplicateLocationsIds.Key}] found on multiple lines: {duplicatedIdLines}");
}
return mergedLocations;
}
private static void EnsureNoRemainingCurlyBrace(TextLine line, Match match)
{
var remainingLine = line.ToString().Substring(match.Index + match.Length);
if (remainingLine.Contains("{") || remainingLine.Contains("}"))
{
Execute.Assertion.FailWith("Unexpected '{{' or '}}' found on line: {0}. Either correctly use the '{{{{message}}}}' " +
"format or remove the curly braces on the line of the expected issue", line.LineNumber);
}
}
private static void ThrowUnexpectedPreciseLocationCount(int count, int line)
{
var message = $"Expecting only one precise location per line, found {count} on line {line}. " +
@"If you want to specify more than one precise location per line you need to omit the Noncompliant comment:
internal class MyClass : IInterface1 // there should be no Noncompliant comment
^^^^^^^ {{Do not create internal classes.}}
^^^^^^^^^^^ @-1 {{IInterface1 is bad for your health.}}";
throw new InvalidOperationException(message);
}
[DebuggerDisplay("ID:{IssueId} @{LineNumber} Primary:{IsPrimary} Start:{Start} Length:{Length} '{Message}'")]
internal class IssueLocation : IIssueLocation
{
public bool IsPrimary { get; set; }
public int LineNumber { get; set; }
public string Message { get; set; }
public string IssueId { get; set; }
public int? Start { get; set; }
public int? Length { get; set; }
public IssueLocation(Diagnostic diagnostic) : this(diagnostic.GetMessage(), diagnostic.Location)
{
IsPrimary = true;
IssueId = diagnostic.Id;
}
public IssueLocation(SecondaryLocation secondaryLocation) : this(secondaryLocation.Message, secondaryLocation.Location) { }
public IssueLocation() { }
private IssueLocation(string message, Location location)
{
Message = message;
LineNumber = location.GetLineNumberToReport();
Start = location.GetLineSpan().StartLinePosition.Character;
Length = location.SourceSpan.Length;
}
}
}
}