|
| 1 | +package edu.hm.hafner.analysis.parser; |
| 2 | + |
| 3 | +import java.io.Serial; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.regex.Matcher; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | +import org.apache.commons.text.StringEscapeUtils; |
| 9 | + |
| 10 | +import edu.hm.hafner.analysis.Issue; |
| 11 | +import edu.hm.hafner.analysis.IssueBuilder; |
| 12 | +import edu.hm.hafner.analysis.LookaheadParser; |
| 13 | +import edu.hm.hafner.analysis.Severity; |
| 14 | +import edu.hm.hafner.util.LookaheadStream; |
| 15 | + |
| 16 | +/** |
| 17 | + * A parser for GCC cc1/cc1plus internal compiler warnings and errors. |
| 18 | + * These messages do not include file names or line numbers and are emitted by |
| 19 | + * the compiler's internal phases. |
| 20 | + * |
| 21 | + * @author Akash Manna |
| 22 | + * @see <a href="https://issues.jenkins.io/browse/JENKINS-73509">Issue 73509</a> |
| 23 | + */ |
| 24 | +public class Gcc4Cc1Parser extends LookaheadParser { |
| 25 | + @Serial |
| 26 | + private static final long serialVersionUID = 1L; |
| 27 | + |
| 28 | + private static final String GCC_CC1_WARNING_PATTERN = "^(?:In .+?:\\s*)?" |
| 29 | + + "(?<compiler>cc1(?:plus)?):\\s*" |
| 30 | + + "(?<severity>warning|error|note):\\s*" |
| 31 | + + "(?<message>.*)$"; |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new instance of {@link Gcc4Cc1Parser}. |
| 35 | + */ |
| 36 | + public Gcc4Cc1Parser() { |
| 37 | + super(GCC_CC1_WARNING_PATTERN); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead, |
| 42 | + final IssueBuilder builder) { |
| 43 | + String compilerName = matcher.group("compiler"); |
| 44 | + String severityLevel = matcher.group("severity"); |
| 45 | + String messageContent = matcher.group("message"); |
| 46 | + |
| 47 | + if (StringUtils.isBlank(compilerName) || StringUtils.isBlank(severityLevel) |
| 48 | + || StringUtils.isBlank(messageContent)) { |
| 49 | + return Optional.empty(); |
| 50 | + } |
| 51 | + |
| 52 | + var completeMessage = new StringBuilder(messageContent); |
| 53 | + while (lookahead.hasNext() && Gcc4CompilerParser.isMessageContinuation(lookahead)) { |
| 54 | + completeMessage.append(' '); |
| 55 | + completeMessage.append(lookahead.next()); |
| 56 | + } |
| 57 | + |
| 58 | + String message = completeMessage.toString(); |
| 59 | + String category = extractCategory(message); |
| 60 | + |
| 61 | + return builder.setFileName("-") |
| 62 | + .setLineStart(0) |
| 63 | + .setCategory(category) |
| 64 | + .setMessage(StringEscapeUtils.escapeXml10(message)) |
| 65 | + .setSeverity(Severity.guessFromString(severityLevel)) |
| 66 | + .buildOptional(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected boolean isLineInteresting(final String line) { |
| 71 | + return line.contains("warning") || line.contains("error") || line.contains("note"); |
| 72 | + } |
| 73 | + |
| 74 | + private String extractCategory(final String message) { |
| 75 | + int start = message.lastIndexOf("[-W"); |
| 76 | + if (start == -1) { |
| 77 | + return "GCC warning"; |
| 78 | + } |
| 79 | + |
| 80 | + int end = message.indexOf(']', start); |
| 81 | + if (end == -1) { |
| 82 | + return "GCC warning"; |
| 83 | + } |
| 84 | + |
| 85 | + String category = message.substring(start + 3, end); |
| 86 | + int equalsPos = category.indexOf('='); |
| 87 | + if (equalsPos != -1) { |
| 88 | + return category.substring(0, equalsPos); |
| 89 | + } |
| 90 | + return category; |
| 91 | + } |
| 92 | +} |
0 commit comments