forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker-framework.groovy
568 lines (519 loc) · 19.8 KB
/
checker-framework.groovy
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import groovy.transform.Field
import groovy.transform.Immutable
import groovy.util.slurpersupport.GPathResult
import groovy.util.slurpersupport.NodeChildren
import groovy.xml.XmlUtil
import java.util.regex.Matcher
import java.util.regex.Pattern
@Field static final String USAGE_STRING = "Usage groovy " +
".${File.separator}.ci${File.separator}checker-framework.groovy" +
" [profile] [-g | --generate-suppression]\n"
int exitCode = 1
if (args.length == 2) {
exitCode = parseArgumentAndExecute(args[0], args[1])
}
else if (args.length == 1) {
exitCode = parseArgumentAndExecute(args[0], null)
}
else {
throw new IllegalArgumentException(USAGE_STRING)
}
System.exit(exitCode)
/**
* Parse the command line arguments passed in and execute the branch based on the arguments.
*
* @param argument command line argument
* @return {@code 0} if command executes successfully, {@code 1} otherwise
*/
private int parseArgumentAndExecute(String argument, String flag) {
final int exitCode
final Set<String> profiles = getCheckerFrameworkProfiles()
if (profiles.contains(argument)) {
if (flag != null && flag != "-g" && flag != "--generate-suppression") {
final String exceptionMessage = "Unexpected flag: '${flag}'\n" + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
exitCode = checkCheckerFrameworkReport(argument, flag)
}
else if (argument == "--list") {
println "Supported profiles:"
profiles.each { println it }
exitCode = 0
}
else {
final String exceptionMessage = "Unexpected argument: '${argument}'\n" + USAGE_STRING
throw new IllegalArgumentException(exceptionMessage)
}
return exitCode
}
/**
* Parse the pom.xml file to get all the available checker framework profiles.
*
* @return A set of all available checker framework profiles
*/
private static Set<String> getCheckerFrameworkProfiles() {
final GPathResult mainNode = new XmlSlurper().parse(".${File.separator}pom.xml")
final NodeChildren ids = mainNode.profiles.profile.id as NodeChildren
final Set<String> profiles = new HashSet<>()
ids.each { node ->
final GPathResult id = node as GPathResult
final String idText = id.text()
if (idText.startsWith("checker-framework-")) {
profiles.add(idText)
}
}
return profiles
}
/**
* Check the generated checker framework report. Parse the errors and compare them with
* the suppressed errors.
*
* @param profile the checker framework profile to execute
* @param flag command line argument flag to determine output format
* @return {@code 0} if checker framework report is as expected, {@code 1} otherwise
*/
private static int checkCheckerFrameworkReport(String profile, String flag) {
final XmlParser xmlParser = new XmlParser()
final String suppressedErrorsFileUri =
".${File.separator}.ci${File.separator}" +
"checker-framework-suppressions${File.separator}${profile}-suppressions.xml"
final List<String> checkerFrameworkErrors = getCheckerFrameworkErrors(profile)
Set<CheckerFrameworkError> errors = Collections.emptySet()
if (!checkerFrameworkErrors.isEmpty()) {
errors = getErrorFromText(checkerFrameworkErrors)
}
final File suppressionFile = new File(suppressedErrorsFileUri)
Set<CheckerFrameworkError> suppressedErrors = Collections.emptySet()
if (suppressionFile.exists()) {
final Node suppressedErrorsNode = xmlParser.parse(suppressedErrorsFileUri)
suppressedErrors = getSuppressedErrors(suppressedErrorsNode)
}
return compareErrors(errors, suppressedErrors, flag)
}
/**
* Generates the checker framework report and filters out the errors.
*
* @param profile the checker framework profile to execute
* @return A set of errors
*/
private static List<List<String>> getCheckerFrameworkErrors(String profile) {
final List<String> checkerFrameworkLines = [] as ArrayList
final String command = "mvn -e --no-transfer-progress clean compile -P${profile}"
final Process process = getOsSpecificCmd(command).execute()
process.in.eachLine { line ->
checkerFrameworkLines.add(line)
println(line)
}
process.waitFor()
final List<List<String>> checkerFrameworkErrors = [][] as ArrayList
for (int index = 0; index < checkerFrameworkLines.size(); index++) {
final String line = checkerFrameworkLines.get(index)
if (line.startsWith("[WARNING]")) {
final List<String> error = [] as ArrayList
error.add(line)
int currentErrorIndex = index + 1
while (currentErrorIndex < checkerFrameworkLines.size()) {
final String currentLine = checkerFrameworkLines.get(currentErrorIndex)
if (currentLine.startsWith("[ERROR]")
|| currentLine.startsWith("[INFO]")
|| currentLine.startsWith("[WARNING]")) {
break
}
error.add(currentLine)
currentErrorIndex++
}
checkerFrameworkErrors.add(error)
}
}
return checkerFrameworkErrors
}
/**
* Get OS specific command.
*
* @param cmd input command
* @return OS specific command
*/
private static String getOsSpecificCmd(String cmd) {
final String osSpecificCmd
if (System.getProperty("os.name").toLowerCase().contains('windows')) {
osSpecificCmd = "cmd /c ${cmd}"
}
else {
osSpecificCmd = cmd
}
return osSpecificCmd
}
/**
* Get a set of {@link CheckerFrameworkError} from text.
*
* @param errorsText errors in text format
* @return A set of errors
*/
private static Set<CheckerFrameworkError> getErrorFromText(List<List<String>> errorsList) {
final Set<CheckerFrameworkError> errors = new HashSet<>()
final Pattern errorExtractingPattern = Pattern
.compile(".*[\\\\/](checkstyle[\\\\/]src.*\\.java):\\[(\\d+)[^]]*][^\\[]*\\[([^]]*)](.*)")
final Pattern filePathExtractingPattern = Pattern.compile("\\[WARNING] (.*\\.java)")
final int fileNameGroup = 1
final int lineNumberGroup = 2
final int specifierGroup = 3
final int messageGroup = 4
errorsList.each { errorList ->
final String error = errorList.get(0)
final Matcher matcher = errorExtractingPattern.matcher(error)
final List<String> details = [] as ArrayList
String fileName = null
String specifier = null
String message = null
String lineContent = null
int lineNumber = 0
if (matcher.matches()) {
fileName = matcher.group(fileNameGroup)
lineNumber = Integer.parseInt(matcher.group(lineNumberGroup))
specifier = XmlUtil.escapeXml(matcher.group(specifierGroup).trim())
message = XmlUtil.escapeXml(matcher.group(messageGroup).trim())
final Matcher filePathMatcher = filePathExtractingPattern.matcher(error)
if (filePathMatcher.find()) {
final String absoluteFilePath = filePathMatcher.group(1)
final File file = new File(absoluteFilePath)
lineContent = XmlUtil.escapeXml(file.readLines().get(lineNumber - 1).trim())
}
if (errorList.size() > 1) {
for (int index = 1; index < errorList.size(); index++) {
final String errorDetail = XmlUtil.escapeXml(errorList.get(index).trim())
if (!errorDetail.isEmpty()) {
details.add(errorDetail)
}
}
}
// Errors extracted from Checker Framework Report are by default considered stable.
final boolean isUnstable = false
final CheckerFrameworkError checkerFrameworkError = new CheckerFrameworkError(
fileName, specifier, message, details, lineContent, lineNumber, isUnstable)
errors.add(checkerFrameworkError)
}
}
return errors
}
/**
* Get the suppressed error. All child nodes of the main {@code suppressedErrors} node
* are parsed.
*
* @param mainNode the main {@code suppressedErrors} node
* @return A set of suppressed errors
*/
private static Set<CheckerFrameworkError> getSuppressedErrors(Node mainNode) {
final List<Node> children = mainNode.children()
final Set<CheckerFrameworkError> suppressedErrors = new HashSet<>()
children.each { node ->
final Node errorNode = node as Node
suppressedErrors.add(getError(errorNode))
}
return suppressedErrors
}
/**
* Construct the {@link CheckerFrameworkError} object from the {@code checkerFrameworkError}
* XML node. The suppression file is parsed to get the {@code checkerFrameworkError} node.
*
* @param errorNode the {@code error} XML node
* @return {@link CheckerFrameworkError} object represented by the {@code error} XML node
*/
private static CheckerFrameworkError getError(Node errorNode) {
final List childNodes = errorNode.children()
final List<String> details = [] as ArrayList
String fileName = null
String specifier = null
String message = null
String lineContent = null
final int lineNumber = -1
childNodes.each {
final Node childNode = it as Node
final String text = childNode.name()
final String childNodeText = XmlUtil.escapeXml(childNode.text())
switch (text) {
case "fileName":
fileName = childNodeText
break
case "specifier":
specifier = childNodeText
break
case "message":
message = childNodeText
break
case "lineContent":
lineContent = childNodeText
break
case "details":
final String[] detailsArray = childNodeText.split("\\n")
detailsArray.each { detail ->
final String detailString = detail.trim()
if (!detailString.isEmpty()) {
details.add(detailString)
}
}
}
}
final String unstableAttributeValue = errorNode.attribute("unstable")
final boolean isUnstable = Boolean.parseBoolean(unstableAttributeValue)
return new CheckerFrameworkError(fileName, specifier, message, details, lineContent,
lineNumber, isUnstable)
}
/**
* Compare the actual and the suppressed errors. The comparison passes successfully
* (i.e. returns 0) when:
* <ol>
* <li>Surviving and suppressed errors are equal.</li>
* <li>There are extra suppressed errors but they are unstable
* i.e. {@code unstable="true"}.</li>
* </ol>
* The comparison fails when (i.e. returns 1) when:
* <ol>
* <li>Surviving errors are not present in the suppressed list.</li>
* <li>There are errors in the suppression list that are not there is surviving list.</li>
* </ol>
*
* @param actualErrors A set of actual errors reported by error prone
* @param suppressedErrors A set of suppressed errors from suppression file
* @param flag command line argument flag to determine output format
* @return {@code 0} if comparison passes successfully
*/
private static int compareErrors(Set<CheckerFrameworkError> actualErrors,
Set<CheckerFrameworkError> suppressedErrors,
String flag) {
final Set<CheckerFrameworkError> unsuppressedErrors =
setDifference(actualErrors, suppressedErrors)
final Set<CheckerFrameworkError> extraSuppressions =
setDifference(suppressedErrors, actualErrors)
final int exitCode
if (actualErrors == suppressedErrors) {
exitCode = 0
}
else if (unsuppressedErrors.isEmpty()
&& !hasOnlyStableErrors(extraSuppressions)) {
exitCode = 0
}
else {
if (!unsuppressedErrors.isEmpty()) {
println "New surviving error(s) found:"
unsuppressedErrors.each {
printError(flag, it)
}
}
if (!extraSuppressions.isEmpty()
&& extraSuppressions.any { !it.isUnstable() }) {
println "\nUnnecessary suppressed error(s) found and should be removed:"
extraSuppressions.each {
if (!it.isUnstable()) {
printError(flag, it)
}
}
}
exitCode = 1
}
if (exitCode == 0) {
println "Build successful with no errors."
}
return exitCode
}
/**
* Whether a set has only stable errors.
*
* @param errors A set of errors
* @return {@code true} if a set has only stable errors
*/
private static boolean hasOnlyStableErrors(Set<CheckerFrameworkError> errors) {
return errors.every { !it.isUnstable() }
}
/**
* Prints the error according to the nature of the flag.
*
* @param flag command line argument flag to determine output format
* @param error error to print
*/
private static void printError(String flag, CheckerFrameworkError error) {
if (flag != null) {
println error.toXmlString()
}
else {
println error
}
}
/**
* Determine the difference between 2 sets. The result is {@code setOne - setTwo}.
*
* @param setOne The first set in the difference
* @param setTwo The second set in the difference
* @return {@code setOne - setTwo}
*/
private static Set<CheckerFrameworkError> setDifference(final Set<CheckerFrameworkError> setOne,
final Set<CheckerFrameworkError> setTwo) {
final Set<CheckerFrameworkError> result = new TreeSet<>(setOne)
result.removeIf { error -> setTwo.contains(error) }
return result
}
/**
* A class to represent the XML {@code checkerFrameworkError} node.
*/
@Immutable
class CheckerFrameworkError implements Comparable<CheckerFrameworkError> {
/**
* Error nodes present in suppressions file do not have a {@code lineNumber}.
* The {@code lineNumber} is set to {@code -1} for such errors.
*/
private static final int LINE_NUMBER_NOT_PRESENT_VALUE = -1
String fileName
String specifier
String message
List<String> details
String lineContent
int lineNumber
/**
* Whether the error is unstable. Unstable errors in suppression list are not flagged as
* unnecessary suppressions. An error is considered to be unstable when:
* <ul>
* <li>
* Error message changes with each run. Some error messages contains strings like
* {@code temp-var-1234}, the numerical part changes with each run so the error is
* considered unstable. In such cases numerical values in {@code details} and
* {@code message} are replaced with empty string while comparing and hashing errors.
* </li>
* <li>
* The error is in general unstable, it sometimes appears and sometimes it does not.
* </li>
*/
boolean unstable
@Override
String toString() {
String toString = """
File Name: "${getFileName()}"
Specifier: "${getSpecifier()}"
Message: "${getMessage()}"
Line Contents: "${getLineContent()}\"""".stripIndent()
if (getLineNumber() != LINE_NUMBER_NOT_PRESENT_VALUE) {
toString += '\nLine Number: ' + getLineNumber()
}
final List<String> details = getDetails()
if (!details.isEmpty()) {
toString += '\nDetails: ' + details.get(0)
if (details.size() > 1) {
for (int index = 1; index < details.size(); index++) {
toString += '\n' + ' ' * 9 + details.get(index)
}
}
}
return toString
}
@Override
int compareTo(CheckerFrameworkError other) {
int i = getFileName() <=> other.getFileName()
if (i != 0) {
return i
}
i = getSpecifier() <=> other.getSpecifier()
if (i != 0) {
return i
}
if (this.isUnstable() || other.isUnstable()) {
final String messageWithoutLineNumber = getMessage().replaceAll('\\d+', '')
final String thatMessageWithoutLineNumber = other.getMessage().replaceAll('\\d+', '')
i = messageWithoutLineNumber <=> thatMessageWithoutLineNumber
if (i != 0) {
return i
}
final List<String> detailsWithoutLineNumber = this.getDetails()*.replaceAll('\\d+', '')
final List<String> thatDetailsWithoutLineNumber =
other.getDetails()*.replaceAll('\\d+', '')
i = detailsWithoutLineNumber.join('') <=> thatDetailsWithoutLineNumber.join('')
if (i != 0) {
return i
}
}
else {
i = getMessage() <=> other.getMessage()
if (i != 0) {
return i
}
i = getDetails().join('') <=> other.getDetails().join('')
if (i != 0) {
return i
}
}
return getLineContent() <=> other.getLineContent()
}
boolean equals(o) {
if (this.is(o)) {
return true
}
if (!(o instanceof CheckerFrameworkError)) {
return false
}
CheckerFrameworkError that = (CheckerFrameworkError) o
if (this.unstable || that.unstable) {
final String messageWithoutLineNumber = this.message.replaceAll('\\d+', '')
final String thatMessageWithoutLineNumber = that.message.replaceAll('\\d+', '')
if (messageWithoutLineNumber != thatMessageWithoutLineNumber) {
return false
}
final List<String> detailsWithoutLineNumber = this.details*.replaceAll('\\d+', '')
final List<String> thatDetailsWithoutLineNumber = that.details*.replaceAll('\\d+', '')
if (!detailsWithoutLineNumber.equals(thatDetailsWithoutLineNumber)) {
return false
}
}
else {
if (!details.equals(that.details)) {
return false
}
if (message != that.message) {
return false
}
}
if (fileName != that.fileName) {
return false
}
if (lineContent != that.lineContent) {
return false
}
return specifier == that.specifier
}
int hashCode() {
int result
if (unstable) {
result = (message != null ? message.replaceAll('\\d+', '').hashCode() : 0)
result = 31 * result + (details != null ? details*.replaceAll(
'\\d+', '').hashCode() : 0)
}
else {
result = (message != null ? message.hashCode() : 0)
result = 31 * result + (details != null ? details.hashCode() : 0)
}
result = 31 * result + (fileName != null ? fileName.hashCode() : 0)
result = 31 * result + (specifier != null ? specifier.hashCode() : 0)
result = 31 * result + (lineContent != null ? lineContent.hashCode() : 0)
return result
}
/**
* XML format of the checker framework error.
*
* @return XML format of the checker framework error
*/
String toXmlString() {
final List<String> details = getDetails()
String toXmlString = """
<checkerFrameworkError unstable="${isUnstable()}">
<fileName>${getFileName()}</fileName>
<specifier>${getSpecifier()}</specifier>
<message>${getMessage()}</message>
<lineContent>${getLineContent()}</lineContent>
""".stripIndent(10)
if (!details.isEmpty()) {
toXmlString += " <details>"
getDetails().each {
toXmlString += '\n' + ' ' * 6 + it
}
toXmlString += "\n </details>\n"
}
toXmlString += " </checkerFrameworkError>"
return toXmlString
}
}