forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGcc4CompilerParser.java
79 lines (67 loc) · 2.64 KB
/
Gcc4CompilerParser.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 edu.hm.hafner.analysis.parser;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.util.LookaheadStream;
import edu.hm.hafner.util.StringContainsUtils;
/**
* A parser for gcc 4.x compiler warnings.
*
* @author Frederic Chateau
*/
public class Gcc4CompilerParser extends LookaheadParser {
private static final long serialVersionUID = 5490211629355204910L;
private static final String GCC_WARNING_PATTERN =
ANT_TASK + "(.+?):(\\d+):(?:(\\d+):)? ?([wW]arning|.*[Ee]rror): (.*)$";
private static final Pattern CLASS_PATTERN = Pattern.compile("\\[-W(.+)]$");
/**
* Creates a new instance of {@link Gcc4CompilerParser}.
*/
public Gcc4CompilerParser() {
super(GCC_WARNING_PATTERN);
}
@Override
protected boolean isLineInteresting(final String line) {
return line.contains("arning") || line.contains("rror");
}
@Override
protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
final IssueBuilder builder) {
StringBuilder message = new StringBuilder(matcher.group(5));
Matcher classMatcher = CLASS_PATTERN.matcher(message.toString());
if (classMatcher.find() && classMatcher.group(1) != null) {
builder.setCategory(classMatcher.group(1));
}
while (lookahead.hasNext() && isMessageContinuation(lookahead)) {
message.append('\n');
message.append(lookahead.next());
}
return builder.setFileName(matcher.group(1))
.setLineStart(matcher.group(2))
.setColumnStart(matcher.group(3))
.setMessage(message.toString())
.setSeverity(Severity.guessFromString(matcher.group(4)))
.buildOptional();
}
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
private boolean isMessageContinuation(final LookaheadStream lookahead) {
String peek = lookahead.peekNext();
if (peek.length() < 3) {
return false;
}
if (peek.charAt(0) == '/' || peek.charAt(0) == '[' || peek.charAt(0) == '<' || peek.charAt(0) == '=') {
return false;
}
if (peek.charAt(1) == ':') {
return false;
}
if (peek.charAt(2) == '/' || peek.charAt(0) == '\\') {
return false;
}
return !StringContainsUtils.containsAnyIgnoreCase(peek, "arning", "rror", "make");
}
}