Search in sources :

Example 1 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method markUsages.

private List<VariableAccess> markUsages(DataFlowNode inode) {
    // undefinitions was once a field... seems like it works fine as a local
    List<VariableAccess> undefinitions = new ArrayList<>();
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            VariableNameDeclaration vnd = entry.getKey();
            if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
                // add definition for initialized variables
                addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
            }
            undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
            for (NameOccurrence occurrence : entry.getValue()) {
                addAccess((JavaNameOccurrence) occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 2 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class DataflowAnomalyAnalysisRule method execute.

public void execute(CurrentPath path) {
    if (maxNumberOfViolationsReached()) {
        return;
    }
    Map<String, Usage> usagesByVarName = new HashMap<>();
    for (DataFlowNode inode : path) {
        if (inode.getVariableAccess() != null) {
            // iterate all variables of this node
            for (VariableAccess va : inode.getVariableAccess()) {
                // get the last usage of the current variable
                Usage lastUsage = usagesByVarName.get(va.getVariableName());
                if (lastUsage != null) {
                    // there was a usage to this variable before
                    checkVariableAccess(inode, va, lastUsage);
                }
                Usage newUsage = new Usage(va.getAccessType(), inode);
                // put the new usage for the variable
                usagesByVarName.put(va.getVariableName(), newUsage);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Example 3 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method computeNow.

/*
     * SRT public void compute(ASTConstructorDeclaration node) {
     * this.computeNow(node); }
     */
private void computeNow(Node node) {
    DataFlowNode inode = node.getDataFlowNode();
    List<VariableAccess> undefinitions = markUsages(inode);
    // all variables are first in state undefinition
    DataFlowNode firstINode = inode.getFlow().get(0);
    firstINode.setVariableAccess(undefinitions);
    // all variables are getting undefined when leaving scope
    DataFlowNode lastINode = inode.getFlow().get(inode.getFlow().size() - 1);
    lastINode.setVariableAccess(undefinitions);
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Example 4 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method addVariableAccess.

/**
 * Adds a VariableAccess to a dataflow node.
 *
 * @param node
 *            location of the access of a variable
 * @param va
 *            variable access to add
 * @param flow
 *            dataflownodes that can contain the node.
 */
private void addVariableAccess(Node node, VariableAccess va, List<DataFlowNode> flow) {
    // backwards to find the right inode (not a method declaration)
    for (int i = flow.size() - 1; i > 0; i--) {
        DataFlowNode inode = flow.get(i);
        if (inode.getNode() == null) {
            continue;
        }
        List<? extends Node> children = inode.getNode().findDescendantsOfType(node.getClass());
        for (Node n : children) {
            if (node.equals(n)) {
                List<VariableAccess> v = new ArrayList<>();
                v.add(va);
                inode.setVariableAccess(v);
                return;
            }
        }
    }
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Node(net.sourceforge.pmd.lang.ast.Node) PLSQLNode(net.sourceforge.pmd.lang.plsql.ast.PLSQLNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList)

Example 5 with VariableAccess

use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.

the class VariableAccessVisitor method markUsages.

private List<VariableAccess> markUsages(DataFlowNode inode) {
    // undefinitions was once a field... seems like it works fine as a local
    List<VariableAccess> undefinitions = new ArrayList<>();
    Set<Map<NameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<NameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            NameDeclaration vnd = entry.getKey();
            if (vnd.getNode().jjtGetParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableOrConstantInitializer.class) != null) {
                // add definition for initialized variables
                addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
            }
            undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
            for (NameOccurrence occurrence : entry.getValue()) {
                addAccess(occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameter) Map(java.util.Map) PLSQLNameOccurrence(net.sourceforge.pmd.lang.plsql.symboltable.PLSQLNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)10 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)6 ArrayList (java.util.ArrayList)4 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Node (net.sourceforge.pmd.lang.ast.Node)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)1 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameter)1 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)1 PLSQLNameOccurrence (net.sourceforge.pmd.lang.plsql.symboltable.PLSQLNameOccurrence)1 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)1 Test (org.junit.Test)1