From 3460211d9556c730e22e9b6633eaaecb93f9d630 Mon Sep 17 00:00:00 2001 From: Dennis Bauer <37552885+DennisBauer@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:03:16 +0100 Subject: [PATCH] Fix parsing of clang warnings with "C/C++:" prefix The prefix was detected as part of the file name leading to a java.nio.file.InvalidPathException in IssueBuilder.isAbsolutePath(). In addition to fixing the path parsing in ClangParser the Exception thrown in the IssueBuilder.isAbsolutePath() is also catched. --- .../edu/hm/hafner/analysis/IssueBuilder.java | 10 ++++-- .../hafner/analysis/parser/ClangParser.java | 14 ++++---- .../analysis/parser/ClangParserTest.java | 34 +++++++++++++++++++ .../hm/hafner/analysis/parser/llvm-clang.txt | 5 +++ 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/edu/hm/hafner/analysis/parser/llvm-clang.txt diff --git a/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java b/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java index bb30006f1..c64f3d172 100644 --- a/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java +++ b/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java @@ -3,6 +3,7 @@ import java.io.Serializable; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; @@ -623,8 +624,13 @@ private static boolean isAbsolutePath(final String stringPath) { catch (URISyntaxException e) { // Catch and ignore as system paths are not URI and we need to check them separately. } - Path path = Paths.get(stringPath); - return path.isAbsolute(); + try { + Path path = Paths.get(stringPath); + return path.isAbsolute(); + } + catch (InvalidPathException e) { + return true; + } } private static String normalizeFileName(@CheckForNull final String platformFileName) { diff --git a/src/main/java/edu/hm/hafner/analysis/parser/ClangParser.java b/src/main/java/edu/hm/hafner/analysis/parser/ClangParser.java index 35d68f943..05f50773d 100644 --- a/src/main/java/edu/hm/hafner/analysis/parser/ClangParser.java +++ b/src/main/java/edu/hm/hafner/analysis/parser/ClangParser.java @@ -18,7 +18,7 @@ public class ClangParser extends LookaheadParser { private static final long serialVersionUID = -3015592762345283182L; - private static final String CLANG_WARNING_PATTERN = "^\\s*(?:\\d+%)?([^%]*?):(\\d+):(?:(\\d+):)?" + "(?:" + private static final String CLANG_WARNING_PATTERN = "^\\s*(?:\\d+%)?(C\\/C\\+\\+: )?([^%]*?):(\\d+):(?:(\\d+):)?" + "(?:" + "(?:\\{\\d+:\\d+-\\d+:\\d+\\})+:)?\\s*(warning|[^\\[\\]]*error):" + "\\s*(.*?)(?:\\[([^\\[]*)\\])?$"; private static final Pattern IGNORE_FORMAT = Pattern.compile("^-\\[.*\\].*$"); @@ -32,17 +32,17 @@ public ClangParser() { @Override protected Optional createIssue(final Matcher matcher, final LookaheadStream lookahead, final IssueBuilder builder) { - String message = matcher.group(5); + String message = matcher.group(6); if (IGNORE_FORMAT.matcher(message).matches()) { return Optional.empty(); } - return builder.setFileName(matcher.group(1)) - .setLineStart(matcher.group(2)) - .setColumnStart(matcher.group(3)) - .setCategory(matcher.group(6)) + return builder.setFileName(matcher.group(2)) + .setLineStart(matcher.group(3)) + .setColumnStart(matcher.group(4)) + .setCategory(matcher.group(7)) .setMessage(message) - .setSeverity(mapPriority(matcher.group(4))) + .setSeverity(mapPriority(matcher.group(5))) .buildOptional(); } diff --git a/src/test/java/edu/hm/hafner/analysis/parser/ClangParserTest.java b/src/test/java/edu/hm/hafner/analysis/parser/ClangParserTest.java index 53093dcca..ff0fc6e75 100644 --- a/src/test/java/edu/hm/hafner/analysis/parser/ClangParserTest.java +++ b/src/test/java/edu/hm/hafner/analysis/parser/ClangParserTest.java @@ -196,4 +196,38 @@ void issue14333() { .hasSeverity(Severity.WARNING_NORMAL); } } + + @Test + void assertThatClangPathsAreCorrectForAllPlatforms() { + Report warnings = parse("llvm-clang.txt"); + + assertThat(warnings).hasSize(3); + + try (SoftAssertions softly = new SoftAssertions()) { + softly.assertThat(warnings.get(0)).hasLineStart(35) + .hasLineEnd(35) + .hasColumnStart(15) + .hasColumnEnd(15) + .hasMessage("unused parameter 'parameter1'") + .hasFileName("/project/src/cpp/MyClass.cpp") + .hasCategory("-Wunused-parameter") + .hasSeverity(Severity.WARNING_NORMAL); + softly.assertThat(warnings.get(1)).hasLineStart(35) + .hasLineEnd(35) + .hasColumnStart(15) + .hasColumnEnd(15) + .hasMessage("unused parameter 'parameter1'") + .hasFileName("C:/project/src/cpp/MyClass.cpp") + .hasCategory("-Wunused-parameter") + .hasSeverity(Severity.WARNING_NORMAL); + softly.assertThat(warnings.get(2)).hasLineStart(35) + .hasLineEnd(35) + .hasColumnStart(15) + .hasColumnEnd(15) + .hasMessage("unused parameter 'parameter1'") + .hasFileName("MyClass.cpp") + .hasCategory("-Wunused-parameter") + .hasSeverity(Severity.WARNING_NORMAL); + } + } } diff --git a/src/test/resources/edu/hm/hafner/analysis/parser/llvm-clang.txt b/src/test/resources/edu/hm/hafner/analysis/parser/llvm-clang.txt new file mode 100644 index 000000000..3989b74ea --- /dev/null +++ b/src/test/resources/edu/hm/hafner/analysis/parser/llvm-clang.txt @@ -0,0 +1,5 @@ +C/C++: /project/src/cpp/MyClass.cpp:35:15: warning: unused parameter 'parameter1' [-Wunused-parameter] + +C/C++: C:\project\src\cpp\MyClass.cpp:35:15: warning: unused parameter 'parameter1' [-Wunused-parameter] + +C/C++: MyClass.cpp:35:15: warning: unused parameter 'parameter1' [-Wunused-parameter] \ No newline at end of file