Search in sources :

Example 6 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project error-prone by google.

the class NullnessPropagationTransfer method handleEqualityComparison.

/**
   * Refines the {@code Nullness} of {@code LocalVariableNode}s used in an equality comparison using
   * the greatest lower bound.
   *
   * @param equalTo whether the comparison is == (false for !=)
   * @param leftNode the left-hand side of the comparison
   * @param rightNode the right-hand side of the comparison
   * @param inputs access to nullness values of the left and right nodes
   * @param thenUpdates the local variables whose nullness values should be updated if the
   *     comparison returns {@code true}
   * @param elseUpdates the local variables whose nullness values should be updated if the
   *     comparison returns {@code false}
   */
private static void handleEqualityComparison(boolean equalTo, Node leftNode, Node rightNode, SubNodeValues inputs, LocalVariableUpdates thenUpdates, LocalVariableUpdates elseUpdates) {
    Nullness leftVal = inputs.valueOfSubNode(leftNode);
    Nullness rightVal = inputs.valueOfSubNode(rightNode);
    Nullness equalBranchValue = leftVal.greatestLowerBound(rightVal);
    LocalVariableUpdates equalBranchUpdates = equalTo ? thenUpdates : elseUpdates;
    LocalVariableUpdates notEqualBranchUpdates = equalTo ? elseUpdates : thenUpdates;
    if (leftNode instanceof LocalVariableNode) {
        LocalVariableNode localVar = (LocalVariableNode) leftNode;
        equalBranchUpdates.set(localVar, equalBranchValue);
        notEqualBranchUpdates.set(localVar, leftVal.greatestLowerBound(rightVal.deducedValueWhenNotEqual()));
    }
    if (rightNode instanceof LocalVariableNode) {
        LocalVariableNode localVar = (LocalVariableNode) rightNode;
        equalBranchUpdates.set(localVar, equalBranchValue);
        notEqualBranchUpdates.set(localVar, rightVal.greatestLowerBound(leftVal.deducedValueWhenNotEqual()));
    }
}
Also used : LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 7 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project error-prone by google.

the class NullnessPropagationTransfer method visitAssignment.

@Override
Nullness visitAssignment(AssignmentNode node, SubNodeValues inputs, LocalVariableUpdates updates) {
    Nullness value = inputs.valueOfSubNode(node.getExpression());
    Node target = node.getTarget();
    if (target instanceof LocalVariableNode) {
        updates.set((LocalVariableNode) target, value);
    }
    if (target instanceof ArrayAccessNode) {
        setNonnullIfLocalVariable(updates, ((ArrayAccessNode) target).getArray());
    }
    if (target instanceof FieldAccessNode) {
        FieldAccessNode fieldAccess = (FieldAccessNode) target;
        ClassAndField targetField = tryGetFieldSymbol(target.getTree());
        setReceiverNonnull(updates, fieldAccess.getReceiver(), targetField);
    }
    /*
     * We propagate the value of the target to the value of the assignment expressions as a whole.
     * We do this regardless of whether the target is a local variable. For example:
     *
     * String s = object.field = "foo"; // Now |s| is non-null.
     *
     * It's not clear to me that this is technically correct, but it works in practice with the
     * bytecode generated by both javac and ecj.
     *
     * http://stackoverflow.com/q/12850676/28465
     */
    return value;
}
Also used : TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) Node(org.checkerframework.dataflow.cfg.node.Node) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Aggregations

LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)7 Node (org.checkerframework.dataflow.cfg.node.Node)4 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)3 ArrayList (java.util.ArrayList)2 Element (javax.lang.model.element.Element)2 ArrayAccessNode (org.checkerframework.dataflow.cfg.node.ArrayAccessNode)2 EqualToNode (org.checkerframework.dataflow.cfg.node.EqualToNode)2 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 LocalStore (com.google.errorprone.dataflow.LocalStore)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 MethodTree (com.sun.source.tree.MethodTree)1 VariableTree (com.sun.source.tree.VariableTree)1 List (java.util.List)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 RegularTransferResult (org.checkerframework.dataflow.analysis.RegularTransferResult)1 UnderlyingAST (org.checkerframework.dataflow.cfg.UnderlyingAST)1 CFGMethod (org.checkerframework.dataflow.cfg.UnderlyingAST.CFGMethod)1 Block (org.checkerframework.dataflow.cfg.block.Block)1 ConditionalBlock (org.checkerframework.dataflow.cfg.block.ConditionalBlock)1