Skip to content

Commit

Permalink
Issue checkstyle#8905: Add basic SARIF support
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Zhang authored and strkkk committed Apr 29, 2021
1 parent 01b18d1 commit 63e3709
Show file tree
Hide file tree
Showing 22 changed files with 1,089 additions and 18 deletions.
8 changes: 7 additions & 1 deletion .ci/jsoref-spellchecker/whitelist.words
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ avoidstarimport
avoidstaticimport
aws
awt
azurewebsites
backend
backport
Backquote
Expand Down Expand Up @@ -491,6 +492,7 @@ gradlew
Grenner
grep
grepped
groupon
grp
Gsp
GString
Expand Down Expand Up @@ -535,6 +537,7 @@ icu
identifiernames
Idref
idx
ietf
Igno
IGNORETHIS
igorminar
Expand Down Expand Up @@ -1066,6 +1069,7 @@ Rethrown
returncount
reviewdog
revwalk
rfc
rfe
rga
Rgb
Expand Down Expand Up @@ -1093,6 +1097,8 @@ rw
rx
sabaka
Sameline
sariflogger
sarifweb
Savinov
saxonica
sbe
Expand Down Expand Up @@ -1297,7 +1303,7 @@ utf
UUID
UWF
uxxxx
Validator
validator
VALUEEEE
vararg
variabledeclarationusagedistance
Expand Down
10 changes: 7 additions & 3 deletions config/pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
<!-- Annotations like '@SuppressWarnings' don't need to be checked, it is better to keep their
strings connected to the annotation instead of separating. -->
<property name="skipAnnotations" value="true"/>
<!-- Keeping the duplicated strings maintains readability -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@SimpleName='SarifLogger']"/>
</properties>
</rule>
<rule ref="category/java/errorprone.xml/EmptyCatchBlock">
Expand Down Expand Up @@ -266,16 +269,17 @@
are big and should not be split without a good reason.
The methods visitToken/leaveToken are a big SWITCH block with a number of IF blocks.
If we split the block to several methods it will damage the readability.
XMLLogger.encode, FallThroughCheck.isTerminated, ElementNode.iterateAxis,
NoWhitespaceAfterCheck.getArrayDeclaratorPreviousElement are also huge switches,
they has to be monolithic.
XMLLogger.encode, SarifLogger.escape, FallThroughCheck.isTerminated,
ElementNode.iterateAxis, NoWhitespaceAfterCheck.getArrayDeclaratorPreviousElement
are also huge switches, they has to be monolithic.
SuppressFilterElement is a single constructor and can't be split easily -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@SimpleName='Main'
or @SimpleName='PackageObjectFactory' or @SimpleName='RequireThisCheck'
or @SimpleName='VariableDeclarationUsageDistanceCheck'
or @SimpleName='HandlerFactory']
| //ClassOrInterfaceDeclaration[@SimpleName='XMLLogger']//MethodDeclaration[@Name='encode']
| //ClassOrInterfaceDeclaration[@SimpleName='SarifLogger']//MethodDeclaration[@Name='escape']
| //ClassOrInterfaceDeclaration[@SimpleName='LeftCurlyCheck'
or @SimpleName='FinalLocalVariableCheck'
or @SimpleName='NPathComplexityCheck']
Expand Down
2 changes: 1 addition & 1 deletion config/sevntu_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- Fixing these cases will decrease code readability -->
<suppress checks="MultipleStringLiteralsExtended"
files="JavadocStyleCheck\.java|XMLLogger\.java"/>
files="JavadocStyleCheck\.java|XMLLogger\.java|SarifLogger\.java"/>
<suppress checks="MultipleStringLiteralsExtended"
files=".*[\\/]src[\\/](test|it)[\\/]"/>
<!-- ParseTreeBuilder is generated, to keep ease of generation violations are suppressed -->
Expand Down
2 changes: 1 addition & 1 deletion config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
|AbbreviationAsWordInNameCheckTest\.java)"/>

<!-- Fixing these cases will decrease code readability -->
<suppress checks="MultipleStringLiterals" files="JavadocStyleCheck\.java|XMLLogger\.java"/>
<suppress checks="MultipleStringLiterals" files="JavadocStyleCheck\.java|XMLLogger\.java|SarifLogger\.java"/>

<!-- There are a lot of setters/getters in the Check.
A small number of methods is left for Check's logic -->
Expand Down
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,31 @@
<artifactId>nondex-maven-plugin</artifactId>
<version>1.1.2</version>
</plugin>

<plugin>
<groupId>com.groupon.maven.plugin.json</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
<configuration>
<validations>
<validation>
<directory>${basedir}/src/test/resources/com/puppycrawl/tools/checkstyle/sariflogger</directory>
<jsonSchema>https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json</jsonSchema>
<includes>
<include>**/*.sarif</include>
</includes>
</validation>
</validations>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -2821,6 +2846,7 @@
<param>com.puppycrawl.tools.checkstyle.DefaultLogger*</param>
<param>com.puppycrawl.tools.checkstyle.Definitions*</param>
<param>com.puppycrawl.tools.checkstyle.XMLLogger*</param>
<param>com.puppycrawl.tools.checkstyle.SarifLogger*</param>
<param>com.puppycrawl.tools.checkstyle.PackageObjectFactory*</param>
<param>com.puppycrawl.tools.checkstyle.PropertiesExpander*</param>
<param>com.puppycrawl.tools.checkstyle.PropertyCacheFile*</param>
Expand All @@ -2843,6 +2869,7 @@
<param>com.puppycrawl.tools.checkstyle.DefaultLoggerTest</param>
<param>com.puppycrawl.tools.checkstyle.DefinitionsTest</param>
<param>com.puppycrawl.tools.checkstyle.XMLLoggerTest</param>
<param>com.puppycrawl.tools.checkstyle.SarifLoggerTest</param>
<param>com.puppycrawl.tools.checkstyle.PackageObjectFactoryTest</param>
<param>com.puppycrawl.tools.checkstyle.PropertiesExpanderTest</param>
<param>com.puppycrawl.tools.checkstyle.PropertyCacheFileTest</param>
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/puppycrawl/tools/checkstyle/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ private static AutomaticBean.OutputStreamOptions getOutputStreamOptions(Path out
enum OutputFormat {
/** XML output format. */
XML,
/** SARIF output format. */
SARIF,
/** Plain output format. */
PLAIN;

Expand All @@ -551,13 +553,18 @@ enum OutputFormat {
* @param out the output stream
* @param options the output stream options
* @return a new AuditListener for this OutputFormat
* @throws IOException if there is any IO exception during logger initialization
*/
public AuditListener createListener(OutputStream out,
AutomaticBean.OutputStreamOptions options) {
public AuditListener createListener(
OutputStream out,
AutomaticBean.OutputStreamOptions options) throws IOException {
final AuditListener result;
if (this == XML) {
result = new XMLLogger(out, options);
}
else if (this == SARIF) {
result = new SarifLogger(out, options);
}
else {
result = new DefaultLogger(out, options);
}
Expand Down Expand Up @@ -697,8 +704,8 @@ private static class CliOptions {
*/
@Option(names = "-f",
description = "Specifies the output format. Valid values: "
+ "${COMPLETION-CANDIDATES} for XMLLogger and DefaultLogger respectively. "
+ "Defaults to ${DEFAULT-VALUE}.")
+ "${COMPLETION-CANDIDATES} for XMLLogger, SarifLogger, "
+ "and DefaultLogger respectively. Defaults to ${DEFAULT-VALUE}.")
private OutputFormat format = DEFAULT_OUTPUT_FORMAT;

/** Option that controls whether to print the AST of the file. */
Expand Down
Loading

0 comments on commit 63e3709

Please sign in to comment.