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);
}
}
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);
}
}
}
}
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);
}
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;
}
}
}
}
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));
}
Aggregations