Search in sources :

Example 41 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class NullnessTransfer method strengthenAnnotationOfEqualTo.

/**
 * {@inheritDoc}
 *
 * <p>Furthermore, this method refines the type to {@code NonNull} for the appropriate branch if
 * an expression is compared to the {@code null} literal (listed as case 1 in the class
 * description).
 */
@Override
protected TransferResult<NullnessValue, NullnessStore> strengthenAnnotationOfEqualTo(TransferResult<NullnessValue, NullnessStore> res, Node firstNode, Node secondNode, NullnessValue firstValue, NullnessValue secondValue, boolean notEqualTo) {
    res = super.strengthenAnnotationOfEqualTo(res, firstNode, secondNode, firstValue, secondValue, notEqualTo);
    if (firstNode instanceof NullLiteralNode) {
        NullnessStore thenStore = res.getThenStore();
        NullnessStore elseStore = res.getElseStore();
        List<Node> secondParts = splitAssignments(secondNode);
        for (Node secondPart : secondParts) {
            Receiver secondInternal = FlowExpressions.internalReprOf(analysis.getTypeFactory(), secondPart);
            if (CFAbstractStore.canInsertReceiver(secondInternal)) {
                thenStore = thenStore == null ? res.getThenStore() : thenStore;
                elseStore = elseStore == null ? res.getElseStore() : elseStore;
                if (notEqualTo) {
                    thenStore.insertValue(secondInternal, NONNULL);
                } else {
                    elseStore.insertValue(secondInternal, NONNULL);
                }
            }
        }
        Set<AnnotationMirror> secondAnnos = secondValue != null ? secondValue.getAnnotations() : AnnotationUtils.createAnnotationSet();
        if (AnnotationUtils.containsSameByClass(secondAnnos, PolyNull.class) || AnnotationUtils.containsSameByClass(secondAnnos, PolyAll.class)) {
            thenStore = thenStore == null ? res.getThenStore() : thenStore;
            elseStore = elseStore == null ? res.getElseStore() : elseStore;
            thenStore.setPolyNullNull(true);
        }
        if (thenStore != null) {
            return new ConditionalTransferResult<>(res.getResultValue(), thenStore, elseStore);
        }
    }
    return res;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) PolyAll(org.checkerframework.framework.qual.PolyAll) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) Node(org.checkerframework.dataflow.cfg.node.Node) PolyNull(org.checkerframework.checker.nullness.qual.PolyNull) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode)

Example 42 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class RegexTransfer method handleMatcherGroupCount.

/**
 * See whether possibleMatcher is a call of groupCount on a Matcher and possibleConstant is a
 * constant. If so, annotate the matcher as constant + 1 if !isAlsoEqual constant if isAlsoEqual
 *
 * @param possibleMatcher the Node that might be a call of Matcher.groupCount()
 * @param possibleConstant the Node that might be a constant
 * @param isAlsoEqual whether the comparison operation is strict or reflexive
 * @param in the TransferInput
 * @param resultIn TransferResult
 * @return the possibly refined output TransferResult
 */
private TransferResult<CFValue, CFStore> handleMatcherGroupCount(Node possibleMatcher, Node possibleConstant, boolean isAlsoEqual, TransferInput<CFValue, CFStore> in, TransferResult<CFValue, CFStore> resultIn) {
    if (!(possibleMatcher instanceof MethodInvocationNode)) {
        return resultIn;
    }
    if (!(possibleConstant instanceof IntegerLiteralNode)) {
        return resultIn;
    }
    MethodAccessNode methodAccessNode = ((MethodInvocationNode) possibleMatcher).getTarget();
    ExecutableElement method = methodAccessNode.getMethod();
    Node receiver = methodAccessNode.getReceiver();
    if (!isMatcherGroupCountMethod(method, receiver)) {
        return resultIn;
    }
    Receiver matcherReceiver = FlowExpressions.internalReprOf(analysis.getTypeFactory(), receiver);
    IntegerLiteralNode iln = (IntegerLiteralNode) possibleConstant;
    int groupCount;
    if (isAlsoEqual) {
        groupCount = iln.getValue();
    } else {
        groupCount = iln.getValue() + 1;
    }
    CFStore thenStore = resultIn.getRegularStore();
    CFStore elseStore = thenStore.copy();
    ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(resultIn.getResultValue(), thenStore, elseStore);
    RegexAnnotatedTypeFactory factory = (RegexAnnotatedTypeFactory) analysis.getTypeFactory();
    AnnotationMirror regexAnnotation = factory.createRegexAnnotation(groupCount);
    thenStore.insertValue(matcherReceiver, regexAnnotation);
    return newResult;
}
Also used : CFStore(org.checkerframework.framework.flow.CFStore) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode)

Example 43 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class LessThanTransfer method refineGTE.

/**
 * Case 2.
 */
@Override
protected void refineGTE(Node left, AnnotationMirror leftAnno, Node right, AnnotationMirror rightAnno, CFStore store, TransferInput<CFValue, CFStore> in) {
    // left >= right so right is less than left
    // Refine right to @LessThan("left + 1")
    LessThanAnnotatedTypeFactory factory = (LessThanAnnotatedTypeFactory) analysis.getTypeFactory();
    // left > right so right is less than left
    // Refine right to @LessThan("left")
    Receiver leftRec = FlowExpressions.internalReprOf(factory, left);
    if (leftRec != null && leftRec.isUnmodifiableByOtherCode()) {
        List<String> lessThanExpressions = LessThanAnnotatedTypeFactory.getLessThanExpressions(rightAnno);
        if (lessThanExpressions == null) {
            // right is already bottom, nothing to refine.
            return;
        }
        lessThanExpressions.add(leftRec.toString() + " + 1");
        Receiver rightRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), right);
        store.insertValue(rightRec, factory.createLessThanQualifier(lessThanExpressions));
    }
}
Also used : Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 44 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class LowerBoundTransfer method notEqualToValue.

/**
 * Refines GTEN1 to NN if it is not equal to -1, and NN to Pos if it is not equal to 0.
 * Implements case 7.
 *
 * @param mLiteral a potential literal
 * @param otherNode the node on the other side of the ==/!=
 * @param otherAnno the annotation of the other side of the ==/!=
 */
private void notEqualToValue(Node mLiteral, Node otherNode, AnnotationMirror otherAnno, CFStore store) {
    Long integerLiteral = getExactValue(mLiteral.getTree(), aTypeFactory.getValueAnnotatedTypeFactory());
    if (integerLiteral == null) {
        return;
    }
    long intLiteral = integerLiteral.longValue();
    if (intLiteral == 0) {
        if (AnnotationUtils.areSameByClass(otherAnno, NonNegative.class)) {
            List<Node> internals = splitAssignments(otherNode);
            for (Node internal : internals) {
                Receiver rec = FlowExpressions.internalReprOf(aTypeFactory, internal);
                store.insertValue(rec, POS);
            }
        }
    } else if (intLiteral == -1) {
        if (AnnotationUtils.areSameByClass(otherAnno, GTENegativeOne.class)) {
            List<Node> internals = splitAssignments(otherNode);
            for (Node internal : internals) {
                Receiver rec = FlowExpressions.internalReprOf(aTypeFactory, internal);
                store.insertValue(rec, NN);
            }
        }
    }
}
Also used : BinaryOperationNode(org.checkerframework.dataflow.cfg.node.BinaryOperationNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) GTENegativeOne(org.checkerframework.checker.index.qual.GTENegativeOne) List(java.util.List)

Example 45 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class SameLenAnnotatedTypeFactory method createCombinedSameLen.

/**
 * For the use of the transfer function; generates a SameLen that includes a and b, as well as
 * everything in sl1 and sl2, if they are SameLen annotations.
 *
 * @param receivers a list of receivers representing arrays to be included in the combined
 *     annotation
 * @param annos a list of the current annotations of the receivers. Must be the same length as
 *     receivers.
 * @return a combined SameLen annotation
 */
public AnnotationMirror createCombinedSameLen(List<FlowExpressions.Receiver> receivers, List<AnnotationMirror> annos) {
    assert receivers.size() == annos.size();
    List<String> values = new ArrayList<>();
    for (int i = 0; i < receivers.size(); i++) {
        Receiver rec = receivers.get(i);
        AnnotationMirror anno = annos.get(i);
        if (shouldUseInAnnotation(rec)) {
            values.add(rec.toString());
        }
        if (AnnotationUtils.areSameByClass(anno, SameLen.class)) {
            values.addAll(getValueOfAnnotationWithStringArgument(anno));
        }
    }
    AnnotationMirror res = getCombinedSameLen(values, new ArrayList<>());
    return res;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Aggregations

Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)55 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Node (org.checkerframework.dataflow.cfg.node.Node)15 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)14 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)10 CFValue (org.checkerframework.framework.flow.CFValue)9 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)8 FieldAccess (org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess)8 NumericalAdditionNode (org.checkerframework.dataflow.cfg.node.NumericalAdditionNode)8 NumericalSubtractionNode (org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode)8 ArrayList (java.util.ArrayList)7 TypeMirror (javax.lang.model.type.TypeMirror)7 ClassName (org.checkerframework.dataflow.analysis.FlowExpressions.ClassName)7 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)7 Tree (com.sun.source.tree.Tree)6 TreePath (com.sun.source.util.TreePath)6 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)6 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)6 MethodTree (com.sun.source.tree.MethodTree)5 CaseNode (org.checkerframework.dataflow.cfg.node.CaseNode)5