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

CMake warnings support #132

Merged
merged 12 commits into from
Mar 31, 2019
Merged
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>edu.hm.hafner</groupId>
<artifactId>analysis-model</artifactId>
<version>4.0.1-SNAPSHOT</version>
<version>4.1.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/parser/CMakeParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.hm.hafner.analysis.parser;

import java.util.Optional;
import java.util.regex.Matcher;

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.util.LookaheadStream;

/**
* A parser for CMake warnings.
*
* @author Uwe Brandt
*/
public class CMakeParser extends LookaheadParser {
private static final long serialVersionUID = 8149238560432255036L;

private static final String CMAKE_WARNING_PATTERN =
"CMake\\s+Warning(?:.*?(?<file>\\S+)){0,1}(?::(?<line>\\d+)\\s+(?<category>\\S+)){0,1}\\s*:";

/**
* Creates a new instance of {@link CMakeParser}.
*/
public CMakeParser() {
super(CMAKE_WARNING_PATTERN);
}

@Override
protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
final IssueBuilder builder) {
// if the category is contained in brackets, remove those brackets
String category = StringUtils.strip(matcher.group("category"), "()");
return builder.setFileName(matcher.group("file"))
.setLineStart(matcher.group("line"))
.setCategory(category)
.setMessage(readMessage(lookahead))
.setSeverity(Severity.WARNING_NORMAL)
.buildOptional();
}

private String readMessage(final LookaheadStream lookahead) {
if (lookahead.hasNext()) {
return lookahead.next();
}
return "";
}
}

52 changes: 52 additions & 0 deletions src/test/java/edu/hm/hafner/analysis/parser/CMakeParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.hm.hafner.analysis.parser;

import edu.hm.hafner.analysis.AbstractParserTest;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.assertj.SoftAssertions;

/**
* Tests the class {@link CMakeParser}.
*
* @author Uwe Brandt
*/
wunderabt marked this conversation as resolved.
Show resolved Hide resolved
class CMakeParserTest extends AbstractParserTest {
CMakeParserTest() {
super("cmake.txt");
}

@Override
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) {
softly.assertThat(report).hasSize(4);
softly.assertThat(report.get(0))
.hasSeverity(Severity.WARNING_NORMAL)
.hasCategory("")
.hasLineStart(0)
.hasMessage("[step1] Manually-specified variables were not used by the project")
.hasFileName("-");
softly.assertThat(report.get(1))
.hasSeverity(Severity.WARNING_NORMAL)
.hasCategory("")
.hasLineStart(0)
.hasMessage("[step2] The build directory is a subdirectory of the source directory.")
wunderabt marked this conversation as resolved.
Show resolved Hide resolved
.hasFileName("CMakeLists.txt");
softly.assertThat(report.get(2))
.hasSeverity(Severity.WARNING_NORMAL)
.hasCategory("option")
.hasLineStart(10)
.hasMessage("I'm the message")
.hasFileName("tools/gtest-1.8/googlemock/CMakeLists.txt");
softly.assertThat(report.get(3))
.hasSeverity(Severity.WARNING_NORMAL)
.hasCategory("message")
.hasLineStart(423)
.hasMessage("Special workaround applied")
.hasFileName("project/utils/fancy.cmake");
}

@Override
protected CMakeParser createParser() {
return new CMakeParser();
}
}

11 changes: 11 additions & 0 deletions src/test/resources/edu/hm/hafner/analysis/parser/cmake.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[step1] CMake Warning:
[step1] Manually-specified variables were not used by the project

[step2] CMake Warning in CMakeLists.txt:
[step2] The build directory is a subdirectory of the source directory.

CMake Warning (dev) at tools/gtest-1.8/googlemock/CMakeLists.txt:10 (option):
I'm the message

CMake Warning at project/utils/fancy.cmake:423 (message):
Special workaround applied