Skip to content

Commit

Permalink
Autoformatting and removed debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
Øyvind Rørtveit committed Feb 13, 2018
1 parent 45d308d commit 5e3d49f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 131 deletions.
7 changes: 3 additions & 4 deletions src/main/java/se/bjurr/violations/lib/ViolationsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ public List<Violation> 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;
Expand Down
182 changes: 93 additions & 89 deletions src/main/java/se/bjurr/violations/lib/parsers/PCLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
}
@Override
public List<Violation> parseReportOutput(String string) throws Exception {
List<Violation> violations = new ArrayList<>();
List<String> 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<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));

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() //
);
}

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 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 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;
}
}
69 changes: 31 additions & 38 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 @@ -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() //
);
Expand Down Expand Up @@ -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<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>)");
}
@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>)");
}
}

0 comments on commit 5e3d49f

Please sign in to comment.