Skip to content

Commit

Permalink
CPPCheck support #3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 6, 2016
1 parent d5879a2 commit e1a7ad7
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ It supports:
* [_PMD_](https://pmd.github.io/)
* [_Findbugs_](http://findbugs.sourceforge.net/)
* [_Checkstyle_](http://checkstyle.sourceforge.net/)
* [_CPPCheck_](http://cppcheck.sourceforge.net/)
* [_CSSLint_](https://github.com/CSSLint/csslint)
* [_JSHint_](http://jshint.com/)

Expand Down
60 changes: 60 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/CPPCheckParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Reporter.CPPCHECK;

import java.io.File;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class CPPCheckParser extends ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
String string = Files.toString(file, Charsets.UTF_8);
List<Violation> violations = newArrayList();
List<String> errorChunks = getChunks(string, "<error", "</error>");
for (String errorChunk : errorChunks) {
String severity = getAttribute(errorChunk, "severity");
String msg = getAttribute(errorChunk, "msg");
String verbose = getAttribute(errorChunk, "verbose");
String id = getAttribute(errorChunk, "id");
List<String> locationChunks = getChunks(errorChunk, "<location", "/>");
for (String locationChunk : locationChunks) {
Integer line = getIntegerAttribute(locationChunk, "line");
String fileString = getAttribute(errorChunk, "file");
violations.add(//
violationBuilder()//
.setReporter(CPPCHECK)//
.setStartLine(line)//
.setFile(fileString)//
.setSeverity(toSeverity(severity))//
.setMessage(msg + ". " + verbose)//
.setRule(id)//
.build()//
);
}
}
return violations;
}

public SEVERITY toSeverity(String severity) {
if (severity.equalsIgnoreCase("error")) {
return ERROR;
}
if (severity.equalsIgnoreCase("warning")) {
return WARN;
}
return INFO;
}

}
4 changes: 3 additions & 1 deletion src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.logging.Logger;

import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.parsers.CPPCheckParser;
import se.bjurr.violations.lib.parsers.CSSLintParser;
import se.bjurr.violations.lib.parsers.CheckStyleParser;
import se.bjurr.violations.lib.parsers.FindbugsParser;
Expand All @@ -20,7 +21,8 @@ public enum Reporter {
CSSLINT(new CSSLintParser()), //
FINDBUGS(new FindbugsParser()), //
JSHINT(new JSHintParser()), //
PMD(new PMDParser());
PMD(new PMDParser()), //
CPPCHECK(new CPPCheckParser());

private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName());
private ViolationsParser violationsParser;
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/se/bjurr/violations/lib/CPPCheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package se.bjurr.violations.lib;

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Reporter.CPPCHECK;

import java.util.List;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class CPPCheckTest {

private static final String MSG_1 = "The scope of the variable 'i' can be reduced. The scope of the variable 'i' can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:&#xa;void f(int x)&#xa;{&#xa; int i = 0;&#xa; if (x) {&#xa; // it's safe to move 'int i = 0' here&#xa; for (int n = 0; n &lt; 10; ++n) {&#xa; // it is possible but not safe to move 'int i = 0' here&#xa; do_something(&amp;i);&#xa; }&#xa; }&#xa;}&#xa;When you see this message it is always safe to reduce the variable scope 1 level.";
private static final String MSG_2 = "The scope of the variable 'i' can be reduced. The scope of the variable 'i' can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:&#xa;void f(int x)&#xa;{&#xa; int i = 0;&#xa; if (x) {&#xa; // it's safe to move 'int i = 0' here&#xa; for (int n = 0; n &lt; 10; ++n) {&#xa; // it is possible but not safe to move 'int i = 0' here&#xa; do_something(&amp;i);&#xa; }&#xa; }&#xa;}&#xa;When you see this message it is always safe to reduce the variable scope 1 level.";

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/cppcheck/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CPPCHECK) //
.violations();

assertThat(actual)//
.contains(//
violationBuilder()//
.setReporter(CPPCHECK)//
.setFile("api.c")//
.setStartLine(498)//
.setEndLine(498)//
.setRule("variableScope")//
.setMessage(MSG_1)//
.setSeverity(INFO)//
.build())//
.contains(//
violationBuilder()//
.setReporter(CPPCHECK)//
.setFile("api_storage.c")//
.setStartLine(104)//
.setEndLine(104)//
.setRule("variableScope")//
.setMessage(MSG_2)//
.setSeverity(ERROR)//
.build())//
.hasSize(3);
}
}
15 changes: 15 additions & 0 deletions src/test/resources/cppcheck/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
<cppcheck version="1.52"/>
<errors>
<error id="variableScope" severity="style" msg="The scope of the variable 'i' can be reduced" verbose="The scope of the variable 'i' can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:&#xa;void f(int x)&#xa;{&#xa; int i = 0;&#xa; if (x) {&#xa; // it's safe to move 'int i = 0' here&#xa; for (int n = 0; n &lt; 10; ++n) {&#xa; // it is possible but not safe to move 'int i = 0' here&#xa; do_something(&amp;i);&#xa; }&#xa; }&#xa;}&#xa;When you see this message it is always safe to reduce the variable scope 1 level.">
<location file="api.c" line="498"/>
</error>
<error id="variableScope" severity="style" msg="The scope of the variable 'n' can be reduced" verbose="The scope of the variable 'n' can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:&#xa;void f(int x)&#xa;{&#xa; int i = 0;&#xa; if (x) {&#xa; // it's safe to move 'int i = 0' here&#xa; for (int n = 0; n &lt; 10; ++n) {&#xa; // it is possible but not safe to move 'int i = 0' here&#xa; do_something(&amp;i);&#xa; }&#xa; }&#xa;}&#xa;When you see this message it is always safe to reduce the variable scope 1 level.">
<location file="api.c" line="498"/>
</error>
<error id="variableScope" severity="error" msg="The scope of the variable 'i' can be reduced" verbose="The scope of the variable 'i' can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:&#xa;void f(int x)&#xa;{&#xa; int i = 0;&#xa; if (x) {&#xa; // it's safe to move 'int i = 0' here&#xa; for (int n = 0; n &lt; 10; ++n) {&#xa; // it is possible but not safe to move 'int i = 0' here&#xa; do_something(&amp;i);&#xa; }&#xa; }&#xa;}&#xa;When you see this message it is always safe to reduce the variable scope 1 level.">
<location file="api_storage.c" line="104"/>
</error>
</errors>
</results>

0 comments on commit e1a7ad7

Please sign in to comment.