Skip to content

Commit

Permalink
Fixed file parsing under Windows, fixed PC-lint parser, added detecti…
Browse files Browse the repository at this point in the history
…on of MISRA errors for PC-lint
  • Loading branch information
Øyvind Rørtveit committed Feb 13, 2018
1 parent be94e08 commit 45d308d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 59 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ It supports:
* [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport)
* [_Klocwork_](http://www.klocwork.com/products-services/klocwork/static-code-analysis)
* [_MyPy_](https://pypi.python.org/pypi/mypy-lang)
* [_PCLint_](http://www.gimpel.com/html/pcl.htm) PC-Lint using the same output format as the Jenkins warnings plugin, [_details here_](https://wiki.jenkins.io/display/JENKINS/PcLint+options)
* [_PerlCritic_](https://github.com/Perl-Critic)
* [_PiTest_](http://pitest.org/)
* [_PyDocStyle_](https://pypi.python.org/pypi/pydocstyle)
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/se/bjurr/violations/lib/ViolationsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ public List<Violation> violations() {
return foundViolations;
}

private String makeWindowsFriendly(String regularExpression)
{
return regularExpression.replace("/", "(?:/|\\\\)");
}

public ViolationsApi withPattern(String regularExpression) {
pattern = regularExpression;
pattern = makeWindowsFriendly(regularExpression);
return this;
}
}
137 changes: 93 additions & 44 deletions src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,104 @@
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getParts;

import java.util.regex.*;
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;
}
@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> lines = getLines(string);
System.out.println(lines.size());
Pattern misraPattern = Pattern.compile("\\[MISRA.*\\]");
for (String line : lines) {
Matcher misraMatcher = misraPattern.matcher(line);
if (misraMatcher.find()) {
parseMisraViolation(line, violations);
} else {
parseGeneralViolation(line, violations);
}
}
return violations;
}

private void parseMisraViolation(String line, List<Violation> violations) {
List<String> parts = getParts(line,
"^([^\\(]+)\\(",
"^([\\d]+)\\): ",
"^(?:Error|Warning|Info|Note) [\\d]+: ([^\\[]*)",
"^\\[(.*),",
"(mandatory|required|advisory)\\]",
"^(.*)$");
if (parts.isEmpty()) {
return;
}
String filename = parts.get(0);
Integer lineNumber = parseInt(parts.get(1));

String severityString = parts.get(4);
SEVERITY severity = toMisraSeverity(severityString);
String rule = parts.get(3) + ", " + severityString;
String message = parts.get(2) + " " + parts.get(5);
violations.add( //
violationBuilder() //
.setParser(PCLINT) //
.setStartLine(lineNumber) //
.setFile(filename) //
.setRule(rule) //
.setSeverity(severity) //
.setMessage(message) //
.build() //
);
}

private void parseGeneralViolation(String line, List<Violation> violations) {
List<String> parts = getParts(line,
"^([^\\(]+)\\(",
"^([\\d]+)\\): ",
"^(Error|Warning|Info|Note) ",
"^([\\d]+): ", "^(.*)$");
if (parts.isEmpty()) {
return;
}
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() //
);
}

private SEVERITY toSeverity(String severity) {
if (severity.equals("Error")) {
return ERROR;
}
if (severity.equals("Warning")) {
return WARN;
}
return INFO;
}

private SEVERITY toMisraSeverity(String severity) {
if (severity.equals("mandatory")) {
return ERROR;
}
if (severity.equals("required")) {
return WARN;
}
return INFO;
}
}
48 changes: 41 additions & 7 deletions src/test/java/se/bjurr/violations/lib/PCLintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PCLintTest {
@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual =
violationsApi() //
.withPattern(".*/pclint/.*\\.txt$") //
Expand All @@ -27,7 +27,7 @@ public void testThatViolationsCanBeParsed() {
.violations();

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

assertThat(actual.get(0)) //
.isEqualTo( //
Expand All @@ -37,7 +37,7 @@ public void testThatViolationsCanBeParsed() {
.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>)") //
"Mismatched essential type categories for binary operator") //
.setSeverity(INFO) //
.build() //
);
Expand All @@ -50,7 +50,7 @@ public void testThatViolationsCanBeParsed() {
.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>)") //
"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)") //
.setSeverity(WARN) //
.build() //
);
Expand All @@ -62,8 +62,8 @@ public void testThatViolationsCanBeParsed() {
.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) //
"Pointer parameter 'txFrame' (line 100) could be declared as pointing to const") //
.setSeverity(INFO) //
.build() //
);
assertThat(actual.get(6)) //
Expand All @@ -74,9 +74,43 @@ public void testThatViolationsCanBeParsed() {
.setStartLine(123) //
.setRule("48") //
.setMessage(
"Bad type [MISRA 2012 Rule 10.1, required] (Error <a href=\"/userContent/LintMsgRef.html#48\">48</a>)") //
"Bad type") //
.setSeverity(ERROR) //
.build() //
);
}

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

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

Violation violation = actual.get(1);

assertThat(violation.getRule().get())
.isEqualTo("MISRA 2012 Rule 10.4, mandatory");
assertThat(violation.getSeverity())
.isEqualTo(ERROR);

violation = actual.get(2);

assertThat(violation.getRule().get())
.isEqualTo("MISRA 2012 Rule 1.3, required");
assertThat(violation.getSeverity())
.isEqualTo(WARN);

violation = actual.get(7);

assertThat(violation.getRule().get())
.isEqualTo("MISRA 2012 Rule 10.1, advisory");
assertThat(violation.getSeverity())
.isEqualTo(INFO);
assertThat(violation.getMessage())
.isEqualTo("Bad type (Error <a href=\"/userContent/LintMsgRef.html#48\">48</a>)");
}
}
19 changes: 12 additions & 7 deletions src/test/resources/pclint/pclint.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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>)
C:\UST3\qse30\Drivers\drvADC.c(84): Note 9029: Mismatched essential type categories for binary operator

--- 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>)
C:\UST3\qse30\Drivers\drvCAN.c(68): Note 9029: Mismatched essential type categories for binary operator [MISRA 2012 Rule 10.4, mandatory] (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]
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)
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)
C:\UST3\qse30\Drivers\drvCAN.c(134): Info 818: Pointer parameter 'txFrame' (line 100) could be declared as pointing to const


--- Module: C:\UST3\qse30\Drivers\drvNTC.c (C)
Expand All @@ -18,4 +18,9 @@ C:\UST3\qse30\Drivers\drvCAN.c(134): Info 818: Pointer parameter 'txFrame' (line


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




0 comments on commit 45d308d

Please sign in to comment.