diff --git a/src/main/java/se/bjurr/violations/lib/ViolationsApi.java b/src/main/java/se/bjurr/violations/lib/ViolationsApi.java index 4190aeb7..00db0faf 100644 --- a/src/main/java/se/bjurr/violations/lib/ViolationsApi.java +++ b/src/main/java/se/bjurr/violations/lib/ViolationsApi.java @@ -84,11 +84,10 @@ public List violations() { return foundViolations; } - private String makeWindowsFriendly(String regularExpression) - { - return regularExpression.replace("/", "(?:/|\\\\)"); + private String makeWindowsFriendly(String regularExpression) { + return regularExpression.replace("/", "(?:/|\\\\)"); } - + public ViolationsApi withPattern(String regularExpression) { pattern = makeWindowsFriendly(regularExpression); return this; diff --git a/src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java b/src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java index e1a94957..794e2958 100644 --- a/src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java +++ b/src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java @@ -9,104 +9,108 @@ 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 java.util.regex.*; import se.bjurr.violations.lib.model.SEVERITY; import se.bjurr.violations.lib.model.Violation; public class PCLintParser implements ViolationsParser { - @Override - public List parseReportOutput(String string) throws Exception { - List violations = new ArrayList<>(); - List 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; - } + @Override + public List parseReportOutput(String string) throws Exception { + List violations = new ArrayList<>(); + List lines = getLines(string); + 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 violations) { + List 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)); - private void parseMisraViolation(String line, List violations) { - List 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() // + ); + } - 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 violations) { + List 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 void parseGeneralViolation(String line, List violations) { - List 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 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; - } + private SEVERITY toMisraSeverity(String severity) { + if (severity.equals("mandatory")) { + return ERROR; + } + if (severity.equals("required")) { + return WARN; + } + return INFO; + } } diff --git a/src/test/java/se/bjurr/violations/lib/PCLintTest.java b/src/test/java/se/bjurr/violations/lib/PCLintTest.java index 130a2a3b..ce8319a6 100644 --- a/src/test/java/se/bjurr/violations/lib/PCLintTest.java +++ b/src/test/java/se/bjurr/violations/lib/PCLintTest.java @@ -18,7 +18,7 @@ public class PCLintTest { @Test public void testThatViolationsCanBeParsed() { String rootFolder = getRootFolder(); - + List actual = violationsApi() // .withPattern(".*/pclint/.*\\.txt$") // @@ -36,8 +36,7 @@ public void testThatViolationsCanBeParsed() { .setFile("C:\\UST3\\qse30\\Drivers\\drvADC.c") // .setStartLine(84) // .setRule("9029") // - .setMessage( - "Mismatched essential type categories for binary operator") // + .setMessage("Mismatched essential type categories for binary operator") // .setSeverity(INFO) // .build() // ); @@ -73,44 +72,38 @@ public void testThatViolationsCanBeParsed() { .setFile("C:\\UST3\\qse30\\Modules\\COMM\\J1939\\Broadcast\\dm13.c") // .setStartLine(123) // .setRule("48") // - .setMessage( - "Bad type") // + .setMessage("Bad type") // .setSeverity(ERROR) // .build() // ); } - @Test - public void testThatSeverityAndRulenumberFromMisraTakesPrecedence() { - String rootFolder = getRootFolder(); - - List 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 48)"); - } + @Test + public void testThatSeverityAndRulenumberFromMisraTakesPrecedence() { + String rootFolder = getRootFolder(); + + List 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 48)"); + } }