Skip to content

Commit

Permalink
Simplify regular expression and extract correct filename from linker.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner authored and Пятизбянцев Илья Андреевич committed Oct 4, 2023
1 parent cb7a590 commit df7c0ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
44 changes: 24 additions & 20 deletions src/main/java/edu/hm/hafner/analysis/parser/MsBuildParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -42,7 +51,7 @@ protected Optional<Issue> 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();
}
Expand All @@ -56,14 +65,6 @@ protected Optional<Issue> 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))
Expand All @@ -87,7 +88,7 @@ protected Optional<Issue> 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
Expand All @@ -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), "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit df7c0ea

Please sign in to comment.