Skip to content

Commit

Permalink
Support CPPLint #3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 6, 2016
1 parent 30ba7fd commit 319eaab
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 6 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ Changelog of Git Changelog.
## Unreleased
### GitHub [#1](https://github.com/tomasbjerre/violations-lib/issues/#1)

**PyLint parser**
**Support CPPLint**


[7a80f1f9db231de](https://github.com/tomasbjerre/git-changelog-lib/commit/7a80f1f9db231de) Tomas Bjerre *2016-03-06 08:15:44*
[7b6ea9ed545ee7f](https://github.com/tomasbjerre/git-changelog-lib/commit/7b6ea9ed545ee7f) Tomas Bjerre *2016-03-06 12:22:35*

**Flake8 (PyLint) and Lint parser**


[bd02db749408fef](https://github.com/tomasbjerre/git-changelog-lib/commit/bd02db749408fef) Tomas Bjerre *2016-03-06 11:18:46*

**Resharper support**

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
This is a library for parsing report files from static code analysis.

It supports:
* [_PMD_](https://pmd.github.io/)
* [_Findbugs_](http://findbugs.sourceforge.net/)
* [_Flake8_](http://flake8.readthedocs.org/en/latest/) ([_PyLint_](https://www.pylint.org/), [_Pep8_](https://github.com/PyCQA/pycodestyle))
* [_Checkstyle_](http://checkstyle.sourceforge.net/)
* [_CPPLint_](https://github.com/theandrewdavis/cpplint)
* [_CPPCheck_](http://cppcheck.sourceforge.net/)
* [_CSSLint_](https://github.com/CSSLint/csslint)
* [_Findbugs_](http://findbugs.sourceforge.net/)
* [_Flake8_](http://flake8.readthedocs.org/en/latest/) ([_PyLint_](https://www.pylint.org/), [_Pep8_](https://github.com/PyCQA/pycodestyle))
* [_JSHint_](http://jshint.com/)
* _Lint_ A common XML format, used by different linters.
* [_PMD_](https://pmd.github.io/)
* [_Resharper_](https://www.jetbrains.com/resharper/)

Very easy to use with a nice builder pattern
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/CppLintParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Integer.parseInt;
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.CPPLINT;

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.io.Files;

/**
* PyLint. Format used by Flake8.
*/
public class CppLintParser extends ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
String string = Files.toString(file, UTF_8);
List<Violation> violations = newArrayList();
List<String> lines = getLines(string);
for (String line : lines) {
List<String> parts = getParts(line, "\\[([^\\]]*)\\]$", "\\[([^\\]]*)\\]$", "^([^:]*):", "^([^:]*):", "(.*)");
if (parts.isEmpty()) {
continue; // Happens for the line "Done processing cpp/test.cpp"
}
Integer severity = parseInt(parts.get(0));
String rule = parts.get(1);
String filename = parts.get(2);
Integer lineNumber = parseInt(parts.get(3));
String message = parts.get(4);
violations.add(//
violationBuilder()//
.setReporter(CPPLINT)//
.setStartLine(lineNumber)//
.setFile(filename)//
.setRule(rule)//
.setSeverity(toSeverity(severity))//
.setMessage(message)//
.build()//
);
}
return violations;
}

public SEVERITY toSeverity(Integer severity) {
if (severity >= 5) {
return ERROR;
}
if (severity >= 3) {
return WARN;
}
return INFO;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Integer.parseInt;
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.quote;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -74,6 +76,30 @@ public static String getContent(String in, String tag) {
throw new RuntimeException("\"" + tag + "\" not found in " + in);
}

public static List<String> getLines(String string) {
return Arrays.asList(string.split("\n"));
}

/**
* Match one regexp at a time. Remove the matched part from the string, trim,
* and match next regexp on that string...
*/
public static List<String> getParts(String string, String... regexpList) {
List<String> parts = newArrayList();
for (String regexp : regexpList) {
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(string);
boolean found = matcher.find();
if (!found) {
return newArrayList();
}
String part = matcher.group(1).trim();
parts.add(part);
string = string.replaceFirst(quote(matcher.group()), "").trim();
}
return parts;
}

/**
* @return List per line in String, with groups from regexpPerLine.
*/
Expand Down
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 @@ -11,6 +11,7 @@
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.CppLintParser;
import se.bjurr.violations.lib.parsers.FindbugsParser;
import se.bjurr.violations.lib.parsers.Flake8Parser;
import se.bjurr.violations.lib.parsers.JSHintParser;
Expand All @@ -28,7 +29,8 @@ public enum Reporter {
PMD(new PMDParser()), //
CPPCHECK(new CPPCheckParser()), //
RESHARPER(new ResharperParser()), //
FLAKE8(new Flake8Parser())//
FLAKE8(new Flake8Parser()), //
CPPLINT(new CppLintParser())//
;

private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName());
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/se/bjurr/violations/lib/CppLintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.reports.Reporter.CPPLINT;

import java.util.List;

import org.junit.Test;

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

public class CppLintTest {

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

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/cpplint/.*\\.txt$") //
.inFolder(rootFolder) //
.findAll(CPPLINT) //
.violations();

assertThat(actual)//
.hasSize(3);

assertThat(actual.get(0).getMessage())//
.isEqualTo("No copyright message found. You should have a line: \"Copyright [year] <Copyright Owner>\"");
assertThat(actual.get(0).getFile())//
.isEqualTo("cpp/test.cpp");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(ERROR);
assertThat(actual.get(0).getRule().get())//
.isEqualTo("legal/copyright");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(0);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(0);

assertThat(actual.get(2).getMessage())//
.isEqualTo("Missing space before ( in while(");
assertThat(actual.get(2).getFile())//
.isEqualTo("cpp/test.cpp");
assertThat(actual.get(2).getSeverity())//
.isEqualTo(ERROR);
assertThat(actual.get(2).getRule().get())//
.isEqualTo("whitespace/parens");
assertThat(actual.get(2).getStartLine())//
.isEqualTo(11);
assertThat(actual.get(2).getEndLine())//
.isEqualTo(11);
}
}
5 changes: 5 additions & 0 deletions src/test/resources/cpplint/cpplint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cpp/test.cpp:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5]
cpp/test.cpp:5: Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5]
cpp/test.cpp:11: Missing space before ( in while( [whitespace/parens] [5]
Done processing cpp/test.cpp
Total errors found: 3

0 comments on commit 319eaab

Please sign in to comment.