Skip to content

Commit c42bc13

Browse files
authored
fix: The CppCheck parser should retain the order of violations (#195)
The CppCheck parser handels individual locations of a warning as separate violations. These violations are then returned to the caller as a sorted set, destroying the insertion order. To visualize the affected locations in the original order, we need to store the insertion order by adding a new property. See https://issues.jenkins.io/browse/JENKINS-75217
1 parent 4dd80bc commit c42bc13

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

violations-lib/src/main/java/se/bjurr/violations/lib/parsers/CPPCheckParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public Set<Violation> parseReportOutput(
3838
Optional<String> verbose = null;
3939
String id = null;
4040
int errorIndex = -1;
41+
int order = 0;
4142
boolean violationAddedFromError = false;
4243
String message = null;
4344
while (xmlr.hasNext()) {
@@ -106,6 +107,7 @@ public Set<Violation> parseReportOutput(
106107
.setSeverity(severity) //
107108
.setRule(id) //
108109
.setGroup(Integer.toString(errorIndex)) //
110+
.setSpecific("order", order++)
109111
.build();
110112
violations.add(v);
111113
violationAddedFromError = true;

violations-lib/src/test/java/se/bjurr/violations/lib/CPPCheckTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void testThatViolationsCanBeParsed() {
5454
.setMessage(MSG_1) //
5555
.setSeverity(INFO) //
5656
.setGroup("1") //
57+
.setSpecific("order", 1)
5758
.build();
5859

5960
final Violation v2 =
@@ -66,6 +67,7 @@ public void testThatViolationsCanBeParsed() {
6667
.setMessage(MSG_2) //
6768
.setSeverity(ERROR) //
6869
.setGroup("2") //
70+
.setSpecific("order", 2)
6971
.build();
7072
ViolationAsserter.assertThat(actual) //
7173
.contains(v2, 2) //
@@ -269,6 +271,24 @@ public void testThatViolationsCanBeParsedFromIssue64519() {
269271
.isEqualTo(0);
270272
}
271273

274+
@Test
275+
public void testThatViolationsCanBeParsedFromIssue75217() {
276+
final String rootFolder = getRootFolder();
277+
278+
final Set<Violation> actual =
279+
violationsApi() //
280+
.withPattern(".*issue75217\\.xml$") //
281+
.inFolder(rootFolder) //
282+
.findAll(CPPCHECK) //
283+
.violations();
284+
285+
assertThat(actual)
286+
.hasSize(2)
287+
.satisfiesExactly(
288+
v -> assertThat(v.getSpecifics()).containsEntry("order", "1"),
289+
v -> assertThat(v.getSpecifics()).containsEntry("order", "0"));
290+
}
291+
272292
@Test
273293
public void testThatColumnCanBeParsed() {
274294
final String rootFolder = getRootFolder();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<results version="2">
3+
<cppcheck version="2.0"/>
4+
<errors>
5+
<error id="missingOverride" severity="style" msg="The function 'reset' overrides a function in a base class but is not marked with a 'override' specifier." verbose="The function 'reset' overrides a function in a base class but is not marked with a 'override' specifier.">
6+
<location file="derived.hpp" line="115" column="7" info="Function in derived class"/>
7+
<location file="base.hpp" line="117" column="15" info="Virtual function in base class"/>
8+
<symbol>reset</symbol>
9+
</error>
10+
</errors>
11+
</results>

0 commit comments

Comments
 (0)