Search in sources :

Example 1 with DataFlowNode

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

the class DataflowAnomalyAnalysisRule method checkVariableAccess.

private void checkVariableAccess(DataFlowNode inode, VariableAccess va, final Usage u) {
    // get the start and end line
    int startLine = u.node.getLine();
    int endLine = inode.getLine();
    Node lastNode = inode.getNode();
    Node firstNode = u.node.getNode();
    if (va.accessTypeMatches(u.accessType) && va.isDefinition()) {
        // DD
        addDaaViolation(rc, lastNode, "DD", va.getVariableName(), startLine, endLine);
    } else if (u.accessType == VariableAccess.UNDEFINITION && va.isReference()) {
        // UR
        addDaaViolation(rc, lastNode, "UR", va.getVariableName(), startLine, endLine);
    } else if (u.accessType == VariableAccess.DEFINITION && va.isUndefinition()) {
        // DU
        addDaaViolation(rc, firstNode, "DU", va.getVariableName(), startLine, endLine);
    }
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Node(net.sourceforge.pmd.lang.ast.Node)

Example 2 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode 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 DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode 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 DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode 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 DataFlowNode

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

the class StatementAndBraceFinderTest method testSimpleCaseStmtHasCorrectTypes.

@Test
public void testSimpleCaseStmtHasCorrectTypes() {
    ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST6).get(0);
    DataFlowNode dfn = null;
    dfn = exp.getDataFlowNode().getFlow().get(0);
    assertTrue(dfn instanceof StartOrEndDataFlowNode);
    dfn = exp.getDataFlowNode().getFlow().get(1);
    assertEquals(2, dfn.getLine());
    assertTrue(dfn.getNode() instanceof ASTProgramUnit);
    dfn = exp.getDataFlowNode().getFlow().get(2);
    assertEquals(4, dfn.getLine());
    assertTrue(dfn.isType(NodeType.SWITCH_START));
    assertTrue(dfn.isType(NodeType.CASE_LAST_STATEMENT));
    dfn = exp.getDataFlowNode().getFlow().get(3);
    assertEquals(5, dfn.getLine());
    assertTrue(dfn.isType(NodeType.CASE_LAST_STATEMENT));
    assertTrue(dfn.isType(NodeType.BREAK_STATEMENT));
    dfn = exp.getDataFlowNode().getFlow().get(4);
    assertEquals(6, dfn.getLine());
    assertTrue(dfn.isType(NodeType.SWITCH_LAST_DEFAULT_STATEMENT));
    assertTrue(dfn.isType(NodeType.BREAK_STATEMENT));
    dfn = exp.getDataFlowNode().getFlow().get(5);
    assertEquals(7, dfn.getLine());
    assertTrue(dfn.isType(NodeType.SWITCH_END));
}
Also used : StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ASTProgramUnit(net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit) ASTExpression(net.sourceforge.pmd.lang.plsql.ast.ASTExpression) Test(org.junit.Test)

Aggregations

DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)35 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)6 ASTExpression (net.sourceforge.pmd.lang.plsql.ast.ASTExpression)6 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 List (java.util.List)3 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)3 ASTProgramUnit (net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 Point (org.eclipse.swt.graphics.Point)2 DAAPathFinder (net.sourceforge.pmd.lang.dfa.pathfinder.DAAPathFinder)1