Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of clang warnings with "C/C++:" prefix #1021

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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]
Loading