From e33175a2276c979c1e364bad7c033b39d0c9e401 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Thu, 1 Dec 2022 17:21:45 -0500 Subject: [PATCH 01/20] addDeepPreference to cfi Co-authored-by: Baorui Zhou --- src/checkers/inference/InferenceVisitor.java | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index df07527f..7eeda197 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -22,6 +22,7 @@ import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.logging.Logger; @@ -261,6 +262,48 @@ public void mainIsNoneOf(AnnotatedTypeMirror ty, AnnotationMirror[] mods, String } } + private void addDeepPreferenceImpl(AnnotatedTypeMirror ty, AnnotationMirror goal, int weight, + java.util.List visited, Tree node) { + if (infer) { + if (visited.contains(ty)) { + return; + } + visited.add(ty); + + final SlotManager slotManager = InferenceMain.getInstance().getSlotManager(); + Slot el = slotManager.getSlot(ty); + + if (el == null) { + logger.warning("InferenceVisitor::addDeepPreferenceImpl: no annotation in type: " + ty); + } else { + addPreference(ty, goal, weight); + } + + if (ty.getKind() == TypeKind.DECLARED) { + AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) ty; + for (AnnotatedTypeMirror typearg : declaredType.getTypeArguments()) { + addDeepPreferenceImpl(typearg, goal, weight, visited, node); + } + } else if (ty.getKind() == TypeKind.ARRAY) { + AnnotatedArrayType arrayType = (AnnotatedArrayType) ty; + addDeepPreferenceImpl(arrayType.getComponentType(), goal, weight, visited, node); + } else if (ty.getKind() == TypeKind.TYPEVAR) { + AnnotatedTypeVariable atv = (AnnotatedTypeVariable) ty; + if (atv.getUpperBound()!=null) { + addDeepPreferenceImpl(atv.getUpperBound(), goal, weight, visited, node); + } + if (atv.getLowerBound()!=null) { + addDeepPreferenceImpl(atv.getLowerBound(), goal, weight, visited, node); + } + } + } + // Else, do nothing + } + + public void addDeepPreference(AnnotatedTypeMirror ty, AnnotationMirror goal, int weight, Tree node) { + addDeepPreferenceImpl(ty, goal, weight, new LinkedList<>(), node); + } + public void addPreference(AnnotatedTypeMirror type, AnnotationMirror anno, int weight) { if (infer) { ConstraintManager cManager = InferenceMain.getInstance().getConstraintManager(); From 38c4b0ba61c2c1ade615d1c8a8ed7573497cc0e0 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Tue, 6 Dec 2022 15:20:46 -0500 Subject: [PATCH 02/20] delete redundant if statements --- src/checkers/inference/InferenceVisitor.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index 7eeda197..208821b8 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -289,12 +289,8 @@ private void addDeepPreferenceImpl(AnnotatedTypeMirror ty, AnnotationMirror goal addDeepPreferenceImpl(arrayType.getComponentType(), goal, weight, visited, node); } else if (ty.getKind() == TypeKind.TYPEVAR) { AnnotatedTypeVariable atv = (AnnotatedTypeVariable) ty; - if (atv.getUpperBound()!=null) { - addDeepPreferenceImpl(atv.getUpperBound(), goal, weight, visited, node); - } - if (atv.getLowerBound()!=null) { - addDeepPreferenceImpl(atv.getLowerBound(), goal, weight, visited, node); - } + addDeepPreferenceImpl(atv.getUpperBound(), goal, weight, visited, node); + addDeepPreferenceImpl(atv.getLowerBound(), goal, weight, visited, node); } } // Else, do nothing From ce6cdaa771ed82cad3e46927049a5370c92c636a Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Wed, 21 Dec 2022 11:02:04 -0500 Subject: [PATCH 03/20] corresponding changes in CFI about enforce-targetlocations --- .../inference/InferenceValidator.java | 27 ++++++ src/checkers/inference/InferenceVisitor.java | 97 ++++++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/checkers/inference/InferenceValidator.java b/src/checkers/inference/InferenceValidator.java index bb798845..9177af61 100644 --- a/src/checkers/inference/InferenceValidator.java +++ b/src/checkers/inference/InferenceValidator.java @@ -1,9 +1,16 @@ package checkers.inference; +import com.sun.source.tree.Tree; import org.checkerframework.common.basetype.BaseTypeChecker; import org.checkerframework.common.basetype.BaseTypeValidator; +import org.checkerframework.framework.qual.TypeUseLocation; import org.checkerframework.framework.type.AnnotatedTypeFactory; +import org.checkerframework.framework.type.AnnotatedTypeMirror; +import org.checkerframework.javacutil.AnnotationUtils; + +import javax.lang.model.element.AnnotationMirror; +import java.util.List; /** * A visitor to validate the types in a tree. @@ -29,4 +36,24 @@ public InferenceValidator(BaseTypeChecker checker, public void setInfer(boolean infer) { this.infer = infer; } + + @Override + protected void validateWildCardTargetLocation(AnnotatedTypeMirror.AnnotatedWildcardType type, Tree tree) { + + InferenceVisitor inferVisitor = (InferenceVisitor) visitor; + if (inferVisitor.getIgnoreTargetLocation()) return; + + AnnotationMirror[] mirrors = new AnnotationMirror[0]; + for (AnnotationMirror am : type.getSuperBound().getAnnotations()) { + inferVisitor.annoIsNoneOf(type, am, + inferVisitor.targetLocationToAnno.get(TypeUseLocation.LOWER_BOUND).toArray(mirrors), + "type.invalid.annotations.on.location", tree); + } + + for (AnnotationMirror am : type.getExtendsBound().getAnnotations()) { + inferVisitor.annoIsNoneOf(type, am, + inferVisitor.targetLocationToAnno.get(TypeUseLocation.UPPER_BOUND).toArray(mirrors), + "type.invalid.annotations.on.location", tree); + } + } } diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index df07527f..e987ad7c 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -6,6 +6,8 @@ import org.checkerframework.common.basetype.BaseAnnotatedTypeFactory; import org.checkerframework.common.basetype.BaseTypeVisitor; import org.checkerframework.common.subtyping.qual.Unqualified; +import org.checkerframework.framework.qual.TargetLocations; +import org.checkerframework.framework.qual.TypeUseLocation; import org.checkerframework.framework.type.AnnotatedTypeMirror; import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType; import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; @@ -21,12 +23,15 @@ import java.lang.annotation.Annotation; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; @@ -46,6 +51,7 @@ import com.sun.source.tree.Tree.Kind; import com.sun.source.tree.VariableTree; +import org.checkerframework.javacutil.TreeUtils; import org.plumelib.util.ArraysPlume; @@ -79,6 +85,12 @@ public class InferenceVisitor> targetLocationToAnno = new HashMap<>(); + public InferenceVisitor(Checker checker, InferenceChecker ichecker, Factory factory, boolean infer) { super((infer) ? ichecker : checker, factory); this.realChecker = checker; @@ -302,7 +314,7 @@ protected void annoIsNot(AnnotatedTypeMirror sourceType, AnnotationMirror effect annoIsNoneOf(sourceType, effectiveAnno, new AnnotationMirror[]{target}, msgKey, node); } - private void annoIsNoneOf(AnnotatedTypeMirror sourceType, AnnotationMirror effectiveAnno, + public void annoIsNoneOf(AnnotatedTypeMirror sourceType, AnnotationMirror effectiveAnno, AnnotationMirror[] targets, String msgKey, Tree node) { if (infer) { final SlotManager slotManager = InferenceMain.getInstance().getSlotManager(); @@ -848,4 +860,87 @@ protected void checkConstructorResult( } } + @Override + protected void initAnnoToTargetLocations() { + // first, init each type-use location contains all type qualifiers + Set> supportQualifiers = atypeFactory.getSupportedTypeQualifiers(); + Set supportedAnnos = AnnotationUtils.createAnnotationSet(); + for (Class ac: supportQualifiers) { + supportedAnnos.add(new AnnotationBuilder( + InferenceMain.getInstance().getRealTypeFactory().getProcessingEnv(), ac).build()); + } + for (TypeUseLocation location : TypeUseLocation.values()) { + targetLocationToAnno.put(location, supportedAnnos); + } + // then, delete some qualifiers which can be applied on that type-use location + // this leaves only qualifiers not allowed on the location. + for (Class qual : supportQualifiers) { + Element elem = elements.getTypeElement(qual.getCanonicalName()); + TargetLocations tls = elem.getAnnotation(TargetLocations.class); + // @Target({ElementType.TYPE_USE})} together with no @TargetLocations(...) means that + // the qualifier can be written on any type use + if (tls == null) { + for (TypeUseLocation location : TypeUseLocation.values()) { + Set amSet = targetLocationToAnno.get(location); + amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + } + continue; + } + for (TypeUseLocation location : tls.value()) { + Set amSet = targetLocationToAnno.get(location); + amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + } + } + } + + @Override + protected void validateVariablesTargetLocation(Tree tree, AnnotatedTypeMirror type) { + if (ignoreTargetLocation) return; + Element element = TreeUtils.elementFromTree(tree); + + if (element != null) { + ElementKind elemKind = element.getKind(); + TypeUseLocation location; + switch (elemKind) { + case LOCAL_VARIABLE: + location = TypeUseLocation.LOCAL_VARIABLE; + break; + case EXCEPTION_PARAMETER: + location = TypeUseLocation.EXCEPTION_PARAMETER; + break; + case PARAMETER: + if (((VariableTree) tree).getName().contentEquals("this")) { + location = TypeUseLocation.RECEIVER; + } else { + location = TypeUseLocation.PARAMETER; + } + break; + case RESOURCE_VARIABLE: + location = TypeUseLocation.RESOURCE_VARIABLE; + break; + case FIELD: + location = TypeUseLocation.FIELD; + break; + case ENUM_CONSTANT: + location = TypeUseLocation.CONSTRUCTOR_RESULT; + // TODO: Add ? mainIsNoneOf(type, targetLocationToAnno.get(TypeUseLocation.FIELD).toArray(mirrors), "type.invalid.annotations.on.location", tree); + break; + default: + throw new BugInCF("Location not matched"); + } + AnnotationMirror[] mirrors = new AnnotationMirror[0]; + mainIsNoneOf(type, targetLocationToAnno.get(location).toArray(mirrors), "type.invalid.annotations.on.location", tree); + } + } + + @Override + protected void validateTargetLocation( + Tree tree, AnnotatedTypeMirror type, TypeUseLocation required) { + if (ignoreTargetLocation) { + return; + } + + mainIsNoneOf(type, targetLocationToAnno.get(required).toArray(new AnnotationMirror[0]), + "type.invalid.annotations.on.location", tree); + } } From 68682b2d55a4fb5d1c92c0b4de7a581904cc9ed4 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Wed, 21 Dec 2022 11:26:14 -0500 Subject: [PATCH 04/20] use getProcessingEnv --- src/checkers/inference/InferenceVisitor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index e987ad7c..d34203cb 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -866,8 +866,7 @@ protected void initAnnoToTargetLocations() { Set> supportQualifiers = atypeFactory.getSupportedTypeQualifiers(); Set supportedAnnos = AnnotationUtils.createAnnotationSet(); for (Class ac: supportQualifiers) { - supportedAnnos.add(new AnnotationBuilder( - InferenceMain.getInstance().getRealTypeFactory().getProcessingEnv(), ac).build()); + supportedAnnos.add(new AnnotationBuilder(atypeFactory.getProcessingEnv(), ac).build()); } for (TypeUseLocation location : TypeUseLocation.values()) { targetLocationToAnno.put(location, supportedAnnos); From e0d08e471027897ad609b13cf3dc89a94bee3800 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Mon, 9 Jan 2023 15:58:07 -0500 Subject: [PATCH 05/20] fix inference initialization crash --- src/checkers/inference/InferenceVisitor.java | 26 +++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index d34203cb..48616770 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -89,13 +89,14 @@ public class InferenceVisitor> targetLocationToAnno = new HashMap<>(); + protected HashMap> targetLocationToAnno = new HashMap<>(); public InferenceVisitor(Checker checker, InferenceChecker ichecker, Factory factory, boolean infer) { super((infer) ? ichecker : checker, factory); this.realChecker = checker; this.infer = infer; ((InferenceValidator)typeValidator).setInfer(infer); + initTargetLocationToAnno(); } @SuppressWarnings("unchecked") @@ -860,8 +861,7 @@ protected void checkConstructorResult( } } - @Override - protected void initAnnoToTargetLocations() { + protected void initTargetLocationToAnno() { // first, init each type-use location contains all type qualifiers Set> supportQualifiers = atypeFactory.getSupportedTypeQualifiers(); Set supportedAnnos = AnnotationUtils.createAnnotationSet(); @@ -869,7 +869,7 @@ protected void initAnnoToTargetLocations() { supportedAnnos.add(new AnnotationBuilder(atypeFactory.getProcessingEnv(), ac).build()); } for (TypeUseLocation location : TypeUseLocation.values()) { - targetLocationToAnno.put(location, supportedAnnos); + targetLocationToAnno.put(location, new HashSet<>(supportedAnnos)); } // then, delete some qualifiers which can be applied on that type-use location // this leaves only qualifiers not allowed on the location. @@ -894,6 +894,11 @@ protected void initAnnoToTargetLocations() { @Override protected void validateVariablesTargetLocation(Tree tree, AnnotatedTypeMirror type) { + if (!infer) { + super.initAnnoToTargetLocations(); + return; + } + if (ignoreTargetLocation) return; Element element = TreeUtils.elementFromTree(tree); @@ -935,11 +940,14 @@ protected void validateVariablesTargetLocation(Tree tree, AnnotatedTypeMirror ty @Override protected void validateTargetLocation( Tree tree, AnnotatedTypeMirror type, TypeUseLocation required) { - if (ignoreTargetLocation) { - return; + if (!this.infer) { + super.validateTargetLocation(tree, type, required); + } else { + if (ignoreTargetLocation) { + return; + } + mainIsNoneOf(type, targetLocationToAnno.get(required).toArray(new AnnotationMirror[0]), + "type.invalid.annotations.on.location", tree); } - - mainIsNoneOf(type, targetLocationToAnno.get(required).toArray(new AnnotationMirror[0]), - "type.invalid.annotations.on.location", tree); } } From 30e76d47b15c791f9a224408562468a3f4d1cc1a Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Fri, 13 Jan 2023 16:40:32 -0500 Subject: [PATCH 06/20] disable all targetlocation constraints for the dataflow typesystem --- src/checkers/inference/InferenceVisitor.java | 7 +++++++ src/dataflow/qual/DataFlowInferenceBottom.java | 2 +- src/dataflow/qual/DataFlowTop.java | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index 48616770..f5e4c36c 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -886,6 +886,13 @@ protected void initTargetLocationToAnno() { continue; } for (TypeUseLocation location : tls.value()) { + if (location == TypeUseLocation.ALL) { + for (TypeUseLocation val : TypeUseLocation.values()) { + Set amSet = targetLocationToAnno.get(val); + amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + } + break; + } Set amSet = targetLocationToAnno.get(location); amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); } diff --git a/src/dataflow/qual/DataFlowInferenceBottom.java b/src/dataflow/qual/DataFlowInferenceBottom.java index 1f3d8e5e..eed69ce7 100644 --- a/src/dataflow/qual/DataFlowInferenceBottom.java +++ b/src/dataflow/qual/DataFlowInferenceBottom.java @@ -16,7 +16,7 @@ @InvisibleQualifier @SubtypeOf({ DataFlow.class }) @Target({ ElementType.TYPE_USE }) -@TargetLocations({ TypeUseLocation.EXPLICIT_LOWER_BOUND }) +@TargetLocations({ TypeUseLocation.ALL}) public @interface DataFlowInferenceBottom { } diff --git a/src/dataflow/qual/DataFlowTop.java b/src/dataflow/qual/DataFlowTop.java index ad9d6ae6..8c6a0d00 100644 --- a/src/dataflow/qual/DataFlowTop.java +++ b/src/dataflow/qual/DataFlowTop.java @@ -13,7 +13,7 @@ @InvisibleQualifier @SubtypeOf({}) @Target({ ElementType.TYPE_USE }) -@TargetLocations({ TypeUseLocation.EXPLICIT_UPPER_BOUND }) +@TargetLocations({ TypeUseLocation.ALL }) public @interface DataFlowTop { } \ No newline at end of file From c3b7c95a1947201035d9fa46c37c8d086751a2e0 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Mon, 16 Jan 2023 10:11:21 -0500 Subject: [PATCH 07/20] bug fix --- src/checkers/inference/InferenceVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index f5e4c36c..e25fd4a7 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -902,7 +902,7 @@ protected void initTargetLocationToAnno() { @Override protected void validateVariablesTargetLocation(Tree tree, AnnotatedTypeMirror type) { if (!infer) { - super.initAnnoToTargetLocations(); + super.validateVariablesTargetLocation(tree, type); return; } From 4704bf59c2c4aaf640f7ac49a3f241f4e77fb26f Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Tue, 31 Jan 2023 15:18:39 -0500 Subject: [PATCH 08/20] change addDeclarationConstraints and handleWasRawDeclaredTypes private access to protected access --- src/checkers/inference/VariableAnnotator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checkers/inference/VariableAnnotator.java b/src/checkers/inference/VariableAnnotator.java index 24d9e65b..330c7426 100644 --- a/src/checkers/inference/VariableAnnotator.java +++ b/src/checkers/inference/VariableAnnotator.java @@ -735,7 +735,7 @@ public Void visitDeclared(final AnnotatedDeclaredType adt, final Tree tree) { return null; } - private boolean handleWasRawDeclaredTypes(AnnotatedDeclaredType adt) { + protected boolean handleWasRawDeclaredTypes(AnnotatedDeclaredType adt) { if (adt.isUnderlyingTypeRaw() && adt.getTypeArguments().size() != 0) { // the type arguments should be wildcards AND if I get the real type of "tree" // it corresponds to the declaration of adt.getUnderlyingType @@ -1678,7 +1678,7 @@ public AnnotationMirror getClassDeclVarAnnot(TypeElement ele) { } - private void addDeclarationConstraints(Slot declSlot, Slot instanceSlot) { + protected void addDeclarationConstraints(Slot declSlot, Slot instanceSlot) { constraintManager.addSubtypeConstraint(instanceSlot, declSlot); } From df5d7e09603c97adb19fce7cf0bf37198d3b2749 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Tue, 28 Feb 2023 10:14:41 -0500 Subject: [PATCH 09/20] get default type for new class tree --- src/checkers/inference/util/SlotDefaultTypeResolver.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index 2f081931..577051fe 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -3,6 +3,7 @@ import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.ArrayTypeTree; import com.sun.source.tree.ClassTree; +import com.sun.source.tree.NewClassTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.PrimitiveTypeTree; import com.sun.source.tree.Tree; @@ -185,5 +186,12 @@ public Void visitAnnotatedType(AnnotatedTypeTree tree, Void unused) { return super.visitAnnotatedType(tree, unused); } + + @Override + public Void visitNewClass(NewClassTree tree, Void unused) { + AnnotatedTypeMirror defaultType = getDefaultTypeFor(tree); + defaultTypes.put(tree.getIdentifier(), defaultType); + return super.visitNewClass(tree, unused); + } } } From 09ffa389597bc6e39635651bfc26fc8e873fb2f7 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Tue, 28 Feb 2023 12:19:22 -0500 Subject: [PATCH 10/20] fix assertion failure --- src/checkers/inference/util/SlotDefaultTypeResolver.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index 577051fe..d5aed1e2 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -3,6 +3,7 @@ import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.ArrayTypeTree; import com.sun.source.tree.ClassTree; +import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.NewClassTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.PrimitiveTypeTree; @@ -190,7 +191,11 @@ public Void visitAnnotatedType(AnnotatedTypeTree tree, Void unused) { @Override public Void visitNewClass(NewClassTree tree, Void unused) { AnnotatedTypeMirror defaultType = getDefaultTypeFor(tree); - defaultTypes.put(tree.getIdentifier(), defaultType); + ExpressionTree type = tree.getIdentifier(); + if ((type instanceof ParameterizedTypeTree) && !((ParameterizedTypeTree) type).getTypeArguments().isEmpty()) + return super.visitNewClass(tree, unused); + + defaultTypes.put(type, defaultType); return super.visitNewClass(tree, unused); } } From f6279cf09e82f558f9b5721118c7612c839b6df5 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Thu, 2 Mar 2023 10:03:28 -0500 Subject: [PATCH 11/20] workaround if it's an anonymous class --- src/checkers/inference/util/SlotDefaultTypeResolver.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index d5aed1e2..34f314ef 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -191,11 +191,12 @@ public Void visitAnnotatedType(AnnotatedTypeTree tree, Void unused) { @Override public Void visitNewClass(NewClassTree tree, Void unused) { AnnotatedTypeMirror defaultType = getDefaultTypeFor(tree); - ExpressionTree type = tree.getIdentifier(); - if ((type instanceof ParameterizedTypeTree) && !((ParameterizedTypeTree) type).getTypeArguments().isEmpty()) + if (InferenceUtil.isAnonymousClass(tree)) { + // don't associate the identifier with the defaulttype if it's an anonymousclass + // should associate the identifier with the underlying type of the defaulttype and annotation to it. return super.visitNewClass(tree, unused); - - defaultTypes.put(type, defaultType); + } + defaultTypes.put(tree.getIdentifier(), defaultType); return super.visitNewClass(tree, unused); } } From fb56232410b9e7f97bd7f0c41645a8a56aa1b51b Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Fri, 3 Mar 2023 13:47:48 -0500 Subject: [PATCH 12/20] get direct super types of the defaultType --- src/checkers/inference/util/SlotDefaultTypeResolver.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index 34f314ef..f06520b7 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -192,11 +192,12 @@ public Void visitAnnotatedType(AnnotatedTypeTree tree, Void unused) { public Void visitNewClass(NewClassTree tree, Void unused) { AnnotatedTypeMirror defaultType = getDefaultTypeFor(tree); if (InferenceUtil.isAnonymousClass(tree)) { - // don't associate the identifier with the defaulttype if it's an anonymousclass - // should associate the identifier with the underlying type of the defaulttype and annotation to it. - return super.visitNewClass(tree, unused); + // don't associate the identifier with the defaultType if it's an anonymousclass + // should associate the identifier with the direct super type of the defaultType. + defaultTypes.put(tree.getIdentifier(), defaultType.directSupertypes().get(0)); + } else { + defaultTypes.put(tree.getIdentifier(), defaultType); } - defaultTypes.put(tree.getIdentifier(), defaultType); return super.visitNewClass(tree, unused); } } From 493963ae006811541522095078b757d1f3b9e72f Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Fri, 3 Mar 2023 14:23:04 -0500 Subject: [PATCH 13/20] fix the case when the anonymous class's directSuper type is an interface --- src/checkers/inference/util/SlotDefaultTypeResolver.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index f06520b7..e2034e46 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -194,7 +194,10 @@ public Void visitNewClass(NewClassTree tree, Void unused) { if (InferenceUtil.isAnonymousClass(tree)) { // don't associate the identifier with the defaultType if it's an anonymousclass // should associate the identifier with the direct super type of the defaultType. - defaultTypes.put(tree.getIdentifier(), defaultType.directSupertypes().get(0)); + // choose the last one of the directSupertypes, which is either the direct super class + // or implemented interface + List superTypes = defaultType.directSupertypes(); + defaultTypes.put(tree.getIdentifier(), superTypes.get(superTypes.size()-1)); } else { defaultTypes.put(tree.getIdentifier(), defaultType); } From bd9cbbc45286a682d5fe2a757de599c097824ea1 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Thu, 30 Mar 2023 11:13:42 -0400 Subject: [PATCH 14/20] add makeDefaultsExplicit option --- tests/checkers/inference/test/CFInferenceTest.java | 7 ++++++- .../test/ImmutableInferenceTestConfiguration.java | 9 ++++++++- .../inference/test/InferenceTestConfiguration.java | 1 + .../test/InferenceTestConfigurationBuilder.java | 14 +++++++++++--- .../inference/test/InferenceTestExecutor.java | 3 +++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/tests/checkers/inference/test/CFInferenceTest.java b/tests/checkers/inference/test/CFInferenceTest.java index b2ee3f9d..cd76a046 100644 --- a/tests/checkers/inference/test/CFInferenceTest.java +++ b/tests/checkers/inference/test/CFInferenceTest.java @@ -30,6 +30,10 @@ public boolean useHacks() { return SystemUtil.getBooleanSystemProperty("use.hacks"); } + public boolean makeDefaultsExplicit() { + return false; + } + public abstract Pair> getSolverNameAndOptions(); public List getAdditionalInferenceOptions() { @@ -54,7 +58,8 @@ public void run() { InferenceTestConfiguration config = InferenceTestConfigurationBuilder.buildDefaultConfiguration(testDir, testFile, testDataDir, checker, checkerOptions, getAdditionalInferenceOptions(), solverArgs.first, - solverArgs.second, useHacks(), shouldEmitDebugInfo, getPathToAfuScripts(), getPathToInferenceScript()); + solverArgs.second, useHacks(), makeDefaultsExplicit(), shouldEmitDebugInfo, getPathToAfuScripts(), + getPathToInferenceScript()); InferenceTestResult testResult = new InferenceTestExecutor().runTest(config); InferenceTestUtilities.assertResultsAreValid(testResult); diff --git a/tests/checkers/inference/test/ImmutableInferenceTestConfiguration.java b/tests/checkers/inference/test/ImmutableInferenceTestConfiguration.java index f44fcf09..12a47261 100644 --- a/tests/checkers/inference/test/ImmutableInferenceTestConfiguration.java +++ b/tests/checkers/inference/test/ImmutableInferenceTestConfiguration.java @@ -16,13 +16,15 @@ public class ImmutableInferenceTestConfiguration implements InferenceTestConfigu private final String solver; private final Map solverArgs; private final boolean shouldUseHacks; + private final boolean makeDefaultsExplicit; private final String pathToAfuScripts; private final String pathToInferenceScript; private final TestConfiguration initialConfig; public ImmutableInferenceTestConfiguration(File outputJaif, File testDataDir, File annotatedSourceDir, Map inferenceJavacArgs, String solver, - Map solverArgs, boolean shouldUseHacks, String pathToAfuScripts, + Map solverArgs, boolean shouldUseHacks, + boolean makeDefaultsExplicit, String pathToAfuScripts, String pathToInferenceScript, TestConfiguration initialConfig) { this.outputJaif = outputJaif; this.testDataDir = testDataDir; @@ -31,6 +33,7 @@ public ImmutableInferenceTestConfiguration(File outputJaif, File testDataDir, Fi this.solver = solver; this.solverArgs = solverArgs; this.shouldUseHacks = shouldUseHacks; + this.makeDefaultsExplicit = makeDefaultsExplicit; this.pathToAfuScripts = pathToAfuScripts; this.initialConfig = initialConfig; this.pathToInferenceScript = pathToInferenceScript; @@ -69,6 +72,10 @@ public boolean shouldUseHacks() { return shouldUseHacks; } + public boolean makeDefaultsExplicit() { + return makeDefaultsExplicit; + } + public String getPathToAfuScripts() { return pathToAfuScripts; } diff --git a/tests/checkers/inference/test/InferenceTestConfiguration.java b/tests/checkers/inference/test/InferenceTestConfiguration.java index b084f7bf..d7c4ce90 100644 --- a/tests/checkers/inference/test/InferenceTestConfiguration.java +++ b/tests/checkers/inference/test/InferenceTestConfiguration.java @@ -20,6 +20,7 @@ public interface InferenceTestConfiguration { List getFlatSolverArgs(); boolean shouldUseHacks(); + boolean makeDefaultsExplicit(); String getPathToAfuScripts(); String getPathToInferenceScript(); diff --git a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java index 1d296209..668b0f1d 100644 --- a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java +++ b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java @@ -17,6 +17,7 @@ public class InferenceTestConfigurationBuilder { private File testDataDir = null; private String solver = null; private boolean shouldUseHacks; + private boolean makeDefaultsExplicit; private String pathToAfuScripts=""; private String pathToInferenceScript=""; @@ -60,6 +61,11 @@ public InferenceTestConfigurationBuilder setShouldUseHacks(boolean shouldUseHack return this; } + public InferenceTestConfigurationBuilder setMakeDefaultsExplicit(boolean makeDefaultsExplicit) { + this.makeDefaultsExplicit = makeDefaultsExplicit; + return this; + } + public InferenceTestConfigurationBuilder setPathToAfuScripts(String pathToAfuScripts) { this.pathToAfuScripts = pathToAfuScripts; return this; @@ -156,8 +162,8 @@ public List validate() { public InferenceTestConfiguration build() { return new ImmutableInferenceTestConfiguration(outputJaif, testDataDir, annotatedSourceDir, new LinkedHashMap<>(inferenceJavacArgs.getOptions()), - solver, new LinkedHashMap<>(solverArgs.getOptions()), shouldUseHacks,pathToAfuScripts, - pathToInferenceScript, initialConfiguration); + solver, new LinkedHashMap<>(solverArgs.getOptions()), shouldUseHacks, makeDefaultsExplicit, + pathToAfuScripts, pathToInferenceScript, initialConfiguration); } public InferenceTestConfiguration validateThenBuild() { @@ -175,7 +181,8 @@ public InferenceTestConfiguration validateThenBuild() { public static InferenceTestConfiguration buildDefaultConfiguration( String testSourcePath, File testFile, File testDataRoot, Class checker, List typecheckOptions, List inferenceOptions, String solverName, List solverOptions, - boolean shouldUseHacks, boolean shouldEmitDebugInfo, String pathToAfuScripts, String pathToInferenceScript) { + boolean shouldUseHacks, boolean makeDefaultsExplicit, boolean shouldEmitDebugInfo, String pathToAfuScripts, + String pathToInferenceScript) { final File defaultInferenceOutDir = new File("testdata/tmp"); final File defaultOutputJaif = new File(defaultInferenceOutDir, "default.jaif"); @@ -192,6 +199,7 @@ public static InferenceTestConfiguration buildDefaultConfiguration( .setAnnotatedSourceDir(defaultAnnotatedSourceDir) .setSolver(solverName) .setShouldUseHacks(shouldUseHacks) + .setMakeDefaultsExplicit(makeDefaultsExplicit) .setPathToAfuScripts(pathToAfuScripts) .setPathToInferenceScript(pathToInferenceScript); diff --git a/tests/checkers/inference/test/InferenceTestExecutor.java b/tests/checkers/inference/test/InferenceTestExecutor.java index 14345bf9..39ee1b9a 100644 --- a/tests/checkers/inference/test/InferenceTestExecutor.java +++ b/tests/checkers/inference/test/InferenceTestExecutor.java @@ -80,6 +80,9 @@ public static InferenceResult infer(InferenceTestConfiguration configuration) { if (configuration.shouldUseHacks()) { options.add("--hacks"); } + if (configuration.makeDefaultsExplicit()) { + options.add("--makeDefaultsExplicit=true"); + } options.add("--jaifFile=" + configuration.getOutputJaif().getAbsolutePath()); From a0a9e74a50d749ee29fb2c99fffe2f50ce507969 Mon Sep 17 00:00:00 2001 From: Haifeng Shi Date: Thu, 30 Mar 2023 11:44:25 -0400 Subject: [PATCH 15/20] add makeDefaultsExplicit option --- src/checkers/inference/InferenceLauncher.java | 2 +- tests/checkers/inference/test/InferenceTestExecutor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/checkers/inference/InferenceLauncher.java b/src/checkers/inference/InferenceLauncher.java index 3d1fdce1..c229d259 100644 --- a/src/checkers/inference/InferenceLauncher.java +++ b/src/checkers/inference/InferenceLauncher.java @@ -205,7 +205,7 @@ public void infer() { Mode mode = Mode.valueOf(InferenceOptions.mode); if (InferenceOptions.makeDefaultsExplicit - && (mode == Mode.ROUNDTRIP || mode == Mode.ROUNDTRIP_TYPECHECK)) { + && (mode == Mode.ROUNDTRIP || mode == Mode.ROUNDTRIP_TYPECHECK || mode == Mode.INFER)) { // 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`) diff --git a/tests/checkers/inference/test/InferenceTestExecutor.java b/tests/checkers/inference/test/InferenceTestExecutor.java index 39ee1b9a..310145d4 100644 --- a/tests/checkers/inference/test/InferenceTestExecutor.java +++ b/tests/checkers/inference/test/InferenceTestExecutor.java @@ -81,7 +81,7 @@ public static InferenceResult infer(InferenceTestConfiguration configuration) { options.add("--hacks"); } if (configuration.makeDefaultsExplicit()) { - options.add("--makeDefaultsExplicit=true"); + options.add("--makeDefaultsExplicit"); } options.add("--jaifFile=" + configuration.getOutputJaif().getAbsolutePath()); From a2a1f615dba7e77ea1de98e0b859fdcfd31fa7dc Mon Sep 17 00:00:00 2001 From: Aosen Xiong Date: Sun, 7 Apr 2024 23:00:42 -0700 Subject: [PATCH 16/20] Fixed compile bugs --- src/checkers/inference/InferenceValidator.java | 3 +-- src/checkers/inference/InferenceVisitor.java | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/checkers/inference/InferenceValidator.java b/src/checkers/inference/InferenceValidator.java index e8506170..888a205f 100644 --- a/src/checkers/inference/InferenceValidator.java +++ b/src/checkers/inference/InferenceValidator.java @@ -48,14 +48,13 @@ protected void validateWildCardTargetLocation(AnnotatedTypeMirror.AnnotatedWildc AnnotationMirror[] mirrors = new AnnotationMirror[0]; for (AnnotationMirror am : type.getSuperBound().getAnnotations()) { inferVisitor.annoIsNoneOf(type, am, - inferVisitor.targetLocationToAnno.get(TypeUseLocation.LOWER_BOUND).toArray(mirrors), inferVisitor.locationToIllegalQuals.get(TypeUseLocation.LOWER_BOUND).toArray(mirrors), "type.invalid.annotations.on.location", tree); } for (AnnotationMirror am : type.getExtendsBound().getAnnotations()) { inferVisitor.annoIsNoneOf(type, am, - inferVisitor.targetLocationToAnno.get(TypeUseLocation.UPPER_BOUND).toArray(mirrors), + inferVisitor.locationToIllegalQuals.get(TypeUseLocation.UPPER_BOUND).toArray(mirrors), "type.invalid.annotations.on.location", tree); } } diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index e9449493..a0b787c3 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -84,7 +84,7 @@ public class InferenceVisitor> createMapForIllegalQuals() { - Map> locationToIllegalQuals = new HashMap<>(); + protected Map createMapForIllegalQuals() { + Map locationToIllegalQuals = new HashMap<>(); // First, init each type-use location to contain all type qualifiers. Set> supportQualifiers = atypeFactory.getSupportedTypeQualifiers(); - Set supportedAnnos = new AnnotationMirrorSet(); + AnnotationMirrorSet supportedAnnos = new AnnotationMirrorSet(); for (Class qual: supportQualifiers) { supportedAnnos.add(new AnnotationBuilder(atypeFactory.getProcessingEnv(), qual).build()); } for (TypeUseLocation location : TypeUseLocation.values()) { - locationToIllegalQuals.put(location, new HashSet<>(supportedAnnos)); + locationToIllegalQuals.put(location, new AnnotationMirrorSet(supportedAnnos)); } // Then, delete some qualifiers which can be applied to that type-use location. // this leaves only qualifiers not allowed on that location. @@ -998,10 +998,10 @@ protected void validateTargetLocation( if (!this.infer) { super.validateTargetLocation(tree, type, required); } else { - if (ignoreTargetLocation) { + if (ignoreTargetLocations) { return; } - mainIsNoneOf(type, targetLocationToAnno.get(required).toArray(new AnnotationMirror[0]), + mainIsNoneOf(type, locationToIllegalQuals.get(required).toArray(new AnnotationMirror[0]), "type.invalid.annotations.on.location", tree); } } From 93635f85fdfb46195e710e6b431cb82daff39e0a Mon Sep 17 00:00:00 2001 From: Aosen Xiong Date: Wed, 24 Apr 2024 12:55:23 -0400 Subject: [PATCH 17/20] Apply spotless --- src/checkers/inference/InferenceLauncher.java | 4 +- .../inference/InferenceValidator.java | 1 - src/checkers/inference/InferenceVisitor.java | 52 +++++++++++-------- .../util/SlotDefaultTypeResolver.java | 6 +-- .../inference/test/CFInferenceTest.java | 21 ++++++-- .../test/InferenceTestConfiguration.java | 4 +- .../InferenceTestConfigurationBuilder.java | 6 +-- 7 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/checkers/inference/InferenceLauncher.java b/src/checkers/inference/InferenceLauncher.java index b53962e5..bed4568b 100644 --- a/src/checkers/inference/InferenceLauncher.java +++ b/src/checkers/inference/InferenceLauncher.java @@ -223,7 +223,9 @@ public void infer() { Mode mode = Mode.valueOf(InferenceOptions.mode); if (InferenceOptions.makeDefaultsExplicit - && (mode == Mode.ROUNDTRIP || mode == Mode.ROUNDTRIP_TYPECHECK || mode == Mode.INFER)) { + && (mode == Mode.ROUNDTRIP + || mode == Mode.ROUNDTRIP_TYPECHECK + || mode == Mode.INFER)) { // 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`) diff --git a/src/checkers/inference/InferenceValidator.java b/src/checkers/inference/InferenceValidator.java index 47ba9eb0..6ffdb887 100644 --- a/src/checkers/inference/InferenceValidator.java +++ b/src/checkers/inference/InferenceValidator.java @@ -9,7 +9,6 @@ import org.checkerframework.framework.type.AnnotatedTypeMirror; import javax.lang.model.element.AnnotationMirror; -import java.util.List; /** A visitor to validate the types in a tree. */ public class InferenceValidator extends BaseTypeValidator { diff --git a/src/checkers/inference/InferenceVisitor.java b/src/checkers/inference/InferenceVisitor.java index 1d203b1d..a3a2736e 100644 --- a/src/checkers/inference/InferenceVisitor.java +++ b/src/checkers/inference/InferenceVisitor.java @@ -25,6 +25,7 @@ import org.checkerframework.framework.type.AnnotatedTypeParameterBounds; import org.checkerframework.framework.util.AnnotatedTypes; import org.checkerframework.javacutil.*; +import org.checkerframework.javacutil.TreeUtils; import org.plumelib.util.ArraysPlume; import java.lang.annotation.Annotation; @@ -52,17 +53,6 @@ import checkers.inference.qual.VarAnnot; import checkers.inference.util.InferenceUtil; -import com.sun.source.tree.CatchTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.ThrowTree; -import com.sun.source.tree.Tree; -import com.sun.source.tree.Tree.Kind; -import com.sun.source.tree.VariableTree; - -import org.checkerframework.javacutil.TreeUtils; -import org.plumelib.util.ArraysPlume; - - /** * InferenceVisitor visits trees in each compilation unit both in typecheck/inference mode. In * typecheck mode, it functions nearly identically to BaseTypeVisitor, i.e. it enforces common @@ -91,7 +81,7 @@ public class InferenceVisitor< protected final boolean infer; protected final Checker realChecker; - /* + /* * Map from type-use location to a list of qualifiers which cannot be used on that location. * This is used to create the inequality constraint in inference. */ @@ -318,8 +308,12 @@ public void mainIsNoneOf( } } - private void addDeepPreferenceImpl(AnnotatedTypeMirror ty, AnnotationMirror goal, int weight, - java.util.List visited, Tree node) { + private void addDeepPreferenceImpl( + AnnotatedTypeMirror ty, + AnnotationMirror goal, + int weight, + java.util.List visited, + Tree node) { if (infer) { if (visited.contains(ty)) { return; @@ -330,7 +324,8 @@ private void addDeepPreferenceImpl(AnnotatedTypeMirror ty, AnnotationMirror goal Slot el = slotManager.getSlot(ty); if (el == null) { - logger.warning("InferenceVisitor::addDeepPreferenceImpl: no annotation in type: " + ty); + logger.warning( + "InferenceVisitor::addDeepPreferenceImpl: no annotation in type: " + ty); } else { addPreference(ty, goal, weight); } @@ -352,7 +347,8 @@ private void addDeepPreferenceImpl(AnnotatedTypeMirror ty, AnnotationMirror goal // Else, do nothing } - public void addDeepPreference(AnnotatedTypeMirror ty, AnnotationMirror goal, int weight, Tree node) { + public void addDeepPreference( + AnnotatedTypeMirror ty, AnnotationMirror goal, int weight, Tree node) { addDeepPreferenceImpl(ty, goal, weight, new LinkedList<>(), node); } @@ -1035,10 +1031,12 @@ protected void checkConstructorResult( protected Map createMapForIllegalQuals() { Map locationToIllegalQuals = new HashMap<>(); // First, init each type-use location to contain all type qualifiers. - Set> supportQualifiers = atypeFactory.getSupportedTypeQualifiers(); + Set> supportQualifiers = + atypeFactory.getSupportedTypeQualifiers(); AnnotationMirrorSet supportedAnnos = new AnnotationMirrorSet(); - for (Class qual: supportQualifiers) { - supportedAnnos.add(new AnnotationBuilder(atypeFactory.getProcessingEnv(), qual).build()); + for (Class qual : supportQualifiers) { + supportedAnnos.add( + new AnnotationBuilder(atypeFactory.getProcessingEnv(), qual).build()); } for (TypeUseLocation location : TypeUseLocation.values()) { locationToIllegalQuals.put(location, new AnnotationMirrorSet(supportedAnnos)); @@ -1053,7 +1051,9 @@ protected Map createMapForIllegalQuals() { if (tls == null) { for (TypeUseLocation location : TypeUseLocation.values()) { AnnotationMirrorSet amSet = locationToIllegalQuals.get(location); - amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + amSet.remove( + AnnotationUtils.getAnnotationByName( + supportedAnnos, qual.getCanonicalName())); } continue; } @@ -1061,12 +1061,16 @@ protected Map createMapForIllegalQuals() { if (location == TypeUseLocation.ALL) { for (TypeUseLocation val : TypeUseLocation.values()) { AnnotationMirrorSet amSet = locationToIllegalQuals.get(val); - amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + amSet.remove( + AnnotationUtils.getAnnotationByName( + supportedAnnos, qual.getCanonicalName())); } break; } AnnotationMirrorSet amSet = locationToIllegalQuals.get(location); - amSet.remove(AnnotationUtils.getAnnotationByName(supportedAnnos, qual.getCanonicalName())); + amSet.remove( + AnnotationUtils.getAnnotationByName( + supportedAnnos, qual.getCanonicalName())); } } return locationToIllegalQuals; @@ -1108,7 +1112,9 @@ protected void validateVariablesTargetLocation(Tree tree, AnnotatedTypeMirror ty break; case ENUM_CONSTANT: location = TypeUseLocation.CONSTRUCTOR_RESULT; - // TODO: Add ? mainIsNoneOf(type, targetLocationToAnno.get(TypeUseLocation.FIELD).toArray(mirrors), "type.invalid.annotations.on.location", tree); + // TODO: Add ? mainIsNoneOf(type, + // targetLocationToAnno.get(TypeUseLocation.FIELD).toArray(mirrors), + // "type.invalid.annotations.on.location", tree); break; default: throw new BugInCF("Location not matched"); diff --git a/src/checkers/inference/util/SlotDefaultTypeResolver.java b/src/checkers/inference/util/SlotDefaultTypeResolver.java index 76fdb4e5..a894e62a 100644 --- a/src/checkers/inference/util/SlotDefaultTypeResolver.java +++ b/src/checkers/inference/util/SlotDefaultTypeResolver.java @@ -3,7 +3,6 @@ import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.ArrayTypeTree; import com.sun.source.tree.ClassTree; -import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.NewClassTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.PrimitiveTypeTree; @@ -190,10 +189,11 @@ public Void visitNewClass(NewClassTree tree, Void unused) { if (InferenceUtil.isAnonymousClass(tree)) { // don't associate the identifier with the defaultType if it's an anonymousclass // should associate the identifier with the direct super type of the defaultType. - // choose the last one of the directSupertypes, which is either the direct super class + // choose the last one of the directSupertypes, which is either the direct super + // class // or implemented interface List superTypes = defaultType.directSupertypes(); - defaultTypes.put(tree.getIdentifier(), superTypes.get(superTypes.size()-1)); + defaultTypes.put(tree.getIdentifier(), superTypes.get(superTypes.size() - 1)); } else { defaultTypes.put(tree.getIdentifier(), defaultType); } diff --git a/tests/checkers/inference/test/CFInferenceTest.java b/tests/checkers/inference/test/CFInferenceTest.java index 8f7b7c0c..a1cbbbac 100644 --- a/tests/checkers/inference/test/CFInferenceTest.java +++ b/tests/checkers/inference/test/CFInferenceTest.java @@ -36,7 +36,7 @@ public boolean useHacks() { public boolean makeDefaultsExplicit() { return false; } - + public abstract IPair> getSolverNameAndOptions(); public List getAdditionalInferenceOptions() { @@ -59,10 +59,21 @@ public void run() { final File testDataDir = new File("testdata"); - InferenceTestConfiguration config = InferenceTestConfigurationBuilder.buildDefaultConfiguration(testDir, - testFile, testDataDir, checker, checkerOptions, getAdditionalInferenceOptions(), solverArgs.first, - solverArgs.second, useHacks(), makeDefaultsExplicit(), shouldEmitDebugInfo, getPathToAfuScripts(), - getPathToInferenceScript()); + InferenceTestConfiguration config = + InferenceTestConfigurationBuilder.buildDefaultConfiguration( + testDir, + testFile, + testDataDir, + checker, + checkerOptions, + getAdditionalInferenceOptions(), + solverArgs.first, + solverArgs.second, + useHacks(), + makeDefaultsExplicit(), + shouldEmitDebugInfo, + getPathToAfuScripts(), + getPathToInferenceScript()); InferenceTestResult testResult = new InferenceTestExecutor().runTest(config); InferenceTestUtilities.assertResultsAreValid(testResult); diff --git a/tests/checkers/inference/test/InferenceTestConfiguration.java b/tests/checkers/inference/test/InferenceTestConfiguration.java index 128a4862..8cd32694 100644 --- a/tests/checkers/inference/test/InferenceTestConfiguration.java +++ b/tests/checkers/inference/test/InferenceTestConfiguration.java @@ -25,9 +25,9 @@ public interface InferenceTestConfiguration { List getFlatSolverArgs(); boolean shouldUseHacks(); - + boolean makeDefaultsExplicit(); - + String getPathToAfuScripts(); String getPathToInferenceScript(); diff --git a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java index cb02f41f..b358f53b 100644 --- a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java +++ b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java @@ -18,8 +18,8 @@ public class InferenceTestConfigurationBuilder { private String solver = null; private boolean shouldUseHacks; private boolean makeDefaultsExplicit; - private String pathToAfuScripts=""; - private String pathToInferenceScript=""; + private String pathToAfuScripts = ""; + private String pathToInferenceScript = ""; private SimpleOptionMap inferenceJavacArgs = new SimpleOptionMap(); private SimpleOptionMap solverArgs = new SimpleOptionMap(); @@ -202,7 +202,7 @@ public static InferenceTestConfiguration buildDefaultConfiguration( String solverName, List solverOptions, boolean shouldUseHacks, - boolean makeDefaultsExplicit + boolean makeDefaultsExplicit, boolean shouldEmitDebugInfo, String pathToAfuScripts, String pathToInferenceScript) { From 799c0fd8185d3df31f53e9e22c238f3792b691b0 Mon Sep 17 00:00:00 2001 From: Aosen Xiong Date: Wed, 24 Apr 2024 13:11:32 -0400 Subject: [PATCH 18/20] Add back missing arguments --- .../inference/test/InferenceTestConfigurationBuilder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java index b358f53b..21fe85a0 100644 --- a/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java +++ b/tests/checkers/inference/test/InferenceTestConfigurationBuilder.java @@ -172,6 +172,7 @@ public InferenceTestConfiguration build() { solver, new LinkedHashMap<>(solverArgs.getOptions()), shouldUseHacks, + makeDefaultsExplicit, pathToAfuScripts, pathToInferenceScript, initialConfiguration); From f30f5a86ff8e03ab78abc830b0228f0ad4d20991 Mon Sep 17 00:00:00 2001 From: Aosen Xiong <82676488+Ao-senXiong@users.noreply.github.com> Date: Thu, 30 May 2024 10:19:44 -0400 Subject: [PATCH 19/20] Change CI target branch --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7af63409..1f6c13ae 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ master ] + branches: [ aosen-master ] pull_request: - branches: [ master ] + branches: [ aosen-master ] workflow_dispatch: From d62668f9417d040d30ccdc90e68cfdcdb9eb7f89 Mon Sep 17 00:00:00 2001 From: Aosen Xiong <82676488+Ao-senXiong@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:08:13 -0400 Subject: [PATCH 20/20] Revert change of targe branch --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1f6c13ae..7af63409 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ aosen-master ] + branches: [ master ] pull_request: - branches: [ aosen-master ] + branches: [ master ] workflow_dispatch: