Skip to content

Commit

Permalink
Added PC-lint parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Rørtveit committed Feb 12, 2018
1 parent 02355e0 commit be94e08
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
63 changes: 63 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package se.bjurr.violations.lib.parsers;

import static java.lang.Integer.parseInt;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.PCLINT;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getParts;

import java.util.ArrayList;
import java.util.List;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class PCLintParser implements ViolationsParser {

@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> lines = getLines(string);
for (String line : lines) {
List<String> parts =
getParts(
line,
"^(.+)\\(",
"^([\\d]+)\\): ",
"^(Error|Warning|Info|Note) ",
"^([\\d]+): ",
"^(.*)$");
if (parts.isEmpty()) {
continue;
}
String filename = parts.get(0);
Integer lineNumber = parseInt(parts.get(1));
SEVERITY severity = toSeverity(parts.get(2));
String rule = parts.get(3);
String message = parts.get(4);
violations.add( //
violationBuilder() //
.setParser(PCLINT) //
.setStartLine(lineNumber) //
.setFile(filename) //
.setRule(rule) //
.setSeverity(severity) //
.setMessage(message) //
.build() //
);
}
return violations;
}

public SEVERITY toSeverity(String severity) {
if (severity.equals("Error")) {
return ERROR;
}
if (severity.equals("Warning")) {
return WARN;
}
return INFO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.CPPLINT;
import static se.bjurr.violations.lib.reports.Parser.PERLCRITIC;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getParts;

Expand Down Expand Up @@ -41,7 +41,7 @@ public List<Violation> parseReportOutput(String string) throws Exception {

violations.add( //
violationBuilder() //
.setParser(CPPLINT) //
.setParser(PERLCRITIC) //
.setStartLine(lineNumber) //
.setColumn(columnNumber) //
.setFile(filename) //
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/se/bjurr/violations/lib/reports/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import se.bjurr.violations.lib.parsers.KlocworkParser;
import se.bjurr.violations.lib.parsers.LintParser;
import se.bjurr.violations.lib.parsers.MyPyParser;
import se.bjurr.violations.lib.parsers.PCLintParser;
import se.bjurr.violations.lib.parsers.PMDParser;
import se.bjurr.violations.lib.parsers.PerlCriticParser;
import se.bjurr.violations.lib.parsers.PiTestParser;
Expand Down Expand Up @@ -73,7 +74,8 @@ public enum Parser {
STYLECOP(new StyleCopParser()), //
XMLLINT(new XMLLintParser()), //
ZPTLINT(new ZPTLintParser()), //
DOCFX(new DocFXParser());
DOCFX(new DocFXParser()), //
PCLINT(new PCLintParser());

private static Logger LOG = Logger.getLogger(Parser.class.getSimpleName());
private transient ViolationsParser violationsParser;
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/se/bjurr/violations/lib/PCLintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.PCLINT;

import java.util.List;
import org.junit.Test;
import se.bjurr.violations.lib.model.Violation;

public class PCLintTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual =
violationsApi() //
.withPattern(".*/pclint/.*\\.txt$") //
.inFolder(rootFolder) //
.findAll(PCLINT) //
.violations();

assertThat(actual) //
.hasSize(7);

assertThat(actual.get(0)) //
.isEqualTo( //
violationBuilder() //
.setParser(PCLINT) //
.setFile("C:\\UST3\\qse30\\Drivers\\drvADC.c") //
.setStartLine(84) //
.setRule("9029") //
.setMessage(
"Mismatched essential type categories for binary operator [MISRA 2012 Rule 10.4, required] (Note <a href=\"/userContent/LintMsgRef.html#9029\">9029</a>)") //
.setSeverity(INFO) //
.build() //
);

assertThat(actual.get(3)) //
.isEqualTo( //
violationBuilder() //
.setParser(PCLINT) //
.setFile("C:\\UST3\\qse30\\Drivers\\drvCAN.c") //
.setStartLine(73) //
.setRule("534") //
.setMessage(
"Ignoring return value of function 'PIC_CAN_Transmit(can_frame_t *)' (compare with line 68, file C:\\UST3\\qse30\\HAL\\hal_ext.h, module C:\\UST3\\qse30\\Drivers\\drvADC.c) [MISRA 2012 Directive 4.7, required], [MISRA 2012 Rule 17.7, required] (Warning <a href=\"/userContent/LintMsgRef.html#534\">534</a>)") //
.setSeverity(WARN) //
.build() //
);
assertThat(actual.get(5)) //
.isEqualTo( //
violationBuilder() //
.setParser(PCLINT) //
.setFile("C:\\UST3\\qse30\\Drivers\\drvCAN.c") //
.setStartLine(134) //
.setRule("818") //
.setMessage(
"Pointer parameter 'txFrame' (line 100) could be declared as pointing to const [MISRA 2012 Rule 8.13, advisory] (Info <a href=\"/userContent/LintMsgRef.html#818\">818</a>)") //
.setSeverity(ERROR) //
.build() //
);
assertThat(actual.get(6)) //
.isEqualTo( //
violationBuilder() //
.setParser(PCLINT) //
.setFile("C:\\UST3\\qse30\\Modules\\COMM\\J1939\\Broadcast\\dm13.c") //
.setStartLine(123) //
.setRule("48") //
.setMessage(
"Bad type [MISRA 2012 Rule 10.1, required] (Error <a href=\"/userContent/LintMsgRef.html#48\">48</a>)") //
.setSeverity(ERROR) //
.build() //
);
}
}
21 changes: 21 additions & 0 deletions src/test/resources/pclint/pclint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PC-lint for C/C++ (NT) Vers. 9.00L, Copyright Gimpel Software 1985-2014

--- Module: C:\UST3\qse30\Drivers\drvADC.c (C)
C:\UST3\qse30\Drivers\drvADC.c(84): Note 9029: Mismatched essential type categories for binary operator [MISRA 2012 Rule 10.4, required] (Note <a href="/userContent/LintMsgRef.html#9029">9029</a>)

--- Module: C:\UST3\qse30\Drivers\drvCAN.c (C)
C:\UST3\qse30\Drivers\drvCAN.c(68): Note 9029: Mismatched essential type categories for binary operator [MISRA 2012 Rule 10.4, required] (Note <a href="/userContent/LintMsgRef.html#9029">9029</a>)
C:\UST3\qse30\Drivers\drvCAN.c(72): Note 931: Both sides have side effects [MISRA 2012 Rule 1.3, required], [MISRA 2012 Rule 13.2, required] (Note <a href="/userContent/LintMsgRef.html#931">931</a>)
C:\UST3\qse30\Drivers\drvCAN.c(73): Warning 534: Ignoring return value of function 'PIC_CAN_Transmit(can_frame_t *)' (compare with line 68, file C:\UST3\qse30\HAL\hal_ext.h, module C:\UST3\qse30\Drivers\drvADC.c) [MISRA 2012 Directive 4.7, required], [MISRA 2012 Rule 17.7, required] (Warning <a href="/userContent/LintMsgRef.html#534">534</a>)
C:\UST3\qse30\Drivers\drvCAN.c(128): Warning 534: Ignoring return value of function 'PIC_CAN_Transmit(can_frame_t *)' (compare with line 68, file C:\UST3\qse30\HAL\hal_ext.h, module C:\UST3\qse30\Drivers\drvADC.c) [MISRA 2012 Directive 4.7, required], [MISRA 2012 Rule 17.7, required] (Warning <a href="/userContent/LintMsgRef.html#534">534</a>)
C:\UST3\qse30\Drivers\drvCAN.c(134): Info 818: Pointer parameter 'txFrame' (line 100) could be declared as pointing to const [MISRA 2012 Rule 8.13, advisory] (Info <a href="/userContent/LintMsgRef.html#818">818</a>)


--- Module: C:\UST3\qse30\Drivers\drvNTC.c (C)

--- Module: C:\UST3\qse30\Drivers\drvPWM.c (C)
(0): Info 766: Header file 'C:\UST3\qse30\Kernel\Common\common_types.h' not used in module 'C:\UST3\qse30\Drivers\drvPWM.c' (Info <a href="/userContent/LintMsgRef.html#766">766</a>)


--- Module: C:\UST3\qse30\Modules\COMM\J1939\Broadcast\dm13.c (C)
C:\UST3\qse30\Modules\COMM\J1939\Broadcast\dm13.c(123): Error 48: Bad type [MISRA 2012 Rule 10.1, required] (Error <a href="/userContent/LintMsgRef.html#48">48</a>)

0 comments on commit be94e08

Please sign in to comment.