Search in sources :

Example 1 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project bazel by bazelbuild.

the class Analysis method callTransferFunction.

/**
     * Call the transfer function for node {@code node}, and set that node as
     * current node first.
     */
protected TransferResult<A, S> callTransferFunction(Node node, TransferInput<A, S> store) {
    if (node.isLValue()) {
        // store.hasTwoStores()), or is the following correct?
        return new RegularTransferResult<A, S>(null, store.getRegularStore());
    }
    store.node = node;
    currentNode = node;
    TransferResult<A, S> transferResult = node.accept(transferFunction, store);
    currentNode = null;
    if (node instanceof ReturnNode) {
        // save a copy of the store to later check if some property held at
        // a given return statement
        storesAtReturnStatements.put((ReturnNode) node, transferResult);
    }
    if (node instanceof AssignmentNode) {
        // store the flow-refined value for effectively final local variables
        AssignmentNode assignment = (AssignmentNode) node;
        Node lhst = assignment.getTarget();
        if (lhst instanceof LocalVariableNode) {
            LocalVariableNode lhs = (LocalVariableNode) lhst;
            Element elem = lhs.getElement();
            if (ElementUtils.isEffectivelyFinal(elem)) {
                finalLocalValues.put(elem, transferResult.getResultValue());
            }
        }
    }
    return transferResult;
}
Also used : ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Node(org.checkerframework.dataflow.cfg.node.Node) Element(javax.lang.model.element.Element) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 2 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project bazel by bazelbuild.

the class FlowExpressions method internalReprOf.

/**
     * We ignore operations such as widening and
     * narrowing when computing the internal representation.
     *
     * @return The internal representation (as {@link Receiver}) of any
     *         {@link Node}. Might contain {@link Unknown}.
     */
public static Receiver internalReprOf(AnnotationProvider provider, Node receiverNode, boolean allowNonDeterminitic) {
    Receiver receiver = null;
    if (receiverNode instanceof FieldAccessNode) {
        FieldAccessNode fan = (FieldAccessNode) receiverNode;
        if (fan.getFieldName().equals("this")) {
            // For some reason, "className.this" is considered a field access.
            // We right this wrong here.
            receiver = new ThisReference(fan.getReceiver().getType());
        } else {
            receiver = internalReprOfFieldAccess(provider, fan);
        }
    } else if (receiverNode instanceof ExplicitThisLiteralNode) {
        receiver = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof ThisLiteralNode) {
        receiver = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof SuperNode) {
        receiver = new ThisReference(receiverNode.getType());
    } else if (receiverNode instanceof LocalVariableNode) {
        LocalVariableNode lv = (LocalVariableNode) receiverNode;
        receiver = new LocalVariable(lv);
    } else if (receiverNode instanceof ArrayAccessNode) {
        ArrayAccessNode a = (ArrayAccessNode) receiverNode;
        receiver = internalReprOfArrayAccess(provider, a);
    } else if (receiverNode instanceof StringConversionNode) {
        // ignore string conversion
        return internalReprOf(provider, ((StringConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof WideningConversionNode) {
        // ignore widening
        return internalReprOf(provider, ((WideningConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof NarrowingConversionNode) {
        // ignore narrowing
        return internalReprOf(provider, ((NarrowingConversionNode) receiverNode).getOperand());
    } else if (receiverNode instanceof ClassNameNode) {
        ClassNameNode cn = (ClassNameNode) receiverNode;
        receiver = new ClassName(cn.getType());
    } else if (receiverNode instanceof ValueLiteralNode) {
        ValueLiteralNode vn = (ValueLiteralNode) receiverNode;
        receiver = new ValueLiteral(vn.getType(), vn);
    } else if (receiverNode instanceof MethodInvocationNode) {
        MethodInvocationNode mn = (MethodInvocationNode) receiverNode;
        ExecutableElement invokedMethod = TreeUtils.elementFromUse(mn.getTree());
        // check if this represents a boxing operation of a constant, in which
        // case we treat the method call as deterministic, because there is no way
        // to behave differently in two executions where two constants are being used.
        boolean considerDeterministic = false;
        if (invokedMethod.toString().equals("valueOf(long)") && mn.getTarget().getReceiver().toString().equals("Long")) {
            Node arg = mn.getArgument(0);
            if (arg instanceof ValueLiteralNode) {
                considerDeterministic = true;
            }
        }
        if (PurityUtils.isDeterministic(provider, invokedMethod) || allowNonDeterminitic || considerDeterministic) {
            List<Receiver> parameters = new ArrayList<>();
            for (Node p : mn.getArguments()) {
                parameters.add(internalReprOf(provider, p));
            }
            Receiver methodReceiver;
            if (ElementUtils.isStatic(invokedMethod)) {
                methodReceiver = new ClassName(mn.getTarget().getReceiver().getType());
            } else {
                methodReceiver = internalReprOf(provider, mn.getTarget().getReceiver());
            }
            receiver = new PureMethodCall(mn.getType(), invokedMethod, methodReceiver, parameters);
        }
    }
    if (receiver == null) {
        receiver = new Unknown(receiverNode.getType());
    }
    return receiver;
}
Also used : MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExecutableElement(javax.lang.model.element.ExecutableElement) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) ExplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ExplicitThisLiteralNode) Node(org.checkerframework.dataflow.cfg.node.Node) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ExplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ExplicitThisLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) ThisLiteralNode(org.checkerframework.dataflow.cfg.node.ThisLiteralNode) ExplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ExplicitThisLiteralNode) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with LocalVariableNode

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

the class TrustingNullnessPropagation method initialStore.

@Override
public LocalStore<Nullness> initialStore(UnderlyingAST underlyingAST, List<LocalVariableNode> parameters) {
    if (parameters == null) {
        // method"
        return LocalStore.empty();
    }
    LocalStore.Builder<Nullness> result = LocalStore.<Nullness>empty().toBuilder();
    for (LocalVariableNode param : parameters) {
        Element element = param.getElement();
        Nullness assumed = nullnessFromAnnotations(element);
        result.setInformation(element, assumed);
    }
    return result.build();
}
Also used : Element(javax.lang.model.element.Element) LocalStore(com.google.errorprone.dataflow.LocalStore) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 4 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project bazel by bazelbuild.

the class Analysis method init.

/** Initialize the analysis with a new control flow graph. */
protected void init(ControlFlowGraph cfg) {
    this.cfg = cfg;
    thenStores = new IdentityHashMap<>();
    elseStores = new IdentityHashMap<>();
    inputs = new IdentityHashMap<>();
    storesAtReturnStatements = new IdentityHashMap<>();
    worklist = new Worklist(cfg);
    nodeValues = new IdentityHashMap<>();
    finalLocalValues = new HashMap<>();
    worklist.add(cfg.getEntryBlock());
    List<LocalVariableNode> parameters = null;
    UnderlyingAST underlyingAST = cfg.getUnderlyingAST();
    if (underlyingAST.getKind() == Kind.METHOD) {
        MethodTree tree = ((CFGMethod) underlyingAST).getMethod();
        parameters = new ArrayList<>();
        for (VariableTree p : tree.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else if (underlyingAST.getKind() == Kind.LAMBDA) {
        LambdaExpressionTree lambda = ((CFGLambda) underlyingAST).getLambdaTree();
        parameters = new ArrayList<>();
        for (VariableTree p : lambda.getParameters()) {
            LocalVariableNode var = new LocalVariableNode(p);
            parameters.add(var);
        // TODO: document that LocalVariableNode has no block that it
        // belongs to
        }
    } else {
    // nothing to do
    }
    S initialStore = transferFunction.initialStore(underlyingAST, parameters);
    Block entry = cfg.getEntryBlock();
    thenStores.put(entry, initialStore);
    elseStores.put(entry, initialStore);
    inputs.put(entry, new TransferInput<>(null, this, initialStore));
}
Also used : MethodTree(com.sun.source.tree.MethodTree) CFGMethod(org.checkerframework.dataflow.cfg.UnderlyingAST.CFGMethod) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) SpecialBlock(org.checkerframework.dataflow.cfg.block.SpecialBlock) Block(org.checkerframework.dataflow.cfg.block.Block) RegularBlock(org.checkerframework.dataflow.cfg.block.RegularBlock) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST)

Example 5 with LocalVariableNode

use of org.checkerframework.dataflow.cfg.node.LocalVariableNode in project bazel by bazelbuild.

the class ConstantPropagationTransfer method visitAssignment.

@Override
public TransferResult<Constant, ConstantPropagationStore> visitAssignment(AssignmentNode n, TransferInput<Constant, ConstantPropagationStore> pi) {
    ConstantPropagationStore p = pi.getRegularStore();
    Node target = n.getTarget();
    Constant info = null;
    if (target instanceof LocalVariableNode) {
        LocalVariableNode t = (LocalVariableNode) target;
        info = p.getInformation(n.getExpression());
        p.setInformation(t, info);
    }
    return new RegularTransferResult<>(info, p);
}
Also used : IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) Node(org.checkerframework.dataflow.cfg.node.Node) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) RegularTransferResult(org.checkerframework.dataflow.analysis.RegularTransferResult)

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