private boolean checkStmt(ASTNode onlyNode, ASTIfElseNode node) { if (!(onlyNode instanceof ASTStatementSequenceNode)) { // only interested in StmtSeq nodes return false; } ASTStatementSequenceNode stmtNode = (ASTStatementSequenceNode) onlyNode; List<Object> statements = stmtNode.getStatements(); if (statements.size() != 1) { // need one stmt only return false; } AugmentedStmt as = (AugmentedStmt) statements.get(0); Stmt stmt = as.get_Stmt(); if (!(stmt instanceof DAbruptStmt)) { // interested in abrupt stmts only return false; } DAbruptStmt abStmt = (DAbruptStmt) stmt; if (!(abStmt.is_Break() || abStmt.is_Continue())) { // interested in breaks and continues only return false; } // make sure that the break is not that of the if // unliekly but good to check SETNodeLabel ifLabel = ((ASTLabeledNode) node).get_Label(); if (ifLabel != null) { if (ifLabel.toString() != null) { if (abStmt.is_Break()) { String breakLabel = abStmt.getLabel().toString(); if (breakLabel != null) { if (breakLabel.compareTo(ifLabel.toString()) == 0) { // is a break of this label return false; } } } } } return true; }
public boolean isIfElseBreakingPossiblePatternOne(ASTIfElseNode node) { List<Object> ifBody = node.getIfBody(); if (ifBody.size() != 1) { // we are only interested if size is one return false; } ASTNode onlyNode = (ASTNode) ifBody.get(0); boolean check = checkStmt(onlyNode, node); if (!check) { return false; } // breaking is possible // break and store newIfNode = new ASTIfNode(((ASTLabeledNode) node).get_Label(), node.get_Condition(), ifBody); remainingBody = node.getElseBody(); return true; }
public boolean isIfElseBreakingPossiblePatternTwo(ASTIfElseNode node) { List<Object> elseBody = node.getElseBody(); if (elseBody.size() != 1) { // we are only interested if size is one return false; } ASTNode onlyNode = (ASTNode) elseBody.get(0); boolean check = checkStmt(onlyNode, node); if (!check) { return false; } // breaking is possible ASTCondition cond = node.get_Condition(); // flip cond.flip(); newIfNode = new ASTIfNode(((ASTLabeledNode) node).get_Label(), cond, elseBody); remainingBody = node.getIfBody(); return true; }
/* We know this method is called when there is a loop node which has a body consisting entirely of one ASTIfElseNode */ public static List getNewNode(ASTNode loopNode, ASTIfElseNode ifElseNode) { // make sure that elsebody has only a stmtseq node List elseBody = ((ASTIfElseNode) ifElseNode).getElseBody(); if (elseBody.size() != 1) { // this is more than one we need one stmtSeq Node return null; } ASTNode tempNode = (ASTNode) elseBody.get(0); if (!(tempNode instanceof ASTStatementSequenceNode)) { // not a stmtSeq return null; } List statements = ((ASTStatementSequenceNode) tempNode).getStatements(); Iterator stmtIt = statements.iterator(); while (stmtIt.hasNext()) { AugmentedStmt as = (AugmentedStmt) stmtIt.next(); Stmt stmt = as.get_Stmt(); if (stmt instanceof DAbruptStmt) { // this is a abrupt stmt DAbruptStmt abStmt = (DAbruptStmt) stmt; if (!(abStmt.is_Break())) { // we need a break return null; } else { if (stmtIt.hasNext()) { // a break should be the last stmt return null; } SETNodeLabel label = abStmt.getLabel(); String labelBroken = label.toString(); String loopLabel = ((ASTLabeledNode) loopNode).get_Label().toString(); if (labelBroken != null && loopLabel != null) { // stmt breaks some label if (labelBroken.compareTo(loopLabel) == 0) { // we have found a break breaking this label // make sure that if the orignal was an ASTWhileNode then there was // ONLY a break statement if (loopNode instanceof ASTWhileNode) { if (statements.size() != 1) { // more than 1 statement return null; } } // pattern matched ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode); if (newWhileNode == null) { return null; } List toReturn = new ArrayList(); toReturn.add(newWhileNode); // Add the statementSequenceNode AFTER the whileNode except for the laststmt if (statements.size() != 1) { // size 1 means that the only stmt is a break stmt Iterator tempIt = statements.iterator(); List newStmts = new ArrayList(); while (tempIt.hasNext()) { Object tempStmt = tempIt.next(); if (tempIt.hasNext()) { newStmts.add(tempStmt); } } toReturn.add(new ASTStatementSequenceNode(newStmts)); } return toReturn; } // labels matched } // non null labels } // end of break stmt } // stmt is an abrupt stmt else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt) { if (!(loopNode instanceof ASTUnconditionalLoopNode)) { // this pattern is only possible for while(true) return null; } if (stmtIt.hasNext()) { // returns should come in the end return null; } // pattern matched ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode); if (newWhileNode == null) { return null; } List toReturn = new ArrayList(); toReturn.add(newWhileNode); // Add the statementSequenceNode AFTER the whileNode Iterator tempIt = statements.iterator(); List newStmts = new ArrayList(); while (tempIt.hasNext()) { Object tempStmt = tempIt.next(); newStmts.add(tempStmt); } toReturn.add(new ASTStatementSequenceNode(newStmts)); return toReturn; } // if stmt was a return stmt } // going through the stmts return null; } // end of method