-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5879a2
commit e1a7ad7
Showing
5 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/se/bjurr/violations/lib/parsers/CPPCheckParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
void f(int x)
{
 int i = 0;
 if (x) {
 // it's safe to move 'int i = 0' here
 for (int n = 0; n < 10; ++n) {
 // it is possible but not safe to move 'int i = 0' here
 do_something(&i);
 }
 }
}
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:
void f(int x)
{
 int i = 0;
 if (x) {
 // it's safe to move 'int i = 0' here
 for (int n = 0; n < 10; ++n) {
 // it is possible but not safe to move 'int i = 0' here
 do_something(&i);
 }
 }
}
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
void f(int x)
{
 int i = 0;
 if (x) {
 // it's safe to move 'int i = 0' here
 for (int n = 0; n < 10; ++n) {
 // it is possible but not safe to move 'int i = 0' here
 do_something(&i);
 }
 }
}
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:
void f(int x)
{
 int i = 0;
 if (x) {
 // it's safe to move 'int i = 0' here
 for (int n = 0; n < 10; ++n) {
 // it is possible but not safe to move 'int i = 0' here
 do_something(&i);
 }
 }
}
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:
void f(int x)
{
 int i = 0;
 if (x) {
 // it's safe to move 'int i = 0' here
 for (int n = 0; n < 10; ++n) {
 // it is possible but not safe to move 'int i = 0' here
 do_something(&i);
 }
 }
}
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> |