Skip to content

Commit

Permalink
Fix parsing of clang warnings with "C/C++:" prefix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DennisBauer committed Feb 8, 2024
1 parent f7c4a82 commit 3460211
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/main/java/edu/hm/hafner/analysis/IssueBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Check warning on line 632 in src/main/java/edu/hm/hafner/analysis/IssueBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 631-632 are not covered by tests

Check warning on line 632 in src/main/java/edu/hm/hafner/analysis/IssueBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/edu/hm/hafner/analysis/IssueBuilder.java#L631-L632

Added lines #L631 - L632 were not covered by tests
}
}

private static String normalizeFileName(@CheckForNull final String platformFileName) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/edu/hm/hafner/analysis/parser/ClangParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("^-\\[.*\\].*$");

Expand All @@ -32,17 +32,17 @@ public ClangParser() {
@Override
protected Optional<Issue> 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();
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/edu/hm/hafner/analysis/parser/ClangParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 3460211

Please sign in to comment.