From df7c0eabd748e1fea4797ec076f29d88f54ec2f2 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 4 Oct 2023 10:44:24 +0200 Subject: [PATCH] Simplify regular expression and extract correct filename from linker. --- .../hafner/analysis/parser/MsBuildParser.java | 44 ++++++++++--------- .../analysis/parser/MsBuildParserTest.java | 6 +-- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/parser/MsBuildParser.java b/src/main/java/edu/hm/hafner/analysis/parser/MsBuildParser.java index 50897364f..fd54043d1 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/MsBuildParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/MsBuildParser.java @@ -22,13 +22,22 @@ public class MsBuildParser extends LookaheadParser { private static final long serialVersionUID = -2141974437420906595L; private static final String MS_BUILD_WARNING_PATTERN - = "(?:^(?:.*)Command line warning ([A-Za-z0-9]+):\\s*(.*)\\s*\\[(.*)\\])|" - + ANT_TASK + "(?:(?:\\s*(?:\\d+|\\d+:\\d+)>)?(?:(?:(?:(.*?)\\((\\d*)(?:,(\\d+))?[a-zA-Z]*?\\)|.*LINK)\\s*:|" - + "(.*):)\\s*([A-z-_]*\\s(?:[Nn]ote|[Ii]nfo|[Ww]arning|(?:fatal\\s*)?[Ee]rror))[^A-Za-z0-9]\\s*:?\\s*([A-Za-z0-9\\-_]+)?" - + "\\s*:\\s(?:\\s*([A-Za-z0-9.]+)\\s*:)?\\s*(.*?)(?: \\[([^\\]]*)[/\\\\][^\\]\\\\]+\\])?" - + "|(.*)\\s*:.*(?:error|warning)\\s*(LNK[0-9]+):\\s*(.*)))$"; - - private final Pattern ignoredToolsPattern = Pattern.compile("(?!.exe)(\\.[^.]+)$"); + = "(?:^(?:.*)Command line warning ([A-Za-z0-9]+):\\s*(.*)\\s*\\[(.*)\\])" + + "|" + + ANT_TASK + "(?:" + + "(?:\\s*(?:\\d+|\\d+:\\d+)>)?(?:(?:(?:(.*?)\\((\\d*)(?:,(\\d+))?[a-zA-Z]*?\\)|.*LINK)" + + "\\s*:|" + + "(.*):)\\s*([A-z-_]*\\s(?:[Nn]ote|[Ii]nfo|[Ww]arning|(?:fatal\\s*)?[Ee]rror))[^A-Za-z0-9]\\s*" + + ":?" + + "\\s*([A-Za-z0-9\\-_]+)?" + + "\\s*:\\s" + + "(?:\\s*([A-Za-z0-9.]+)\\s*:)?" + + "\\s*(.*?)" + + "(?: \\[([^\\]]*)[/\\\\][^\\]\\\\]+\\])?" + + "))$"; + + private static final Pattern IGNORED_TOOLS_PATTERN = Pattern.compile("(?!.exe)(\\.[^.]+)$"); + private static final Pattern LINKER_CAUSE = Pattern.compile(".*imported by '([A-Za-z0-9\\-_.]+)'.*"); /** * Creates a new instance of {@link MsBuildParser}. @@ -42,7 +51,7 @@ protected Optional createIssue(final Matcher matcher, final LookaheadStre final IssueBuilder builder) { String fileName = determineFileName(matcher); - Matcher fileExtensionMatcher = ignoredToolsPattern.matcher(fileName); + Matcher fileExtensionMatcher = IGNORED_TOOLS_PATTERN.matcher(fileName); if (!fileExtensionMatcher.find()) { return Optional.empty(); } @@ -56,14 +65,6 @@ protected Optional createIssue(final Matcher matcher, final LookaheadStre .setSeverity(Severity.WARNING_NORMAL) .buildOptional(); } - if (StringUtils.isNotBlank(matcher.group(13))) { - return builder.setLineStart(0) - .setCategory(matcher.group(14)) - .setType(matcher.group(13)) - .setMessage(matcher.group(15)) - .setSeverity(Severity.guessFromString(matcher.group(8))) - .buildOptional(); - } if (StringUtils.isNotEmpty(matcher.group(10))) { return builder.setLineStart(matcher.group(5)) .setColumnStart(matcher.group(6)) @@ -87,7 +88,7 @@ protected Optional createIssue(final Matcher matcher, final LookaheadStre } /** - * Determines the name of the file that is cause of the warning. + * Determines the name of the file that is the cause of the warning. * * @param matcher * the matcher to get the matches from @@ -103,12 +104,15 @@ private String determineFileName(final Matcher matcher) { else if (StringUtils.isNotBlank(matcher.group(7))) { fileName = matcher.group(7); } - else if (StringUtils.isNotBlank(matcher.group(13))) { - fileName = matcher.group(13); - } else { fileName = matcher.group(4); } + if (StringUtils.isBlank(fileName)) { + var linker = LINKER_CAUSE.matcher(matcher.group(11)); + if (linker.matches()) { + return linker.group(1); + } + } if (StringUtils.isBlank(fileName)) { fileName = StringUtils.substringBetween(matcher.group(11), "'"); } diff --git a/src/test/java/edu/hm/hafner/analysis/parser/MsBuildParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/MsBuildParserTest.java index fe180fd09..1a60723e6 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/MsBuildParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/MsBuildParserTest.java @@ -464,16 +464,16 @@ void issue3582() { @Test void issue63580() { Report warnings = parse("issue63580.txt"); - + assertThat(warnings).hasSize(1); assertThatReportHasSeverities(warnings, 0, 0, 1, 0); try (SoftAssertions softly = new SoftAssertions()) { softly.assertThat(warnings.get(0)) - .hasFileName("TestLib.lib") + .hasFileName("def.obj") .hasCategory("LNK4217") .hasSeverity(Severity.WARNING_NORMAL) - .hasMessage("cannot open input file 'TestLib.lib'") + .hasMessage("symbol 'XYZ' defined in 'abc.obj' is imported by 'def.obj' in function 'FGH'") .hasDescription("") .hasPackageName("-") .hasLineStart(0)