Search in sources :

Example 31 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method propagateToSubtractionOperands.

/**
 * The subtraction node, {@code node}, is known to be {@code typeOfSubtraction}.
 *
 * <p>This means that the left node is less than or equal to the length of the array when the
 * right node is subtracted from the left node. Note that unlike {@link
 * #propagateToAdditionOperand(LessThanLengthOf, Node, Node, TransferInput, CFStore)} and {@link
 * #propagateToMultiplicationOperand(LessThanLengthOf, Node, Node, TransferInput, CFStore)}, this
 * method takes the NumericalSubtractionNode instead of the two operand nodes. This implements
 * case 4.
 *
 * @param typeOfSubtraction type of node
 * @param node subtraction node that has typeOfSubtraction
 * @param in a TransferInput
 * @param store location to store the refined type
 */
private void propagateToSubtractionOperands(LessThanLengthOf typeOfSubtraction, NumericalSubtractionNode node, TransferInput<CFValue, CFStore> in, CFStore store) {
    UBQualifier left = getUBQualifier(node.getLeftOperand(), in);
    UBQualifier newInfo = typeOfSubtraction.minusOffset(node.getRightOperand(), atypeFactory);
    UBQualifier newLeft = left.glb(newInfo);
    JavaExpression leftJe = JavaExpression.fromNode(node.getLeftOperand());
    store.insertValue(leftJe, atypeFactory.convertUBQualifierToAnnotation(newLeft));
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 32 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method refineSubtrahendWithOffset.

/**
 * Refines the subtrahend in a subtraction which is greater than or equal to a certain offset. The
 * type of the subtrahend is refined to the type of the minuend with the offset added. This is
 * case 10.
 *
 * <p>This is based on the fact that if {@code (minuend - subtrahend) >= offset}, and {@code
 * minuend + o < l}, then {@code subtrahend + o + offset < l}.
 *
 * <p>If {@code gtNode} is not a {@link NumericalSubtractionNode}, the method does nothing.
 *
 * @param gtNode the node that is greater or equal to the offset
 * @param offsetNode a node part of the offset
 * @param offsetAddOne whether to add one to the offset
 * @param in input of the transfer function
 * @param store location to store the refined types
 */
private void refineSubtrahendWithOffset(Node gtNode, Node offsetNode, boolean offsetAddOne, TransferInput<CFValue, CFStore> in, CFStore store) {
    if (gtNode instanceof NumericalSubtractionNode) {
        NumericalSubtractionNode subtractionNode = (NumericalSubtractionNode) gtNode;
        Node minuend = subtractionNode.getLeftOperand();
        UBQualifier minuendQual = getUBQualifier(minuend, in);
        Node subtrahend = subtractionNode.getRightOperand();
        UBQualifier subtrahendQual = getUBQualifier(subtrahend, in);
        UBQualifier newQual = subtrahendQual.glb(minuendQual.plusOffset(offsetNode, atypeFactory).plusOffset(offsetAddOne ? 1 : 0));
        JavaExpression subtrahendJe = JavaExpression.fromNode(subtrahend);
        store.insertValue(subtrahendJe, atypeFactory.convertUBQualifierToAnnotation(newQual));
    }
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) Node(org.checkerframework.dataflow.cfg.node.Node) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode)

Example 33 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundTransfer method getUBQualifier.

/**
 * Returns the UBQualifier for node. It does this by finding a {@link CFValue} for node. First it
 * checks the store in the transfer input. If one isn't there, the analysis is checked. If the
 * UNKNOWN qualifier is returned, then the AnnotatedTypeMirror from the type factory is used.
 *
 * @param n node
 * @param in transfer input
 * @return the UBQualifier for node
 */
private UBQualifier getUBQualifier(Node n, TransferInput<CFValue, CFStore> in) {
    QualifierHierarchy hierarchy = analysis.getTypeFactory().getQualifierHierarchy();
    JavaExpression je = JavaExpression.fromNode(n);
    CFValue value = null;
    if (CFAbstractStore.canInsertJavaExpression(je)) {
        value = in.getRegularStore().getValue(je);
    }
    if (value == null) {
        value = analysis.getValue(n);
    }
    UBQualifier qualifier = getUBQualifier(hierarchy, value);
    if (qualifier.isUnknown()) {
        // The qualifier from the store or analysis might be UNKNOWN if there was some error.
        // For example,
        // @LTLength("a") int i = 4;  // error
        // The type of i in the store is @UpperBoundUnknown, but the type of i as computed by
        // the type factory is @LTLength("a"), so use that type.
        CFValue valueFromFactory = getValueFromFactory(n.getTree(), n);
        return getUBQualifier(hierarchy, valueFromFactory);
    }
    return qualifier;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy)

Example 34 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class LowerBoundTransfer method refineGTE.

/**
 * Refines left to exactly the level of right, since in the worst case they're equal. Modifies an
 * existing type in the store, but has to be careful not to overwrite a more precise existing
 * type.
 *
 * <p>This implements parts of cases 1, 2, 3, and 4 using the decomposition strategy described in
 * this class's Javadoc.
 */
@Override
protected void refineGTE(Node left, AnnotationMirror leftAnno, Node right, AnnotationMirror rightAnno, CFStore store, TransferInput<CFValue, CFStore> in) {
    if (rightAnno == null || leftAnno == null) {
        return;
    }
    JavaExpression leftJe = JavaExpression.fromNode(left);
    AnnotationMirror newLBType = aTypeFactory.getQualifierHierarchy().greatestLowerBound(rightAnno, leftAnno);
    store.insertValue(leftJe, newLBType);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) JavaExpression(org.checkerframework.dataflow.expression.JavaExpression)

Example 35 with JavaExpression

use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.

the class UpperBoundVisitor method processSubsequenceForLHS.

/* Returns the new value of the left hand side after processing the arrays named in the lhs.
   * Iff varLtlQual includes LTL(lhsSeq),
   * lhsSeq has HSS, and expQual includes LTL(a, -from), then the LTL(lhsSeq) will be removed from varLtlQual
   */
private UBQualifier processSubsequenceForLHS(LessThanLengthOf varLtlQual, UBQualifier expQual) {
    UBQualifier newLHS = varLtlQual;
    for (String lhsSeq : varLtlQual.getSequences()) {
        // check is lhsSeq is an actual LTL
        if (varLtlQual.hasSequenceWithOffset(lhsSeq, 0)) {
            JavaExpression lhsSeqExpr = parseJavaExpressionString(lhsSeq, atypeFactory, getCurrentPath());
            Subsequence subSeq = Subsequence.getSubsequenceFromReceiver(lhsSeqExpr, atypeFactory);
            if (subSeq != null) {
                String from = subSeq.from;
                String a = subSeq.array;
                if (expQual.hasSequenceWithOffset(a, Subsequence.negateString(from))) {
                    // This cast is safe because LTLs cannot contain duplicates.
                    // Note that this updates newLHS on each iteration from its old value,
                    // so even if there are multiple HSS arrays the result will be correct.
                    newLHS = ((LessThanLengthOf) newLHS).removeOffset(lhsSeq, 0);
                }
            }
        }
    }
    return newLHS;
}
Also used : JavaExpression(org.checkerframework.dataflow.expression.JavaExpression) StringToJavaExpression(org.checkerframework.framework.util.StringToJavaExpression) HasSubsequence(org.checkerframework.checker.index.qual.HasSubsequence) Subsequence(org.checkerframework.checker.index.Subsequence)

Aggregations

JavaExpression (org.checkerframework.dataflow.expression.JavaExpression)87 AnnotationMirror (javax.lang.model.element.AnnotationMirror)39 Node (org.checkerframework.dataflow.cfg.node.Node)23 StringToJavaExpression (org.checkerframework.framework.util.StringToJavaExpression)21 CFValue (org.checkerframework.framework.flow.CFValue)20 ExecutableElement (javax.lang.model.element.ExecutableElement)19 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)19 ArrayList (java.util.ArrayList)15 CFStore (org.checkerframework.framework.flow.CFStore)15 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)13 JavaExpressionParseException (org.checkerframework.framework.util.JavaExpressionParseUtil.JavaExpressionParseException)13 Tree (com.sun.source.tree.Tree)12 List (java.util.List)11 FieldAccess (org.checkerframework.dataflow.expression.FieldAccess)11 MethodTree (com.sun.source.tree.MethodTree)10 TreePath (com.sun.source.util.TreePath)10 HashMap (java.util.HashMap)10 Map (java.util.Map)9 Element (javax.lang.model.element.Element)9 VariableElement (javax.lang.model.element.VariableElement)9