Search in sources :

Example 16 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

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<>();
    blockCount = maxCountBeforeWidening == -1 ? null : 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) IdentityHashMap(java.util.IdentityHashMap) 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 17 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class FlowExpressions method getParametersOfEnclosingMethod.

/**
 * Returns Receiver objects for the formal parameters of the method in which path is enclosed.
 *
 * @param annotationProvider annotationProvider
 * @param path TreePath that is enclosed by the method
 * @return list of Receiver objects for the formal parameters of the method in which path is
 *     enclosed
 */
public static List<Receiver> getParametersOfEnclosingMethod(AnnotationProvider annotationProvider, TreePath path) {
    MethodTree methodTree = TreeUtils.enclosingMethod(path);
    if (methodTree == null) {
        return null;
    }
    List<Receiver> internalArguments = new ArrayList<>();
    for (VariableTree arg : methodTree.getParameters()) {
        internalArguments.add(internalReprOf(annotationProvider, new LocalVariableNode(arg)));
    }
    return internalArguments;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 18 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method getInitializedInvariantFields.

/**
 * Returns the (non-static) fields that have the invariant annotation and are initialized in a
 * given store.
 */
public List<VariableTree> getInitializedInvariantFields(Store store, TreePath path) {
    // TODO: Instead of passing the TreePath around, can we use
    // getCurrentClassTree?
    ClassTree currentClass = TreeUtils.enclosingClass(path);
    List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
    List<VariableTree> initializedFields = new ArrayList<>();
    for (VariableTree field : fields) {
        VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
        if (!ElementUtils.isStatic(fieldElem)) {
            // Does this field need to satisfy the invariant?
            if (hasFieldInvariantAnnotation(field)) {
                // Has the field been initialized?
                if (store.isFieldInitialized(fieldElem)) {
                    initializedFields.add(field);
                }
            }
        }
    }
    return initializedFields;
}
Also used : NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 19 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method areAllFieldsCommittedOnly.

/**
 * Are all fields committed-only?
 */
protected boolean areAllFieldsCommittedOnly(ClassTree classTree) {
    if (!useFbc) {
        // initialized objects.
        return true;
    }
    for (Tree member : classTree.getMembers()) {
        if (!member.getKind().equals(Tree.Kind.VARIABLE)) {
            continue;
        }
        VariableTree var = (VariableTree) member;
        VariableElement varElt = TreeUtils.elementFromDeclaration(var);
        // var is not committed-only
        if (getDeclAnnotation(varElt, NotOnlyInitialized.class) != null) {
            // not of constructor which is where this is used
            if (!varElt.getModifiers().contains(Modifier.STATIC)) {
                return false;
            }
        }
    }
    return true;
}
Also used : VariableTree(com.sun.source.tree.VariableTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) VariableElement(javax.lang.model.element.VariableElement) NotOnlyInitialized(org.checkerframework.checker.initialization.qual.NotOnlyInitialized)

Example 20 with VariableTree

use of com.sun.source.tree.VariableTree in project checker-framework by typetools.

the class InitializationAnnotatedTypeFactory method getUninitializedInvariantFields.

/**
 * Returns the (non-static) fields that have the invariant annotation and are not yet
 * initialized in a given store.
 */
public List<VariableTree> getUninitializedInvariantFields(Store store, TreePath path, boolean isStatic, List<? extends AnnotationMirror> receiverAnnotations) {
    ClassTree currentClass = TreeUtils.enclosingClass(path);
    List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
    List<VariableTree> violatingFields = new ArrayList<>();
    for (VariableTree field : fields) {
        if (isUnused(field, receiverAnnotations)) {
            // don't consider unused fields
            continue;
        }
        VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
        if (ElementUtils.isStatic(fieldElem) == isStatic) {
            // Does this field need to satisfy the invariant?
            if (hasFieldInvariantAnnotation(field)) {
                // Has the field been initialized?
                if (!store.isFieldInitialized(fieldElem)) {
                    violatingFields.add(field);
                }
            }
        }
    }
    return violatingFields;
}
Also used : NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12