public static Tree parentIgnoreParentheses(Tree tree) { Tree parent = tree.parent(); if (parent.is(Kind.PARENTHESISED_EXPRESSION)) { return parentIgnoreParentheses(parent); } return parent; }
private static Tree biggestTreeWithSameTruthiness(Tree condition, boolean isTruthy, Set<Tree> neverExecutedCode) { Tree parent = CheckUtils.parentIgnoreParentheses(condition); if ((parent.is(Kind.CONDITIONAL_OR) && isTruthy) || (parent.is(Kind.CONDITIONAL_AND) && !isTruthy)) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) parent; if (binaryExpressionTree.leftOperand().equals(condition)) { neverExecutedCode.add(binaryExpressionTree.rightOperand()); } return biggestTreeWithSameTruthiness(parent, isTruthy, neverExecutedCode); } return condition; }
@Override public void afterBlockElement(ProgramState currentState, Tree element) { Tree parent = CheckUtils.parentIgnoreParentheses(element); if (parent.is(Kind.RETURN_STATEMENT)) { SymbolicValue value = currentState.peekStack(); Constraint constraint = currentState.getConstraint(value); valuesPerReturn.put((ReturnStatementTree) parent, new ValueConstraint(value, constraint)); } }
private static Set<Tree> getNeverExecutedCode(Tree condition, boolean isTruthy) { Set<Tree> neverExecutedCode = new HashSet<>(); Tree biggestTreeWithSameTruthiness = biggestTreeWithSameTruthiness(condition, isTruthy, neverExecutedCode); Tree parent = CheckUtils.parentIgnoreParentheses(biggestTreeWithSameTruthiness); if (parent.is(Kind.IF_STATEMENT)) { IfStatementTree ifStatementTree = (IfStatementTree) parent; if (isTruthy) { if (ifStatementTree.elseClause() != null) { neverExecutedCode.add(ifStatementTree.elseClause()); } } else { neverExecutedCode.add(ifStatementTree.statement()); } } else if (parent.is(KindSet.LOOP_KINDS) && !isTruthy) { neverExecutedCode.add(((IterationStatementTree) parent).statement()); } else if (parent.is(Kind.CONDITIONAL_EXPRESSION)) { ConditionalExpressionTree conditionalExpressionTree = (ConditionalExpressionTree) parent; neverExecutedCode.add(isTruthy ? conditionalExpressionTree.falseExpression() : conditionalExpressionTree.trueExpression()); } return neverExecutedCode; }