Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/checkers/inference/InferenceLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,17 @@ public void infer() {
addIfNotNull("--cfArgs", InferenceOptions.cfArgs, argList);

addIfTrue("--hacks", InferenceOptions.hacks, argList);
addIfTrue("--writeDefaultAnnotations", InferenceOptions.writeDefaultAnnotations, argList);

Mode mode = Mode.valueOf(InferenceOptions.mode);
if (InferenceOptions.makeDefaultsExplicit
&& (mode == Mode.ROUNDTRIP || mode == Mode.ROUNDTRIP_TYPECHECK)) {
// Two conditions have to be met to make defaults explicit:
// 1. the command-line flag `makeDefaultsExplicit` is provided
// 2. the inference solution will be written back to the source code (roundtrip `mode`)
if (InferenceOptions.writeDefaultAnnotations) {
outStream.println("writeDefaultAnnotations will have no effect when makeDefaultsExplicit is true");
}
argList.add("--makeDefaultsExplicit");
}

Expand Down
24 changes: 21 additions & 3 deletions src/checkers/inference/InferenceMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public class InferenceMain {
// Eventually we will get rid of this.
private boolean hackMode;

private boolean writeDefaultAnnotations;

private ResultHandler resultHandler;

public void setResultHandler(ResultHandler resultHandler) {
Expand Down Expand Up @@ -149,7 +151,7 @@ public void run() {
solve();
// solverResult = null covers case when debug solver is used, but in this case
// shouldn't exit
if (solverResult != null && !solverResult.hasSolution()) {
if (solverResult != null && !solverResult.hasSolution() && !writeDefaultAnnotations) {
logger.info("No solution, exiting...");
System.exit(1);
}
Expand Down Expand Up @@ -185,6 +187,10 @@ private void startCheckerFramework() {
hackMode = true;
}

if (InferenceOptions.writeDefaultAnnotations) {
writeDefaultAnnotations = true;
}

if (InferenceOptions.javacOptions != null) {
checkerFrameworkArgs.addAll(InferenceOptions.javacOptions);
}
Expand Down Expand Up @@ -248,7 +254,11 @@ private void writeJaif() {
}
}

JaifBuilder builder = new JaifBuilder(values, annotationClasses, realChecker.isInsertMainModOfLocalVar());
boolean insertMainModOfLocalVar = realChecker.isInsertMainModOfLocalVar();
if (writeDefaultAnnotations) {
insertMainModOfLocalVar = true; // insert annotations of main modifier of local variables
}
JaifBuilder builder = new JaifBuilder(values, annotationClasses, insertMainModOfLocalVar);
String jaif = builder.createJaif();
writer.println(jaif);

Expand Down Expand Up @@ -294,12 +304,20 @@ private void solve() {
AnnotationMirror result = solverResult.getSolutionForVariable(slot.getId());
if (result != null && slot instanceof SourceVariableSlot) {
AnnotationMirror defaultAnnotation = ((SourceVariableSlot) slot).getDefaultAnnotation();

if (defaultAnnotation != null && AnnotationUtils.areSame(defaultAnnotation, result)) {
// Don't need to write a solution that's equivalent to the default annotation.
result = null;
}
}

else if (writeDefaultAnnotations && slot instanceof SourceVariableSlot) {
AnnotationMirror defaultAnnotation = ((SourceVariableSlot) slot).getDefaultAnnotation();
System.out.println("For " + slot.getId() + "Result: " + result + " Default: " + defaultAnnotation);
if (defaultAnnotation != null) {
result = defaultAnnotation;
}
}

return result;
}

Expand Down
3 changes: 3 additions & 0 deletions src/checkers/inference/InferenceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class InferenceOptions {
@Option("Should we log certain exceptions rather than crash")
public static boolean hacks;

@Option("Should we annotate source code with default annotations")
public static boolean writeDefaultAnnotations;

/**
* The type system to use for checker, solver, and related command-line
* options. If you use this option, all required command-line
Expand Down