Skip to content

Commit

Permalink
Use spotless and reformat code (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ao-senXiong authored Apr 21, 2024
1 parent f279c9b commit 214612f
Show file tree
Hide file tree
Showing 261 changed files with 7,941 additions and 6,616 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
run: |
python3 -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Spotless Check
if: matrix.jdk == 11 || matrix.jdk == 17
run: ./gradlew spotlessCheck
- name: Build
run: |
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
Expand Down
133 changes: 95 additions & 38 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
buildscript {
dependencies {
if (JavaVersion.current() >= JavaVersion.VERSION_11) {
// Code formatting; defines targets "spotlessApply" and "spotlessCheck".
// https://github.com/diffplug/spotless/tags ; see tags starting "gradle/"
// Only works on JDK 11+.
classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.22.0'
}
}
}

plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
Expand All @@ -19,22 +30,54 @@ ext {
// On a Java 8 JVM, use error-prone javac and source/target 8.
// On a Java 9+ JVM, use the host javac, default source/target, and required module flags.
isJava8 = JavaVersion.current() == JavaVersion.VERSION_1_8

isJava11plus = JavaVersion.current() >= JavaVersion.VERSION_11
errorproneJavacVersion = '9+181-r4173-1'

// Keep in sync with checker-framework/build.gradle.
// TODO: find a way to directly use that variable.
compilerArgsForRunningCF = [
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"--add-opens", "jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-exports",
"jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"--add-opens",
"jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
]
if (isJava11plus) {
apply plugin: 'com.diffplug.spotless'
spotless {
format 'misc', {
target '*.md', '*.tex', '.gitignore', 'Makefile'
indentWithSpaces(2)
trimTrailingWhitespace()
}

java {
googleJavaFormat().aosp()
importOrder('com', 'jdk', 'lib', 'lombok', 'org', 'java', 'javax')
formatAnnotations().addTypeAnnotation("PolyInitialized")
}

groovyGradle {
target '**/*.gradle'
greclipse() // which formatter Spotless should use to format .gradle files.
indentWithSpaces(4)
trimTrailingWhitespace()
}
}
}
}

println '===================================='
Expand Down Expand Up @@ -127,7 +170,7 @@ test {
dependsOn('dist')

systemProperties 'path.afu.scripts': "${afu}/scripts",
'use.hacks': true
'use.hacks': true

systemProperties += [JDK_JAR: "${checkerFrameworkPath}/checker/dist/jdk8.jar"]

Expand All @@ -136,7 +179,9 @@ test {
}

if (isJava8) {
jvmArgs += ["-Xbootclasspath/p:${configurations.javacJar.asPath}"]
jvmArgs += [
"-Xbootclasspath/p:${configurations.javacJar.asPath}"
]
} else {
// Without this, the test throw "java.lang.OutOfMemoryError: Java heap space"
// Corresponding pull request: https://github.com/opprop/checker-framework-inference/pull/263
Expand Down Expand Up @@ -166,21 +211,21 @@ test {
"Skipped: ${result.skippedTestCount}, " +
"Time elapsed: ${seconds} sec\n"
}

}
}

compileJava {
dependsOn(buildLingeling)
dependsOn(buildDLJC)
options.compilerArgs = [
'-implicit:class',
'-Awarns',
'-Xmaxwarns', '10000',
// Can't use this because JSON library contains raw types:
// '-Xlint:unchecked',
'-Xlint:deprecation',
'-Werror',
'-implicit:class',
'-Awarns',
'-Xmaxwarns',
'10000',
// Can't use this because JSON library contains raw types:
// '-Xlint:unchecked',
'-Xlint:deprecation',
'-Werror',
]
}

Expand Down Expand Up @@ -214,7 +259,7 @@ task dist(dependsOn: shadowJar, type: Copy) {
"${checkerFrameworkPath}/checker/dist/checker-qual.jar",
"${checkerFrameworkPath}/checker/dist/checker-util.jar",
"${checkerFrameworkPath}/checker/dist/javac.jar",
)
)
into file('dist')
}

Expand Down Expand Up @@ -270,20 +315,30 @@ task cloneAndBuildDependencies(type: Exec) {
task testCheckerInferenceScript(type: Exec, dependsOn: dist) {
description 'Basic sanity check of scripts/inference'
executable './scripts/inference'
args = ['--mode', 'TYPECHECK',
'--checker', 'ostrusted.OsTrustedChecker',
'--solver', 'checkers.inference.solver.PropagationSolver',
'testdata/ostrusted/Test.java']
args = [
'--mode',
'TYPECHECK',
'--checker',
'ostrusted.OsTrustedChecker',
'--solver',
'checkers.inference.solver.PropagationSolver',
'testdata/ostrusted/Test.java'
]
}

task testCheckerInferenceDevScript(type: Exec, dependsOn: [dist, dependenciesJar]) {
description 'Basic sanity check of scripts/inference-dev'
executable './scripts/inference-dev'
args = ['--mode', 'INFER',
'--checker', 'interning.InterningChecker',
'--solver', 'checkers.inference.solver.MaxSat2TypeSolver',
'--hacks=true',
'testdata/interning/MapAssignment.java']
args = [
'--mode',
'INFER',
'--checker',
'interning.InterningChecker',
'--solver',
'checkers.inference.solver.MaxSat2TypeSolver',
'--hacks=true',
'testdata/interning/MapAssignment.java'
]
}

task testDataflowExternalSolvers(type: Exec, dependsOn: [dist, dependenciesJar]) {
Expand All @@ -294,10 +349,10 @@ task testDataflowExternalSolvers(type: Exec, dependsOn: [dist, dependenciesJar])
afterEvaluate {
// Create a task for each test class whose name is the same as the class name.
sourceSets.test.java.filter {
it.path.contains('tests/checkers') &&
it.path.endsWith('Test.java') &&
!it.path.contains('CFInferenceTest')
}.forEach { file ->
it.path.contains('tests/checkers') &&
it.path.endsWith('Test.java') &&
!it.path.contains('CFInferenceTest')
}.forEach { file ->
String junitClassName = file.name.replaceAll(".java", "")
tasks.create(name: "${junitClassName}", type: Test, group: 'Verification') {
description "Run ${junitClassName} tests."
Expand All @@ -310,16 +365,18 @@ afterEvaluate {
dependsOn(shadowJar)

systemProperties 'path.afu.scripts': "${afu}/scripts",
'path.inference.script': "${projectDir}/scripts/inference",
'use.hacks': true,
JDK_JAR: "${checkerFrameworkPath}/checker/dist/jdk8.jar"
'path.inference.script': "${projectDir}/scripts/inference",
'use.hacks': true,
JDK_JAR: "${checkerFrameworkPath}/checker/dist/jdk8.jar"

if (project.hasProperty('emit.test.debug')) {
systemProperties += ["emit.test.debug": 'true']
}

if (isJava8) {
jvmArgs += ["-Xbootclasspath/p:${configurations.javacJar.asPath}"]
jvmArgs += [
"-Xbootclasspath/p:${configurations.javacJar.asPath}"
]
}

testLogging {
Expand Down
47 changes: 27 additions & 20 deletions src/checkers/inference/BaseInferrableChecker.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package checkers.inference;

import com.sun.source.tree.Tree;
import com.sun.source.util.Trees;

import org.checkerframework.common.basetype.BaseAnnotatedTypeFactory;
import org.checkerframework.framework.flow.CFAnalysis;
import org.checkerframework.framework.flow.CFStore;
import org.checkerframework.framework.flow.CFTransfer;
import org.checkerframework.framework.flow.CFValue;
import org.checkerframework.framework.type.GenericAnnotatedTypeFactory;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;
Expand All @@ -14,13 +18,7 @@
import checkers.inference.dataflow.InferenceTransfer;
import checkers.inference.model.ConstraintManager;

import com.sun.source.tree.Tree;
import com.sun.source.util.Trees;

/**
* Default implementation of InferrableChecker.
*
*/
/** Default implementation of InferrableChecker. */
public abstract class BaseInferrableChecker extends InferenceChecker implements InferrableChecker {

@Override
Expand All @@ -29,7 +27,7 @@ public void initChecker() {
// except for the last line assigning the visitor
{
Trees trees = Trees.instance(processingEnv);
assert( trees != null ); /*nninvariant*/
assert (trees != null); /*nninvariant*/
this.trees = trees;

this.messager = processingEnv.getMessager();
Expand All @@ -40,7 +38,8 @@ public void initChecker() {
}

@Override
public InferenceVisitor<?, ?> createVisitor(InferenceChecker ichecker, BaseAnnotatedTypeFactory factory, boolean infer) {
public InferenceVisitor<?, ?> createVisitor(
InferenceChecker ichecker, BaseAnnotatedTypeFactory factory, boolean infer) {
return new InferenceVisitor<>(this, ichecker, factory, infer);
}

Expand All @@ -51,11 +50,11 @@ public BaseInferenceRealTypeFactory createRealTypeFactory(boolean infer) {

@Override
public CFAnalysis createInferenceAnalysis(
InferenceChecker checker,
GenericAnnotatedTypeFactory<CFValue, CFStore, CFTransfer, CFAnalysis> factory,
SlotManager slotManager,
ConstraintManager constraintManager,
InferrableChecker realChecker) {
InferenceChecker checker,
GenericAnnotatedTypeFactory<CFValue, CFStore, CFTransfer, CFAnalysis> factory,
SlotManager slotManager,
ConstraintManager constraintManager,
InferrableChecker realChecker) {

return new InferenceAnalysis(checker, factory, slotManager, constraintManager, realChecker);
}
Expand All @@ -81,12 +80,20 @@ public boolean shouldStoreConstantSlots() {
}

@Override
public InferenceAnnotatedTypeFactory createInferenceATF(InferenceChecker inferenceChecker,
InferrableChecker realChecker, BaseAnnotatedTypeFactory realTypeFactory,
SlotManager slotManager, ConstraintManager constraintManager) {
InferenceAnnotatedTypeFactory InferenceAFT = new InferenceAnnotatedTypeFactory(
inferenceChecker, realChecker.withCombineConstraints(), realTypeFactory, realChecker,
slotManager, constraintManager);
public InferenceAnnotatedTypeFactory createInferenceATF(
InferenceChecker inferenceChecker,
InferrableChecker realChecker,
BaseAnnotatedTypeFactory realTypeFactory,
SlotManager slotManager,
ConstraintManager constraintManager) {
InferenceAnnotatedTypeFactory InferenceAFT =
new InferenceAnnotatedTypeFactory(
inferenceChecker,
realChecker.withCombineConstraints(),
realTypeFactory,
realChecker,
slotManager,
constraintManager);
return InferenceAFT;
}

Expand Down
29 changes: 14 additions & 15 deletions src/checkers/inference/BytecodeTypeAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@

import checkers.inference.util.CopyUtil;


/**
* Adds annotations to types that have come from bytecode. Note, these types will
* receive concrete "real" annotations from the real type factory AND a VarAnnot.
* Adds annotations to types that have come from bytecode. Note, these types will receive concrete
* "real" annotations from the real type factory AND a VarAnnot.
*
* The VarAnnot is so that we have an annotation in both hierarchies and so that
* for cases like Verigames the shape of a gameboard won't differ depending on
* whether a library was in bytecode or source code. Note: I believe this is
* much less of an issue these days since the game is more akin to
* human aided automatic solving.
* <p>The VarAnnot is so that we have an annotation in both hierarchies and so that for cases like
* Verigames the shape of a gameboard won't differ depending on whether a library was in bytecode or
* source code. Note: I believe this is much less of an issue these days since the game is more akin
* to human aided automatic solving.
*/
public class BytecodeTypeAnnotator {
private final AnnotatedTypeFactory realTypeFactory;
private final InferenceAnnotatedTypeFactory inferenceTypeFactory;


public BytecodeTypeAnnotator(InferenceAnnotatedTypeFactory inferenceTypeFactory,
AnnotatedTypeFactory realTypeFactory) {
public BytecodeTypeAnnotator(
InferenceAnnotatedTypeFactory inferenceTypeFactory,
AnnotatedTypeFactory realTypeFactory) {
this.realTypeFactory = realTypeFactory;
this.inferenceTypeFactory = inferenceTypeFactory;
}

/**
* Get the type of element from the realTypeFactory. Copy it's annotations to inferenceType.
* Add a @VarAnnot to all definite type use locations (locations that can be defaulted) in inferenceType and
* add an equality constraint between it and the "real" annotations
* Get the type of element from the realTypeFactory. Copy it's annotations to inferenceType. Add
* a @VarAnnot to all definite type use locations (locations that can be defaulted) in
* inferenceType and add an equality constraint between it and the "real" annotations
*
* @param element The bytecode declaration from which inferenceType was created
* @param inferenceType The type of element. inferenceType will be annotated by this method
* @param inferenceType The type of element. inferenceType will be annotated by this method
*/
public void annotate(final Element element, final AnnotatedTypeMirror inferenceType) {
final AnnotatedTypeMirror realType = realTypeFactory.getAnnotatedType(element);
Expand Down
Loading

0 comments on commit 214612f

Please sign in to comment.