-
Notifications
You must be signed in to change notification settings - Fork 39
/
CppLintParser.java
78 lines (73 loc) · 2.61 KB
/
CppLintParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package se.bjurr.violations.lib.parsers;
import static java.lang.Integer.parseInt;
import static java.util.logging.Level.SEVERE;
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.CPPLINT;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getParts;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import se.bjurr.violations.lib.ViolationsLogger;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
public class CppLintParser implements ViolationsParser {
@Override
public Set<Violation> parseReportOutput(
final String string, final ViolationsLogger violationsLogger) throws Exception {
final Set<Violation> violations = new TreeSet<>();
final List<String> lines = getLines(string);
for (final String line : lines) {
final List<String> parts =
getParts(line, "\\[([^\\]]*)\\]$", "\\[([^\\]]*)\\]$", "^([^:]*):", "^([^:]*):", "(.*)");
if (parts.isEmpty()) {
continue; // Happens for the line "Done processing cpp/test.cpp"
}
try {
final Integer severity = parseInt(parts.get(0));
final String rule = parts.get(1);
final String filename = parts.get(2);
int lineNumber;
if (parts.get(3).equalsIgnoreCase("None")) {
lineNumber = 0;
} else {
lineNumber = parseInt(parts.get(3));
}
final String message = parts.get(4);
violations.add( //
violationBuilder() //
.setParser(CPPLINT) //
.setStartLine(lineNumber) //
.setFile(filename) //
.setRule(rule) //
.setSeverity(this.toSeverity(severity)) //
.setMessage(message) //
.build() //
);
} catch (final IndexOutOfBoundsException | NullPointerException e) {
violationsLogger.log(
SEVERE,
"Was unable to parse: \""
+ line
+ "\" found parts: "
+ parts
+ " content:\n\n"
+ string,
e);
}
}
return violations;
}
public SEVERITY toSeverity(final Integer severity) {
if (severity >= 5) {
return ERROR;
}
if (severity >= 3) {
return WARN;
}
return INFO;
}
}