Search in sources :

Example 6 with StringConversionNode

use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.

the class CFGTranslationPhaseOne method stringConversion.

/**
 * Convert the input node to String type, if it isn't already.
 *
 * @param node an input node
 * @return a Node with the value promoted to String, which may be the input node
 */
protected Node stringConversion(Node node) {
    // For string conversion, see JLS 5.1.11
    if (!TypesUtils.isString(node.getType())) {
        Node converted = new StringConversionNode(node.getTree(), node, stringType);
        addToConvertedLookupMap(converted);
        insertNodeAfter(converted, node);
        return converted;
    } else {
        return node;
    }
}
Also used : NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode)

Example 7 with StringConversionNode

use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.

the class JavaExpression method fromNode.

/**
 * We ignore operations such as widening and narrowing when computing the internal representation.
 *
 * @param receiverNode a node to convert to a JavaExpression
 * @return the internal representation of the given node. Might contain {@link Unknown}.
 */
public static JavaExpression fromNode(Node receiverNode) {
    JavaExpression result = null;
    if (receiverNode instanceof FieldAccessNode) {
        result = fromNodeFieldAccess((FieldAccessNode) receiverNode);
    } else if (receiverNode instanceof ExplicitThisNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof ThisNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof SuperNode) {
        result = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof LocalVariableNode) {
        LocalVariableNode lv = (LocalVariableNode) receiverNode;
        result = new LocalVariable(lv);
    } else if (receiverNode instanceof ArrayAccessNode) {
        ArrayAccessNode a = (ArrayAccessNode) receiverNode;
        result = fromArrayAccess(a);
    } else if (receiverNode instanceof StringConversionNode) {
        // ignore string conversion
        return fromNode(((StringConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof WideningConversionNode) {
        // ignore widening
        return fromNode(((WideningConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof NarrowingConversionNode) {
        // ignore narrowing
        return fromNode(((NarrowingConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof UnaryOperationNode) {
        UnaryOperationNode uopn = (UnaryOperationNode) receiverNode;
        return new UnaryOperation(uopn, fromNode(uopn.getOperand()));
    } else if (receiverNode instanceof BinaryOperationNode) {
        BinaryOperationNode bopn = (BinaryOperationNode) receiverNode;
        return new BinaryOperation(bopn, fromNode(bopn.getLeftOperand()), fromNode(bopn.getRightOperand()));
    } else if (receiverNode instanceof ClassNameNode) {
        ClassNameNode cn = (ClassNameNode) receiverNode;
        result = new ClassName(cn.getType());
    } else if (receiverNode instanceof ValueLiteralNode) {
        ValueLiteralNode vn = (ValueLiteralNode) receiverNode;
        result = new ValueLiteral(vn.getType(), vn);
    } else if (receiverNode instanceof ArrayCreationNode) {
        ArrayCreationNode an = (ArrayCreationNode) receiverNode;
        List<@Nullable JavaExpression> dimensions = CollectionsPlume.mapList(JavaExpression::fromNode, an.getDimensions());
        List<JavaExpression> initializers = CollectionsPlume.mapList(JavaExpression::fromNode, an.getInitializers());
        result = new ArrayCreation(an.getType(), dimensions, initializers);
    } else if (receiverNode instanceof MethodInvocationNode) {
        MethodInvocationNode mn = (MethodInvocationNode) receiverNode;
        MethodInvocationTree t = mn.getTree();
        if (t == null) {
            throw new BugInCF("Unexpected null tree for node: " + mn);
        }
        assert TreeUtils.isUseOfElement(t) : "@AssumeAssertion(nullness): tree kind";
        ExecutableElement invokedMethod = TreeUtils.elementFromUse(t);
        // Note that the method might be nondeterministic.
        List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromNode, mn.getArguments());
        JavaExpression methodReceiver;
        if (ElementUtils.isStatic(invokedMethod)) {
            methodReceiver = new ClassName(mn.getTarget().getReceiver().getType());
        } else {
            methodReceiver = fromNode(mn.getTarget().getReceiver());
        }
        result = new MethodCall(mn.getType(), invokedMethod, methodReceiver, parameters);
    }
    if (result == null) {
        result = new Unknown(receiverNode);
    }
    return result;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) BinaryOperationNode(org.checkerframework.dataflow.cfg.node.BinaryOperationNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) List(java.util.List) ArrayList(java.util.ArrayList) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) BugInCF(org.checkerframework.javacutil.BugInCF) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) UnaryOperationNode(org.checkerframework.dataflow.cfg.node.UnaryOperationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 8 with StringConversionNode

use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.

the class ValueTransfer method getStringLengthRange.

/**
 * Returns a range of possible lengths for {@code subNode}, as casted to a String.
 *
 * @param subNode some subnode of {@code p}
 * @param p TransferInput
 * @return a range of possible lengths for {@code subNode}, as casted to a String
 */
private Range getStringLengthRange(Node subNode, TransferInput<CFValue, CFStore> p) {
    CFValue value = p.getValueOfSubNode(subNode);
    AnnotationMirror anno = getValueAnnotation(value);
    if (anno == null) {
        return null;
    }
    String annoName = AnnotationUtils.annotationName(anno);
    if (annoName.equals(ValueAnnotatedTypeFactory.ARRAYLENRANGE_NAME)) {
        return atypeFactory.getRange(anno);
    } else if (annoName.equals(ValueAnnotatedTypeFactory.BOTTOMVAL_NAME)) {
        return Range.NOTHING;
    }
    TypeKind subNodeTypeKind = subNode.getType().getKind();
    // handle values converted to string (ints, longs, longs with @IntRange)
    if (subNode instanceof StringConversionNode) {
        return getStringLengthRange(((StringConversionNode) subNode).getOperand(), p);
    } else if (isIntRange(subNode, p)) {
        return getIntRangeStringLengthRange(subNode, p);
    } else if (subNodeTypeKind == TypeKind.INT) {
        // ints are between 1 and 11 characters long
        return Range.create(1, 11);
    } else if (subNodeTypeKind == TypeKind.LONG) {
        // longs are between 1 and 20 characters long
        return Range.create(1, 20);
    }
    return Range.create(0, Integer.MAX_VALUE);
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode)

Aggregations

StringConversionNode (org.checkerframework.dataflow.cfg.node.StringConversionNode)8 ArrayList (java.util.ArrayList)5 List (java.util.List)4 ArrayAccessNode (org.checkerframework.dataflow.cfg.node.ArrayAccessNode)4 ClassNameNode (org.checkerframework.dataflow.cfg.node.ClassNameNode)4 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)4 LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)4 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)4 NarrowingConversionNode (org.checkerframework.dataflow.cfg.node.NarrowingConversionNode)4 SuperNode (org.checkerframework.dataflow.cfg.node.SuperNode)4 ValueLiteralNode (org.checkerframework.dataflow.cfg.node.ValueLiteralNode)4 WideningConversionNode (org.checkerframework.dataflow.cfg.node.WideningConversionNode)4 AnnotationMirror (javax.lang.model.element.AnnotationMirror)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)3 Range (org.checkerframework.common.value.util.Range)3 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)3 Node (org.checkerframework.dataflow.cfg.node.Node)3 TypeKind (javax.lang.model.type.TypeKind)2 ExplicitThisLiteralNode (org.checkerframework.dataflow.cfg.node.ExplicitThisLiteralNode)2