Search in sources :

Example 1 with LocalStore

use of com.google.errorprone.dataflow.LocalStore 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 2 with LocalStore

use of com.google.errorprone.dataflow.LocalStore in project error-prone by google.

the class NullnessPropagationTransfer method fieldInitializerNullnessIfAvailable.

@Nullable
private Nullness fieldInitializerNullnessIfAvailable(ClassAndField accessed) {
    if (!traversed.add(accessed.symbol)) {
        // TODO(kmb): Try to recognize problems with initialization order
        return NULL;
    }
    try {
        JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(context);
        TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(accessed.symbol);
        // missing types.
        if (fieldDeclPath == null || fieldDeclPath.getCompilationUnit() != compilationUnit || !(fieldDeclPath.getLeaf() instanceof VariableTree)) {
            return null;
        }
        ExpressionTree initializer = ((VariableTree) fieldDeclPath.getLeaf()).getInitializer();
        if (initializer == null) {
            return null;
        }
        ClassTree classTree = (ClassTree) fieldDeclPath.getParentPath().getLeaf();
        // Run flow analysis on field initializer.  This is inefficient compared to just walking
        // the initializer expression tree but it avoids duplicating the logic from this transfer
        // function into a method that operates on Javac Nodes.
        TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer);
        UnderlyingAST ast = new UnderlyingAST.CFGStatement(initializerPath.getLeaf(), classTree);
        ControlFlowGraph cfg = CFGBuilder.build(initializerPath, javacEnv, ast, /* assumeAssertionsEnabled */
        false, /* assumeAssertionsDisabled */
        false);
        Analysis<Nullness, LocalStore<Nullness>, NullnessPropagationTransfer> analysis = new Analysis<>(javacEnv, this);
        analysis.performAnalysis(cfg);
        return analysis.getValue(initializerPath.getLeaf());
    } finally {
        traversed.remove(accessed.symbol);
    }
}
Also used : VariableTree(com.sun.source.tree.VariableTree) ClassTree(com.sun.source.tree.ClassTree) LocalStore(com.google.errorprone.dataflow.LocalStore) TreePath(com.sun.source.util.TreePath) Analysis(org.checkerframework.dataflow.analysis.Analysis) ControlFlowGraph(org.checkerframework.dataflow.cfg.ControlFlowGraph) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ExpressionTree(com.sun.source.tree.ExpressionTree) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST) Nullable(javax.annotation.Nullable)

Example 3 with LocalStore

use of com.google.errorprone.dataflow.LocalStore in project error-prone by google.

the class TrustingNullnessAnalysis method getFieldInitializerNullness.

/**
 * Returns {@link Nullness} of the initializer of the {@link VariableTree} at the leaf of the
 * given {@code fieldDeclPath}. Returns {@link Nullness#NULL} should there be no initializer.
 */
// TODO(kmb): Fold this functionality into Dataflow.expressionDataflow
public Nullness getFieldInitializerNullness(TreePath fieldDeclPath, Context context) {
    Tree decl = fieldDeclPath.getLeaf();
    checkArgument(decl instanceof VariableTree && ((JCVariableDecl) decl).sym.getKind() == ElementKind.FIELD, "Leaf of fieldDeclPath must be a field declaration: %s", decl);
    ExpressionTree initializer = ((VariableTree) decl).getInitializer();
    if (initializer == null) {
        // An uninitialized field is null or 0 to start :)
        return ((JCVariableDecl) decl).type.isPrimitive() ? Nullness.NONNULL : Nullness.NULL;
    }
    TreePath initializerPath = TreePath.getPath(fieldDeclPath, initializer);
    ClassTree classTree = (ClassTree) fieldDeclPath.getParentPath().getLeaf();
    JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(context);
    UnderlyingAST ast = new UnderlyingAST.CFGStatement(decl, classTree);
    ControlFlowGraph cfg = CFGBuilder.build(initializerPath, javacEnv, ast, /* assumeAssertionsEnabled */
    false, /* assumeAssertionsDisabled */
    false);
    try {
        nullnessPropagation.setContext(context).setCompilationUnit(fieldDeclPath.getCompilationUnit());
        Analysis<Nullness, LocalStore<Nullness>, TrustingNullnessPropagation> analysis = new Analysis<>(javacEnv, nullnessPropagation);
        analysis.performAnalysis(cfg);
        return analysis.getValue(initializer);
    } finally {
        nullnessPropagation.setContext(null).setCompilationUnit(null);
    }
}
Also used : VariableTree(com.sun.source.tree.VariableTree) ClassTree(com.sun.source.tree.ClassTree) LocalStore(com.google.errorprone.dataflow.LocalStore) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) TreePath(com.sun.source.util.TreePath) Analysis(org.checkerframework.dataflow.analysis.Analysis) ControlFlowGraph(org.checkerframework.dataflow.cfg.ControlFlowGraph) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ExpressionTree(com.sun.source.tree.ExpressionTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST)

Aggregations

LocalStore (com.google.errorprone.dataflow.LocalStore)3 ClassTree (com.sun.source.tree.ClassTree)2 ExpressionTree (com.sun.source.tree.ExpressionTree)2 VariableTree (com.sun.source.tree.VariableTree)2 TreePath (com.sun.source.util.TreePath)2 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)2 Analysis (org.checkerframework.dataflow.analysis.Analysis)2 ControlFlowGraph (org.checkerframework.dataflow.cfg.ControlFlowGraph)2 UnderlyingAST (org.checkerframework.dataflow.cfg.UnderlyingAST)2 Tree (com.sun.source.tree.Tree)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 Nullable (javax.annotation.Nullable)1 Element (javax.lang.model.element.Element)1 LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)1